mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-22 04:01:27 +00:00
feat: add net usage
This commit is contained in:
parent
e0380d149b
commit
9a7930bc25
|
@ -146,3 +146,43 @@ class PrometheusQueries:
|
|||
end_timestamp,
|
||||
step,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def network_usage(
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60, # seconds
|
||||
) -> PrometheusQueryResult:
|
||||
"""
|
||||
Get network usage information for both download and upload.
|
||||
|
||||
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 network 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 = """
|
||||
(
|
||||
sum(rate(node_network_receive_bytes_total{device!="lo"}[5m])) as download,
|
||||
sum(rate(node_network_transmit_bytes_total{device!="lo"}[5m])) as upload
|
||||
)
|
||||
"""
|
||||
|
||||
return PrometheusQueries._send_query(
|
||||
query,
|
||||
start_timestamp,
|
||||
end_timestamp,
|
||||
step,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue