diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 7d1b3f4..250d16b 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -12,10 +12,7 @@ def run_metrics( *_metrics: metrics.Metric, ) -> None: # run metrics in executor - tasks = [ - executor.submit(metric) - for metric in _metrics - ] + tasks = [executor.submit(metric) for metric in _metrics] # wait for finish # pair up each result with its task index @@ -25,10 +22,7 @@ def run_metrics( ) # extract reports in task index order - reports = ( - report - for _, report in sorted(results, key=lambda x: x[0]) - ) + reports = (report for _, report in sorted(results, key=lambda x: x[0])) # create summary report report = metrics.Report.summary(*reports) @@ -52,7 +46,8 @@ async def async_main_loop() -> None: await asyncio.gather( asyncio.sleep(SETTINGS.interval), loop.run_in_executor( - None, run_metrics, + None, + run_metrics, pool, # metrics are reported in this order metrics.cpu, diff --git a/kiwi_simple_metrics/metrics/__init__.py b/kiwi_simple_metrics/metrics/__init__.py index 8a2a250..70522fa 100644 --- a/kiwi_simple_metrics/metrics/__init__.py +++ b/kiwi_simple_metrics/metrics/__init__.py @@ -11,7 +11,7 @@ Metric: TypeAlias = Callable[[], Report | None] __all__ = [ "Report", "Metric", - + # "cpu", "disk", "external", diff --git a/kiwi_simple_metrics/metrics/_report.py b/kiwi_simple_metrics/metrics/_report.py index 5d8bac4..ca75baf 100644 --- a/kiwi_simple_metrics/metrics/_report.py +++ b/kiwi_simple_metrics/metrics/_report.py @@ -17,7 +17,8 @@ class ReportData: @classmethod def from_settings( - cls, *, + cls, + *, name: str, value: float, settings: MetricSettings, @@ -32,12 +33,12 @@ class ReportData: @classmethod def from_free_total( - cls, *, + cls, + *, name: str, free: float, total: float, settings: MetricSettings, - ) -> Self: return cls.from_settings( name=name, @@ -53,8 +54,10 @@ class ReportData: value=self.value, ), failed=( - self.value > self.threshold and not self.inverted - or self.value < self.threshold and self.inverted + self.value > self.threshold + and not self.inverted + or self.value < self.threshold + and self.inverted ), ) @@ -74,26 +77,17 @@ class Report: @classmethod def summary(cls, *_reports: Any) -> Self: - reports = [ - report - for report in _reports - if isinstance(report, Report) - ] + reports = [report for report in _reports if isinstance(report, Report)] return cls( - result=SETTINGS.separator.join( - report.result - for report in reports - ), - failed=any( - report.failed - for report in reports - ), + result=SETTINGS.separator.join(report.result for report in reports), + failed=any(report.failed for report in reports), ) @classmethod def aggregate( - cls, *, + cls, + *, settings: MetricSettings, get_data: Callable[[], Iterator[ReportData]], ) -> Self | None: @@ -109,24 +103,19 @@ class Report: result=settings.report_outer.format( name=settings.name, inner=SETTINGS.separator.join( - report.result - for report in reports[:settings.count] + report.result for report in reports[: settings.count] ), ), - failed=any( - report.failed - for report in reports - ), + failed=any(report.failed for report in reports), ) def push_webhook(self) -> None: - if (url := SETTINGS.webhook.url if not self.failed - else SETTINGS.webhook.fail) is None: + if ( + url := SETTINGS.webhook.url if not self.failed else SETTINGS.webhook.fail + ) is None: return requests.get( - url=str(url).format( - result=urllib.parse.quote_plus(self.result) - ), + url=str(url).format(result=urllib.parse.quote_plus(self.result)), verify=not SETTINGS.webhook.insecure, ) diff --git a/kiwi_simple_metrics/metrics/disk.py b/kiwi_simple_metrics/metrics/disk.py index ff072ed..d699ee8 100644 --- a/kiwi_simple_metrics/metrics/disk.py +++ b/kiwi_simple_metrics/metrics/disk.py @@ -24,13 +24,18 @@ def _hwdata() -> Iterator[ReportData]: return str(path) - yield from sorted([ - ReportData.from_free_total( - name=get_path_name(path), - **get_path_statvfs(path), - settings=SETTINGS.disk, - ) for path in SETTINGS.disk.paths - ], key=lambda d: d.value, reverse=True) + yield from sorted( + [ + ReportData.from_free_total( + name=get_path_name(path), + **get_path_statvfs(path), + settings=SETTINGS.disk, + ) + for path in SETTINGS.disk.paths + ], + key=lambda d: d.value, + reverse=True, + ) def disk() -> Report | None: diff --git a/kiwi_simple_metrics/metrics/external.py b/kiwi_simple_metrics/metrics/external.py index 46b9166..9e97108 100644 --- a/kiwi_simple_metrics/metrics/external.py +++ b/kiwi_simple_metrics/metrics/external.py @@ -36,11 +36,9 @@ def _hwdata() -> Iterator[ReportData]: # extract and check name (fail if empty) # => IndexError, AssertionError - assert (name := "".join( - char - for char in output[0] - if char.isprintable() - )[:100]) != "" + assert ( + name := "".join(char for char in output[0] if char.isprintable())[:100] + ) != "" # check exit status # => AssertionError @@ -66,7 +64,8 @@ def _hwdata() -> Iterator[ReportData]: # extract and check inversion # => AssertionError assert (inverted := output[2].strip().lower()) in ( - "normal", "inverted", + "normal", + "inverted", ) except (AssertionError, ValueError): @@ -85,10 +84,7 @@ def _hwdata() -> Iterator[ReportData]: format=SETTINGS.external.report, ) - yield from ( - parse_output(exe) - for exe in SETTINGS.external.executables - ) + yield from (parse_output(exe) for exe in SETTINGS.external.executables) def external() -> Report | None: diff --git a/kiwi_simple_metrics/settings/metric.py b/kiwi_simple_metrics/settings/metric.py index ff8dae3..795708b 100644 --- a/kiwi_simple_metrics/settings/metric.py +++ b/kiwi_simple_metrics/settings/metric.py @@ -38,7 +38,12 @@ class MetricSettings(BaseModel): except ValueError: if str(value).strip().lower() not in ( - "none", "null", "all", "yes", "any", "full", + "none", + "null", + "all", + "yes", + "any", + "full", "oddly_specific_value_42", ): print(