Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(device-merge): keep the survivor's curated service name on a collision
Pouzorc903c0dcdc28
diff --git a/backend/app/services/device_merge.py b/backend/app/services/device_merge.pyindex 9674822..da1ce9c 100644--- a/backend/app/services/device_merge.py+++ b/backend/app/services/device_merge.py@@ -344,7 +344,14 @@ async def merge_devices(setattr(winner, field, value)winner.ip = _merged_ip(winner.ip, loser.ip)winner.mac = winner.mac or loser.mac- winner.services = merge_services(winner.services, loser.services)+ # discovered=: the winner is the row the canvas points at (an approved+ # row wins `_collapse_targets`), and a loser is usually a pending row a+ # scan just minted, carrying the fingerprint's guess at a name. Both+ # rows describe one device, so two entries on one port are one service+ # — and the curated side of it is the winner's. Without this the guess+ # would overwrite a name, icon or category the user had chosen, in a+ # collapse that runs unattended during a background scan.+ winner.services = merge_services(winner.services, loser.services, discovered=True)winner.properties = merge_properties(winner.properties, loser.properties)winner.show_hardware = winner.show_hardware or loser.show_hardwarefor source in add_source(loser.discovery_sources, loser.discovery_source):diff --git a/backend/app/services/inventory_sync.py b/backend/app/services/inventory_sync.pyindex ef93433..9a2c14a 100644--- a/backend/app/services/inventory_sync.py+++ b/backend/app/services/inventory_sync.py@@ -199,12 +199,6 @@ def merge_properties(base: list[Any] | None, incoming: list[Any] | None) -> listreturn out-def _service_key(svc: Any) -> Any:- if not isinstance(svc, dict):- return repr(svc)- return (svc.get("port"), svc.get("protocol"), (svc.get("service_name") or "").lower())--def _service_identity_key(svc: Any) -> Any:"""What makes two service entries *the same service*, for merging.@@ -212,8 +206,9 @@ def _service_identity_key(svc: Any) -> Any:is called. The name is a label on it — the scanner guesses it from a bannerand the user corrects it — so keying identity on the name made a rename looklike a second service, and the next scan re-added the original under its own- guess (issue #469). :func:`_service_view_key` keeps using the name-inclusive- key: its strings are persisted in node views, and this is not that.+ guess (issue #469). :func:`_service_view_key` renders the same identity, so+ the two never disagree about what one service is; keys already persisted in+ node views are narrowed to it on read by :func:`normalize_view_key`.Only a service with no port falls back to the name, because nothing elsetells two of them apart — a hand-added entry that names a host rather than a@@ -263,8 +258,13 @@ def normalize_view_key(key: str, kind: str) -> str:name in one is a snapshot of what the service was called when the view waswritten, so a renamed service cannot be found by re-deriving it — the keyhas to be narrowed instead. A key that names a port drops everything after- it; one that does not is port-less and is already in its final form, as is- every property key.+ it; one that does not is port-less and keeps its name, as does every+ property key.++ A port-less service renders its absent port as ``None`` today, but a key+ written for one carrying ``""`` stored an empty segment instead. Both mean+ the same absent port, so the empty one is spelled the new way rather than+ left to miss.Applied on read, so a view migrates the next time its node is saved ratherthan needing a migration of its own.@@ -272,7 +272,11 @@ def normalize_view_key(key: str, kind: str) -> str:if kind != "services":return keyparts = key.split("|", 2)- if len(parts) != 3 or parts[0] in ("", "None"):+ if len(parts) != 3:+ return key+ if parts[0] == "":+ return f"None|{parts[1]}|{parts[2]}"+ if parts[0] == "None":return keyreturn f"{parts[0]}|{parts[1]}"diff --git a/backend/tests/test_device_merge.py b/backend/tests/test_device_merge.pyindex e6ddd9d..dd6b986 100644--- a/backend/tests/test_device_merge.py+++ b/backend/tests/test_device_merge.py@@ -554,3 +554,74 @@ async def test_merge_route_requires_auth(client, db_session):"/api/v1/scan/pending/merge", json={"winner_id": "w", "loser_ids": ["l"]})assert res.status_code in (401, 403)+++async def test_a_merge_keeps_the_survivor_name_for_a_service_on_the_same_port(db_session):+ """Two rows describing one device describe one service per port.++ The winner is the row the canvas points at, and a loser is usually a pending+ row a scan minted, carrying the fingerprint's guess. Collapsing the pair must+ not let that guess overwrite the name, icon and category the user curated —+ the collapse runs unattended during a background scan.+ """+ winner = await _device(+ db_session, id="w", ip="192.168.1.62",+ services=[{+ "port": 80, "protocol": "tcp", "service_name": "Homepage",+ "icon": "brand:homepage", "category": "monitoring",+ }],+ )+ loser = await _device(+ db_session, id="l", ip="192.168.1.62",+ services=[{+ "port": 80, "protocol": "tcp", "service_name": "nginx",+ "icon": "Globe", "category": "web", "path": "/dash",+ }],+ )++ await merge_devices(db_session, winner, [loser])+ await db_session.flush()++ assert len(winner.services) == 1+ assert winner.services[0]["service_name"] == "Homepage"+ assert winner.services[0]["icon"] == "brand:homepage"+ assert winner.services[0]["category"] == "monitoring"+ # Everything that is not the user's call still comes across.+ assert winner.services[0]["path"] == "/dash"+++async def test_a_merge_still_takes_a_service_the_survivor_never_had(db_session):+ """Curating the collision must not stop the union doing its job."""+ winner = await _device(+ db_session, id="w", ip="192.168.1.62",+ services=[{"port": 80, "protocol": "tcp", "service_name": "Homepage"}],+ )+ loser = await _device(+ db_session, id="l", ip="192.168.1.62",+ services=[{"port": 5678, "protocol": "tcp", "service_name": "n8n"}],+ )++ await merge_devices(db_session, winner, [loser])+ await db_session.flush()++ assert [s["service_name"] for s in winner.services] == ["Homepage", "n8n"]+++async def test_a_merge_names_a_service_the_survivor_left_unnamed(db_session):+ """A blank name is silence, so the loser's fills it rather than being dropped."""+ winner = await _device(+ db_session, id="w", ip="192.168.1.62",+ services=[{"port": 80, "protocol": "tcp"}],+ )+ loser = await _device(+ db_session, id="l", ip="192.168.1.62",+ services=[{"port": 80, "protocol": "tcp", "service_name": "nginx"}],+ )++ await merge_devices(db_session, winner, [loser])+ await db_session.flush()++ assert [s["service_name"] for s in winner.services] == ["nginx"]diff --git a/backend/tests/test_inventory_sync.py b/backend/tests/test_inventory_sync.pyindex cec31dc..d7fc82d 100644--- a/backend/tests/test_inventory_sync.py+++ b/backend/tests/test_inventory_sync.py@@ -21,6 +21,7 @@ from app.services.inventory_sync import (link_facts,merge_properties,merge_services,+ normalize_view_key,seed_node_views,)@@ -1643,3 +1644,44 @@ class TestPerNodeView:assert hydrated_node(node, device)["services"] == [{"protocol": "tcp", "service_name": "Vaultwarden", "visible": False}]++ @pytest.mark.asyncio+ async def test_a_port_less_key_stored_with_an_empty_port_still_matches(self, db_session):+ """`port: ""` used to render an empty first segment, `None` renders it now.++ Both spell the same absent port, so the older one is narrowed to today's+ rather than missing and dragging the service back into view.+ """+ device = InventoryDevice(+ id="d-1", services=[{"port": "", "protocol": "tcp", "service_name": "Vaultwarden"}]+ )+ node = _node(await _design(db_session), device_id="d-1")+ node.display_view = {+ "services": [{"key": "|tcp|vaultwarden", "visible": False}],+ "properties": [],+ }+ assert hydrated_node(node, device)["services"] == [+ {"port": "", "protocol": "tcp", "service_name": "Vaultwarden", "visible": False}+ ]+++class TestNormalizeViewKey:+ """Stored view keys, narrowed to the identity the row is addressed by."""++ def test_a_key_naming_a_port_drops_the_name(self):+ assert normalize_view_key("3001|tcp|uptime kuma", "services") == "3001|tcp"++ def test_a_key_already_in_todays_form_is_left_alone(self):+ assert normalize_view_key("3001|tcp", "services") == "3001|tcp"++ def test_a_port_less_key_keeps_its_name(self):+ assert normalize_view_key("None|tcp|vaultwarden", "services") == "None|tcp|vaultwarden"++ def test_an_empty_port_is_spelled_the_way_it_is_rendered_today(self):+ assert normalize_view_key("|tcp|vaultwarden", "services") == "None|tcp|vaultwarden"++ def test_a_name_carrying_a_pipe_survives_the_split(self):+ assert normalize_view_key("None|tcp|a|b", "services") == "None|tcp|a|b"++ def test_a_property_key_is_never_touched(self):+ assert normalize_view_key("rack", "properties") == "rack"