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