Files
gandalf/tests/test_attention.py
jaredandClaude Opus 5.5 de75f71bf8
Lint / JS (eslint) (push) Successful in 1m11s
Lint / Python (flake8) (push) Successful in 1m55s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 54s
Lint / Python (flake8) (pull_request) Successful in 53s
Test / Python Tests (pytest) (push) Successful in 1m5s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 55s
Test / Python Tests (pytest) (pull_request) Successful in 1m3s
Modem access code: NAT sessions, and a ping from the modem when slow
With modem.access_code in config.json, the monitor logs in to the
BGW320 (md5 of code + nonce, as its own page does) for two read-only
checks, stored in monitor_state.modem_checks:

- NAT sessions in use every 30 minutes; Needs attention warns at 80%.
- While UniFi says the internet is slow, a ping from the modem itself
  to 1.1.1.1 (at most every 10 minutes). Compared with UniFi's latency
  through the modem, it says whether the delay is past the modem (the
  provider), in the modem, or gone. The Internet line in Needs
  attention and the WAN / Fiber panel show the result.

A refused code is reported and not retried for 30 minutes; other
failures pause the checks for 10. The unauthenticated status polling
is unchanged. Tested against the real gateway and a fake one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 18:32:31 -05:00

184 lines
8.8 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 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, or between it and the router.') 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 modem, on the provider's side") 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'] == []