fix: Do not try to fetch stats if prometheus is not active

This commit is contained in:
Inex Code 2024-07-26 18:50:00 +03:00
parent 8bd1a9354f
commit dc64ce1cfa

View file

@ -1,7 +1,9 @@
import strawberry import strawberry
from typing import Optional from typing import Optional
from datetime import datetime 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 @strawberry.type
@ -13,6 +15,9 @@ class Monitoring:
end: Optional[datetime] = None, end: Optional[datetime] = None,
step: int = 60, step: int = 60,
) -> MonitoringResponse: ) -> MonitoringResponse:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.disk_usage(start, end, step) return MonitoringQueries.disk_usage(start, end, step)
@strawberry.field @strawberry.field
@ -22,6 +27,9 @@ class Monitoring:
end: Optional[datetime] = None, end: Optional[datetime] = None,
step: int = 60, step: int = 60,
) -> MonitoringResponse: ) -> MonitoringResponse:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.memory_usage(start, end, step) return MonitoringQueries.memory_usage(start, end, step)
@strawberry.field @strawberry.field
@ -31,6 +39,9 @@ class Monitoring:
end: Optional[datetime] = None, end: Optional[datetime] = None,
step: int = 60, step: int = 60,
) -> MonitoringResponse: ) -> MonitoringResponse:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.cpu_usage(start, end, step) return MonitoringQueries.cpu_usage(start, end, step)
@strawberry.field @strawberry.field
@ -40,4 +51,7 @@ class Monitoring:
end: Optional[datetime] = None, end: Optional[datetime] = None,
step: int = 60, step: int = 60,
) -> MonitoringResponse: ) -> MonitoringResponse:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.cpu_usage(start, end, step) return MonitoringQueries.cpu_usage(start, end, step)