diff --git a/selfprivacy_api/graphql/queries/monitoring.py b/selfprivacy_api/graphql/queries/monitoring.py index 81ce16a..eab43b6 100644 --- a/selfprivacy_api/graphql/queries/monitoring.py +++ b/selfprivacy_api/graphql/queries/monitoring.py @@ -1,7 +1,9 @@ import strawberry from typing import Optional from datetime import datetime -from selfprivacy_api.utils.monitoring import MonitoringQueries, MonitoringResponse +from selfprivacy_api.models.services import ServiceStatus +from selfprivacy_api.services.prometheus import Prometheus +from selfprivacy_api.utils.monitoring import MonitoringQueries, MonitoringQueryError, MonitoringResponse @strawberry.type @@ -13,6 +15,9 @@ class Monitoring: end: Optional[datetime] = None, step: int = 60, ) -> MonitoringResponse: + if Prometheus().get_status() != ServiceStatus.ACTIVE: + return MonitoringQueryError(error="Prometheus is not running") + return MonitoringQueries.disk_usage(start, end, step) @strawberry.field @@ -22,6 +27,9 @@ class Monitoring: end: Optional[datetime] = None, step: int = 60, ) -> MonitoringResponse: + if Prometheus().get_status() != ServiceStatus.ACTIVE: + return MonitoringQueryError(error="Prometheus is not running") + return MonitoringQueries.memory_usage(start, end, step) @strawberry.field @@ -31,6 +39,9 @@ class Monitoring: end: Optional[datetime] = None, step: int = 60, ) -> MonitoringResponse: + if Prometheus().get_status() != ServiceStatus.ACTIVE: + return MonitoringQueryError(error="Prometheus is not running") + return MonitoringQueries.cpu_usage(start, end, step) @strawberry.field @@ -40,4 +51,7 @@ class Monitoring: end: Optional[datetime] = None, step: int = 60, ) -> MonitoringResponse: + if Prometheus().get_status() != ServiceStatus.ACTIVE: + return MonitoringQueryError(error="Prometheus is not running") + return MonitoringQueries.cpu_usage(start, end, step)