Files
gandalf/tests/test_attention.py
T
jaredandClaude Opus 5.5 8ed32b06e8
Lint / JS (eslint) (push) Successful in 13s
Security / Python Security (bandit) (push) Successful in 1m0s
Lint / JS (eslint) (pull_request) Successful in 17s
Lint / Python (flake8) (pull_request) Successful in 52s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Lint / Python (flake8) (push) Successful in 1m4s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (pull_request) Successful in 1m11s
Test / Python Tests (pytest) (push) Successful in 1m23s
Test / Python Tests (pytest) (pull_request) Successful in 1m14s
WAN: side-by-side latency test, the provider's "is it your router?" test, run by Gandalf
AT&T asked for a device plugged straight into the modem during a slow
spell. While the internet is slow, every check now has the BGW320 ping
1.1.1.1 itself while Gandalf, at the same moment, times TCP handshakes to
1.1.1.1 through the router and to the modem's LAN side (no raw socket
needed). A slow ping from the modem itself can't be the router's doing.

Slow is relative to the line's learned normal (median of the modem's own
pings over 7 days): three times normal and 40 ms over it. 2026-11-26's
110 ms on a 14 ms line was called "clear" by the fixed 150 ms.

Every result is kept 7 days; the "Internet degraded" ticket and its
Cleared note sum up the episode in words that can go to the provider.
Also fixes the degraded ticket saying "the modem's ping was slow too"
for the verdict that meant the opposite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 23:06:38 -05:00

220 lines
11 KiB
Python

