mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-11 10:29:30 +00:00
feat: add swap usage query
This commit is contained in:
parent
95a025d993
commit
389ec2c81c
|
@ -38,6 +38,13 @@ class MemoryMonitoring:
|
|||
|
||||
return MonitoringQueries.memory_usage_overall(self.start, self.end, self.step)
|
||||
|
||||
@strawberry.field
|
||||
def swap_usage_overall(self) -> MonitoringValuesResult:
|
||||
if Prometheus().get_status() != ServiceStatus.ACTIVE:
|
||||
return MonitoringQueryError(error="Prometheus is not running")
|
||||
|
||||
return MonitoringQueries.swap_usage_overall(self.start, self.end, self.step)
|
||||
|
||||
@strawberry.field
|
||||
def average_usage_by_service(self) -> MonitoringMetricsResult:
|
||||
if Prometheus().get_status() != ServiceStatus.ACTIVE:
|
||||
|
|
|
@ -264,6 +264,52 @@ class MonitoringQueries:
|
|||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def swap_usage_overall(
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60, # seconds
|
||||
) -> MonitoringValuesResult:
|
||||
"""
|
||||
Get swap memory usage.
|
||||
|
||||
Args:
|
||||
start (datetime, optional): The start time.
|
||||
Defaults to 20 minutes ago if not provided.
|
||||
end (datetime, optional): The end time.
|
||||
Defaults to current time if not provided.
|
||||
step (int): Interval in seconds for querying swap memory usage data.
|
||||
"""
|
||||
|
||||
if start is None:
|
||||
start = datetime.now() - timedelta(minutes=20)
|
||||
|
||||
if end is None:
|
||||
end = datetime.now()
|
||||
|
||||
start_timestamp = int(start.timestamp())
|
||||
end_timestamp = int(end.timestamp())
|
||||
|
||||
query = (
|
||||
"100 - (100 * (node_memory_SwapFree_bytes / node_memory_SwapTotal_bytes))"
|
||||
)
|
||||
|
||||
data = MonitoringQueries._send_range_query(
|
||||
query, start_timestamp, end_timestamp, step, result_type="matrix"
|
||||
)
|
||||
|
||||
if isinstance(data, MonitoringQueryError):
|
||||
return data
|
||||
|
||||
return MonitoringValues(
|
||||
values=list(
|
||||
map(
|
||||
MonitoringQueries._prometheus_value_to_monitoring_value,
|
||||
data["result"][0]["values"],
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def memory_usage_max_by_slice(
|
||||
start: Optional[datetime] = None,
|
||||
|
|
Loading…
Reference in a new issue