refactor: Wrap overall monitoring results to allow more specific reports

This commit is contained in:
Inex Code 2024-07-29 15:53:41 +03:00
parent 7c620d0056
commit 3b518b82f0
2 changed files with 96 additions and 34 deletions

View file

@ -11,6 +11,62 @@ from selfprivacy_api.utils.monitoring import (
)
@strawberry.type
class CpuMonitoring:
start: Optional[datetime]
end: Optional[datetime]
step: int
@strawberry.field
def overall_usage(self) -> MonitoringValuesResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.cpu_usage(self.start, self.end, self.step)
@strawberry.type
class MemoryMonitoring:
start: Optional[datetime]
end: Optional[datetime]
step: int
@strawberry.field
def overall_usage(self) -> MonitoringValuesResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.memory_usage(self.start, self.end, self.step)
@strawberry.type
class DiskMonitoring:
start: Optional[datetime]
end: Optional[datetime]
step: int
@strawberry.field
def overall_usage(self) -> MonitoringMetricsResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.disk_usage(self.start, self.end, self.step)
@strawberry.type
class NetworkMonitoring:
start: Optional[datetime]
end: Optional[datetime]
step: int
@strawberry.field
def overall_usage(self) -> MonitoringMetricsResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.network_usage(self.start, self.end, self.step)
@strawberry.type
class Monitoring:
@strawberry.field
@ -19,11 +75,8 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> MonitoringValuesResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.cpu_usage(start, end, step)
) -> CpuMonitoring:
return CpuMonitoring(start=start, end=end, step=step)
@strawberry.field
def memory_usage(
@ -31,11 +84,8 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> MonitoringValuesResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.memory_usage(start, end, step)
) -> MemoryMonitoring:
return MemoryMonitoring(start=start, end=end, step=step)
@strawberry.field
def disk_usage(
@ -43,11 +93,8 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> MonitoringMetricsResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.disk_usage(start, end, step)
) -> DiskMonitoring:
return DiskMonitoring(start=start, end=end, step=step)
@strawberry.field
def network_usage(
@ -55,8 +102,5 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> MonitoringMetricsResult:
if Prometheus().get_status() != ServiceStatus.ACTIVE:
return MonitoringQueryError(error="Prometheus is not running")
return MonitoringQueries.network_usage(start, end, step)
) -> NetworkMonitoring:
return NetworkMonitoring(start=start, end=end, step=step)

View file

@ -31,14 +31,24 @@ class MonitoringQueryError:
error: str
@strawberry.type
class MonitoringValues:
values: List[MonitoringValue]
@strawberry.type
class MonitoringMetrics:
metrics: List[MonitoringMetric]
MonitoringValuesResult = Annotated[
Union[List[MonitoringValue], MonitoringQueryError],
Union[MonitoringValues, MonitoringQueryError],
strawberry.union("MonitoringValuesResult"),
]
MonitoringMetricsResult = Annotated[
Union[List[MonitoringMetric], MonitoringQueryError],
Union[MonitoringMetrics, MonitoringQueryError],
strawberry.union("MonitoringMetricsResult"),
]
@ -131,10 +141,12 @@ class MonitoringQueries:
if isinstance(data, MonitoringQueryError):
return data
return list(
map(
MonitoringQueries._prometheus_value_to_monitoring_value,
data["result"][0]["values"],
return MonitoringValues(
values=list(
map(
MonitoringQueries._prometheus_value_to_monitoring_value,
data["result"][0]["values"],
)
)
)
@ -173,10 +185,12 @@ class MonitoringQueries:
if isinstance(data, MonitoringQueryError):
return data
return list(
map(
MonitoringQueries._prometheus_value_to_monitoring_value,
data["result"][0]["values"],
return MonitoringValues(
values=list(
map(
MonitoringQueries._prometheus_value_to_monitoring_value,
data["result"][0]["values"],
)
)
)
@ -215,8 +229,10 @@ class MonitoringQueries:
if isinstance(data, MonitoringQueryError):
return data
return MonitoringQueries._prometheus_response_to_monitoring_metrics(
data, "device"
return MonitoringMetrics(
metrics=MonitoringQueries._prometheus_response_to_monitoring_metrics(
data, "device"
)
)
@staticmethod
@ -259,6 +275,8 @@ class MonitoringQueries:
if isinstance(data, MonitoringQueryError):
return data
return MonitoringQueries._prometheus_response_to_monitoring_metrics(
data, "device"
return MonitoringMetrics(
metrics=MonitoringQueries._prometheus_response_to_monitoring_metrics(
data, "device"
)
)