1- from typing import Optional
1+ from typing import List , Optional
22
33from contaxy import __version__ , config
44from contaxy .config import settings
55from contaxy .managers .auth import AuthManager
6- from contaxy .operations import SystemOperations
6+ from contaxy .operations import (
7+ FileOperations ,
8+ JobOperations ,
9+ ServiceOperations ,
10+ SystemOperations ,
11+ )
712from contaxy .operations .json_db import JsonDocumentOperations
813from contaxy .operations .project import ProjectOperations
9- from contaxy .schema .auth import ADMIN_ROLE , USERS_KIND , AccessLevel , UserRegistration
10- from contaxy .schema .project import ProjectCreation
14+ from contaxy .schema import File , Job , Service
15+ from contaxy .schema .auth import (
16+ ADMIN_ROLE ,
17+ USERS_KIND ,
18+ AccessLevel ,
19+ User ,
20+ UserRegistration ,
21+ )
22+ from contaxy .schema .project import Project , ProjectCreation
1123from contaxy .schema .system import SystemInfo , SystemStatistics
1224from contaxy .utils import auth_utils
1325from contaxy .utils .state_utils import GlobalState , RequestState
@@ -23,6 +35,9 @@ def __init__(
2335 json_db_manager : JsonDocumentOperations ,
2436 auth_manager : AuthManager ,
2537 project_manager : ProjectOperations ,
38+ service_manager : ServiceOperations ,
39+ job_manager : JobOperations ,
40+ file_manager : FileOperations ,
2641 ):
2742 """Initializes the system manager.
2843
@@ -32,12 +47,18 @@ def __init__(
3247 json_db_manager: Json document manager instance.
3348 auth_manager: Auth manager instance.
3449 project_manager: Project manager instance.
50+ service_manager: Service manager instance.
51+ job_manager: Job manager instance.
52+ file_manager: File manager instance.
3553 """
3654 self ._global_state = global_state
3755 self ._request_state = request_state
3856 self ._auth_manager = auth_manager
3957 self ._project_manager = project_manager
4058 self ._json_db_manager = json_db_manager
59+ self ._service_manager = service_manager
60+ self ._job_manager = job_manager
61+ self ._file_manager = file_manager
4162
4263 def get_system_info (self ) -> SystemInfo :
4364 return SystemInfo (
@@ -49,10 +70,18 @@ def is_healthy(self) -> bool:
4970 # TODO: do real healthchecks
5071 return True
5172
52- def get_system_statistics (self ) -> SystemStatistics :
53- # TODO: Implement system statistics
73+ def get_system_statistics (self , include_technical : bool ) -> SystemStatistics :
74+ projects = self ._list_all_projects (include_technical )
75+ users = self ._list_all_users (include_technical )
76+ jobs = self ._list_all_jobs (projects )
77+ services = self ._list_all_services (projects )
78+ files = self ._list_all_files (projects )
5479 return SystemStatistics (
55- project_count = 0 , user_count = 0 , job_count = 0 , service_count = 0 , file_count = 0
80+ project_count = len (jobs ),
81+ user_count = len (users ),
82+ job_count = len (jobs ),
83+ service_count = len (services ),
84+ file_count = len (files ),
5685 )
5786
5887 def initialize_system (
@@ -103,3 +132,45 @@ def initialize_system(
103132 )
104133
105134 auth_utils .create_user_project (admin_user , self ._project_manager )
135+
136+ def _list_all_projects (self , include_technical : bool ) -> List [Project ]:
137+ # Temporarily set authorized_access to None so that list_projects returns all projects
138+ # TODO: Find better solution to get all projects
139+ temp = self ._request_state .authorized_access
140+ self ._request_state .authorized_access = None
141+ projects = self ._project_manager .list_projects ()
142+ self ._request_state .authorized_access = temp
143+
144+ if not include_technical :
145+ projects = [proj for proj in projects if not proj .technical_project ]
146+ return projects
147+
148+ def _list_all_users (self , include_technical : bool ) -> List [User ]:
149+ users = self ._auth_manager .list_users ()
150+ if not include_technical :
151+ users = [user for user in users if not user .technical_user ]
152+ return users
153+
154+ def _list_all_jobs (self , projects : List [Project ]) -> List [Job ]:
155+ return [
156+ job
157+ for project in projects
158+ if project .id
159+ for job in self ._job_manager .list_jobs (project .id )
160+ ]
161+
162+ def _list_all_services (self , projects : List [Project ]) -> List [Service ]:
163+ return [
164+ service
165+ for project in projects
166+ if project .id
167+ for service in self ._service_manager .list_services (project .id )
168+ ]
169+
170+ def _list_all_files (self , projects : List [Project ]) -> List [File ]:
171+ return [
172+ file
173+ for project in projects
174+ if project .id
175+ for file in self ._file_manager .list_files (project .id )
176+ ]
0 commit comments