Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(inventory): match a merged view's keys to services one for one
Pouzor47f9f360fa29
diff --git a/backend/app/services/device_merge.py b/backend/app/services/device_merge.pyindex 2b02906..ff90569 100644--- a/backend/app/services/device_merge.py+++ b/backend/app/services/device_merge.py@@ -163,6 +163,41 @@ async def _merge_documents(db: AsyncSession, winner: InventoryDevice, loser_ids:return orphaned+def _seeds_no_entry_speaks_for(+ listed: list[str], seeds: list[dict[str, Any]], kind: str+) -> list[dict[str, Any]]:+ """The seeded keys no entry in ``listed`` already speaks for.++ One entry in a view speaks for one service, so the match is one-for-one. A+ key that names a service exactly claims it; what is left over is a key+ written before #503, which names a port and no site, and claims one of the+ services on that port.++ Both halves matter. Matching on the exact key alone would call a service+ such a key already speaks for "missing" and append it again as shown, which+ unhides what the canvas hid on purpose. Treating a whole port as spoken for+ because one key mentions it goes wrong the other way: a second site the+ merge has just folded in would never be added, and would arrive hidden —+ exactly the facts this function exists to reveal.+ """+ remaining = list(seeds)+ leftover: list[str] = []+ for key in listed:+ seed = next((s for s in remaining if s["key"] == key), None)+ if seed is None:+ leftover.append(key)+ else:+ remaining.remove(seed)+ if kind != "services":+ return remaining+ for key in leftover:+ port = view_key_port(key)+ seed = next((s for s in remaining if view_key_port(s["key"]) == port), None)+ if seed is not None:+ remaining.remove(seed)+ return remaining++async def _show_merged_facts(db: AsyncSession, winner: InventoryDevice) -> int:"""Make the survivor's facts visible on every canvas that draws it.@@ -194,24 +229,12 @@ async def _show_merged_facts(db: AsyncSession, winner: InventoryDevice) -> int:# Normalized: a view written before #469 addresses a service by a# key that carries its name, and comparing it raw against a freshly# seeded key would call an already-listed service missing.- listed = {+ listed = [normalize_view_key(str(e.get("key")), kind)for e in entriesif isinstance(e, dict)- }- if kind == "services":- # A view written before #503 names a port and no site, so a- # freshly seeded key never matches it as written. Both reduce to- # the same port, and that is what says this canvas has already- # spoken about the service — including to hide it.- listed |= {view_key_port(key) for key in listed}- missing = [- entry- for entry in seeds[kind]- if view_key_port(entry["key"]) not in listed- ]- else:- missing = [entry for entry in seeds[kind] if entry["key"] not in listed]+ ]+ missing = _seeds_no_entry_speaks_for(listed, seeds[kind], kind)if missing:view[kind] = [*entries, *missing]changed = Truediff --git a/backend/tests/test_device_merge.py b/backend/tests/test_device_merge.pyindex 02de54e..cf8127f 100644--- a/backend/tests/test_device_merge.py+++ b/backend/tests/test_device_merge.py@@ -216,6 +216,81 @@ async def test_merge_keeps_a_hidden_service_hidden_on_a_view_that_names_no_site(assert node.display_view["services"] == [{"key": "443|tcp", "visible": False}]+async def test_merge_shows_a_second_site_the_survivor_brings_to_a_shared_port(db_session):+ """A view naming one service on a port speaks for that one, not for the port.++ The survivor serves two sites on 443 and the node's view knows only the+ first. Reading the view as "this canvas has been told about 443" would+ leave the second hidden — the merge would fold the facts in and the canvas+ would look exactly as bare as before, which is what this function exists to+ prevent.+ """+ design = await _design(db_session)+ winner = await _device(+ db_session, id="w", ip="192.168.1.62",+ services=[+ {"service_name": "Pi-hole", "port": 443, "protocol": "tcp", "host": "a.lan"},+ {"service_name": "Gitea", "port": 443, "protocol": "tcp", "host": "b.lan"},+ ],+ )+ loser = await _device(+ db_session, id="l", ip="192.168.1.62",+ services=[{"service_name": "Pi-hole", "port": 443, "protocol": "tcp", "host": "a.lan"}],+ )+ node = await _node(db_session, design, loser)+ node.display_view = {+ "services": [{"key": "443|tcp|a.lan|", "visible": True}],+ "properties": [],+ }+ await db_session.flush()++ await merge_devices(db_session, winner, [loser])+ await db_session.flush()+ await db_session.refresh(node)++ assert node.display_view["services"] == [+ {"key": "443|tcp|a.lan|", "visible": True},+ {"key": "443|tcp|b.lan|", "visible": True},+ ]+++async def test_merge_leaves_a_hidden_site_hidden_while_showing_its_neighbour(db_session):+ """One key hides one site; the other is still new and still arrives shown.++ The two halves of the rule meet here: the hidden entry claims its own site+ and nothing else, so the site beside it on the same port is added.+ """+ design = await _design(db_session)+ winner = await _device(+ db_session, id="w", ip="192.168.1.62",+ services=[+ {"service_name": "Pi-hole", "port": 443, "protocol": "tcp", "host": "a.lan"},+ {"service_name": "Gitea", "port": 443, "protocol": "tcp", "host": "b.lan"},+ ],+ )+ loser = await _device(+ db_session, id="l", ip="192.168.1.62",+ services=[{"service_name": "Pi-hole", "port": 443, "protocol": "tcp", "host": "a.lan"}],+ )+ node = await _node(db_session, design, loser)+ node.display_view = {+ "services": [{"key": "443|tcp|a.lan|", "visible": False}],+ "properties": [],+ }+ await db_session.flush()++ await merge_devices(db_session, winner, [loser])+ await db_session.flush()+ await db_session.refresh(node)++ assert node.display_view["services"] == [+ {"key": "443|tcp|a.lan|", "visible": False},+ {"key": "443|tcp|b.lan|", "visible": True},+ ]++@pytest.mark.asyncioasync def test_merge_collapses_two_nodes_that_now_draw_one_device(db_session):"""Both rows drawn on the *same* canvas: one node survives, the oldest."""