"""The dashboard's "Needs attention" list (attention.py)."""
import attention
def _alert(target, severity='critical', **kw):
return dict({'target_name': target, 'severity': severity, 'is_suppressed': False}, **kw)
def _outlet(index, on=True, target=None, power_w=10.0, match=None):
return {'index': index, 'on': on, 'target': target, 'power_w': power_w,
'match': match or ('name' if target else 'none')}
class TestAlerts:
def test_one_line_for_all_alerts_with_the_targets(self):
out = attention.build([_alert('pbs'), _alert('24RJ45-POE', 'warning')], [], None)
[line] = out['broken']
assert line['text'] == '2 active alerts (1 critical)'
assert line['severity'] == 'bad'
assert line['detail'] == '24RJ45-POE, pbs'
assert line['href'] == '#events-table-wrap'
def test_suppressed_and_info_alerts_are_left_out(self):
events = [_alert('pbs', is_suppressed=True), _alert('large1', 'info')]
assert attention.build(events, [], None)['broken'] == []
def test_warnings_only_is_amber(self):
[line] = attention.build([_alert('x', 'warning')], [], None)['broken']
assert line['severity'] == 'warn' and line['text'] == '1 active alert'
class TestPlannedReboot:
def test_rebooting_host_is_one_info_line_linking_to_it(self):
ev = _alert('monitor-01', 'info', event_type='host_rebooting',
description='Planned reboot: monitor-01 rebooted on request at 2026-11-06 23:17:49 UTC')
[line] = attention.build([ev], [], None)['broken']
assert line['text'] == 'monitor-01 is rebooting (planned)'
assert line['severity'] == 'info' and line['href'] == '/device/monitor-01'
assert line['detail'].startswith('Planned reboot')
def test_it_ranks_below_real_alerts(self):
ev = _alert('monitor-01', 'info', event_type='host_rebooting')
out = attention.build([ev, _alert('pbs')], [], None)['broken']
assert [x['severity'] for x in out] == ['bad', 'info']
class TestPower:
def test_frozen_readings_and_an_off_outlet_feeding_a_device(self):
power = [{'node': 'RPDU', 'stale_since': '2026-11-15 05:26:54 UTC',
'outlets': [_outlet(19, on=False, target='pbs'), _outlet(3, on=False)]}]
broken = attention.build([], power, None)['broken']
assert [b['text'] for b in broken] == ['RPDU outlet 19 is off, and it feeds pbs',
'RPDU: outlet watts are frozen']
def test_powered_outlets_with_nothing_assigned(self):
power = [{'node': 'RPDU', 'outlets': [
_outlet(1, target='large1'), _outlet(2, power_w=40), _outlet(3, power_w=1),
_outlet(4, power_w=40, match='cleared'), _outlet(5, on=False, power_w=0)]}]
[line] = attention.build([], power, None)['tidy']
assert line['text'] == '1 powered outlet with no device assigned'
assert line['detail'] == 'RPDU 2'
class TestCutsheet:
VIEW = {
'cables': [{'id': 1, 'label': 'C01', 'status': 'installed', 'problem': True},
{'id': 2, 'label': 'C02', 'status': 'retired', 'problem': True},
{'id': 3, 'label': 'C03', 'status': 'installed', 'problem': False}],
'suggestions': [{}, {}, {}],
'port_names': [{'kind': 'wrong', 'switch': 'POE', 'port_idx': 11, 'port_name': 'Large1 Management'},
{'kind': 'empty', 'switch': 'POE', 'port_idx': 20, 'port_name': 'WAP'},
{'kind': 'unnamed', 'switch': 'POE', 'port_idx': 4, 'port_name': 'Port 4'}],
'labels': {'cables': [{}, {}], 'ports': [{}]},
}
def test_every_cutsheet_todo(self):
out = attention.build([], [], self.VIEW)
assert [b['text'] for b in out['broken']] == ['1 cable not where the cutsheet says']
assert out['broken'][0]['detail'] == 'C01'
assert out['broken'][0]['href'].startswith('/cutsheet?problems=1')
tidy = {t['text']: t for t in out['tidy']}
assert set(tidy) == {'1 UniFi port name pointing at the wrong device', '1 named port with nothing plugged in',
'3 live links not on the cutsheet', '3 labels to print and stick on'}
assert tidy['1 UniFi port name pointing at the wrong device']['detail'] == 'POE port 11 "Large1 Management"'
# Worst first
assert out['tidy'][0]['severity'] == 'warn'
def test_unnamed_ports_alone_are_not_worth_a_line(self):
view = {'port_names': [{'kind': 'unnamed', 'switch': 'POE', 'port_idx': 4, 'port_name': 'Port 4'}]}
assert attention.build([], [], view) == {'broken': [], 'tidy': []}
class TestRack:
def test_overlaps_and_items_outside_the_rack(self):
racks = {'Rack 1': {'overlaps': [{'name': 'NAS', 'units': 'U5-6', 'with': ['storage-01']}],
'outside': [{'name': 'Shelf', 'units': 'U43'}]}}
texts = [t['text'] for t in attention.build([], [], None, racks)['tidy']]
assert texts == ['NAS overlaps storage-01 at U5-6', 'Shelf at U43 is outside Rack 1']
def test_nothing_to_report(self):
assert attention.build([], [], {}, {}) == {'broken': [], 'tidy': []}
NOW = 1_000_000.0
def _quality(states, downtime=(), latencies=None, updated=NOW):
"""states: newest last, one per 5 minutes ending at NOW."""
n = len(states)
return {'periods': [{'ts': NOW - (n - i) * 300, 'state': s} for i, s in enumerate(states)],
'downtime': [{'ts': NOW - ago, 'seconds': s} for ago, s in downtime],
'latencies': latencies or {}, 'updated': updated}
def _wan(q):
return [e for e in attention.build([], [], None, wan_quality=q, now=NOW)['broken']
if e['text'].startswith('Internet')]
class TestWan:
def test_healthy_says_nothing(self):
assert _wan(_quality(['ok'] * 288)) == []
def test_latency_in_the_last_hour(self):
[e] = _wan(_quality(['ok'] * 276 + ['latency', 'ok'] * 3 + ['latency'] * 3 + ['ok'] * 3,
latencies={'google': 26, 'cloudflare': 12}))
assert e['severity'] == 'warn' and e['href'] == '#modem-section'
assert e['text'] == 'Internet: high latency in 6 of the last 12 five-minute periods'
assert 'UniFi flagged 6 of the last 288' in e['detail']
assert 'Now: cloudflare 12 ms, google 26 ms' in e['detail']
def test_a_couple_of_slow_periods_are_not_worth_a_line(self):
assert _wan(_quality(['ok'] * 10 + ['latency'] * 2)) == []
def test_old_trouble_has_cleared(self):
assert _wan(_quality(['latency'] * 100 + ['ok'] * 12)) == []
def test_packet_loss_is_broken(self):
[e] = _wan(_quality(['ok'] * 9 + ['loss', 'latency', 'down']))
assert e['severity'] == 'bad'
assert e['text'] == 'Internet: packet loss in 2 of the last 12 five-minute periods'
def test_a_drop_in_the_last_hour(self):
[e] = _wan(_quality(['ok'] * 12, downtime=[(900, 90), (7200, 30)]))
assert e['text'] == 'Internet dropped for 90 s, 15 min ago'
def test_several_drops(self):
[e] = _wan(_quality(['ok'] * 12, downtime=[(900, 90), (1800, 30)]))
assert e['text'] == 'Internet dropped 2 times in the last hour, 120 s in all'
def test_stale_copy_is_ignored(self):
assert _wan(_quality(['loss'] * 12, updated=NOW - 3600)) == []
def test_none_is_fine(self):
assert _wan(None) == [] and _wan({}) == []
def test_internet_comes_before_alerts_of_the_same_severity(self):
out = attention.build([_alert('x', 'warning')], [], None,
wan_quality=_quality(['latency'] * 12), now=NOW)['broken']
assert out[0]['text'].startswith('Internet')
class TestModemChecks:
def _broken(self, checks, quality=None):
return attention.build([], [], None, wan_quality=quality, now=NOW, modem_checks=checks)
def test_gateway_ping_explains_the_latency(self):
checks = {'ping': {'at': NOW - 120, 'avg_ms': 20.4, 'loss_pct': 0, 'ours_ms': 1043, 'verdict': 'gateway'}}
[e] = self._broken(checks, _quality(['latency'] * 12))['broken']
assert ("Modem's own ping 2 min ago: 20 ms; ours through it 1043 ms, "
"so the delay is in the modem's forwarding or the router's WAN side.") in e['detail']
def test_upstream_verdict(self):
checks = {'ping': {'at': NOW - 60, 'avg_ms': 900.0, 'loss_pct': 20, 'ours_ms': 1000, 'verdict': 'upstream'}}
[e] = self._broken(checks, _quality(['latency'] * 12))['broken']
assert ("900 ms, 20% lost; ours through it 1000 ms, "
'so the delay is past the router: in the modem or the line') in e['detail']
def test_lan_verdict(self):
checks = {'ping': {'at': NOW - 60, 'avg_ms': 14.0, 'ours_ms': 300, 'lan_ms': 250, 'verdict': 'lan'}}
[e] = self._broken(checks, _quality(['latency'] * 12))['broken']
assert 'the hop to the modem is slow: the router, the cable or the modem LAN port' in e['detail']
def test_old_ping_is_left_out(self):
checks = {'ping': {'at': NOW - 7200, 'avg_ms': 20.0, 'verdict': 'clear'}}
[e] = self._broken(checks, _quality(['latency'] * 12))['broken']
assert "Modem's own ping" not in e['detail']
def test_nat_nearly_full(self):
[e] = self._broken({'nat': {'in_use': 30000, 'available': 32767}})['broken']
assert e['text'] == 'Modem NAT table 92% full' and e['severity'] == 'warn'
assert '30,000 of 32,767' in e['detail']
def test_nat_normal_says_nothing(self):
out = self._broken({'nat': {'in_use': 2858, 'available': 32767}})
assert out == {'broken': [], 'tidy': []}
def test_login_failure_is_housekeeping(self):
[e] = self._broken({'error': {'login': True, 'message': 'refused', 'at': NOW}})['tidy']
assert e['text'].startswith('Modem login failed')
def test_other_errors_stay_quiet(self):
assert self._broken({'error': {'login': False, 'message': 'timeout', 'at': NOW}})['tidy'] == []
class TestSmartPowerLeftOff:
OUTLET = {'device': 'RPDU', 'index': 13}
def test_left_off_after_restart_is_broken(self):
r = {'finished_at': 1.0, 'smart_power_paused': [self.OUTLET], 'smart_power_error': 'HTTP 500'}
out = attention.build([], [], None, modem_restart=r)
e = out['broken'][0]
assert e['severity'] == 'bad' and 'still off' in e['text']
assert 'RPDU outlet 13' in e['detail'] and 'HTTP 500' in e['detail']
def test_restored_or_in_progress_says_nothing(self):
for r in ({'finished_at': 1.0, 'smart_power_paused': [self.OUTLET], 'smart_power_restored': True},
{'finished_at': None, 'smart_power_paused': [self.OUTLET]}, None):
assert attention.build([], [], None, modem_restart=r)['broken'] == []