Skip to content

Commit 78bd5ea

Browse files
committed
Implement statistics endpoint
1 parent 09a5d9d commit 78bd5ea

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

backend/src/contaxy/api/endpoints/system.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ def check_health(
6969
responses={**AUTH_ERROR_RESPONSES},
7070
)
7171
def get_system_statistics(
72+
include_technical: bool = True,
7273
component_manager: ComponentManager = Depends(get_component_manager),
7374
token: str = Depends(get_api_token),
7475
) -> Any:
7576
"""Returns statistics about this instance."""
7677
component_manager.verify_access(token, "system", AccessLevel.READ)
77-
return component_manager.get_system_manager().get_system_statistics()
78+
return component_manager.get_system_manager().get_system_statistics(
79+
include_technical
80+
)
7881

7982

8083
@router.post(

backend/src/contaxy/clients/system.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ def is_healthy(self, request_kwargs: Dict = {}) -> bool:
2222
handle_errors(response)
2323
return True
2424

25-
def get_system_statistics(self, request_kwargs: Dict = {}) -> SystemStatistics:
26-
response = self._client.get("/system/statistics", **request_kwargs)
25+
def get_system_statistics(
26+
self, include_technical: bool = True, request_kwargs: Dict = {}
27+
) -> SystemStatistics:
28+
response = self._client.get(
29+
"/system/statistics",
30+
params={"include_technical": include_technical},
31+
**request_kwargs,
32+
)
2733
handle_errors(response)
2834
return parse_raw_as(SystemStatistics, response.text)
2935

backend/src/contaxy/managers/components.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def get_system_manager(self) -> SystemManager:
160160
self.get_json_db_manager(),
161161
self.get_auth_manager(),
162162
self.get_project_manager(),
163+
self.get_service_manager(),
164+
self.get_job_manager(),
165+
self.get_file_manager(),
163166
)
164167

165168
assert self._system_manager is not None

backend/src/contaxy/managers/system.py

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
from typing import Optional
1+
from typing import List, Optional
22

33
from contaxy import __version__, config
44
from contaxy.config import settings
55
from 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+
)
712
from contaxy.operations.json_db import JsonDocumentOperations
813
from 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
1123
from contaxy.schema.system import SystemInfo, SystemStatistics
1224
from contaxy.utils import auth_utils
1325
from 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+
]

backend/src/contaxy/operations/system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def is_healthy(self) -> bool:
1313
pass
1414

1515
@abstractmethod
16-
def get_system_statistics(self) -> SystemStatistics:
16+
def get_system_statistics(self, include_technical: bool) -> SystemStatistics:
1717
pass
1818

1919
@abstractmethod

0 commit comments

Comments
 (0)