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

View file

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

View file

@ -5,7 +5,7 @@
from datetime import datetime from datetime import datetime
import pytest import pytest
from selfprivacy_api.utils.prometheus import PrometheusQueryResult from selfprivacy_api.utils.prometheus import MonitoringQueryResult
from tests.test_graphql.common import ( from tests.test_graphql.common import (
assert_empty, assert_empty,
get_data, get_data,
@ -78,7 +78,7 @@ def generate_mock_query_with_options(name):
def prometheus_result_from_dict(dict): 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 @pytest.fixture