Topology: power map, what each outlet feeds
Lint / JS (eslint) (push) Successful in 21s
Lint / Python (flake8) (push) Successful in 2m36s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Test / Python Tests (pytest) (push) Successful in 2m46s
Lint / JS (eslint) (pull_request) Successful in 15s
Lint / Python (flake8) (pull_request) Successful in 2m14s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (push) Successful in 1m33s
Security / Python Security (bandit) (pull_request) Successful in 54s
Test / Python Tests (pytest) (pull_request) Successful in 1m3s

- Each PDU, UPS and power strip card lists its outlets that feed
  something, draw power or are off, with live watts. Each device fed
  gets a power badge with what it draws. Hovering a power card or a
  device draws its feeds as dotted lines and keeps both ends lit.
- What an outlet feeds: a saved assignment, else the outlet's UniFi name
  when it names a device (or the modem), else a MAC in its name that
  belongs to a host NIC (the monitor's host_macs) or UniFi device.
- Admins click an outlet to set what it feeds, nothing, or automatic.
  Saved in a new power_feeds table (created with the cutsheet tables);
  PUT /api/topology/power/<device>/<outlet>, admin and JSON only, with
  the device, outlet and target checked. UniFi is never changed.
- An outlet that is off but feeds something is listed under "to look at".
- Power gear is shown by default again, now that it has something to say.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-14 20:28:13 -05:00
1 parent dcf5fdf4cc
commit 415d2222e7
10 files changed
+492 -22

No files matched your search

+48
View File
@@ -743,3 +743,51 @@ class TestTopology:
patch.object(gandalf.db, 'check_suppressed', return_value=True):
data = client.get('/api/topology', headers=AUTH).get_json()
assert next(n for n in data['nodes'] if n['name'] == 'large1')['alert_count'] == 0
class TestPowerFeedApi:
LINK_STATS = json.dumps({'hosts': {}, 'unifi_switches': {}, 'unifi_devices': {
'RPDU': {'outlets': [{'index': 7, 'name': 'Outlet 7', 'on': True, 'power_w': 48.0}]}}})
SNAPSHOT = json.dumps({
'hosts': {'large1': {'status': 'up', 'interfaces': {}}},
'unifi': [{'name': 'RPDU', 'type': 'usw', 'model': 'USPPDUP', 'connected': True, 'mac': '', 'ip': ''}],
})
def _put(self, client, path, body, headers=AUTH):
state = dict(STATE, link_stats=self.LINK_STATS, network_snapshot=self.SNAPSHOT)
gandalf._cutsheet_schema_ready = True
with patch.object(gandalf.db, 'get_state', side_effect=lambda k, d=None: state.get(k, d)):
return client.put(path, json=body, headers=headers)
def test_admin_assigns_an_outlet(self, client):
resp = self._put(client, '/api/topology/power/RPDU/7', {'target': 'large1'})
assert resp.status_code == 200
gandalf.db.set_power_feed.assert_called_once_with('RPDU', 7, 'large1', 'jared')
def test_empty_means_nothing_and_null_means_automatic(self, client):
assert self._put(client, '/api/topology/power/RPDU/7', {'target': ''}).status_code == 200
assert self._put(client, '/api/topology/power/RPDU/7', {'target': None}).status_code == 200
calls = [c.args for c in gandalf.db.set_power_feed.call_args_list]
assert calls == [('RPDU', 7, '', 'jared'), ('RPDU', 7, None, 'jared')]
def test_needs_admin(self, client):
resp = self._put(client, '/api/topology/power/RPDU/7', {'target': 'large1'},
headers=dict(AUTH, **{'Remote-Groups': 'users'}))
assert resp.status_code in (401, 403)
gandalf.db.set_power_feed.assert_not_called()
def test_unknown_target_source_or_outlet(self, client):
assert self._put(client, '/api/topology/power/RPDU/7', {'target': 'nope'}).status_code == 400
assert self._put(client, '/api/topology/power/NOPE/7', {'target': 'large1'}).status_code == 404
assert self._put(client, '/api/topology/power/RPDU/99', {'target': 'large1'}).status_code == 404
gandalf.db.set_power_feed.assert_not_called()
def test_body_must_say_target(self, client):
assert self._put(client, '/api/topology/power/RPDU/7', {}).status_code == 400
assert self._put(client, '/api/topology/power/RPDU/7', {'target': 5}).status_code == 400
def test_must_be_json(self, client):
gandalf._cutsheet_schema_ready = True
resp = client.put('/api/topology/power/RPDU/7', data='target=large1', headers=AUTH,
content_type='application/x-www-form-urlencoded')
assert resp.status_code == 400