2023-08-31 15:31:25 +00:00
|
|
|
from dataclasses import dataclass
|
2023-08-31 22:10:33 +00:00
|
|
|
from typing import Any, Callable, Iterator, Self
|
2023-08-31 11:17:19 +00:00
|
|
|
|
2023-08-31 22:25:18 +00:00
|
|
|
from ..settings import SETTINGS, MetricSettings
|
2023-08-31 11:17:19 +00:00
|
|
|
|
|
|
|
|
2023-08-31 15:23:00 +00:00
|
|
|
@dataclass(slots=True, kw_only=True)
|
|
|
|
class ReportData:
|
|
|
|
name: str
|
|
|
|
value: float
|
2023-09-01 15:18:57 +00:00
|
|
|
threshold: float
|
|
|
|
inverted: bool
|
|
|
|
format: str
|
2023-08-31 15:23:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-09-01 15:18:57 +00:00
|
|
|
def from_settings(
|
|
|
|
cls, *,
|
|
|
|
name: str,
|
|
|
|
value: float,
|
|
|
|
settings: MetricSettings,
|
|
|
|
) -> Self:
|
2023-08-31 15:23:00 +00:00
|
|
|
return cls(
|
2023-09-01 15:18:57 +00:00
|
|
|
name=name,
|
|
|
|
value=value,
|
|
|
|
threshold=settings.threshold,
|
|
|
|
inverted=settings.inverted,
|
|
|
|
format=settings.report,
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_free_total(
|
|
|
|
cls, *,
|
|
|
|
name: str,
|
|
|
|
free: float,
|
|
|
|
total: float,
|
|
|
|
settings: MetricSettings,
|
|
|
|
|
|
|
|
) -> Self:
|
|
|
|
return cls.from_settings(
|
2023-08-31 15:23:00 +00:00
|
|
|
name=name,
|
|
|
|
value=(total - free) / total * 100,
|
2023-09-01 15:18:57 +00:00
|
|
|
settings=settings,
|
2023-08-31 15:23:00 +00:00
|
|
|
)
|
|
|
|
|
2023-09-01 15:18:57 +00:00
|
|
|
@property
|
|
|
|
def report(self) -> "Report":
|
2023-08-31 15:31:25 +00:00
|
|
|
return Report(
|
2023-09-01 15:18:57 +00:00
|
|
|
result=self.format.format(
|
2023-08-31 15:31:25 +00:00
|
|
|
name=self.name,
|
|
|
|
value=self.value,
|
|
|
|
),
|
|
|
|
failed=(
|
2023-09-01 15:18:57 +00:00
|
|
|
self.value > self.threshold and not self.inverted
|
|
|
|
or self.value < self.threshold and self.inverted
|
2023-08-31 15:31:25 +00:00
|
|
|
),
|
2023-08-31 15:23:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-31 15:31:25 +00:00
|
|
|
@dataclass(slots=True, kw_only=True, frozen=True)
|
2023-08-31 11:17:19 +00:00
|
|
|
class Report:
|
|
|
|
result: str
|
2023-08-31 15:31:25 +00:00
|
|
|
failed: bool = False
|
2023-08-31 11:17:19 +00:00
|
|
|
|
2023-08-31 23:09:50 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
state = "OK" if not self.failed else "FAIL"
|
|
|
|
|
2023-08-31 23:49:02 +00:00
|
|
|
return SETTINGS.log.format.format(
|
2023-08-31 23:09:50 +00:00
|
|
|
state=state,
|
|
|
|
result=self.result,
|
|
|
|
)
|
|
|
|
|
2023-08-31 22:10:33 +00:00
|
|
|
@classmethod
|
2023-08-31 23:49:02 +00:00
|
|
|
def summary(cls, *_reports: Any) -> Self:
|
2023-08-31 22:10:33 +00:00
|
|
|
reports = [
|
|
|
|
report
|
|
|
|
for report in _reports
|
|
|
|
if isinstance(report, Report)
|
|
|
|
]
|
|
|
|
|
|
|
|
return cls(
|
2023-08-31 22:25:18 +00:00
|
|
|
result=SETTINGS.separator.join(
|
2023-08-31 22:10:33 +00:00
|
|
|
report.result
|
|
|
|
for report in reports
|
|
|
|
),
|
|
|
|
failed=any(
|
|
|
|
report.failed
|
|
|
|
for report in reports
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-08-31 11:17:19 +00:00
|
|
|
@classmethod
|
2023-08-31 15:23:00 +00:00
|
|
|
def aggregate(
|
2023-08-31 11:17:19 +00:00
|
|
|
cls, *,
|
|
|
|
settings: MetricSettings,
|
2023-08-31 15:31:25 +00:00
|
|
|
get_data: Callable[[], Iterator[ReportData]],
|
|
|
|
) -> Self | None:
|
|
|
|
if not settings.enabled:
|
|
|
|
return None
|
|
|
|
|
2023-09-01 15:18:57 +00:00
|
|
|
reports = [data.report for data in get_data()]
|
2023-08-31 11:17:19 +00:00
|
|
|
|
2023-08-31 15:23:00 +00:00
|
|
|
return cls(
|
2023-08-31 15:31:25 +00:00
|
|
|
result=settings.report_outer.format(
|
2023-08-31 15:23:00 +00:00
|
|
|
name=settings.name,
|
2023-08-31 22:25:18 +00:00
|
|
|
inner=SETTINGS.separator.join(
|
2023-08-31 15:23:00 +00:00
|
|
|
report.result
|
|
|
|
for report in reports[:settings.count]
|
|
|
|
),
|
|
|
|
),
|
|
|
|
failed=any(
|
|
|
|
report.failed
|
|
|
|
for report in reports
|
|
|
|
),
|
|
|
|
)
|