From e0380d149b81213062e1ccf85f5720b8b13636e2 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Thu, 25 Jul 2024 16:48:34 +0300 Subject: [PATCH] chore: Review fixes --- selfprivacy_api/migrations/add_monitoring.py | 2 +- .../services/prometheus/__init__.py | 6 +- selfprivacy_api/utils/prometheus.py | 78 ++++++++++++------- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/selfprivacy_api/migrations/add_monitoring.py b/selfprivacy_api/migrations/add_monitoring.py index 965e234..82f7b64 100644 --- a/selfprivacy_api/migrations/add_monitoring.py +++ b/selfprivacy_api/migrations/add_monitoring.py @@ -33,5 +33,5 @@ class AddMonitoring(Migration): if "monitoring" not in data["modules"]: data["modules"]["monitoring"] = { "enable": True, - "location": BlockDevices().get_root_block_device().name + "location": BlockDevices().get_root_block_device().name, } diff --git a/selfprivacy_api/services/prometheus/__init__.py b/selfprivacy_api/services/prometheus/__init__.py index a7516f5..028709a 100644 --- a/selfprivacy_api/services/prometheus/__init__.py +++ b/selfprivacy_api/services/prometheus/__init__.py @@ -12,7 +12,7 @@ from selfprivacy_api.services.prometheus.icon import PROMETHEUS_ICON class Prometheus(Service): - """Class representing Pleroma service.""" + """Class representing Prometheus service.""" @staticmethod def get_id() -> str: @@ -24,7 +24,7 @@ class Prometheus(Service): @staticmethod def get_description() -> str: - return "Prometheus is a free software application used for event monitoring and alerting." + return "Prometheus is used for resource monitoring and alerts." @staticmethod def get_svg_icon() -> str: @@ -49,7 +49,7 @@ class Prometheus(Service): @staticmethod def get_backup_description() -> str: - return "For Prometheus backups are not available." + return "Backups are not available for Prometheus." @staticmethod def get_status() -> ServiceStatus: diff --git a/selfprivacy_api/utils/prometheus.py b/selfprivacy_api/utils/prometheus.py index 5547497..60f12aa 100644 --- a/selfprivacy_api/utils/prometheus.py +++ b/selfprivacy_api/utils/prometheus.py @@ -44,81 +44,105 @@ 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 (datetime, optional): The start time. Defaults to 20 minutes ago if not provided. - end (int, optional): Unix timestamp indicating the end time. + end (datetime, optional): 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()) + if start is None: + start = datetime.now() - timedelta(minutes=20) - if not end: - end = int(datetime.now().timestamp()) + if end is None: + end = datetime.now() + + start_timestamp = int(start.timestamp()) + end_timestamp = int(end.timestamp()) query = '100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)' - return PrometheusQueries._send_query(query, start, end, step) + return PrometheusQueries._send_query( + query, + start_timestamp, + end_timestamp, + step, + ) @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: """ Get memory usage. Args: - start (int, optional): Unix timestamp indicating the start time. + start (datetime, optional): The start time. Defaults to 20 minutes ago if not provided. - end (int, optional): Unix timestamp indicating the end time. + end (datetime, optional): The end time. Defaults to current time if not provided. step (int): Interval in seconds for querying memory usage data. """ - if not start: - start = int((datetime.now() - timedelta(minutes=20)).timestamp()) + if start is None: + start = datetime.now() - timedelta(minutes=20) - if not end: - end = int(datetime.now().timestamp()) + if end is None: + end = datetime.now() + + start_timestamp = int(start.timestamp()) + end_timestamp = int(end.timestamp()) query = "100 - (100 * (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))" - return PrometheusQueries._send_query(query, start, end, step) + return PrometheusQueries._send_query( + query, + start_timestamp, + end_timestamp, + step, + ) @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: """ Get disk usage information. Args: - start (int, optional): Unix timestamp indicating the start time. + start (datetime, optional): The start time. Defaults to 20 minutes ago if not provided. - end (int, optional): Unix timestamp indicating the end time. + end (datetime, optional): 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()) + if start is None: + start = datetime.now() - timedelta(minutes=20) - if not end: - end = int(datetime.now().timestamp()) + if end is None: + end = datetime.now() + + start_timestamp = int(start.timestamp()) + end_timestamp = int(end.timestamp()) 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(query, start, end, step) + return PrometheusQueries._send_query( + query, + start_timestamp, + end_timestamp, + step, + )