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

View file

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