diff --git a/kiwi_simple_metrics/metrics/_report.py b/kiwi_simple_metrics/metrics/_report.py index 695dd0e..0b799f4 100644 --- a/kiwi_simple_metrics/metrics/_report.py +++ b/kiwi_simple_metrics/metrics/_report.py @@ -8,23 +8,50 @@ from ..settings import SETTINGS, MetricSettings class ReportData: name: str value: float + threshold: float + inverted: bool + format: str @classmethod - def from_free_total(cls, *, name: str, free: float, total: float) -> Self: + def from_settings( + cls, *, + name: str, + value: float, + settings: MetricSettings, + ) -> Self: return cls( name=name, - value=(total - free) / total * 100, + value=value, + threshold=settings.threshold, + inverted=settings.inverted, + format=settings.report, ) - def report(self, settings: MetricSettings) -> "Report": + @classmethod + def from_free_total( + cls, *, + name: str, + free: float, + total: float, + settings: MetricSettings, + + ) -> Self: + return cls.from_settings( + name=name, + value=(total - free) / total * 100, + settings=settings, + ) + + @property + def report(self) -> "Report": return Report( - result=settings.report.format( + result=self.format.format( name=self.name, value=self.value, ), failed=( - self.value > settings.threshold and not settings.inverted - or self.value < settings.threshold and settings.inverted + self.value > self.threshold and not self.inverted + or self.value < self.threshold and self.inverted ), ) @@ -70,10 +97,7 @@ class Report: if not settings.enabled: return None - reports = [ - data.report(settings) - for data in get_data() - ] + reports = [data.report for data in get_data()] return cls( result=settings.report_outer.format( diff --git a/kiwi_simple_metrics/metrics/cpu.py b/kiwi_simple_metrics/metrics/cpu.py index 58947b1..6649ba1 100644 --- a/kiwi_simple_metrics/metrics/cpu.py +++ b/kiwi_simple_metrics/metrics/cpu.py @@ -7,9 +7,10 @@ from ._report import Report, ReportData def _hwdata() -> Iterator[ReportData]: - yield ReportData( + yield ReportData.from_settings( name=SETTINGS.cpu.name, value=psutil.cpu_percent(interval=1), + settings=SETTINGS.cpu, ) diff --git a/kiwi_simple_metrics/metrics/disk.py b/kiwi_simple_metrics/metrics/disk.py index 09ce3fb..cc78884 100644 --- a/kiwi_simple_metrics/metrics/disk.py +++ b/kiwi_simple_metrics/metrics/disk.py @@ -17,6 +17,7 @@ def _hwdata() -> Iterator[ReportData]: ReportData.from_free_total( name=str(path), **get_path_statvfs(path), + settings=SETTINGS.disk, ) for path in SETTINGS.disk.paths ], key=lambda d: d.value, reverse=True) diff --git a/kiwi_simple_metrics/metrics/external.py b/kiwi_simple_metrics/metrics/external.py index 681a6df..7759ed4 100644 --- a/kiwi_simple_metrics/metrics/external.py +++ b/kiwi_simple_metrics/metrics/external.py @@ -8,6 +8,9 @@ def _hwdata() -> Iterator[ReportData]: yield ReportData( name="Foo", value=69.42, + threshold=80, + inverted=False, + format=SETTINGS.cpu.report, ) diff --git a/kiwi_simple_metrics/metrics/memory.py b/kiwi_simple_metrics/metrics/memory.py index 6446042..52bbdd3 100644 --- a/kiwi_simple_metrics/metrics/memory.py +++ b/kiwi_simple_metrics/metrics/memory.py @@ -11,9 +11,10 @@ def _hwdata() -> Iterator[ReportData]: swap = psutil.swap_memory() if SETTINGS.memory.swap == "exclude": - yield ReportData( + yield ReportData.from_settings( name=SETTINGS.memory.name_ram, value=vmem.percent, + settings=SETTINGS.memory, ) elif SETTINGS.memory.swap == "combine": @@ -21,16 +22,19 @@ def _hwdata() -> Iterator[ReportData]: name=SETTINGS.memory.name, free=vmem.available + swap.free, total=vmem.total + swap.total, + settings=SETTINGS.memory, ) else: # SETTINGS.memory.swap == "include" - yield ReportData( + yield ReportData.from_settings( name=SETTINGS.memory.name_ram, value=vmem.percent, + settings=SETTINGS.memory, ) - yield ReportData( + yield ReportData.from_settings( name=SETTINGS.memory.name_swap, value=swap.percent, + settings=SETTINGS.memory, )