mirror of
https://github.com/yavook/kiwi-simple-metrics.git
synced 2024-12-11 15:43:02 +00:00
"black" code style
This commit is contained in:
parent
e02a703af8
commit
f46068b49e
6 changed files with 48 additions and 58 deletions
|
@ -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,
|
||||
|
|
|
@ -11,7 +11,7 @@ Metric: TypeAlias = Callable[[], Report | None]
|
|||
__all__ = [
|
||||
"Report",
|
||||
"Metric",
|
||||
|
||||
#
|
||||
"cpu",
|
||||
"disk",
|
||||
"external",
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -24,13 +24,18 @@ def _hwdata() -> Iterator[ReportData]:
|
|||
|
||||
return str(path)
|
||||
|
||||
yield from sorted([
|
||||
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)
|
||||
)
|
||||
for path in SETTINGS.disk.paths
|
||||
],
|
||||
key=lambda d: d.value,
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
|
||||
def disk() -> Report | None:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue