WAN: ticket sustained slowdowns; reboot drops no longer mean "unstable"
Lint / JS (eslint) (push) Successful in 11s
Lint / Python (flake8) (push) Successful in 1m28s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m38s
Lint / Python (flake8) (pull_request) Successful in 57s
Test / Python Tests (pytest) (push) Successful in 1m20s
Lint / JS (eslint) (pull_request) Successful in 17s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 2m54s
Test / Python Tests (pytest) (pull_request) Successful in 3m9s

On 2026-11-23/24 the WAN was degraded for about 20 hours with no ticket,
and the only ticket came from the owner's reboot fixing it.

- wan_degraded (P2): half or more of the last 2 hours of UniFi WAN health
  periods slow, lossy or down. Closes with a recovery note once the last
  2 hours are at most a quarter bad. Mentions a slow ping from the modem.
- "WAN connection unstable" waits 10 minutes, then counts only drops that
  no modem restart explains (Smart Power, uptime reset, or the Restart
  modem button, folded into one restart when within 15 minutes).
- Both list the last 7 days of restarts and how long each lasted, and
  suggest asking AT&T for a replacement after 2+ restarts in a week.

Closes #58

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-23 23:51:26 -05:00
1 parent bba483bb05
commit a31437f2e5
5 files changed
+346 -17

No files matched your search

+103
View File
@@ -383,6 +383,109 @@ def quality_counts(periods: List[dict], since: float) -> Dict[str, int]:
return counts
# A sustained slowdown: half of the last two hours' 5 minute periods slow, lossy or down.
# It counts as over once the last two hours have at most a quarter of their periods bad,
# so one good stretch in the middle of a bad day doesn't close and reopen it.
DEGRADED_WINDOW_S = 7200
DEGRADED_FRACTION = 0.5
DEGRADED_MIN_PERIODS = 12
RECOVERED_WINDOW_S = 7200
RECOVERED_FRACTION = 0.25
def _bad_share(periods: List[dict], since: float) -> tuple:
counts = quality_counts(periods, since)
total = sum(counts.values())
return counts, total, (total - counts['ok']) / total if total else 0.0
def degradation(periods: List[dict], newest: float, fraction: float = DEGRADED_FRACTION) -> Optional[dict]:
"""{'latency', 'loss', 'down', 'bad', 'total'} over the last two hours if the WAN has
been degraded for most of them, else None. *newest* anchors the window (the newest
period's start), so a stale timeline can't look healthy or sick by the clock alone.
Needs at least an hour of periods to decide."""
counts, total, share = _bad_share(periods, newest - DEGRADED_WINDOW_S + QUALITY_PERIOD_S)
if total < DEGRADED_MIN_PERIODS or share < fraction:
return None
return dict(counts, bad=total - counts['ok'], total=total)
def recovered(periods: List[dict], newest: float) -> bool:
"""True once the last two hours have been mostly fine again."""
_counts, total, share = _bad_share(periods, newest - RECOVERED_WINDOW_S + QUALITY_PERIOD_S)
return total > 0 and share <= RECOVERED_FRACTION
# Restart entries this close together are one restart (a reboot, then Smart Power, say).
_EPISODE_GAP_S = 900
def restart_episodes(history: dict, button: Optional[dict], since: float) -> List[dict]:
"""Modem restarts since *since*, oldest first: [{'at', 'by'}].
Folds together the UniFi log (Smart Power), uptime resets (any reboot, including one
from the AT&T app or the modem's own button) and Gandalf's Restart modem button.
Entries within 15 minutes of each other are one restart; 'by' names the most specific
cause seen: the Gandalf button, then Smart Power, then "outside Gandalf".
"""
entries = [(r['at'], 'smart power' if r.get('by') else 'outside') for r in history.get('restarts') or []
if isinstance(r.get('at'), (int, float))]
if button and isinstance(button.get('at'), (int, float)):
entries.append((button['at'], 'gandalf'))
rank = {'gandalf': 0, 'smart power': 1, 'outside': 2}
episodes: List[dict] = []
for at, by in sorted(entries):
if at < since:
continue
if episodes and at - episodes[-1]['last'] <= _EPISODE_GAP_S:
ep = episodes[-1]
ep['last'] = at
if rank[by] < rank[ep['by']]:
ep['by'] = by
else:
episodes.append({'at': at, 'last': at, 'by': by})
return [{'at': e['at'], 'by': e['by']} for e in episodes]
def unexplained_drops(outages: List[dict], episodes: List[dict], since: float) -> List[dict]:
"""WAN drops since *since* that a modem restart doesn't explain (a restart takes the
WAN down from a few minutes before its logged time to 15 minutes after)."""
def explained(o):
return any(e['at'] - 300 <= o['failed_at'] <= e['at'] + _EPISODE_GAP_S for e in episodes)
return [o for o in outages or []
if isinstance(o.get('failed_at'), (int, float)) and o['failed_at'] >= since and not explained(o)]
_RESTART_BY = {'gandalf': "Gandalf's Restart modem button", 'smart power': 'Smart Power power-cycled it',
'outside': 'restarted outside Gandalf (AT&T app, modem button or power)'}
def _span(seconds: float) -> str:
days, rest = divmod(int(seconds), 86400)
hours = rest // 3600
return f'{days} d {hours} h' if days else f'{hours} h {rest % 3600 // 60} min'
def restart_lines(episodes: List[dict], now: float) -> List[str]:
"""Markdown lines: each restart, and how long the connection went before the next one."""
lines = []
for i, e in enumerate(episodes):
when = time.strftime('%Y-%m-%d %H:%M UTC', time.gmtime(e['at']))
nxt = episodes[i + 1]['at'] if i + 1 < len(episodes) else None
after = (f', next restart {_span(nxt - e["at"])} later' if nxt
else f', {_span(now - e["at"])} ago')
lines.append(f'- {when}: {_RESTART_BY[e["by"]]}{after}')
return lines
def replacement_hint(episodes: List[dict]) -> str:
"""Advice when restarts keep being needed: two or more in a week."""
if len(episodes) >= 2:
return (f'The modem has needed {len(episodes)} restarts in the last 7 days. If each one only '
f'fixes it for a few days, ask AT&T for a replacement BGW320.')
return ''
# ---------------------------------------------------------------------------
# Logged-in checks, used only when an access code is configured. Two pages are
# read: the NAT table's session counts, and the Troubleshoot page's ping test,