metrics._report return type

This commit is contained in:
Jörn-Michael Miehe 2023-08-31 11:02:23 +00:00
parent d73f5319ca
commit b9259d306d

View file

@ -17,10 +17,7 @@ def _report(
settings: MetricSettings, settings: MetricSettings,
name: str, name: str,
value: float, value: float,
) -> Report | None: ) -> Report:
if not settings.enabled:
return None
result = settings.report.format(name=name, value=value) result = settings.report.format(name=name, value=value)
if ( if (
@ -34,6 +31,9 @@ def _report(
def cpu_metric() -> Report | None: def cpu_metric() -> Report | None:
if not SETTINGS.cpu.enabled:
return None
value = psutil.cpu_percent(interval=1) value = psutil.cpu_percent(interval=1)
return _report( return _report(
settings=SETTINGS.cpu, settings=SETTINGS.cpu,
@ -46,26 +46,23 @@ def disk_metric() -> Report | None:
if not SETTINGS.disk.enabled: if not SETTINGS.disk.enabled:
return None return None
def vfs_to_percent(sv: os.statvfs_result) -> float: def path_to_free_percent(path: os.PathLike) -> float:
try: try:
sv = os.statvfs(path)
return sv.f_bavail / sv.f_blocks * 100 return sv.f_bavail / sv.f_blocks * 100
except ZeroDivisionError: except ZeroDivisionError:
return 0 return 0
data = sorted([ data = sorted([
(str(path), vfs_to_percent(os.statvfs(path))) (str(path), path_to_free_percent(path))
for path in SETTINGS.disk.paths for path in SETTINGS.disk.paths
], key=lambda d: d[1]) ], key=lambda d: d[1])
reports = [ reports = [_report(
report settings=SETTINGS.disk,
for path, percent in data name=path,
if (report := _report( value=percent,
settings=SETTINGS.disk, ) for path, percent in data]
name=path,
value=percent,
)) is not None
]
return Report( return Report(
", ".join(report.result for report in reports), ", ".join(report.result for report in reports),