feat: add net usage

This commit is contained in:
dettlaff 2024-07-25 20:01:48 +04:00
parent e0380d149b
commit 9a7930bc25

View file

@ -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,
)