From dc64ce1cfa74cde884402f9d620c9fabfc478cfd Mon Sep 17 00:00:00 2001 From: Inex Code Date: Fri, 26 Jul 2024 18:50:00 +0300 Subject: [PATCH] fix: Do not try to fetch stats if prometheus is not active --- selfprivacy_api/graphql/queries/monitoring.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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)