From 63dc67f84e485f101d2e82adfad2558212db06f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:09:19 +0000 Subject: [PATCH] defaults handling and metric names --- .vscode/launch.json | 2 -- kiwi_simple_metrics/metrics.py | 4 ++-- kiwi_simple_metrics/settings.py | 23 ++++++++++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0ff1e92..5872269 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,8 +10,6 @@ "request": "launch", "module": "kiwi_simple_metrics.main", "env": { - "METRIC_DISK__THRESHOLD": "15", // workaround! - "METRIC_DISK__INVERTED": "True", // workaround! "METRIC_DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]", }, "justMyCode": true diff --git a/kiwi_simple_metrics/metrics.py b/kiwi_simple_metrics/metrics.py index 85e3432..58242d9 100644 --- a/kiwi_simple_metrics/metrics.py +++ b/kiwi_simple_metrics/metrics.py @@ -37,7 +37,7 @@ def cpu_metric() -> Report | None: value = psutil.cpu_percent(interval=1) return _report( settings=SETTINGS.cpu, - name="CPU", + name=SETTINGS.cpu.name, value=value, ) @@ -71,7 +71,7 @@ def disk_metric() -> Report | None: return Report( SETTINGS.disk.report_outer.format( - name="DISK FREE", + name=SETTINGS.disk.name, inner=report_inner, ), failed=any( diff --git a/kiwi_simple_metrics/settings.py b/kiwi_simple_metrics/settings.py index 8852d95..8011ee0 100644 --- a/kiwi_simple_metrics/settings.py +++ b/kiwi_simple_metrics/settings.py @@ -5,6 +5,9 @@ from pydantic_settings import BaseSettings, SettingsConfigDict class MetricSettings(BaseModel): + # metric name + name: str + # metric will be reported enabled: bool = True @@ -18,7 +21,21 @@ class MetricSettings(BaseModel): inverted: bool = False +class CpuMS(MetricSettings): + name: str = "CPU" + threshold: float = math.inf + + +class MemoryMS(MetricSettings): + name: str = "Memory" + threshold: float = 90 + + class DiskMS(MetricSettings): + name: str = "Disk Free" + threshold: float = 15 + inverted: bool = True + # outer format string for reporting report_outer: str = "{name}: [{inner}]" @@ -35,9 +52,9 @@ class Settings(BaseSettings): env_nested_delimiter="__", ) - cpu: MetricSettings = MetricSettings(threshold=math.inf) - memory: MetricSettings = MetricSettings(threshold=90) - disk: DiskMS = DiskMS(threshold=15, inverted=True) + cpu: CpuMS = CpuMS() + memory: MemoryMS = MemoryMS() + disk: DiskMS = DiskMS() SETTINGS = Settings()