"""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')