feat(ui): host page leads with its alerts; old readings say how old (#5210)
#5210 "Problems first, with since-time, across every list" (milestone 477), second batch. - Host page: an "Alerts on this host" strip first (rules on its agent series, its containers or its monitors: alert_svc.watches_host); its monitors problems first with "Down 12m · reason"; Docker above Ansible. - Uptime page and widget: worst 24h uptime first, then 7d and 30d; hosts with no uptime yet last. - Agent panel and fleet widget: last seen as "3m ago", not a clock time. - SNMP: a device is "Reachable" only while it answered within the last 3 polls; older readings say "No answer" with their age, and every device says when it was last read (was: reachable whenever any reading existed). - Traefik: a router missing from 3 scrapes in a row is listed apart as no longer reported, by when it was last seen, instead of keeping its last state (a removed router stayed Failing). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -113,6 +113,17 @@ async def host_status(db, host_ids: list[str]) -> dict[str, dict]:
|
||||
return out
|
||||
|
||||
|
||||
def worst_uptime_first(hosts, uptime: dict) -> list:
|
||||
"""Hosts by their 24h uptime, worst first, then 7d and 30d to break ties;
|
||||
hosts with no uptime yet (no monitors) last, by name."""
|
||||
def key(h):
|
||||
u = uptime.get(h.id) or {}
|
||||
windows = [u.get(w) for w in ("24h", "7d", "30d")]
|
||||
known = [w for w in windows if w is not None]
|
||||
return (not known, *[101.0 if w is None else w for w in windows], h.name.lower())
|
||||
return sorted(hosts, key=key)
|
||||
|
||||
|
||||
def host_rank(status: dict | None, agent: dict | None) -> int:
|
||||
"""Where a host sorts in the Hosts list: a down monitor first, then an
|
||||
agent gone quiet, then monitors not yet checked, then the healthy; a host
|
||||
@@ -137,16 +148,23 @@ async def host_monitors(db, host_id: str) -> list[dict]:
|
||||
recent = await _last_n_results(db, ids)
|
||||
uptime = await _uptime_by_monitor(db, ids)
|
||||
data = []
|
||||
from steward.monitors.service import down_reason, down_since
|
||||
for m in monitors:
|
||||
mrows = recent.get(m.id, [])
|
||||
latest = mrows[-1] if mrows else None
|
||||
state = ("paused" if not m.enabled else "pending" if latest is None
|
||||
else "up" if latest.is_up else "down")
|
||||
data.append({
|
||||
"monitor": m, "latest": latest,
|
||||
"monitor": m, "latest": latest, "state": state,
|
||||
"heartbeat": [{"state": "up" if r.is_up else "down",
|
||||
"title": _heartbeat_title(m.type, r)} for r in mrows],
|
||||
"uptime_pct": uptime.get(m.id, {}).get("24h"),
|
||||
"down_since": await down_since(db, m.id) if state == "down" else None,
|
||||
"down_reason": down_reason(latest) if state == "down" else None,
|
||||
})
|
||||
return data
|
||||
# Problems first: down, then not yet checked, then up, paused last.
|
||||
order = {"down": 0, "pending": 1, "up": 2, "paused": 3}
|
||||
return sorted(data, key=lambda d: (order[d["state"]], d["monitor"].name.lower()))
|
||||
|
||||
|
||||
async def agent_overview_by_host(
|
||||
|
||||
Reference in New Issue
Block a user