From db96082a31c5522fcdffc9f7859dd604560821ab 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 22:10:33 +0000 Subject: [PATCH] add Report.concat(*reports) --- kiwi_simple_metrics/main.py | 17 +++++------------ kiwi_simple_metrics/metrics/__init__.py | 3 +++ kiwi_simple_metrics/metrics/_report.py | 21 ++++++++++++++++++++- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 59c49b0..d875093 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -1,19 +1,12 @@ from . import metrics -from .settings import SETTINGS def main() -> None: - # env parameters - print(SETTINGS.model_dump()) - - # CPU metric - print(metrics.cpu()) - - # MEM metric - print(metrics.memory()) - - # DISK metric - print(metrics.disk()) + print(metrics.Report.concat( + metrics.cpu(), + metrics.memory(), + metrics.disk(), + )) if __name__ == "__main__": diff --git a/kiwi_simple_metrics/metrics/__init__.py b/kiwi_simple_metrics/metrics/__init__.py index bada324..5eac512 100644 --- a/kiwi_simple_metrics/metrics/__init__.py +++ b/kiwi_simple_metrics/metrics/__init__.py @@ -1,8 +1,11 @@ +from ._report import Report from .cpu import cpu from .disk import disk from .memory import memory __all__ = [ + "Report", + "cpu", "disk", "memory", diff --git a/kiwi_simple_metrics/metrics/_report.py b/kiwi_simple_metrics/metrics/_report.py index 86cd0b4..3ccb57b 100644 --- a/kiwi_simple_metrics/metrics/_report.py +++ b/kiwi_simple_metrics/metrics/_report.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Callable, Iterator, Self +from typing import Any, Callable, Iterator, Self from ..settings import MetricSettings @@ -34,6 +34,25 @@ class Report: result: str failed: bool = False + @classmethod + def concat(cls, *_reports: Any) -> Self: + reports = [ + report + for report in _reports + if isinstance(report, Report) + ] + + return cls( + result=", ".join( + report.result + for report in reports + ), + failed=any( + report.failed + for report in reports + ), + ) + @classmethod def aggregate( cls, *,