feat(ui): shared |bytes |rate |duration |ago filters and status_class from Settings → Thresholds (#5093)
CI / lint (push) Successful in 3s
CI / unit (push) Successful in 1m28s
CI / integration (push) Successful in 1m7s
CI / publish (push) Successful in 2m40s

#5093 "Shared format filters and status_class driven by Settings → Thresholds".

- steward/core/formatting.py: human_bytes (binary units), human_rate,
  human_duration (two largest units), ago (a <time> titled with the absolute
  UTC time) and status_class(value, kind, thresholds) -> ok/warn/crit/unknown,
  which raises on a kind the thresholds don't name.
- app.py registers them as the bytes, rate, duration and ago filters and the
  status_class global; threshold_style is now graded by status_class (and caps
  its weight at 500).
- Replaces the copies: docker's _human_bytes/_human_uptime, core alerts'
  _duration, the host-agent fmt_bps/fmt_bytes macros, the Traefik and UniFi
  byte-rate arithmetic, the swarm-failures ago macro and two uptime spans.
- Tests: a table per helper, and every template compiled against the app's
  filters, so an unknown filter fails CI rather than a page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 14:25:06 -04:00
co-authored by Claude Opus 5.5
parent 65411d3ce8
commit bf08169176
17 changed files with 287 additions and 186 deletions
+12 -25
View File
@@ -427,9 +427,9 @@ def to_thresholds_cfg(settings: dict[str, Any]) -> dict:
"""Per-metric (warn, crit, direction) policy for degraded-value coloring.
Centralizes "what counts as degraded" so templates just name a metric kind
via the threshold_style() jinja global. `dir` is "high" (higher is worse,
e.g. CPU) or "low" (lower is worse, e.g. uptime %). Latency reuses the
existing ping good/warn thresholds.
via the status_class() and threshold_style() jinja globals. `dir` is
"high" (higher is worse, e.g. CPU) or "low" (lower is worse, e.g. uptime
%). Latency reuses the existing ping good/warn thresholds.
"""
g = settings.get
return {
@@ -445,31 +445,18 @@ def to_thresholds_cfg(settings: dict[str, Any]) -> dict:
}
def threshold_style_for(value: Any, kind: str, thresholds: dict) -> str:
"""Return an inline-style fragment (amber at warn, red at crit) for a metric.
_STATUS_STYLE = {"warn": "color:var(--yellow);font-weight:500;",
"crit": "color:var(--red);font-weight:500;"}
The Python twin of the old _macros.metric_style, but reading configurable
cutoffs from `thresholds` (see to_thresholds_cfg) and handling both
directions. Empty string when normal/unknown/None — drop straight into a
def threshold_style_for(value: Any, kind: str, thresholds: dict) -> str:
"""Return an inline-style fragment (amber at warn, red at crit) for a metric,
graded by formatting.status_class against `thresholds` (to_thresholds_cfg).
Empty string when the reading is normal or missing — drop straight into a
span's style="".
"""
if value is None:
return ""
th = thresholds.get(kind)
if not th:
return ""
warn, crit, direction = th["warn"], th["crit"], th.get("dir", "high")
if direction == "low":
if value < crit:
return "color:var(--red);font-weight:700;"
if value < warn:
return "color:var(--yellow);font-weight:600;"
else:
if value >= crit:
return "color:var(--red);font-weight:700;"
if value >= warn:
return "color:var(--yellow);font-weight:600;"
return ""
from steward.core.formatting import status_class
return _STATUS_STYLE.get(status_class(value, kind, thresholds), "")
def to_plugins_cfg(settings: dict[str, Any]) -> dict: