Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(inventory): stop a renamed service being duplicated by the next scan
Pouzor394925f2f097
diff --git a/backend/app/services/inventory_sync.py b/backend/app/services/inventory_sync.pyindex 0eb04b8..5e84b6b 100644--- a/backend/app/services/inventory_sync.py+++ b/backend/app/services/inventory_sync.py@@ -205,6 +205,28 @@ def _service_key(svc: Any) -> Any: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.++ A port is the identity: one port on one protocol is one service, whatever it+ is called. The name is a label on it — the scanner guesses it from a banner+ and the user corrects it — so keying identity on the name made a rename look+ like 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.++ Only a service with no port falls back to the name, because nothing else+ tells two of them apart — a hand-added entry that names a host rather than a+ port, say. Those still key exactly as they did.+ """+ if not isinstance(svc, dict):+ return repr(svc)+ port = svc.get("port")+ if port in (None, ""):+ return (None, svc.get("protocol"), (svc.get("service_name") or "").lower())+ return (port, svc.get("protocol"))++# --- Per-node view of the device's list facts -----------------------------## The row owns the services and the properties; a node owns which of them it@@ -360,11 +382,13 @@ def _stamped(item: Any, visible: bool) -> Any:return {**item, "visible": visible}-# How a service looks is the user's call. A fingerprint only ever guesses it-# from a port number and a banner, so a scan that re-finds a known service must-# not repaint the icon someone picked by hand — that happened on every "Scan-# network", across every canvas drawing the device, at once.-_CURATED_SERVICE_FIELDS = ("icon", "category")+# How a service looks — and what it is called — is the user's call. A+# fingerprint only ever guesses it from a port number and a banner, so a scan+# that re-finds a known service must not repaint the icon someone picked by hand+# — that happened on every "Scan network", across every canvas drawing the+# device, at once. `service_name` is curated for the same reason: correcting a+# bad guess is the point of editing a service, and a rescan must not undo it.+_CURATED_SERVICE_FIELDS = ("icon", "category", "service_name")def merge_services(@@ -373,17 +397,28 @@ def merge_services(*,discovered: bool = False,) -> list[Any]:- """Union two service lists on (port, protocol, name); incoming wins.+ """Union two service lists on (port, protocol); incoming wins.``discovered`` marks ``incoming`` as scanner output rather than a user edit:it still adds services and refreshes facts, but leaves an established iconand category alone. Either way a blank incoming value never clears one thatis already set — an absent field is silence, not a reset.++ ``base`` is collapsed on the same key as it is copied, so a row that already+ holds the pre-#469 duplicates heals on the next merge instead of needing a+ migration. First entry wins: the user's renamed service was there before the+ scan appended its guess, so it is the one that survives."""- out: list[Any] = [dict(s) if isinstance(s, dict) else s for s in (base or [])]- index = {_service_key(s): i for i, s in enumerate(out)}+ out: list[Any] = []+ index: dict[Any, int] = {}+ for svc in base or []:+ key = _service_identity_key(svc)+ if key in index:+ continue+ out.append(dict(svc) if isinstance(svc, dict) else svc)+ index[key] = len(out) - 1for svc in incoming or []:- key = _service_key(svc)+ key = _service_identity_key(svc)pos = index.get(key)if pos is None:out.append(dict(svc) if isinstance(svc, dict) else svc)diff --git a/backend/tests/test_inventory_sync.py b/backend/tests/test_inventory_sync.pyindex 9ed7260..19cceab 100644--- a/backend/tests/test_inventory_sync.py+++ b/backend/tests/test_inventory_sync.py@@ -73,7 +73,7 @@ class TestMergeRules:assert out[0]["value"] == "A1"assert out[0]["icon"] == "Server"- def test_services_union_on_port_protocol_and_name(self):+ def test_services_union_on_port_and_protocol(self):base = [{"port": 22, "protocol": "tcp", "service_name": "ssh"}]incoming = [{"port": 22, "protocol": "tcp", "service_name": "SSH", "icon": "Terminal"},@@ -84,6 +84,77 @@ class TestMergeRules:assert out[0]["icon"] == "Terminal"assert out[1]["port"] == 80+ def test_a_renamed_service_is_not_duplicated_by_the_next_scan(self):+ """Issue #469: identity is the port, not the name the scanner guessed.++ The user renamed 3001/tcp from "Uptime Kuma" to "Homepage"; the rescan+ fingerprints the same port and offers its own guess again.+ """+ base = [{"port": 3001, "protocol": "tcp", "service_name": "Homepage"}]+ incoming = [{"port": 3001, "protocol": "tcp", "service_name": "Uptime Kuma"}]+ out = merge_services(base, incoming, discovered=True)+ assert len(out) == 1+ assert out[0]["service_name"] == "Homepage"++ def test_a_scan_still_names_a_service_that_never_had_one(self):+ out = merge_services(+ [{"port": 3001, "protocol": "tcp"}],+ [{"port": 3001, "protocol": "tcp", "service_name": "Uptime Kuma"}],+ discovered=True,+ )+ assert len(out) == 1+ assert out[0]["service_name"] == "Uptime Kuma"++ def test_a_user_edit_still_renames_a_service(self):+ """The guard is for scanner output only — an edit is an edit."""+ out = merge_services(+ [{"port": 3001, "protocol": "tcp", "service_name": "Uptime Kuma"}],+ [{"port": 3001, "protocol": "tcp", "service_name": "Homepage"}],+ )+ assert len(out) == 1+ assert out[0]["service_name"] == "Homepage"++ def test_a_scan_refreshes_facts_that_are_not_curated(self):+ """Only name, icon and category are the user's; the rest is observation."""+ out = merge_services(+ [{"port": 3001, "protocol": "tcp", "service_name": "Homepage", "path": "/old"}],+ [{"port": 3001, "protocol": "tcp", "service_name": "Uptime Kuma", "path": "/new"}],+ discovered=True,+ )+ assert out[0]["service_name"] == "Homepage"+ assert out[0]["path"] == "/new"++ def test_duplicates_already_on_the_row_collapse_on_the_next_merge(self):+ """Rows written before the #469 fix heal without a migration.++ First entry wins: the user's rename was there before the scan appended+ its guess underneath it.+ """+ base = [+ {"port": 3001, "protocol": "tcp", "service_name": "Homepage", "icon": "House"},+ {"port": 3001, "protocol": "tcp", "service_name": "Uptime Kuma"},+ {"port": 9090, "protocol": "tcp", "service_name": "Watchtower"},+ {"port": 9090, "protocol": "tcp", "service_name": "Prometheus"},+ ]+ out = merge_services(base, None)+ assert [s["service_name"] for s in out] == ["Homepage", "Watchtower"]+ assert out[0]["icon"] == "House"++ def test_port_less_services_still_key_on_their_name(self):+ """Nothing else tells two of them apart — they must not collapse."""+ base = [{"protocol": "tcp", "service_name": "Vaultwarden", "host": "vault.lan"}]+ incoming = [{"protocol": "tcp", "service_name": "Gitea", "host": "git.lan"}]+ out = merge_services(base, incoming)+ assert len(out) == 2+ assert [s["service_name"] for s in out] == ["Vaultwarden", "Gitea"]++ def test_the_same_port_on_udp_and_tcp_stays_two_services(self):+ out = merge_services(+ [{"port": 53, "protocol": "tcp", "service_name": "dns"}],+ [{"port": 53, "protocol": "udp", "service_name": "dns"}],+ )+ assert len(out) == 2+def test_a_scan_never_repaints_an_icon_the_user_picked(self):"""Every "Scan network" used to overwrite a hand-picked brand icon."""