mirror of
https://github.com/yavook/kiwi-simple-metrics.git
synced 2024-11-25 08:53:00 +00:00
metrics._report return type
This commit is contained in:
parent
d73f5319ca
commit
b9259d306d
1 changed files with 12 additions and 15 deletions
|
@ -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
|
|
||||||
for path, percent in data
|
|
||||||
if (report := _report(
|
|
||||||
settings=SETTINGS.disk,
|
settings=SETTINGS.disk,
|
||||||
name=path,
|
name=path,
|
||||||
value=percent,
|
value=percent,
|
||||||
)) is not None
|
) for path, percent in data]
|
||||||
]
|
|
||||||
|
|
||||||
return Report(
|
return Report(
|
||||||
", ".join(report.result for report in reports),
|
", ".join(report.result for report in reports),
|
||||||
|
|
Loading…
Reference in a new issue