mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-22 04:01:27 +00:00
fix: use GraphQL DateTime type for start and end arguments in monitoring.py
This commit is contained in:
parent
2797b0b331
commit
3bddfb15c4
|
@ -1,5 +1,6 @@
|
|||
import strawberry
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from selfprivacy_api.utils.prometheus import PrometheusQueries, PrometheusQueryResult
|
||||
|
||||
|
||||
|
@ -8,8 +9,8 @@ class Monitoring:
|
|||
@strawberry.field
|
||||
def disk_usage(
|
||||
self,
|
||||
start: Optional[int] = None,
|
||||
end: Optional[int] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60,
|
||||
) -> PrometheusQueryResult:
|
||||
return PrometheusQueries.disk_usage(start, end, step)
|
||||
|
@ -17,8 +18,8 @@ class Monitoring:
|
|||
@strawberry.field
|
||||
def memory_usage(
|
||||
self,
|
||||
start: Optional[int] = None,
|
||||
end: Optional[int] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60,
|
||||
) -> PrometheusQueryResult:
|
||||
return PrometheusQueries.memory_usage(start, end, step)
|
||||
|
@ -26,8 +27,8 @@ class Monitoring:
|
|||
@strawberry.field
|
||||
def cpu_usage(
|
||||
self,
|
||||
start: Optional[int] = None,
|
||||
end: Optional[int] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60,
|
||||
) -> PrometheusQueryResult:
|
||||
return PrometheusQueries.cpu_usage(start, end, step)
|
||||
|
|
|
@ -22,14 +22,14 @@ class PrometheusQueryResult:
|
|||
|
||||
class PrometheusQueries:
|
||||
@staticmethod
|
||||
def _send_query(query: str, start: int, end: int, step: int):
|
||||
def _send_query(query: str, start: datetime, end: datetime, step: int):
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{PROMETHEUS_URL}/api/v1/query_range",
|
||||
params={
|
||||
"query": query,
|
||||
"start": start,
|
||||
"end": end,
|
||||
"start": int(start.timestamp()),
|
||||
"end": int(start.timestamp()),
|
||||
"step": step,
|
||||
},
|
||||
)
|
||||
|
@ -47,26 +47,26 @@ class PrometheusQueries:
|
|||
|
||||
@staticmethod
|
||||
def cpu_usage(
|
||||
start: Optional[int] = None,
|
||||
end: Optional[int] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60, # seconds
|
||||
) -> PrometheusQueryResult:
|
||||
"""
|
||||
Get CPU information.
|
||||
|
||||
Args:
|
||||
start (int, optional): Unix timestamp indicating the start time.
|
||||
start (int, optional): Unix timestamp (in seconds) indicating the start time.
|
||||
Defaults to 20 minutes ago if not provided.
|
||||
end (int, optional): Unix timestamp indicating the end time.
|
||||
end (int, optional): Unix timestamp (in seconds) indicating the end time.
|
||||
Defaults to current time if not provided.
|
||||
step (int): Interval in seconds for querying disk usage data.
|
||||
"""
|
||||
|
||||
if not start:
|
||||
start = int((datetime.now() - timedelta(minutes=20)).timestamp())
|
||||
start = datetime.now() - timedelta(minutes=20)
|
||||
|
||||
if not end:
|
||||
end = int(datetime.now().timestamp())
|
||||
end = datetime.now()
|
||||
|
||||
query = '100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)'
|
||||
|
||||
|
@ -74,8 +74,8 @@ class PrometheusQueries:
|
|||
|
||||
@staticmethod
|
||||
def memory_usage(
|
||||
start: Optional[int] = None,
|
||||
end: Optional[int] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60, # seconds
|
||||
) -> PrometheusQueryResult:
|
||||
"""
|
||||
|
@ -90,10 +90,10 @@ class PrometheusQueries:
|
|||
"""
|
||||
|
||||
if not start:
|
||||
start = int((datetime.now() - timedelta(minutes=20)).timestamp())
|
||||
start = datetime.now() - timedelta(minutes=20)
|
||||
|
||||
if not end:
|
||||
end = int(datetime.now().timestamp())
|
||||
end = datetime.now()
|
||||
|
||||
query = "100 - (100 * (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))"
|
||||
|
||||
|
@ -101,8 +101,8 @@ class PrometheusQueries:
|
|||
|
||||
@staticmethod
|
||||
def disk_usage(
|
||||
start: Optional[int] = None,
|
||||
end: Optional[int] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
step: int = 60, # seconds
|
||||
) -> PrometheusQueryResult:
|
||||
"""
|
||||
|
@ -117,10 +117,10 @@ class PrometheusQueries:
|
|||
"""
|
||||
|
||||
if not start:
|
||||
start = int((datetime.now() - timedelta(minutes=20)).timestamp())
|
||||
start = datetime.now() - timedelta(minutes=20)
|
||||
|
||||
if not end:
|
||||
end = int(datetime.now().timestamp())
|
||||
end = datetime.now()
|
||||
|
||||
query = '100 - (100 * ((node_filesystem_avail_bytes{mountpoint="/",fstype!="rootfs"} ) / (node_filesystem_size_bytes{mountpoint="/",fstype!="rootfs"}) ))'
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
# pylint: disable=unused-argument
|
||||
# pylint: disable=missing-function-docstring
|
||||
|
||||
from datetime import datetime
|
||||
import pytest
|
||||
|
||||
from selfprivacy_api.utils.prometheus import PrometheusQueryResult
|
||||
|
@ -68,7 +69,7 @@ def generate_mock_query(name):
|
|||
|
||||
def generate_mock_query_with_options(name):
|
||||
return f"""
|
||||
query Query($start: Int, $end: Int, $step: Int) {{
|
||||
query Query($start: DateTime, $end: DateTime, $step: Int) {{
|
||||
monitoring {{
|
||||
{name}(start: $start, end: $end, step: $step) {{ resultType, result }}
|
||||
}}
|
||||
|
@ -131,8 +132,8 @@ def test_graphql_get_disk_usage_with_options(
|
|||
json={
|
||||
"query": generate_mock_query_with_options("diskUsage"),
|
||||
"variables": {
|
||||
"start": 1720136108,
|
||||
"end": 1720137319,
|
||||
"start": datetime.fromtimestamp(1720136108).isoformat(),
|
||||
"end": datetime.fromtimestamp(1720137319).isoformat(),
|
||||
"step": 90,
|
||||
},
|
||||
},
|
||||
|
@ -168,8 +169,8 @@ def test_graphql_get_memory_usage_with_options(
|
|||
json={
|
||||
"query": generate_mock_query_with_options("memoryUsage"),
|
||||
"variables": {
|
||||
"start": 1720136108,
|
||||
"end": 1720137319,
|
||||
"start": datetime.fromtimestamp(1720136108).isoformat(),
|
||||
"end": datetime.fromtimestamp(1720137319).isoformat(),
|
||||
"step": 90,
|
||||
},
|
||||
},
|
||||
|
@ -203,8 +204,8 @@ def test_graphql_get_cpu_usage_with_options(client, authorized_client, mock_cpu_
|
|||
json={
|
||||
"query": generate_mock_query_with_options("cpuUsage"),
|
||||
"variables": {
|
||||
"start": 1720136108,
|
||||
"end": 1720137319,
|
||||
"start": datetime.fromtimestamp(1720136108).isoformat(),
|
||||
"end": datetime.fromtimestamp(1720137319).isoformat(),
|
||||
"step": 90,
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue