Internet health from UniFi: Needs attention line and a 24 hour strip
Lint / JS (eslint) (push) Successful in 1m10s
Lint / Python (flake8) (push) Successful in 2m31s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m26s
Lint / Python (flake8) (pull_request) Successful in 1m4s
Test / Python Tests (pytest) (push) Successful in 1m13s
Lint / JS (eslint) (pull_request) Successful in 7s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 1m0s
Test / Python Tests (pytest) (pull_request) Successful in 1m3s

UniFi already rates the WAN every 5 minutes (fine, high latency, packet
loss, down), the same timeline its Network app charts. The monitor now
reads the last day of it from the dashboard API each cycle, read only,
instead of Gandalf pinging on its own.

- modem.wan_quality() turns the dashboard data into periods, drops and
  current service latencies; stored as monitor_state.wan_quality.
- Needs attention: "Internet: packet loss in N of the last 12 five-minute
  periods" (2+ lossy periods, red), "high latency" (3+ bad periods,
  amber), and drops in the last hour. Stale copies are ignored.
- WAN / Fiber panel: a 24 hour strip with a legend and latency now,
  also shown while the modem itself can't be read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-16 12:38:39 -05:00
1 parent f1355c4bd8
commit 6a717af3f2
11 files changed
+344 -8

No files matched your search

+50
View File
@@ -310,3 +310,53 @@ def record_uptime_reset(history: dict, boot_ts: float) -> None:
return
restarts.append({'at': boot_ts, 'by': None, 'confirmed': True})
del restarts[:-_HISTORY_LIMIT]
# ---------------------------------------------------------------------------
# WAN quality: UniFi's own 5 minute health timeline (high latency, packet loss, down),
# the same data its Network app charts. Read only; Gandalf doesn't measure it again.
# ---------------------------------------------------------------------------
QUALITY_STATES = ('ok', 'latency', 'loss', 'down') # mildest to worst
QUALITY_PERIOD_S = 300
def wan_quality(dashboard: dict, now: float) -> Optional[dict]:
"""The primary WAN's health timeline from UniFi's aggregated dashboard, or None.
Returns {'wan', 'periods': [{'ts', 'state'}], 'downtime': [{'ts', 'seconds'}],
'latencies': {service: ms}, 'updated'}. Each period is 5 minutes; its state is the
worst flag UniFi set for it.
"""
details = ((dashboard or {}).get('wan_history') or {}).get('wan_history_details') or []
if not details:
return None
primary = next((d for d in details if d.get('network_group') == 'WAN'), details[0])
periods = []
for h in primary.get('health_history') or []:
if not isinstance(h.get('timestamp'), (int, float)):
continue
state = ('down' if h.get('wan_downtime') else 'loss' if h.get('packet_loss')
else 'latency' if h.get('high_latency') else 'ok')
periods.append({'ts': h['timestamp'] / 1000, 'state': state})
periods.sort(key=lambda p: p['ts'])
downtime = sorted(
({'ts': d['timestamp'] / 1000, 'seconds': d.get('downtime_in_seconds') or 0}
for d in primary.get('downtime_history') or [] if isinstance(d.get('timestamp'), (int, float))),
key=lambda d: d['ts'])
latencies = {}
for w in ((dashboard.get('wan') or {}).get('wan_details') or []):
if w.get('network_group') == primary.get('network_group'):
for s in (w.get('stats') or {}).get('service_latencies') or []:
if s.get('service_name') and isinstance(s.get('latency'), (int, float)):
latencies[s['service_name']] = s['latency']
return {'wan': primary.get('network_group') or '', 'periods': periods, 'downtime': downtime,
'latencies': latencies, 'updated': now}
def quality_counts(periods: List[dict], since: float) -> Dict[str, int]:
"""How many periods starting at or after *since* were in each state."""
counts = {s: 0 for s in QUALITY_STATES}
for p in periods:
if p['ts'] >= since and p['state'] in counts:
counts[p['state']] += 1
return counts