fix: rename Prometheus to Monitoring

This commit is contained in:
dettlaff 2024-07-26 16:20:31 +04:00
parent 1dae9046f2
commit b17b4c2682
3 changed files with 29 additions and 29 deletions

View file

@ -1,7 +1,7 @@
import strawberry
from typing import Optional
from datetime import datetime
from selfprivacy_api.utils.prometheus import PrometheusQueries, Response
from selfprivacy_api.utils.prometheus import MonitoringQueries, MonitoringResponse
@strawberry.type
@ -12,8 +12,8 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> Response:
return PrometheusQueries.disk_usage(start, end, step)
) -> MonitoringResponse:
return MonitoringQueries.disk_usage(start, end, step)
@strawberry.field
def memory_usage(
@ -21,8 +21,8 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> Response:
return PrometheusQueries.memory_usage(start, end, step)
) -> MonitoringResponse:
return MonitoringQueries.memory_usage(start, end, step)
@strawberry.field
def cpu_usage(
@ -30,8 +30,8 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> Response:
return PrometheusQueries.cpu_usage(start, end, step)
) -> MonitoringResponse:
return MonitoringQueries.cpu_usage(start, end, step)
@strawberry.field
def network_usage(
@ -39,5 +39,5 @@ class Monitoring:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60,
) -> Response:
return PrometheusQueries.cpu_usage(start, end, step)
) -> MonitoringResponse:
return MonitoringQueries.cpu_usage(start, end, step)

View file

@ -15,25 +15,25 @@ PROMETHEUS_URL = "http://localhost:9001"
@strawberry.type
@dataclass
class PrometheusQueryResult:
class MonitoringQueryResult:
result_type: str
result: JSON
@strawberry.type
class PrometheusQueryError:
class MonitoringQueryError:
error: str
Response = Annotated[
Union[PrometheusQueryResult, PrometheusQueryError],
strawberry.union("PrometheusQueryResponse"),
MonitoringResponse = Annotated[
Union[MonitoringQueryResult, MonitoringQueryError],
strawberry.union("MonitoringQueryResponse"),
]
class PrometheusQueries:
class MonitoringQueries:
@staticmethod
def _send_query(query: str, start: int, end: int, step: int) -> Response:
def _send_query(query: str, start: int, end: int, step: int) -> MonitoringResponse:
try:
response = requests.get(
f"{PROMETHEUS_URL}/api/v1/query",
@ -45,15 +45,15 @@ class PrometheusQueries:
},
)
if response.status_code != 200:
return PrometheusQueryError(
return MonitoringQueryError(
error="Prometheus returned unexpected HTTP status code"
)
json = response.json()
return PrometheusQueryResult(
return MonitoringQueryResult(
result_type=json["data"]["resultType"], result=json["data"]["result"]
)
except Exception as error:
return PrometheusQueryError(
return MonitoringQueryError(
error=f"Prometheus request failed! Error: {str(error)}"
)
@ -62,7 +62,7 @@ class PrometheusQueries:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60, # seconds
) -> Response:
) -> MonitoringResponse:
"""
Get CPU information.
@ -85,7 +85,7 @@ class PrometheusQueries:
query = '100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)'
return PrometheusQueries._send_query(
return MonitoringQueries._send_query(
query,
start_timestamp,
end_timestamp,
@ -97,7 +97,7 @@ class PrometheusQueries:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60, # seconds
) -> Response:
) -> MonitoringResponse:
"""
Get memory usage.
@ -120,7 +120,7 @@ class PrometheusQueries:
query = "100 - (100 * (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))"
return PrometheusQueries._send_query(
return MonitoringQueries._send_query(
query,
start_timestamp,
end_timestamp,
@ -132,7 +132,7 @@ class PrometheusQueries:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60, # seconds
) -> Response:
) -> MonitoringResponse:
"""
Get disk usage information.
@ -155,7 +155,7 @@ class PrometheusQueries:
query = """100 - (100 * sum by (device) (node_filesystem_avail_bytes{fstype!="rootfs"}) / sum by (device) (node_filesystem_size_bytes{fstype!="rootfs"}))"""
return PrometheusQueries._send_query(
return MonitoringQueries._send_query(
query,
start_timestamp,
end_timestamp,
@ -167,7 +167,7 @@ class PrometheusQueries:
start: Optional[datetime] = None,
end: Optional[datetime] = None,
step: int = 60, # seconds
) -> Response:
) -> MonitoringResponse:
"""
Get network usage information for both download and upload.
@ -195,7 +195,7 @@ class PrometheusQueries:
)
"""
return PrometheusQueries._send_query(
return MonitoringQueries._send_query(
query,
start_timestamp,
end_timestamp,

View file

@ -5,7 +5,7 @@
from datetime import datetime
import pytest
from selfprivacy_api.utils.prometheus import PrometheusQueryResult
from selfprivacy_api.utils.prometheus import MonitoringQueryResult
from tests.test_graphql.common import (
assert_empty,
get_data,
@ -78,7 +78,7 @@ def generate_mock_query_with_options(name):
def prometheus_result_from_dict(dict):
return PrometheusQueryResult(result_type=dict["resultType"], result=dict["result"])
return MonitoringQueryResult(result_type=dict["resultType"], result=dict["result"])
@pytest.fixture