Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

feat(inventory): drop the device columns from nodes

Pouzor9b77e43ca121
diff --git a/backend/app/api/routes/canvas.py b/backend/app/api/routes/canvas.py
index c1cea55..722450d 100644
--- a/backend/app/api/routes/canvas.py
+++ b/backend/app/api/routes/canvas.py
@@ -12,7 +12,13 @@ from app.db.models import CanvasState, Design, Edge, Node
from app.schemas.canvas import CanvasSaveRequest, CanvasStateResponse
from app.schemas.edges import EdgeResponse
from app.schemas.nodes import NodeResponse
-from app.services.inventory_sync import hydrated_node, link_node, load_devices_for
+from app.services.inventory_sync import (
+ facts_from_payload,
+ hydrated_node,
+ link_facts,
+ load_devices_for,
+ node_columns,
+)
router = APIRouter()
@@ -81,22 +87,24 @@ async def save_canvas(
# Upsert nodes, then route their device facts to the inventory row that owns
# them — an edit made on this canvas is an edit to the device itself, and
# every other canvas showing it follows.
- saved_nodes: list[Node] = []
+ saved: list[tuple[Node, dict[str, Any]]] = []
for node_data in body.nodes:
db_node = await db.get(Node, node_data.id)
payload = node_data.model_dump()
payload["design_id"] = design_id
+ facts = facts_from_payload(payload, label=node_data.label, node_type=node_data.type)
+ columns = node_columns(payload)
if db_node:
- for field, value in payload.items():
+ for field, value in columns.items():
setattr(db_node, field, value)
else:
- db_node = Node(**payload)
+ db_node = Node(**columns)
db.add(db_node)
- saved_nodes.append(db_node)
+ saved.append((db_node, facts))
await db.flush()
- for node in saved_nodes:
- await link_node(db, node, overwrite_scalars=True, replace_lists=True)
+ for node, facts in saved:
+ await link_facts(db, node, facts, overwrite_scalars=True, replace_lists=True)
# Upsert edges
for edge_data in body.edges:
diff --git a/backend/app/api/routes/liveview.py b/backend/app/api/routes/liveview.py
index 79c4fec..3f76e1b 100644
--- a/backend/app/api/routes/liveview.py
+++ b/backend/app/api/routes/liveview.py
@@ -13,6 +13,7 @@ from app.db.models import CanvasState, Design, Edge, Node
from app.schemas.canvas import CanvasStateResponse
from app.schemas.edges import EdgeResponse
from app.schemas.nodes import NodeResponse
+from app.services.inventory_sync import hydrated_node, load_devices_for
router = APIRouter()
@@ -59,13 +60,15 @@ async def liveview_canvas(
if design_id is None:
return CanvasStateResponse(nodes=[], edges=[], viewport={"x": 0, "y": 0, "zoom": 1}, custom_style=None)
- nodes = (await db.execute(select(Node).where(Node.design_id == design_id))).scalars().all()
+ nodes = list((await db.execute(select(Node).where(Node.design_id == design_id))).scalars().all())
edges = (await db.execute(select(Edge).where(Edge.design_id == design_id))).scalars().all()
state = await db.get(CanvasState, design_id)
viewport: dict[str, Any] = state.viewport if state else {"x": 0, "y": 0, "zoom": 1}
custom_style: dict[str, Any] | None = state.custom_style if state else None
+ # Device facts come off the inventory row, same as the editable canvas.
+ devices = await load_devices_for(db, nodes)
return CanvasStateResponse(
- nodes=[NodeResponse.model_validate(n) for n in nodes],
+ nodes=[NodeResponse.model_validate(hydrated_node(n, devices.get(n.device_id or ""))) for n in nodes],
edges=[EdgeResponse.model_validate(e) for e in edges],
viewport=viewport,
custom_style=custom_style,
diff --git a/backend/app/api/routes/nodes.py b/backend/app/api/routes/nodes.py
index 80187b0..f2177a9 100644
--- a/backend/app/api/routes/nodes.py
+++ b/backend/app/api/routes/nodes.py
@@ -8,7 +8,14 @@ from app.api.deps import get_current_user
from app.db.database import get_db
from app.db.models import Design, InventoryDevice, Node
from app.schemas.nodes import NodeCreate, NodeResponse, NodeUpdate
-from app.services.inventory_sync import hydrated_node, link_node, load_devices_for
+from app.services.inventory_sync import (
+ facts_from_payload,
+ facts_from_update,
+ hydrated_node,
+ link_facts,
+ load_devices_for,
+ node_columns,
+)
from app.services.node_dedupe import find_duplicate_node
router = APIRouter()
@@ -108,12 +115,14 @@ async def create_node(body: NodeCreate, db: AsyncSession = Depends(get_db), _: s
if data["pos_y"] is None:
data["pos_y"] = 0.0
- node = Node(**data)
+ node = Node(**node_columns(data))
db.add(node)
await db.flush()
# A new device node gets (or joins) its Device Inventory row — the canvas is
# one more way to document hardware, not a parallel store.
- await link_node(db, node)
+ await link_facts(
+ db, node, facts_from_payload(data, label=data["label"], node_type=data["type"])
+ )
await db.commit()
await db.refresh(node)
return await _hydrate(db, node)
@@ -140,10 +149,20 @@ async def update_node(
node = await db.get(Node, node_id)
if not node:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Node not found")
- for field, value in body.model_dump(exclude_unset=True).items():
+ sent = body.model_dump(exclude_unset=True)
+ for field, value in node_columns(sent).items():
setattr(node, field, value)
# The user edited the device, not just its drawing: push the facts down.
- await link_node(db, node, overwrite_scalars=True, replace_lists=True)
+ # Only what was sent counts — an omitted field must not clear the row, and
+ # lists are replaced only when the client actually sent them.
+ facts = facts_from_update(sent)
+ await link_facts(
+ db,
+ node,
+ facts,
+ overwrite_scalars=True,
+ replace_lists="properties" in sent or "services" in sent,
+ )
await db.commit()
await db.refresh(node)
return await _hydrate(db, node)
diff --git a/backend/app/api/routes/proxmox.py b/backend/app/api/routes/proxmox.py
index 9e2283a..267d2b6 100644
--- a/backend/app/api/routes/proxmox.py
+++ b/backend/app/api/routes/proxmox.py
@@ -35,7 +35,7 @@ from app.schemas.proxmox import (
from app.schemas.scan import ScanRunResponse
from app.services.discovery_sources import add_source
from app.services.mac_utils import normalize_mac
-from app.services.node_dedupe import dedupe_nodes_by_ieee
+from app.services.node_dedupe import dedupe_nodes_by_device
from app.services.proxmox_service import (
build_proxmox_cluster_links,
build_proxmox_properties,
@@ -254,7 +254,7 @@ async def _persist_pending_import(
Update-in-place only. Nothing is ever deleted; hidden rows stay hidden.
"""
- await dedupe_nodes_by_ieee(db)
+ await dedupe_nodes_by_device(db)
cluster_pairs = build_proxmox_cluster_links(nodes_raw)
cluster_members = {ieee for pair in cluster_pairs for ieee in pair}
@@ -270,51 +270,44 @@ async def _persist_pending_import(
mac = normalize_mac(n.get("mac"))
props = build_proxmox_properties(n)
- # 1) Already on a canvas? Match by ieee OR ip OR mac (the cross-source
- # dedup key — a stopped VM has no IP but its configured NIC MAC still
- # matches an ARP-scanned node). Refresh in place: merge properties, adopt
- # the pve identity onto a scanned node, backfill blank specs/hostname/mac.
- # Do NOT stomp user-set type/status.
- node_filter = [Node.ieee_address == ieee]
- if ip:
- node_filter.append(Node.ip == ip)
- if mac:
- node_filter.append(Node.mac == mac)
- existing_nodes = (
- await db.execute(select(Node).where(or_(*node_filter)).order_by(Node.id))
- ).scalars().all()
-
- if existing_nodes:
- for en in existing_nodes:
- en.properties = merge_proxmox_properties(en.properties, props)
- if not en.ieee_address:
- en.ieee_address = ieee
- if ip and not en.ip:
- en.ip = ip
- if mac and not en.mac:
- en.mac = mac
- en.hostname = en.hostname or n.get("hostname")
- en.cpu_count = en.cpu_count or n.get("cpu_count")
- en.ram_gb = en.ram_gb or n.get("ram_gb")
- en.disk_gb = en.disk_gb or n.get("disk_gb")
- # A cluster host needs one left + one right handle for the
- # cluster edge endpoints (both default to 0).
- if ieee in cluster_members:
- en.left_handles = max(en.left_handles or 0, 1)
- en.right_handles = max(en.right_handles or 0, 1)
- await _ensure_inventory_row(db, ieee, ip, mac, n, props, approved=True)
- pending_updated += 1
- continue
-
- # 2) Not on canvas — upsert the pending inventory row.
+ # Match by ieee OR ip OR mac (the cross-source dedup key — a stopped VM
+ # has no IP but its configured NIC MAC still matches an ARP-scanned
+ # device). The inventory row owns the facts, so the import merges into
+ # it whether or not the device is drawn anywhere; a node only needs its
+ # cluster handles adjusted. Do NOT stomp user-set type/status.
pending = await _find_pending(db, ieee, ip, mac)
+ drawn = bool(
+ pending
+ and (
+ await db.execute(select(Node.id).where(Node.device_id == pending.id).limit(1))
+ ).scalar_one_or_none()
+ )
+
if pending is None:
db.add(_new_pending(ieee, ip, mac, n, props, status="pending"))
pending_created += 1
else:
- _refresh_pending(pending, ieee, ip, mac, n, props)
+ if drawn:
+ # Already on a canvas: refresh the facts but leave the lifecycle
+ # alone (_refresh_pending would revive it as pending).
+ await _ensure_inventory_row(db, ieee, ip, mac, n, props, approved=True)
+ else:
+ _refresh_pending(pending, ieee, ip, mac, n, props)
+ pending.cpu_count = pending.cpu_count or n.get("cpu_count")
+ pending.ram_gb = pending.ram_gb or n.get("ram_gb")
+ pending.disk_gb = pending.disk_gb or n.get("disk_gb")
pending_updated += 1
+ # A cluster host needs one left + one right handle for the cluster
+ # edge endpoints (both default to 0) — the one piece of this that is
+ # genuinely about how the node is drawn.
+ if ieee in cluster_members:
+ for en in (
+ await db.execute(select(Node).where(Node.device_id == pending.id))
+ ).scalars().all():
+ en.left_handles = max(en.left_handles or 0, 1)
+ en.right_handles = max(en.right_handles or 0, 1)
+
links_recorded = await _replace_links(db, edges_raw, cluster_pairs)
await db.commit()
@@ -357,6 +350,11 @@ def _new_pending(
vendor=n.get("vendor"),
model=n.get("model"),
properties=props,
+ # Specs the guest reports. They used to land only on the canvas node;
+ # the inventory row owns them now.
+ cpu_count=n.get("cpu_count"),
+ ram_gb=n.get("ram_gb"),
+ disk_gb=n.get("disk_gb"),
status=status,
discovery_source=_PROXMOX_GUEST_SOURCE,
discovery_sources=[_PROXMOX_GUEST_SOURCE],
diff --git a/backend/app/api/routes/racks.py b/backend/app/api/routes/racks.py
index 3af0291..03184cc 100644
--- a/backend/app/api/routes/racks.py
+++ b/backend/app/api/routes/racks.py
@@ -286,37 +286,38 @@ async def rack_inventory(
# to find anything in the first place.
pinned = {device_id: node_id for device_id, node_id in mounts if device_id and node_id}
- # Correlate against canvas nodes for live status: IEEE first (stable), then IP.
+ # Correlate against canvas nodes: a node names the inventory row it draws,
+ # so the IEEE/IP guessing this used to do is gone — the link is explicit.
nodes = (await db.execute(select(Node))).scalars().all()
by_id = {n.id: n for n in nodes}
- by_ieee = {n.ieee_address: n for n in nodes if n.ieee_address}
- by_ip: dict[str, Node] = {}
+ by_device: dict[str, Node] = {}
for node in nodes:
- for token in _ip_tokens(node.ip):
- by_ip.setdefault(token, node)
+ if node.device_id:
+ by_device.setdefault(node.device_id, node)
# Which canvas the matched node lives on — the rack prints it so the user
# knows where the logical twin of a mount is drawn.
design_names = {
d.id: d.name for d in (await db.execute(select(Design))).scalars().all()
}
+ # A pinned node may draw a *different* device than the mount names, so its
+ # view is read off its own row.
+ devices_by_id = {d.id: d for d in devices}
+
items = []
for device in devices:
if device.suggested_type in _UNRACKABLE_TYPES:
continue
# A node the user linked by hand wins; a deleted one falls back to the
- # guess rather than reporting a node that no longer exists.
- linked: Node | None = by_id.get(pinned.get(device.id) or "")
- if linked is None and device.ieee_address:
- linked = by_ieee.get(device.ieee_address)
- if linked is None:
- linked = next((by_ip[t] for t in _ip_tokens(device.ip) if t in by_ip), None)
- # The node is the richer record once a device has been approved onto a
- # canvas; the inventory row is what discovery last saw. Report both and
- # let the panel prefer the node.
- services = _services(device.services) or (
- _services(linked.services) if linked else []
- )
+ # node that draws this device, if any.
+ linked: Node | None = by_id.get(pinned.get(device.id) or "") or by_device.get(device.id)
+ # The logical view is whatever the linked node actually draws. Usually
+ # that is this same row — the node_* fields then simply mirror it.
+ node_device = None
+ if linked is not None:
+ node_device = devices_by_id.get(linked.device_id or "")
+ if node_device is None and linked.device_id:
+ node_device = await db.get(InventoryDevice, linked.device_id)
items.append(
RackInventoryItem(
id=device.id,
@@ -328,21 +329,25 @@ async def rack_inventory(
mac=device.mac or device.ieee_address,
hostname=device.hostname,
os=device.os,
- services=services,
+ services=_services(device.services),
node_id=linked.id if linked else None,
- node_status=linked.status if linked else None,
- node_label=linked.label if linked else None,
- node_type=linked.type if linked else None,
- node_ip=linked.ip if linked else None,
- node_mac=(linked.mac or linked.ieee_address) if linked else None,
- node_hostname=linked.hostname if linked else None,
- node_os=linked.os if linked else None,
- node_check_method=linked.check_method if linked else None,
+ node_status=node_device.status_live if node_device else None,
+ node_label=(
+ (node_device.label if node_device else None) or linked.label
+ ) if linked else None,
+ node_type=(
+ (node_device.type if node_device else None) or linked.type
+ ) if linked else None,
+ node_ip=node_device.ip if node_device else None,
+ node_mac=(node_device.mac or node_device.ieee_address) if node_device else None,
+ node_hostname=node_device.hostname if node_device else None,
+ node_os=node_device.os if node_device else None,
+ node_check_method=node_device.check_method if node_device else None,
node_design_id=linked.design_id if linked else None,
node_design_name=(
design_names.get(linked.design_id) if linked and linked.design_id else None
),
- node_last_seen=linked.last_seen if linked else None,
+ node_last_seen=node_device.last_seen if node_device else None,
racked=device.id in mounted,
)
)
diff --git a/backend/app/api/routes/scan.py b/backend/app/api/routes/scan.py
index c93cdee..1c23cc6 100644
--- a/backend/app/api/routes/scan.py
+++ b/backend/app/api/routes/scan.py
@@ -28,9 +28,10 @@ from app.schemas.scan import (
InventoryDeviceUpdate,
ScanRunResponse,
)
-from app.services.inventory_sync import merge_node_into_device
+from app.services.discovery_sources import add_source
+from app.services.inventory_sync import find_device_for, merge_properties, merge_services
from app.services.mac_utils import normalize_mac
-from app.services.node_dedupe import dedupe_nodes_by_ieee, find_duplicate_node
+from app.services.node_dedupe import dedupe_nodes_by_device, find_duplicate_node
from app.services.scanner import DeepScanOptions, _valid_port_range, request_cancel, run_scan
from app.services.zigbee_service import (
build_zigbee_properties,
@@ -250,65 +251,38 @@ def _agg(values: list[datetime], *, newest: bool) -> datetime | None:
async def _canvas_correlation(
db: AsyncSession, devices: list[InventoryDevice]
) -> dict[str, dict[str, Any]]:
- """Correlate each device to existing canvas nodes by ``ieee_address``, ``mac``
- or ``ip``.
+ """Correlate each device to the canvas nodes drawing it.
Returns, per device id: the number of distinct canvases (designs) it appears
- on, plus aggregated timestamps from every matching node — created_at (oldest),
- last_scan / updated_at / last_seen (newest). One node query, grouped in Python
- (node counts are small for a homelab), so no N+1 per device.
-
- IP matching is per-address: a node's ``ip`` may hold several comma-separated
- addresses (e.g. an IPv6 added before the IPv4), so we index each token, not
- the raw string. MAC is a stable identifier immune to such IP edits, so it is
- matched too — cumulatively with ieee/ip (issue #258).
+ on, plus aggregated node timestamps — created_at (oldest) and updated_at
+ (newest). A node names the inventory row it draws, so this is a group-by on
+ ``device_id`` rather than the ieee/mac/ip guesswork it used to be.
+
+ ``last_scan`` and ``last_seen`` come off the device row itself now: they are
+ observations of the device, not of any one drawing of it.
"""
if not devices:
return {}
rows = (
await db.execute(
- select(
- Node.ip,
- Node.mac,
- Node.ieee_address,
- Node.design_id,
- Node.created_at,
- Node.last_scan,
- Node.updated_at,
- Node.last_seen,
- ).where(Node.design_id.isnot(None))
+ select(Node.device_id, Node.design_id, Node.created_at, Node.updated_at)
+ .where(Node.design_id.isnot(None), Node.device_id.isnot(None))
)
).all()
- # Index matching nodes by ip token, mac and ieee so a device can look up any.
- by_ip: dict[str, list[Any]] = {}
- by_mac: dict[str, list[Any]] = {}
- by_ieee: dict[str, list[Any]] = {}
+ by_device: dict[str, list[Any]] = {}
for row in rows:
- for tok in _ip_tokens(row.ip):
- by_ip.setdefault(tok, []).append(row)
- if row.mac:
- by_mac.setdefault(row.mac, []).append(row)
- if row.ieee_address:
- by_ieee.setdefault(row.ieee_address, []).append(row)
+ by_device.setdefault(row.device_id, []).append(row)
info: dict[str, dict[str, Any]] = {}
for d in devices:
- matched = []
- if d.ieee_address:
- matched += by_ieee.get(d.ieee_address, [])
- if d.mac:
- matched += by_mac.get(d.mac, [])
- for tok in _ip_tokens(d.ip):
- matched += by_ip.get(tok, [])
- # De-duplicate nodes matched by more than one identifier.
- matched = list({id(m): m for m in matched}.values())
+ matched = by_device.get(d.id, [])
designs = {m.design_id for m in matched}
info[d.id] = {
"canvas_count": len(designs),
"node_created_at": _agg([m.created_at for m in matched], newest=False),
- "node_last_scan": _agg([m.last_scan for m in matched], newest=True),
+ "node_last_scan": d.last_scan,
"node_last_modified": _agg([m.updated_at for m in matched], newest=True),
- "node_last_seen": _agg([m.last_seen for m in matched], newest=True),
+ "node_last_seen": d.last_seen,
}
return info
@@ -347,13 +321,35 @@ async def create_pending(
Lands as ``status="pending"`` like a discovery would, so the existing approve
/ hide / restore flows apply unchanged.
"""
+ # One device is one row. If this host is already known — by ieee, ip or mac —
+ # the user is documenting the device they already have, so fill in what the
+ # row is missing rather than splitting it in two.
+ mac = normalize_mac(body.mac)
+ existing = await find_device_for(db, ip=body.ip, mac=mac, ieee=None)
+ if existing is not None:
+ for field in (
+ "hostname", "ip", "os", "suggested_type", "model", "vendor", "label", "type",
+ "notes", "cpu_count", "cpu_model", "ram_gb", "disk_gb", "check_method",
+ "check_target", "friendly_name", "device_subtype",
+ ):
+ value = getattr(body, field, None)
+ if value not in (None, "") and getattr(existing, field, None) in (None, ""):
+ setattr(existing, field, value)
+ existing.mac = existing.mac or mac
+ existing.properties = merge_properties(existing.properties, body.properties)
+ existing.services = merge_services(existing.services, body.services)
+ existing.discovery_sources = add_source(existing.discovery_sources, body.discovery_source)
+ await db.commit()
+ await db.refresh(existing)
+ return (await _with_canvas_counts(db, [existing]))[0]
+
device = InventoryDevice(
hostname=body.hostname,
ip=body.ip,
# Canonical form, like every other write path: dedup compares MACs by
# equality, so a hand-typed "AA-BB-CC-11-22-33" would never match the
# scanned "aa:bb:cc:11:22:33" and approve would build a duplicate node.
- mac=normalize_mac(body.mac),
+ mac=mac,
suggested_type=body.suggested_type,
model=body.model,
vendor=body.vendor,
@@ -468,7 +464,7 @@ async def bulk_approve_devices(
_: str = Depends(get_current_user),
) -> dict[str, Any]:
# Repair any legacy same-canvas duplicate nodes before placing more.
- await dedupe_nodes_by_ieee(db)
+ await dedupe_nodes_by_device(db)
# Target the design the user is on; fall back to the first design.
default_design_id = payload.design_id
@@ -489,25 +485,18 @@ async def bulk_approve_devices(
)
devices = result.scalars().all()
- # What already sits on the target canvas, so we skip devices already placed
- # here (by ip, mac or ieee_address) instead of creating duplicate nodes. We
- # map to the existing node id so the skip report can point the user at it. A
- # value may be a Node still pending flush (in-batch duplicate) — resolved to
- # its id after the flush below. IPs are indexed per comma-separated token so
- # a node whose ip is "fe80::1, 192.168.1.5" still matches a device scanned as
- # 192.168.1.5 (issue #258).
+ # What already sits on the target canvas. A node names the inventory row it
+ # draws, so "already placed" is now a device-id lookup rather than a guess
+ # across ip/mac/ieee. The value may be a Node still pending flush (in-batch
+ # duplicate) — resolved to its id after the flush below.
existing = (
await db.execute(
- select(Node.id, Node.ip, Node.mac, Node.ieee_address).where(
- Node.design_id == default_design_id
+ select(Node.id, Node.device_id).where(
+ Node.design_id == default_design_id, Node.device_id.isnot(None)
)
)
).all()
- placed_ips: dict[str, Any] = {
- tok: nid for nid, ip, _, _ in existing for tok in _ip_tokens(ip)
- }
- placed_mac: dict[str, Any] = {mac: nid for nid, _, mac, _ in existing if mac}
- placed_ieee: dict[str, Any] = {ieee: nid for nid, _, _, ieee in existing if ieee}
+ placed: dict[str, Any] = {device_id: nid for nid, device_id in existing}
created_nodes: list[Node] = []
approved_devices: list[InventoryDevice] = []
@@ -521,68 +510,58 @@ async def bulk_approve_devices(
"match": "rack", "value": "rack device", "_ref": None,
})
continue
- # Record which identifier collided so the caller can explain each skip
- # (and, for existing on-canvas nodes, link to the node already there).
- ip_hit = next((t for t in _ip_tokens(device.ip) if t in placed_ips), None)
- if ip_hit is not None:
- skipped_devices.append({
- "device_id": device.id,
- "label": device.hostname or device.friendly_name or device.ip or "device",
- "match": "ip", "value": ip_hit, "_ref": placed_ips[ip_hit],
- })
- continue
- if device.ieee_address is not None and device.ieee_address in placed_ieee:
- skipped_devices.append({
- "device_id": device.id,
- "label": device.hostname or device.friendly_name or device.ieee_address or "device",
- "match": "ieee", "value": device.ieee_address, "_ref": placed_ieee[device.ieee_address],
- })
- continue
- if device.mac is not None and device.mac in placed_mac:
+ # Report which identifier names the device, so the caller can explain
+ # the skip and link to the node already drawing it.
+ if device.id in placed:
+ match, value = (
+ ("ip", device.ip) if device.ip
+ else ("ieee", device.ieee_address) if device.ieee_address
+ else ("mac", device.mac)
+ )
skipped_devices.append({
"device_id": device.id,
- "label": device.hostname or device.friendly_name or device.mac or "device",
- "match": "mac", "value": device.mac, "_ref": placed_mac[device.mac],
+ "label": device.hostname or device.friendly_name or value or "device",
+ "match": match, "value": value, "_ref": placed[device.id],
})
continue
device.status = "approved"
node_type = device.suggested_type or "generic"
is_wireless = _is_wireless(node_type)
cluster_host = await _is_proxmox_cluster_member(db, device.ieee_address)
+ # Enrich the row, not the node: a mesh device's radio properties and the
+ # default check method describe the device itself.
+ device.type = device.type or node_type
+ device.label = device.label or device.hostname or device.friendly_name or device.ip or "device"
+ device.properties = (
+ _wireless_properties(node_type, device.ieee_address, device.vendor, device.model, device.lqi)
+ if is_wireless
+ else merge_mac_property(list(device.properties or []), device.mac)
+ )
+ if is_wireless:
+ # A mesh device answers no ICMP; being in the mesh is the liveness.
+ device.check_method = "none"
+ device.status_live = "online"
+ elif not device.check_method:
+ # Default to ping so the status checker actually polls it. Without
+ # this the scheduler skips it (check_method NULL -> no check).
+ device.check_method = "ping" if device.ip else None
node = Node(
- label=device.hostname or device.friendly_name or device.ip or "device",
- type=node_type,
- ip=device.ip,
- mac=device.mac,
- hostname=device.hostname,
- status="online" if is_wireless else "unknown",
- services=device.services or [],
- ieee_address=device.ieee_address,
- properties=_wireless_properties(
- node_type, device.ieee_address, device.vendor, device.model, device.lqi
- ) if is_wireless else merge_mac_property(list(device.properties or []), device.mac),
- # Default to ping so the status checker actually polls the new node.
- # Without this the scheduler skips it (check_method NULL → no check).
- check_method="none" if is_wireless else ("ping" if device.ip else None),
- # Cluster hosts get side handles for their host↔host cluster edge.
+ label=device.label,
+ type=device.type,
+ # Cluster hosts get side handles for their host<->host cluster edge.
left_handles=1 if cluster_host else 0,
right_handles=1 if cluster_host else 0,
design_id=default_design_id,
- # The node draws this inventory row; the row keeps owning the facts.
+ # The node draws this inventory row; the row owns the facts.
device_id=device.id,
)
db.add(node)
created_nodes.append(node)
approved_devices.append(device)
- # Track within this batch so a duplicate selection (same ip/mac/ieee) is
- # not placed twice on the same canvas. Store the Node so a later in-batch
- # skip can resolve to its id after flush.
- for tok in _ip_tokens(device.ip):
- placed_ips[tok] = node
- if device.mac:
- placed_mac[device.mac] = node
- if device.ieee_address:
- placed_ieee[device.ieee_address] = node
+ # Track within this batch so a duplicate selection is not placed twice
+ # on the same canvas. Stores the Node so a later in-batch skip can
+ # resolve to its id after flush.
+ placed[device.id] = node
await db.flush() # populates node.id from Python-side default before reading
# node_ids and approved_device_ids stay index-aligned for the client's mapping.
node_ids = [n.id for n in created_nodes]
@@ -716,40 +695,56 @@ async def approve_device(
raise HTTPException(status_code=409, detail=conflict)
device.status = "approved"
- # Prefer the MAC discovered during the scan (stored on the pending device);
+ # Prefer the MAC discovered during the scan (stored on the inventory row);
# fall back to whatever the approve payload carried.
_mac = device.mac or node_data.mac
cluster_host = await _is_proxmox_cluster_member(db, device.ieee_address)
- node = Node(
- label=node_data.label,
- type=node_data.type,
- ip=node_data.ip,
- mac=_mac,
- hostname=node_data.hostname,
- status="online" if wireless else node_data.status,
- services=node_data.services or [],
- ieee_address=device.ieee_address,
- properties=_wireless_properties(
+
+ # The approve dialog is an edit of the device, so its values land on the row
+ # — the node that follows only says where it is drawn. A blank field in the
+ # payload never clears what discovery already found.
+ device.label = node_data.label or device.label
+ device.type = node_data.type or device.type
+ device.ip = node_data.ip or device.ip
+ device.mac = _mac
+ device.hostname = node_data.hostname or device.hostname
+ device.services = merge_services(device.services, node_data.services)
+ device.properties = (
+ _wireless_properties(
node_data.type, device.ieee_address, device.vendor, device.model, device.lqi
- ) if wireless else merge_mac_property(
+ )
+ if wireless
+ else merge_mac_property(
merge_zigbee_properties(list(device.properties or []), node_data.properties or []),
_mac,
- ),
- check_method="none" if wireless else (node_data.check_method or ("ping" if node_data.ip else None)),
- check_target=None if wireless else node_data.check_target,
- # Cluster hosts get side handles for their host↔host cluster edge.
+ )
+ )
+ if wireless:
+ # A mesh device answers no ICMP; being in the mesh is the liveness.
+ device.check_method = "none"
+ device.check_target = None
+ device.status_live = "online"
+ else:
+ device.check_method = node_data.check_method or device.check_method or (
+ "ping" if device.ip else None
+ )
+ device.check_target = node_data.check_target or device.check_target
+ if node_data.status:
+ device.status_live = node_data.status
+
+ node = Node(
+ label=device.label or node_data.label,
+ type=device.type or node_data.type,
+ # Cluster hosts get side handles for their host<->host cluster edge.
left_handles=1 if cluster_host else 0,
right_handles=1 if cluster_host else 0,
design_id=node_design_id,
- # The node draws this inventory row; the row keeps owning the facts.
+ # The node draws this inventory row; the row owns the facts.
device_id=device.id,
)
db.add(node)
await db.flush()
node_id = node.id
- # The approve payload may carry user edits (a renamed label, a typed IP) the
- # inventory row has never seen — fold them back so it stays the source.
- merge_node_into_device(device, node, overwrite_scalars=False, replace_lists=False)
edges = await _resolve_pending_links_for_ieee(db, device.ieee_address, node_design_id)
@@ -818,13 +813,16 @@ async def _resolve_pending_links_for_ieee(
for link in links
}
other_ieees.add(ieee)
+ # A node reaches its ieee through the inventory row it draws.
nodes_q = await db.execute(
- select(Node).where(
- Node.ieee_address.in_(other_ieees),
+ select(InventoryDevice.ieee_address, Node)
+ .join(Node, Node.device_id == InventoryDevice.id)
+ .where(
+ InventoryDevice.ieee_address.in_(other_ieees),
Node.design_id == design_id,
)
)
- by_ieee = {n.ieee_address: n for n in nodes_q.scalars().all() if n.ieee_address}
+ by_ieee = {row_ieee: node for row_ieee, node in nodes_q.all() if row_ieee}
self_node = by_ieee.get(ieee)
if self_node is None:
diff --git a/backend/app/api/routes/stats.py b/backend/app/api/routes/stats.py
index 6705269..c3bb989 100644
--- a/backend/app/api/routes/stats.py
+++ b/backend/app/api/routes/stats.py
@@ -30,10 +30,23 @@ async def summary(
"""
_check_key(x_api_key)
+ # A node's reachability lives on the Device Inventory row it draws, so the
+ # per-canvas counts come from the join. A node with no row (canvas
+ # furniture) counts as unknown.
status_rows = (
- await db.execute(select(Node.status, func.count()).group_by(Node.status))
+ await db.execute(
+ select(InventoryDevice.status_live, func.count())
+ .select_from(Node)
+ .join(InventoryDevice, Node.device_id == InventoryDevice.id)
+ .group_by(InventoryDevice.status_live)
+ )
).all()
- counts = {row[0]: row[1] for row in status_rows}
+ counts = {(row[0] or "unknown"): row[1] for row in status_rows}
+ unlinked = (
+ await db.execute(select(func.count()).select_from(Node).where(Node.device_id.is_(None)))
+ ).scalar_one()
+ if unlinked:
+ counts["unknown"] = counts.get("unknown", 0) + unlinked
pending = (
await db.execute(
@@ -45,7 +58,10 @@ async def summary(
zigbee = (
await db.execute(
- select(func.count()).select_from(Node).where(Node.ieee_address.isnot(None))
+ select(func.count())
+ .select_from(Node)
+ .join(InventoryDevice, Node.device_id == InventoryDevice.id)
+ .where(InventoryDevice.ieee_address.isnot(None))
)
).scalar_one()
diff --git a/backend/app/api/routes/status.py b/backend/app/api/routes/status.py
index 27db337..1a20399 100644
--- a/backend/app/api/routes/status.py
+++ b/backend/app/api/routes/status.py
@@ -71,20 +71,36 @@ async def _broadcast(payload: str) -> None:
_drop(conn)
-async def broadcast_status(node_id: str, status: str, checked_at: str, response_time_ms: int | None = None) -> None:
+async def broadcast_status(
+ device_id: str,
+ node_ids: list[str],
+ status: str,
+ checked_at: str,
+ response_time_ms: int | None = None,
+) -> None:
+ """Report one device's reachability to every canvas drawing it.
+
+ A device is checked once, not once per canvas, so the result is addressed by
+ device and carries the nodes it lights up. ``node_ids`` is empty for a
+ device that is in the inventory but on no canvas.
+ """
await _broadcast(json.dumps({
"type": "status",
- "node_id": node_id,
+ "device_id": device_id,
+ "node_ids": node_ids,
"status": status,
"checked_at": checked_at,
"response_time_ms": response_time_ms,
}))
-async def broadcast_service_status(node_id: str, services: list[dict[str, object]], checked_at: str) -> None:
+async def broadcast_service_status(
+ device_id: str, node_ids: list[str], services: list[dict[str, object]], checked_at: str
+) -> None:
await _broadcast(json.dumps({
"type": "service_status",
- "node_id": node_id,
+ "device_id": device_id,
+ "node_ids": node_ids,
"services": services,
"checked_at": checked_at,
}))
diff --git a/backend/app/api/routes/zigbee.py b/backend/app/api/routes/zigbee.py
index 15055e8..37cdf13 100644
--- a/backend/app/api/routes/zigbee.py
+++ b/backend/app/api/routes/zigbee.py
@@ -27,7 +27,7 @@ from app.schemas.zigbee import (
ZigbeeTestConnectionRequest,
ZigbeeTestConnectionResponse,
)
-from app.services.node_dedupe import dedupe_nodes_by_ieee
+from app.services.node_dedupe import dedupe_nodes_by_device
from app.services.zigbee_service import (
build_zigbee_properties,
fetch_networkmap,
@@ -36,6 +36,15 @@ from app.services.zigbee_service import (
)
logger = logging.getLogger(__name__)
+
+
+async def _is_drawn(db: AsyncSession, device_id: str) -> bool:
+ """True while at least one canvas node still draws this device."""
+ return (
+ await db.execute(select(Node.id).where(Node.device_id == device_id).limit(1))
+ ).scalar_one_or_none() is not None
+
+
router = APIRouter()
@@ -192,7 +201,7 @@ async def _persist_pending_import(
"""
# Repair any pre-existing duplicate nodes (same IEEE) before upserting, so
# the by-IEEE lookups below resolve to a single row.
- await dedupe_nodes_by_ieee(db)
+ await dedupe_nodes_by_device(db)
# Coordinator is no longer auto-placed, so the response's coordinator fields
# stay unset — retained for backward-compatible response shape.
@@ -213,55 +222,10 @@ async def _persist_pending_import(
# the pending inventory like every other device, so the user approves it
# explicitly. Only the shared paths below run for it.
- # If the device has already been approved as a canvas Node, refresh its
- # properties on every canvas it sits on. Still ensure the discovery
- # inventory carries a row for it (status="approved") so it shows in the
- # inventory list with an "In N canvas" badge — legacy auto-placed
- # coordinators never got a pending row, which is why they went missing.
- existing_nodes = (
- await db.execute(
- select(Node).where(Node.ieee_address == ieee).order_by(Node.id)
- )
- ).scalars().all()
- if existing_nodes:
- for existing_node in existing_nodes:
- existing_node.properties = merge_zigbee_properties(
- existing_node.properties, props
- )
- inv = (
- await db.execute(
- select(InventoryDevice).where(InventoryDevice.ieee_address == ieee)
- )
- ).scalar_one_or_none()
- if inv is None:
- db.add(
- InventoryDevice(
- ieee_address=ieee,
- friendly_name=n.get("friendly_name"),
- hostname=n.get("friendly_name"),
- suggested_type=n.get("type"),
- device_subtype=n.get("device_type"),
- model=n.get("model"),
- vendor=n.get("vendor"),
- lqi=n.get("lqi"),
- status="approved",
- discovery_source="zigbee",
- )
- )
- pending_created += 1
- else:
- # Refresh metadata but never change the row's status (an approved
- # device stays approved; a hidden one stays hidden).
- inv.friendly_name = n.get("friendly_name") or inv.friendly_name
- inv.suggested_type = n.get("type") or inv.suggested_type
- inv.device_subtype = n.get("device_type") or inv.device_subtype
- inv.model = n.get("model") or inv.model
- inv.vendor = n.get("vendor") or inv.vendor
- if n.get("lqi") is not None:
- inv.lqi = n.get("lqi")
- pending_updated += 1
- continue
-
+ # Properties belong to the inventory row now, so one upsert serves
+ # every canvas drawing this device — there is no per-node refresh left
+ # to do. The row's status is never touched here: an approved device
+ # stays approved, a hidden one stays hidden.
result = await db.execute(
select(InventoryDevice).where(InventoryDevice.ieee_address == ieee)
)
@@ -277,6 +241,7 @@ async def _persist_pending_import(
model=n.get("model"),
vendor=n.get("vendor"),
lqi=n.get("lqi"),
+ properties=props,
status="pending",
discovery_source="zigbee",
)
@@ -290,11 +255,11 @@ async def _persist_pending_import(
pending.vendor = n.get("vendor") or pending.vendor
if n.get("lqi") is not None:
pending.lqi = n.get("lqi")
- if pending.status == "approved":
- # The device was approved earlier but its canvas Node no longer
- # exists (no Node matched the IEEE above) — it was deleted. Revive
- # the row to "pending" so it reappears in the Pending list on
- # re-import instead of being silently swallowed. (Issue #167)
+ pending.properties = merge_zigbee_properties(list(pending.properties or []), props)
+ if pending.status == "approved" and not await _is_drawn(db, pending.id):
+ # Approved earlier but no canvas draws it any more — the node was
+ # deleted. Revive the row to "pending" so it reappears in the list
+ # on re-import instead of being silently swallowed. (Issue #167)
pending.status = "pending"
elif pending.status == "hidden":
# Re-imported a hidden device → leave it hidden, just refresh fields.
diff --git a/backend/app/api/routes/zwave.py b/backend/app/api/routes/zwave.py
index 22db66c..2ae7b7b 100644
--- a/backend/app/api/routes/zwave.py
+++ b/backend/app/api/routes/zwave.py
@@ -27,7 +27,7 @@ from app.schemas.zwave import (
ZwaveTestConnectionRequest,
ZwaveTestConnectionResponse,
)
-from app.services.node_dedupe import dedupe_nodes_by_ieee
+from app.services.node_dedupe import dedupe_nodes_by_device
from app.services.zwave_service import (
build_zwave_properties,
fetch_zwave_network,
@@ -36,6 +36,15 @@ from app.services.zwave_service import (
)
logger = logging.getLogger(__name__)
+
+
+async def _is_drawn(db: AsyncSession, device_id: str) -> bool:
+ """True while at least one canvas node still draws this device."""
+ return (
+ await db.execute(select(Node.id).where(Node.device_id == device_id).limit(1))
+ ).scalar_one_or_none() is not None
+
+
router = APIRouter()
@@ -189,7 +198,7 @@ async def _persist_pending_import(
"""
# Repair any pre-existing same-canvas duplicate nodes before upserting, so
# the by-IEEE lookups below resolve cleanly.
- await dedupe_nodes_by_ieee(db)
+ await dedupe_nodes_by_device(db)
# Coordinator is no longer auto-placed, so the response's coordinator fields
# stay unset — retained for backward-compatible response shape.
@@ -208,54 +217,10 @@ async def _persist_pending_import(
# the pending inventory like every other device, so the user approves it
# explicitly. Only the shared paths below run for it.
- # Already approved as a canvas Node → refresh props on every canvas it
- # sits on. Still ensure the discovery inventory carries a row for it
- # (status="approved") so it shows in the inventory list with an
- # "In N canvas" badge — legacy auto-placed coordinators never got a
- # pending row, which is why they went missing.
- existing_nodes = (
- await db.execute(
- select(Node).where(Node.ieee_address == ieee).order_by(Node.id)
- )
- ).scalars().all()
- if existing_nodes:
- for existing_node in existing_nodes:
- existing_node.properties = merge_zwave_properties(
- existing_node.properties, props
- )
- inv = (
- await db.execute(
- select(InventoryDevice).where(InventoryDevice.ieee_address == ieee)
- )
- ).scalar_one_or_none()
- if inv is None:
- db.add(
- InventoryDevice(
- ieee_address=ieee,
- friendly_name=n.get("friendly_name"),
- hostname=n.get("friendly_name"),
- suggested_type=n.get("type"),
- device_subtype=n.get("device_type"),
- model=n.get("model"),
- vendor=n.get("vendor"),
- lqi=n.get("lqi"),
- status="approved",
- discovery_source="zwave",
- )
- )
- pending_created += 1
- else:
- # Refresh metadata but never change the row's status.
- inv.friendly_name = n.get("friendly_name") or inv.friendly_name
- inv.suggested_type = n.get("type") or inv.suggested_type
- inv.device_subtype = n.get("device_type") or inv.device_subtype
- inv.model = n.get("model") or inv.model
- inv.vendor = n.get("vendor") or inv.vendor
- if n.get("lqi") is not None:
- inv.lqi = n.get("lqi")
- pending_updated += 1
- continue
-
+ # Properties belong to the inventory row now, so one upsert serves
+ # every canvas drawing this device — there is no per-node refresh left
+ # to do. The row's status is never touched here: an approved device
+ # stays approved, a hidden one stays hidden.
result = await db.execute(
select(InventoryDevice).where(InventoryDevice.ieee_address == ieee)
)
@@ -271,6 +236,7 @@ async def _persist_pending_import(
model=n.get("model"),
vendor=n.get("vendor"),
lqi=n.get("lqi"),
+ properties=props,
status="pending",
discovery_source="zwave",
)
@@ -282,9 +248,10 @@ async def _persist_pending_import(
pending.device_subtype = n.get("device_type") or pending.device_subtype
pending.model = n.get("model") or pending.model
pending.vendor = n.get("vendor") or pending.vendor
- if pending.status == "approved":
- # Approved earlier but the canvas Node is gone (deleted) — revive
- # to "pending" so it reappears in the list instead of vanishing.
+ pending.properties = merge_zwave_properties(list(pending.properties or []), props)
+ if pending.status == "approved" and not await _is_drawn(db, pending.id):
+ # Approved earlier but no canvas draws it any more (the node was
+ # deleted) — revive to "pending" so it reappears in the list.
pending.status = "pending"
elif pending.status == "hidden":
pass
diff --git a/backend/app/core/scheduler.py b/backend/app/core/scheduler.py
index 470b1ea..07134c7 100644
--- a/backend/app/core/scheduler.py
+++ b/backend/app/core/scheduler.py
@@ -7,10 +7,11 @@ from typing import TYPE_CHECKING, Any
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession
from app.core.config import settings
from app.db.database import AsyncSessionLocal
-from app.db.models import Node
+from app.db.models import InventoryDevice, Node
from app.services.status_checker import check_node, check_services
if TYPE_CHECKING:
@@ -22,13 +23,23 @@ logger = logging.getLogger(__name__)
scheduler: AsyncIOScheduler = AsyncIOScheduler()
-async def _check_single_node(
- node_id: str,
+async def _nodes_by_device(db: AsyncSession) -> dict[str, list[str]]:
+ """Map each device id to the nodes drawing it, across every canvas."""
+ rows = (await db.execute(select(Node.id, Node.device_id).where(Node.device_id.is_not(None)))).all()
+ out: dict[str, list[str]] = {}
+ for node_id, device_id in rows:
+ out.setdefault(device_id, []).append(node_id)
+ return out
+
+
+async def _check_single_device(
+ device_id: str,
+ node_ids: list[str],
check_method: str,
check_target: str | None,
ip: str | None,
) -> tuple[str, dict[str, object] | None]:
- """Run a single node check; returns (node_id, result_or_None).
+ """Run a single device check; returns (device_id, result_or_None).
Accepts plain scalars — not an ORM object — so there is no risk of
DetachedInstanceError when the originating session has already closed.
@@ -37,45 +48,60 @@ async def _check_single_node(
try:
check_result = await check_node(check_method, check_target, ip)
+ raw_ms = check_result["response_time_ms"]
+ response_ms = raw_ms if isinstance(raw_ms, int) else None
now = datetime.now(timezone.utc)
async with AsyncSessionLocal() as db:
- n = await db.get(Node, node_id)
- if n:
- n.status = check_result["status"]
- n.response_time_ms = check_result["response_time_ms"]
+ device = await db.get(InventoryDevice, device_id)
+ if device:
+ device.status_live = str(check_result["status"])
+ device.response_time_ms = response_ms
if check_result["status"] == "online":
- n.last_seen = now
+ device.last_seen = now
await db.commit()
await broadcast_status(
- node_id=node_id,
- status=check_result["status"],
+ device_id=device_id,
+ node_ids=node_ids,
+ status=str(check_result["status"]),
checked_at=now.isoformat(),
- response_time_ms=check_result["response_time_ms"],
+ response_time_ms=response_ms,
)
- return node_id, check_result
+ return device_id, check_result
except Exception as exc:
- logger.error("Status check failed for node %s: %s", node_id, exc)
- return node_id, None
+ logger.error("Status check failed for device %s: %s", device_id, exc)
+ return device_id, None
async def _run_status_checks() -> None:
- """Check all nodes concurrently and broadcast results via WebSocket."""
+ """Check every device once and broadcast the result to every canvas.
+
+ Device-scoped, not node-scoped: a host drawn on three canvases used to be
+ pinged three times and could report three different states. Hidden devices
+ are skipped — hiding one is how a user says "stop showing me this".
+ """
async with AsyncSessionLocal() as db:
- result = await db.execute(select(Node))
- nodes = result.scalars().all()
+ devices = (
+ await db.execute(
+ select(InventoryDevice).where(
+ InventoryDevice.check_method.is_not(None),
+ InventoryDevice.check_method != "",
+ InventoryDevice.status != "hidden",
+ )
+ )
+ ).scalars().all()
+ node_map = await _nodes_by_device(db)
# Extract scalars while the session is open to avoid DetachedInstanceError
checkable = [
- (n.id, n.check_method, n.check_target, n.ip)
- for n in nodes
- if n.check_method
+ (d.id, node_map.get(d.id, []), d.check_method or "ping", d.check_target, d.ip)
+ for d in devices
]
if not checkable:
return
await asyncio.gather(*[
- _check_single_node(node_id, method, target, ip)
- for node_id, method, target, ip in checkable
+ _check_single_device(device_id, node_ids, method, target, ip)
+ for device_id, node_ids, method, target, ip in checkable
])
@@ -89,27 +115,35 @@ def _node_host(ip: str | None, hostname: str | None) -> str | None:
async def _run_service_checks() -> None:
- """Check every service of every node and broadcast per-service results."""
+ """Check every service of every device and broadcast per-service results.
+
+ Device-scoped for the same reason as the status check: the services belong
+ to the device, so one pass serves every canvas showing it.
+ """
if not settings.service_check_enabled:
return
from app.api.routes.status import broadcast_service_status # avoid circular import
async with AsyncSessionLocal() as db:
- result = await db.execute(select(Node))
- nodes = result.scalars().all()
+ devices = (
+ await db.execute(select(InventoryDevice).where(InventoryDevice.status != "hidden"))
+ ).scalars().all()
+ node_map = await _nodes_by_device(db)
checkable = [
- (n.id, _node_host(n.ip, n.hostname), list(n.services or []))
- for n in nodes
- if n.services
+ (d.id, node_map.get(d.id, []), _node_host(d.ip, d.hostname), list(d.services or []))
+ for d in devices
+ if d.services
]
now = datetime.now(timezone.utc).isoformat()
- for node_id, host, services in checkable:
+ for device_id, node_ids, host, services in checkable:
try:
statuses = await check_services(host, services)
- await broadcast_service_status(node_id=node_id, services=statuses, checked_at=now)
+ await broadcast_service_status(
+ device_id=device_id, node_ids=node_ids, services=statuses, checked_at=now
+ )
except Exception as exc:
- logger.error("Service checks failed for node %s: %s", node_id, exc)
+ logger.error("Service checks failed for device %s: %s", device_id, exc)
async def _run_proxmox_sync() -> None:
diff --git a/backend/app/db/database.py b/backend/app/db/database.py
index b220f32..d5fc88b 100644
--- a/backend/app/db/database.py
+++ b/backend/app/db/database.py
@@ -486,6 +486,94 @@ async def init_db() -> None:
await _try_migrate(conn, sql, label=label)
await _backfill_node_devices()
+ await _drop_legacy_node_columns()
+
+
+
+# Columns `nodes` carried before 3.3.0, when a node owned the device facts. They
+# belong to `device_inventory` now; the backfill above copies them across, and
+# this rebuild removes them.
+_LEGACY_NODE_COLUMNS = (
+ "hostname", "ip", "mac", "os", "status", "check_method", "check_target",
+ "services", "notes", "cpu_count", "cpu_model", "ram_gb", "disk_gb",
+ "show_hardware", "properties", "ieee_address", "last_seen", "last_scan",
+ "response_time_ms",
+)
+
+# What a node keeps: how the device is drawn on one canvas.
+_NODE_COLUMNS_SQL = (
+ "id VARCHAR PRIMARY KEY,"
+ "type VARCHAR NOT NULL,"
+ "label VARCHAR NOT NULL,"
+ "design_id VARCHAR REFERENCES designs(id) ON DELETE SET NULL,"
+ "device_id VARCHAR REFERENCES device_inventory(id) ON DELETE SET NULL,"
+ "pos_x FLOAT,"
+ "pos_y FLOAT,"
+ "parent_id VARCHAR REFERENCES nodes(id) ON DELETE CASCADE,"
+ "container_mode BOOLEAN,"
+ "custom_colors JSON,"
+ "custom_icon VARCHAR,"
+ "show_port_numbers BOOLEAN,"
+ "width FLOAT,"
+ "height FLOAT,"
+ "bottom_handles INTEGER,"
+ "top_handles INTEGER,"
+ "left_handles INTEGER,"
+ "right_handles INTEGER,"
+ "created_at DATETIME,"
+ "updated_at DATETIME"
+)
+
+_NODE_KEPT = (
+ "id, type, label, design_id, device_id, pos_x, pos_y, parent_id, container_mode, "
+ "custom_colors, custom_icon, show_port_numbers, width, height, bottom_handles, "
+ "top_handles, left_handles, right_handles, created_at, updated_at"
+)
+
+
+async def _drop_legacy_node_columns() -> None:
+ """Remove the device columns from `nodes` (3.3.0) — SQLite table rebuild.
+
+ Runs only after the backfill has linked every device node to its inventory
+ row. If any non-furniture node is still unlinked the drop is skipped and
+ logged: the columns are the only remaining copy of that node's facts, and
+ losing them is not recoverable.
+ """
+ async with engine.begin() as conn:
+ info = (await conn.exec_driver_sql("PRAGMA table_info(nodes)")).fetchall()
+ present = {row[1] for row in info}
+ if not (present & set(_LEGACY_NODE_COLUMNS)):
+ return # Already migrated.
+
+ unlinked = (
+ await conn.exec_driver_sql(
+ "SELECT COUNT(*) FROM nodes WHERE device_id IS NULL "
+ "AND type NOT IN ('group', 'groupRect', 'text')"
+ )
+ ).scalar()
+ if unlinked:
+ logger.warning(
+ "Keeping the legacy node columns: %d node(s) have no inventory row. "
+ "The backfill must link every device node before they can be dropped.",
+ unlinked,
+ )
+ return
+
+ logger.info("Migrating nodes: the device columns move to device_inventory")
+ try:
+ await conn.exec_driver_sql("PRAGMA foreign_keys = OFF")
+ await conn.exec_driver_sql(f"CREATE TABLE nodes_new ({_NODE_COLUMNS_SQL})")
+ await conn.exec_driver_sql(
+ f"INSERT INTO nodes_new ({_NODE_KEPT}) SELECT {_NODE_KEPT} FROM nodes"
+ )
+ await conn.exec_driver_sql("DROP TABLE nodes")
+ await conn.exec_driver_sql("ALTER TABLE nodes_new RENAME TO nodes")
+ await conn.exec_driver_sql(
+ "CREATE INDEX IF NOT EXISTS ix_nodes_device_id ON nodes(device_id)"
+ )
+ await conn.exec_driver_sql("PRAGMA foreign_keys = ON")
+ except OperationalError as exc:
+ logger.warning("nodes device-column drop failed: %s", exc)
async def _backfill_node_devices() -> None:
diff --git a/backend/app/db/models.py b/backend/app/db/models.py
index cc59447..34b80fe 100644
--- a/backend/app/db/models.py
+++ b/backend/app/db/models.py
@@ -28,6 +28,15 @@ class Design(Base):
class Node(Base):
+ """How a device is drawn on one canvas.
+
+ A node owns presentation only — position, size, colours, icon, handles,
+ nesting. What the device *is* (addresses, services, properties, notes,
+ hardware, check method, live status) belongs to the `device_inventory` row
+ named by `device_id`, so one device reads the same on every canvas. The API
+ still reports both together: see `services/inventory_sync.hydrated_node`.
+ """
+
__tablename__ = "nodes"
id: Mapped[str] = mapped_column(String, primary_key=True, default=_uuid)
@@ -40,38 +49,19 @@ class Node(Base):
device_id: Mapped[str | None] = mapped_column(
String, ForeignKey("device_inventory.id", ondelete="SET NULL"), index=True, nullable=True
)
- hostname: Mapped[str | None] = mapped_column(String)
- ip: Mapped[str | None] = mapped_column(String)
- mac: Mapped[str | None] = mapped_column(String)
- os: Mapped[str | None] = mapped_column(String)
- status: Mapped[str] = mapped_column(String, default="unknown")
- check_method: Mapped[str | None] = mapped_column(String)
- check_target: Mapped[str | None] = mapped_column(String)
- services: Mapped[list[Any]] = mapped_column(JSON, default=list)
- notes: Mapped[str | None] = mapped_column(Text)
pos_x: Mapped[float] = mapped_column(Float, default=0)
pos_y: Mapped[float] = mapped_column(Float, default=0)
parent_id: Mapped[str | None] = mapped_column(String, ForeignKey("nodes.id", ondelete="CASCADE"))
container_mode: Mapped[bool] = mapped_column(Boolean, default=False)
custom_colors: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
custom_icon: Mapped[str | None] = mapped_column(String, nullable=True)
- cpu_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
- cpu_model: Mapped[str | None] = mapped_column(String, nullable=True)
- ram_gb: Mapped[float | None] = mapped_column(Float, nullable=True)
- disk_gb: Mapped[float | None] = mapped_column(Float, nullable=True)
- show_hardware: Mapped[bool] = mapped_column(Boolean, default=False)
show_port_numbers: Mapped[bool] = mapped_column(Boolean, default=False)
- properties: Mapped[list[Any]] = mapped_column(JSON, default=list)
width: Mapped[float | None] = mapped_column(Float, nullable=True)
height: Mapped[float | None] = mapped_column(Float, nullable=True)
bottom_handles: Mapped[int] = mapped_column(Integer, default=1)
top_handles: Mapped[int] = mapped_column(Integer, default=1)
left_handles: Mapped[int] = mapped_column(Integer, default=0)
right_handles: Mapped[int] = mapped_column(Integer, default=0)
- ieee_address: Mapped[str | None] = mapped_column(String, index=True, nullable=True)
- last_seen: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
- last_scan: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
- response_time_ms: Mapped[int | None] = mapped_column(Integer)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, onupdate=_now)
children: Mapped[list["Node"]] = relationship("Node", back_populates="parent")
diff --git a/backend/app/services/inventory_sync.py b/backend/app/services/inventory_sync.py
index 35259f7..67875a2 100644
--- a/backend/app/services/inventory_sync.py
+++ b/backend/app/services/inventory_sync.py
@@ -13,10 +13,12 @@ Nothing here commits — the caller owns the transaction.
from __future__ import annotations
+import json
import logging
+from collections.abc import Mapping
from typing import Any
-from sqlalchemy import or_, select
+from sqlalchemy import or_, select, text
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.models import InventoryDevice, Node
@@ -170,19 +172,23 @@ def merge_services(base: list[Any] | None, incoming: list[Any] | None) -> list[A
return out
-def merge_node_into_device(
+def merge_facts_into_device(
device: InventoryDevice,
- node: Node,
+ facts: Mapping[str, Any],
*,
overwrite_scalars: bool,
replace_lists: bool,
) -> None:
- """Fold a node's device facts into its inventory row, in place.
+ """Fold one view of a device into its inventory row, in place.
+
+ ``facts`` is a plain mapping — what a canvas save sent, or what a legacy
+ node's columns held — so this rule lives in one place regardless of where
+ the view came from.
- Two independent knobs, because the three callers need three combinations:
+ Two independent knobs, because the callers need three combinations:
- * ``overwrite_scalars`` — a non-blank node value replaces the row's. True
- for the backfill (nodes are visited oldest-edit-first, so the most
+ * ``overwrite_scalars`` — a non-blank incoming value replaces the row's.
+ True for the backfill (nodes are visited oldest-edit-first, so the most
recently edited canvas is the last writer and wins) and for a user's save.
False on approve, where the row was just discovered and the node is only a
placement. A blank *never* clears an established value in either mode.
@@ -191,91 +197,89 @@ def merge_node_into_device(
would come straight back on the next one. The backfill unions, so nothing
any canvas recorded is lost.
"""
- for field in DEVICE_SCALARS:
- incoming = getattr(node, field, None)
+ for field in (*DEVICE_SCALARS, "label", "type"):
+ incoming = facts.get(field)
if _blank(incoming):
continue
if overwrite_scalars or _blank(getattr(device, field, None)):
setattr(device, field, incoming)
- if not _blank(node.label) and (overwrite_scalars or _blank(device.label)):
- device.label = node.label
- if not _blank(node.type) and (overwrite_scalars or _blank(device.type)):
- device.type = node.type
- if not _blank(node.ieee_address) and _blank(device.ieee_address):
+ if not _blank(facts.get("ieee_address")) and _blank(device.ieee_address):
# Identity, never overwritten — two IEEEs mean two devices.
- device.ieee_address = node.ieee_address
- if node.show_hardware and not device.show_hardware:
+ device.ieee_address = facts["ieee_address"]
+ if facts.get("show_hardware") and not device.show_hardware:
device.show_hardware = True
if replace_lists:
- device.properties = list(node.properties or [])
- device.services = list(node.services or [])
+ device.properties = list(facts.get("properties") or [])
+ device.services = list(facts.get("services") or [])
else:
- device.properties = merge_properties(device.properties, node.properties)
- device.services = merge_services(device.services, node.services)
+ device.properties = merge_properties(device.properties, facts.get("properties"))
+ device.services = merge_services(device.services, facts.get("services"))
# Live status: keep the freshest observation rather than the last writer.
- if node.last_seen and (device.last_seen is None or node.last_seen > device.last_seen):
- device.last_seen = node.last_seen
- device.status_live = node.status or device.status_live
- device.response_time_ms = node.response_time_ms
- elif device.status_live in (None, "", "unknown") and node.status:
- device.status_live = node.status
- if node.last_scan and (device.last_scan is None or node.last_scan > device.last_scan):
- device.last_scan = node.last_scan
+ last_seen, status, last_scan = facts.get("last_seen"), facts.get("status"), facts.get("last_scan")
+ if last_seen and (device.last_seen is None or last_seen > device.last_seen):
+ device.last_seen = last_seen
+ device.status_live = status or device.status_live
+ device.response_time_ms = facts.get("response_time_ms")
+ elif device.status_live in (None, "", "unknown") and status:
+ device.status_live = status
+ if last_scan and (device.last_scan is None or last_scan > device.last_scan):
+ device.last_scan = last_scan
-def device_from_node(node: Node) -> InventoryDevice:
- """Mint the inventory row for a node that has none.
+def device_from_facts(facts: Mapping[str, Any]) -> InventoryDevice:
+ """Mint the inventory row for a device that has none.
Tagged ``canvas`` so the inventory filters can tell hand-drawn gear apart
from anything a scan or import found.
"""
- device = InventoryDevice(
- label=node.label,
- type=node.type,
- hostname=node.hostname,
- ip=node.ip,
- mac=node.mac,
- os=node.os,
- ieee_address=node.ieee_address,
- services=list(node.services or []),
- properties=list(node.properties or []),
- notes=node.notes,
- cpu_count=node.cpu_count,
- cpu_model=node.cpu_model,
- ram_gb=node.ram_gb,
- disk_gb=node.disk_gb,
- show_hardware=bool(node.show_hardware),
- check_method=node.check_method,
- check_target=node.check_target,
- suggested_type=node.type,
- friendly_name=node.label,
+ return InventoryDevice(
+ label=facts.get("label"),
+ type=facts.get("type"),
+ hostname=facts.get("hostname"),
+ ip=facts.get("ip"),
+ mac=facts.get("mac"),
+ os=facts.get("os"),
+ ieee_address=facts.get("ieee_address"),
+ services=list(facts.get("services") or []),
+ properties=list(facts.get("properties") or []),
+ notes=facts.get("notes"),
+ cpu_count=facts.get("cpu_count"),
+ cpu_model=facts.get("cpu_model"),
+ ram_gb=facts.get("ram_gb"),
+ disk_gb=facts.get("disk_gb"),
+ show_hardware=bool(facts.get("show_hardware")),
+ check_method=facts.get("check_method"),
+ check_target=facts.get("check_target"),
+ suggested_type=facts.get("type"),
+ friendly_name=facts.get("label"),
# On a canvas already, so it is past the pending queue.
status="approved",
- status_live=node.status or "unknown",
- last_seen=node.last_seen,
- last_scan=node.last_scan,
- response_time_ms=node.response_time_ms,
+ status_live=facts.get("status") or "unknown",
+ last_seen=facts.get("last_seen"),
+ last_scan=facts.get("last_scan"),
+ response_time_ms=facts.get("response_time_ms"),
discovery_source=CANVAS_SOURCE,
discovery_sources=[CANVAS_SOURCE],
)
- return device
-async def link_node(
+async def link_facts(
db: AsyncSession,
node: Node,
+ facts: Mapping[str, Any],
*,
overwrite_scalars: bool = False,
replace_lists: bool = False,
) -> InventoryDevice | None:
"""Point one node at its inventory row, creating or merging as needed.
- Returns the row, or ``None`` for canvas furniture. The two flags are handed
- to :func:`merge_node_into_device`; see it for when each applies. Flushes so
- a freshly minted row has an id to link to, but does not commit.
+ ``facts`` is this node's view of the device — the fields a canvas save sent,
+ or a legacy node's columns during the backfill. Returns the row, or ``None``
+ for canvas furniture. Flushes so a freshly minted row has an id to link to,
+ but does not commit.
"""
if is_furniture(node.type):
node.device_id = None
@@ -285,15 +289,17 @@ async def link_node(
if node.device_id:
device = await db.get(InventoryDevice, node.device_id)
if device is None:
- device = await find_device_for(db, ip=node.ip, mac=node.mac, ieee=node.ieee_address)
+ device = await find_device_for(
+ db, ip=facts.get("ip"), mac=facts.get("mac"), ieee=facts.get("ieee_address")
+ )
if device is None:
- device = device_from_node(node)
+ device = device_from_facts(facts)
db.add(device)
await db.flush()
else:
- merge_node_into_device(
- device, node, overwrite_scalars=overwrite_scalars, replace_lists=replace_lists
+ merge_facts_into_device(
+ device, facts, overwrite_scalars=overwrite_scalars, replace_lists=replace_lists
)
device.discovery_sources = add_source(device.discovery_sources, CANVAS_SOURCE)
@@ -301,6 +307,50 @@ async def link_node(
return device
+def node_columns(payload: Mapping[str, Any]) -> dict[str, Any]:
+ """The subset of a node payload that is still a `nodes` column.
+
+ The wire shape carries the device facts flat on the node; they belong to the
+ inventory row now, so they are dropped here and applied through
+ :func:`link_facts` instead.
+ """
+ allowed = {c.name for c in Node.__table__.columns}
+ return {k: v for k, v in payload.items() if k in allowed}
+
+
+# Fields that are both a node column and a device fact: the node keeps a copy so
+# a half-migrated database still renders, but the row is the truth.
+_SHARED_FIELDS = ("label", "type")
+
+# Everything the inventory row owns, as it appears in a node payload.
+DEVICE_FACT_FIELDS = (
+ *DEVICE_SCALARS, "services", "properties", "show_hardware", "status", *_SHARED_FIELDS,
+)
+
+
+def facts_from_update(sent: Mapping[str, Any]) -> dict[str, Any]:
+ """The device facts inside a partial node update — only what was sent.
+
+ An omitted field must not clear the row, so unsent keys are simply absent.
+ """
+ return {k: v for k, v in sent.items() if k in DEVICE_FACT_FIELDS}
+
+
+def facts_from_payload(payload: Mapping[str, Any], *, label: str, node_type: str) -> dict[str, Any]:
+ """The device facts inside a node save/create payload.
+
+ The wire shape still carries them flat on the node, so this is where they
+ are separated from the presentation fields the node itself keeps.
+ """
+ facts: dict[str, Any] = {
+ field: payload.get(field)
+ for field in (*DEVICE_SCALARS, "services", "properties", "show_hardware", "status")
+ }
+ facts["label"] = label
+ facts["type"] = node_type
+ return facts
+
+
async def load_devices_for(db: AsyncSession, nodes: list[Node]) -> dict[str, InventoryDevice]:
"""Fetch the inventory rows a batch of nodes points at, keyed by device id."""
ids = {n.device_id for n in nodes if n.device_id}
@@ -317,8 +367,7 @@ def hydrated_node(node: Node, device: InventoryDevice | None) -> dict[str, Any]:
The device fields stay on the wire exactly where they have always been, so
every reader — the canvas, the live view, the MCP server — is unaffected by
- the split. Falls back to the node's own columns when it has no row (canvas
- furniture, or a device row deleted out from under it).
+ the split. Canvas furniture has no row and simply reports the defaults.
"""
payload: dict[str, Any] = {
c.name: getattr(node, c.name) for c in node.__table__.columns
@@ -333,59 +382,104 @@ def hydrated_node(node: Node, device: InventoryDevice | None) -> dict[str, Any]:
payload["services"] = device.services or []
payload["properties"] = device.properties or []
payload["show_hardware"] = bool(device.show_hardware)
- payload["ieee_address"] = device.ieee_address or node.ieee_address
- # Live status still reaches the node while the status checker is node-scoped
- # (it moves to the row in the next step), so prefer whichever side has an
- # actual observation rather than blanking the canvas in between.
- if device.status_live and device.status_live != "unknown":
- payload["status"] = device.status_live
- payload["last_seen"] = device.last_seen or node.last_seen
- payload["last_scan"] = device.last_scan or node.last_scan
- payload["response_time_ms"] = (
- device.response_time_ms if device.response_time_ms is not None else node.response_time_ms
- )
+ payload["ieee_address"] = device.ieee_address
+ payload["status"] = device.status_live or "unknown"
+ payload["last_seen"] = device.last_seen
+ payload["last_scan"] = device.last_scan
+ payload["response_time_ms"] = device.response_time_ms
return payload
+# The device columns `nodes` carried before 3.3.0. They are read once, by the
+# backfill, and then dropped — so they are named here as raw SQL rather than as
+# model attributes that no longer exist.
+_LEGACY_NODE_COLUMNS = (
+ "hostname", "ip", "mac", "os", "status", "check_method", "check_target",
+ "services", "notes", "cpu_count", "cpu_model", "ram_gb", "disk_gb",
+ "show_hardware", "properties", "ieee_address", "last_seen", "last_scan",
+ "response_time_ms",
+)
+
+
+async def _legacy_columns_present(db: AsyncSession) -> list[str]:
+ """Which pre-3.3.0 device columns still exist on `nodes`."""
+ rows = (await db.execute(text("PRAGMA table_info(nodes)"))).all()
+ present = {r[1] for r in rows}
+ return [c for c in _LEGACY_NODE_COLUMNS if c in present]
+
+
+def _decode_json(value: Any) -> Any:
+ """Raw SQL hands back JSON columns as text; the ORM would have decoded them."""
+ if isinstance(value, str):
+ try:
+ return json.loads(value)
+ except json.JSONDecodeError:
+ return []
+ return value
+
+
async def backfill_node_devices(db: AsyncSession) -> dict[str, int]:
"""Link every pre-3.3.0 canvas node to a Device Inventory row.
- Non-destructive: it writes ``nodes.device_id`` and fills blank inventory
- fields, and never deletes a node or a row. Two nodes on two canvases
- describing the same host converge on one row — that convergence is the point.
+ Non-destructive: it writes ``nodes.device_id`` and fills the inventory row,
+ and never deletes a node or a row. Two nodes on two canvases describing the
+ same host converge on one row — that convergence is the point.
Nodes are processed oldest-edit-first so that, where two canvases disagree
- on a scalar, the most recently edited node is the last writer and wins.
- Returns counts for the boot log. Does not commit.
+ on a scalar, the most recently edited node is the last writer and wins;
+ properties and services are unioned, so nothing any canvas recorded is lost.
+
+ Reads the legacy columns with raw SQL because the model no longer declares
+ them — this runs on the boot that drops them, once. Returns counts for the
+ boot log. Does not commit.
"""
- nodes = (
+ legacy = await _legacy_columns_present(db)
+ if not legacy:
+ # Already migrated: the columns are gone, so nothing can be left to read.
+ return {"linked": 0, "created": 0, "merged": 0}
+
+ placeholders = ", ".join(legacy)
+ furniture = ", ".join(f"'{t}'" for t in sorted(FURNITURE_TYPES))
+ rows = (
await db.execute(
- select(Node)
- .where(Node.device_id.is_(None), Node.type.not_in(tuple(FURNITURE_TYPES)))
- .order_by(Node.updated_at, Node.created_at, Node.id)
+ text(
+ f"SELECT id, label, type, design_id, {placeholders} FROM nodes "
+ f"WHERE device_id IS NULL AND type NOT IN ({furniture}) "
+ "ORDER BY updated_at, created_at, id"
+ )
)
- ).scalars().all()
- if not nodes:
+ ).mappings().all()
+ if not rows:
return {"linked": 0, "created": 0, "merged": 0}
created = merged = 0
- for node in nodes:
- existing = await find_device_for(db, ip=node.ip, mac=node.mac, ieee=node.ieee_address)
- # Nodes arrive oldest-edit-first, so overwriting scalars leaves the most
- # recently edited canvas as the last writer. Lists stay unioned — nothing
- # any canvas recorded is dropped.
- device = await link_node(db, node, overwrite_scalars=True)
+ for row in rows:
+ facts: dict[str, Any] = {c: row[c] for c in legacy}
+ facts["services"] = _decode_json(facts.get("services")) or []
+ facts["properties"] = _decode_json(facts.get("properties")) or []
+ facts["label"] = row["label"]
+ facts["type"] = row["type"]
+
+ existing = await find_device_for(
+ db, ip=facts.get("ip"), mac=facts.get("mac"), ieee=facts.get("ieee_address")
+ )
+ node = await db.get(Node, row["id"])
+ if node is None: # pragma: no cover - the row was just read
+ continue
+ device = await link_facts(db, node, facts, overwrite_scalars=True)
if device is None:
continue
if existing is None:
created += 1
- logger.info("Inventory backfill: node %s (%s) created device %s", node.id, node.label, device.id)
+ logger.info(
+ "Inventory backfill: node %s (%s) created device %s", node.id, row["label"], device.id
+ )
else:
merged += 1
logger.info(
"Inventory backfill: node %s (%s, design %s) merged into device %s",
- node.id, node.label, node.design_id, device.id,
+ node.id, row["label"], row["design_id"], device.id,
)
await db.flush()
- return {"linked": len(nodes), "created": created, "merged": merged}
+ return {"linked": len(rows), "created": created, "merged": merged}
diff --git a/backend/app/services/node_dedupe.py b/backend/app/services/node_dedupe.py
index b27fa2e..25c928b 100644
--- a/backend/app/services/node_dedupe.py
+++ b/backend/app/services/node_dedupe.py
@@ -1,43 +1,29 @@
-"""Collapse *true* duplicate canvas nodes: same ``ieee_address`` **and** same
-``design_id`` (i.e. the same device placed twice on the *same* canvas).
-
-The same device legitimately appears on multiple canvases — placing a device on
-another design creates a second :class:`Node` for that IEEE by design (see the
-per-design guard in ``bulk_approve_devices``). Those cross-design rows are NOT
-duplicates and must be preserved. Only two rows sharing both the IEEE *and* the
-design are corrupt, and only those are collapsed here.
-
-This module provides an idempotent, **loss-free** repair: per ``(ieee,
-design_id)`` group with >1 node it keeps the oldest as canonical, merges the
-extras' data into it (properties, missing scalar fields, services), re-points
-every edge and ``parent_id`` reference onto the canonical node, then deletes the
-extras. Edges are de-duplicated and self-loops dropped after re-pointing so no
-dangling or redundant links remain.
-"""
+"""Duplicate canvas nodes: the same device drawn twice on the *same* canvas.
+
+A device legitimately appears on several canvases — one :class:`Node` per
+design, all pointing at one ``device_inventory`` row. Two nodes drawing that row
+on the *same* design are the corrupt case, and the only one collapsed here.
+Identity is the device link, not the addresses: a node names the row it draws,
+so "same device" is an id comparison rather than a guess across ieee/ip/mac.
+The repair is idempotent — the oldest node stays, edges and ``parent_id``
+references are re-pointed onto it, then the extras go. No device data is lost in
+the process because none of it lives on the node any more.
+"""
from __future__ import annotations
import logging
from typing import Any
-from sqlalchemy import or_, select
+from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.models import Edge, Node
-from app.services.zigbee_service import merge_zigbee_properties
+from app.services.inventory_sync import find_device_for
logger = logging.getLogger(__name__)
-def _ip_tokens(ip: str | None) -> list[str]:
- """Split a Node ``ip`` field into individual, whitespace-trimmed addresses.
-
- The canvas stores several addresses in one comma-separated string once a
- user adds e.g. an IPv6 address, so identity matching must compare per token.
- """
- return [t.strip() for t in ip.split(",") if t.strip()] if ip else []
-
-
async def find_duplicate_node(
db: AsyncSession,
design_id: str | None,
@@ -45,153 +31,84 @@ async def find_duplicate_node(
mac: str | None,
ieee: str | None = None,
) -> dict[str, Any] | None:
- """Return conflict details if an equivalent node (same ieee, ip OR mac)
- already sits on ``design_id``, else ``None``.
-
- Scoped to a single design on purpose: the same device may legitimately
- appear on several canvases (one :class:`Node` per design). Only a second
- node for the same ieee/ip/mac on the *same* design is a duplicate — which
- the create/approve endpoints turn into a 409 so the UI can offer "go to
- existing" vs "add duplicate anyway", uniformly for IEEE (Zigbee/Z-Wave) and
- plain IP/ARP hosts.
-
- (:func:`dedupe_nodes_by_ieee` still repairs *pre-existing* same-canvas IEEE
- duplicates; this guard prevents new ones unless the user forces them.)
+ """Return conflict details if this device is already drawn on ``design_id``.
+
+ Identity is resolved once, by the inventory row the addresses point at
+ (``find_device_for``: ieee > ip > mac), and a duplicate is simply a second
+ node on the same design drawing that row. Scoped to one design on purpose:
+ the same device may legitimately appear on several canvases.
+
+ The create/approve endpoints turn this into a 409 so the UI can offer "go to
+ existing" vs "add duplicate anyway".
"""
- ip_toks = _ip_tokens(ip)
- conds = []
- if ieee:
- conds.append(Node.ieee_address == ieee)
- # A node's ip may hold several comma-separated addresses (e.g. an IPv6 added
- # before the IPv4), so narrow with a substring match then confirm per-token
- # in Python — exact ``Node.ip == ip`` would miss those rows (issue #258).
- for tok in ip_toks:
- conds.append(Node.ip.contains(tok))
- if mac:
- conds.append(Node.mac == mac)
- if not conds:
+ device = await find_device_for(db, ip=ip, mac=mac, ieee=ieee)
+ if device is None:
return None
- candidates = (
+ existing = (
await db.execute(
- select(Node).where(Node.design_id == design_id, or_(*conds))
+ select(Node)
+ .where(Node.design_id == design_id, Node.device_id == device.id)
+ .order_by(Node.created_at, Node.id)
)
- ).scalars().all()
-
- # Confirm a real match (the ip ``contains`` above can false-positive, e.g.
- # "1.2.3.4" inside "1.2.3.40"), preferring ieee > ip > mac.
- def _matches(node: Node) -> tuple[str, str | None] | None:
- if ieee and node.ieee_address == ieee:
- return "ieee", node.ieee_address
- node_toks = set(_ip_tokens(node.ip))
- hit = next((t for t in ip_toks if t in node_toks), None)
- if hit is not None:
- return "ip", hit
- if mac and node.mac == mac:
- return "mac", mac
+ ).scalars().first()
+ if existing is None:
return None
- existing = None
- matched: tuple[str, str | None] | None = None
- for node in candidates:
- m = _matches(node)
- if m is not None:
- existing, matched = node, m
- break
- if existing is None or matched is None:
- return None
- match, value = matched
+ # Report which address identified the device, as the UI prints it, in the
+ # same precedence find_device_for used: ieee > ip > mac.
+ match: str
+ value: str | None
+ device_ips = {t.strip() for t in (device.ip or "").split(",") if t.strip()}
+ shared_ip = next((t.strip() for t in (ip or "").split(",") if t.strip() in device_ips), None)
+ if ieee and device.ieee_address == ieee:
+ match, value = "ieee", ieee
+ elif shared_ip:
+ match, value = "ip", shared_ip
+ elif mac and device.mac == mac:
+ match, value = "mac", mac
+ else:
+ match, value = "ip", ip
return {
"duplicate": True,
"existing_node_id": existing.id,
- "existing_label": existing.label,
+ "existing_label": device.label or existing.label,
"match": match,
"value": value,
}
-# Scalar Node fields worth carrying over from a duplicate when the canonical
-# node has no value. Positions, design, parent and identity fields are left on
-# the canonical node untouched (that's the row the user actually placed).
-_FILLABLE_FIELDS = (
- "hostname",
- "ip",
- "mac",
- "os",
- "check_method",
- "check_target",
- "notes",
- "cpu_count",
- "cpu_model",
- "ram_gb",
- "disk_gb",
- "custom_icon",
- "last_seen",
- "last_scan",
-)
-
-
-def _merge_services(
- a: list[Any] | None, b: list[Any] | None
-) -> list[Any]:
- """Union two service lists, de-duplicated, order-stable."""
- out: list[Any] = list(a or [])
- seen = {repr(s) for s in out}
- for s in b or []:
- if repr(s) not in seen:
- out.append(s)
- seen.add(repr(s))
- return out
-
-
-def _merge_into_canonical(canonical: Node, dup: Node) -> None:
- """Fold ``dup``'s data into ``canonical`` in place (no field lost)."""
- canonical.properties = merge_zigbee_properties(
- canonical.properties, dup.properties or []
- )
- canonical.services = _merge_services(canonical.services, dup.services)
- for field in _FILLABLE_FIELDS:
- if getattr(canonical, field, None) in (None, "") and getattr(dup, field, None) not in (None, ""):
- setattr(canonical, field, getattr(dup, field))
- # Prefer a human label over a bare IEEE/hex fallback.
- if (not canonical.label or canonical.label == canonical.ieee_address) and dup.label:
- canonical.label = dup.label
-
-
-async def dedupe_nodes_by_ieee(db: AsyncSession) -> int:
- """Merge duplicate nodes sharing an ``ieee_address`` AND ``design_id``.
-
- Returns the number of nodes removed. Idempotent: a no-op when every
- ``(ieee, design)`` pair maps to at most one node. Nodes with the same IEEE
- on *different* designs are left untouched (valid cross-canvas placement).
+
+async def dedupe_nodes_by_device(db: AsyncSession) -> int:
+ """Merge duplicate nodes drawing the same device on the same canvas.
+
+ Returns the number of nodes removed. Idempotent. Nodes drawing one device on
+ *different* designs are left alone — that is valid cross-canvas placement.
+ The device facts live on the inventory row, so nothing has to be merged out
+ of the extras: only edges and parent links are re-pointed before they go.
Does not commit — the caller owns the transaction.
"""
rows = (
await db.execute(
select(Node)
- .where(Node.ieee_address.is_not(None))
- .order_by(Node.ieee_address, Node.created_at, Node.id)
+ .where(Node.device_id.is_not(None))
+ .order_by(Node.device_id, Node.created_at, Node.id)
)
).scalars().all()
groups: dict[tuple[str, str | None], list[Node]] = {}
for node in rows:
- groups.setdefault((node.ieee_address, node.design_id), []).append(node) # type: ignore[arg-type]
+ groups.setdefault((node.device_id, node.design_id), []).append(node) # type: ignore[arg-type]
removed = 0
- for (ieee, _design), nodes in groups.items():
+ for (device_id, _design), nodes in groups.items():
if len(nodes) < 2:
continue
canonical, *dups = nodes # oldest first (ordered above)
dup_ids = {d.id for d in dups}
- for dup in dups:
- _merge_into_canonical(canonical, dup)
-
# Re-point edges + parents, then drop self-loops / duplicates.
edges = (
await db.execute(
- select(Edge).where(
- Edge.source.in_(dup_ids) | Edge.target.in_(dup_ids)
- )
+ select(Edge).where(Edge.source.in_(dup_ids) | Edge.target.in_(dup_ids))
)
).scalars().all()
for edge in edges:
@@ -200,19 +117,15 @@ async def dedupe_nodes_by_ieee(db: AsyncSession) -> int:
if edge.target in dup_ids:
edge.target = canonical.id
- # Re-point children whose parent was a duplicate.
children = (
await db.execute(select(Node).where(Node.parent_id.in_(dup_ids)))
).scalars().all()
for child in children:
child.parent_id = canonical.id
- # Collapse self-loops and now-redundant parallel edges.
all_edges = (
await db.execute(
- select(Edge).where(
- (Edge.source == canonical.id) | (Edge.target == canonical.id)
- )
+ select(Edge).where((Edge.source == canonical.id) | (Edge.target == canonical.id))
)
).scalars().all()
seen_pairs: set[tuple[str, str, str]] = set()
@@ -232,8 +145,8 @@ async def dedupe_nodes_by_ieee(db: AsyncSession) -> int:
removed += 1
logger.info(
- "Deduped IEEE %s: merged %d duplicate node(s) into %s",
- ieee, len(dups), canonical.id,
+ "Deduped device %s: merged %d duplicate node(s) into %s",
+ device_id, len(dups), canonical.id,
)
if removed:
diff --git a/backend/app/services/scanner.py b/backend/app/services/scanner.py
index dc6bd95..5e7bd54 100644
--- a/backend/app/services/scanner.py
+++ b/backend/app/services/scanner.py
@@ -15,7 +15,7 @@ from sqlalchemy import or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.config import settings
-from app.db.models import InventoryDevice, Node, ScanRun
+from app.db.models import InventoryDevice, ScanRun
from app.services.discovery_sources import add_source
from app.services.fingerprint import fingerprint_ports, suggest_node_type
from app.services.http_probe import probe_open_ports
@@ -599,6 +599,10 @@ async def run_scan(
# Merged row carries both sources (e.g. ["proxmox", "arp"]).
keep.discovery_sources = add_source(keep.discovery_sources, discovery_source)
# status preserved — an approved device stays approved.
+ # Stamp last_scan on the row so every canvas drawing the device
+ # shows when the scanner last observed it. The row is the one
+ # place that fact belongs; a node only draws it.
+ keep.last_scan = datetime.now(timezone.utc)
else:
db.add(InventoryDevice(
ip=ip,
@@ -610,25 +614,10 @@ async def run_scan(
status="pending",
discovery_source=discovery_source,
discovery_sources=[discovery_source],
+ last_scan=datetime.now(timezone.utc),
))
devices_found += 1
- # Stamp last_scan on any canvas node that matches this device by IP
- # (or MAC, when known) so the inventory shows when the scanner last
- # observed it. Match both the normalized and raw MAC so a legacy
- # canvas node whose mac predates normalization still matches. Across
- # designs.
- node_match = [Node.ip == ip]
- for m in {norm_mac, host.get("mac")}:
- if m:
- node_match.append(Node.mac == m)
- matching_nodes = (await db.execute(
- select(Node).where(or_(*node_match))
- )).scalars().all()
- scanned_at = datetime.now(timezone.utc)
- for node in matching_nodes:
- node.last_scan = scanned_at
-
await db.commit()
await broadcast_scan_update(run_id=run_id, devices_found=devices_found)
diff --git a/backend/tests/scan/helpers.py b/backend/tests/scan/helpers.py
index a078f14..6d5268d 100644
--- a/backend/tests/scan/helpers.py
+++ b/backend/tests/scan/helpers.py
@@ -11,23 +11,54 @@ async def _add_design(db_session, name: str) -> str:
return design.id
-def _node(design_id: str, *, ip=None, ieee=None, mac=None) -> Node:
+def _node(design_id: str, *, ip=None, ieee=None, mac=None, device_id=None) -> Node:
+ """A canvas node. Addresses belong to the inventory row it draws, so pass
+ `device_id`, or let `_node_for` mint the row from the addresses."""
return Node(
- id=str(uuid.uuid4()), label="n", type="server", status="online",
- ip=ip, mac=mac, ieee_address=ieee, services=[], pos_x=0.0, pos_y=0.0,
- design_id=design_id,
+ id=str(uuid.uuid4()), label="n", type="server", pos_x=0.0, pos_y=0.0,
+ design_id=design_id, device_id=device_id,
)
+async def _node_for(db_session, design_id: str, *, ip=None, ieee=None, mac=None, **device_kwargs) -> Node:
+ """A node plus the inventory row carrying its addresses, linked.
+
+ Reuses a row that already describes these addresses — one device is one row,
+ so a test placing the same host on a second canvas gets a second node, not a
+ second device.
+ """
+ from app.services.inventory_sync import find_device_for
+
+ device = await find_device_for(db_session, ip=ip, mac=mac, ieee=ieee)
+ if device is None:
+ device = InventoryDevice(
+ id=str(uuid.uuid4()), ip=ip, mac=mac, ieee_address=ieee,
+ status="approved", status_live="online", **device_kwargs,
+ )
+ db_session.add(device)
+ await db_session.flush()
+ node = _node(design_id, device_id=device.id)
+ db_session.add(node)
+ await db_session.commit()
+ return node
+
+
async def _seed_zigbee_pending_pair(db_session):
"""Create a coordinator Node + a pending device + a link between them."""
from app.db.models import InventoryDeviceLink, Node
+ coord_device = InventoryDevice(
+ ieee_address="0xCOORD",
+ friendly_name="Coordinator",
+ suggested_type="zigbee_coordinator",
+ status="approved",
+ )
+ db_session.add(coord_device)
+ await db_session.flush()
coord = Node(
label="Coordinator",
type="zigbee_coordinator",
- status="unknown",
- ieee_address="0xCOORD",
+ device_id=coord_device.id,
)
db_session.add(coord)
diff --git a/backend/tests/scan/test_scan_approve.py b/backend/tests/scan/test_scan_approve.py
index ebd2aad..bfb2c32 100644
--- a/backend/tests/scan/test_scan_approve.py
+++ b/backend/tests/scan/test_scan_approve.py
@@ -6,13 +6,13 @@ from httpx import AsyncClient
from sqlalchemy import select
from app.db.models import Design, InventoryDevice, Node
-from tests.scan.helpers import _add_design, _node
+from tests.scan.helpers import _add_design, _node_for
@pytest.mark.asyncio
async def test_canvas_count_ignores_nodes_without_design(client, headers, db_session, pending_device):
# A node with no design_id is not "on a canvas".
- db_session.add(_node(None, ip="192.168.1.100"))
+ await _node_for(db_session, None, ip="192.168.1.100")
await db_session.commit()
res = await client.get("/api/v1/scan/pending", headers=headers)
@@ -57,16 +57,17 @@ async def test_approve_device_conflicts_on_existing_ieee_same_design(
design = Design(name="d1")
db_session.add(design)
await db_session.flush()
- existing = Node(
- label="sensor", type="zigbee_enddevice", ieee_address="0xZZZ",
- services=[], design_id=design.id,
- )
- db_session.add(existing)
device = InventoryDevice(
id=str(uuid.uuid4()), ieee_address="0xZZZ", suggested_type="zigbee_enddevice",
status="pending", discovery_source="zigbee",
)
db_session.add(device)
+ await db_session.flush()
+ # The node already on this design draws that very row.
+ existing = Node(
+ label="sensor", type="zigbee_enddevice", device_id=device.id, design_id=design.id,
+ )
+ db_session.add(existing)
await db_session.commit()
res = await client.post(
@@ -86,7 +87,7 @@ async def test_approve_device_conflicts_on_existing_ieee_same_design(
# No second node created; device stays pending until the user decides.
nodes = (
- await db_session.execute(select(Node).where(Node.ieee_address == "0xZZZ"))
+ await db_session.execute(select(Node).join(InventoryDevice, Node.device_id == InventoryDevice.id).where(InventoryDevice.ieee_address == "0xZZZ"))
).scalars().all()
assert len(nodes) == 1
@@ -99,15 +100,15 @@ async def test_approve_device_force_creates_duplicate_ieee(
design = Design(name="d1")
db_session.add(design)
await db_session.flush()
- db_session.add(Node(
- label="sensor", type="zigbee_enddevice", ieee_address="0xZZZ",
- services=[], design_id=design.id,
- ))
device = InventoryDevice(
id=str(uuid.uuid4()), ieee_address="0xZZZ", suggested_type="zigbee_enddevice",
status="pending", discovery_source="zigbee",
)
db_session.add(device)
+ await db_session.flush()
+ db_session.add(Node(
+ label="sensor", type="zigbee_enddevice", device_id=device.id, design_id=design.id,
+ ))
await db_session.commit()
res = await client.post(
@@ -120,7 +121,7 @@ async def test_approve_device_force_creates_duplicate_ieee(
)
assert res.status_code == 200
nodes = (
- await db_session.execute(select(Node).where(Node.ieee_address == "0xZZZ"))
+ await db_session.execute(select(Node).join(InventoryDevice, Node.device_id == InventoryDevice.id).where(InventoryDevice.ieee_address == "0xZZZ"))
).scalars().all()
assert len(nodes) == 2
@@ -133,8 +134,7 @@ async def test_approve_device_conflicts_on_existing_ip(
silently duplicated: the approve returns 409 with the existing node so the
UI can ask the user."""
design = await _add_design(db_session, "Home")
- existing = _node(design, ip="192.168.1.100")
- db_session.add(existing)
+ existing = await _node_for(db_session, design, ip="192.168.1.100")
await db_session.commit()
res = await client.post(
@@ -163,10 +163,8 @@ async def test_approve_device_conflicts_on_existing_mac(
):
"""MAC match (device re-IP'd via DHCP) also triggers the duplicate guard."""
design = await _add_design(db_session, "Home")
- existing = Node(id=str(uuid.uuid4()), label="n", type="server", status="online",
- ip="10.0.0.9", mac="aa:bb:cc:dd:ee:ff", services=[], design_id=design)
- db_session.add(existing)
- await db_session.commit()
+ # The node already on this design draws a row carrying that MAC.
+ await _node_for(db_session, design, ip="10.0.0.9", mac="aa:bb:cc:dd:ee:ff")
# pending_device carries mac aa:bb:cc:dd:ee:ff but a different ip.
res = await client.post(
@@ -188,8 +186,7 @@ async def test_approve_device_conflicts_on_ip_in_comma_list(
as. Exact-string matching missed it (issue #258); per-token matching catches
the duplicate."""
design = await _add_design(db_session, "Home")
- existing = _node(design, ip="fe80::1, 192.168.1.100")
- db_session.add(existing)
+ existing = await _node_for(db_session, design, ip="fe80::1, 192.168.1.100")
await db_session.commit()
res = await client.post(
@@ -212,7 +209,7 @@ async def test_approve_device_no_conflict_on_ip_substring(
"""The ip guard must match whole addresses, not substrings: a node at
10.0.0.40 is not a duplicate of a device at 10.0.0.4."""
design = await _add_design(db_session, "Home")
- db_session.add(_node(design, ip="10.0.0.40"))
+ await _node_for(db_session, design, ip="10.0.0.40")
await db_session.commit()
res = await client.post(
@@ -230,7 +227,7 @@ async def test_approve_device_force_creates_duplicate(
):
"""force=True (user confirmed) bypasses the guard and creates the node."""
design = await _add_design(db_session, "Home")
- db_session.add(_node(design, ip="192.168.1.100"))
+ await _node_for(db_session, design, ip="192.168.1.100")
await db_session.commit()
res = await client.post(
@@ -251,7 +248,7 @@ async def test_approve_device_allows_same_ip_on_other_design(
"""The guard is per-design: the same host on a different canvas is fine."""
other = await _add_design(db_session, "Lab")
target = await _add_design(db_session, "Home")
- db_session.add(_node(other, ip="192.168.1.100")) # exists on a DIFFERENT design
+ await _node_for(db_session, other, ip="192.168.1.100") # exists on a DIFFERENT design
await db_session.commit()
res = await client.post(
@@ -273,13 +270,16 @@ async def test_approve_device_places_already_approved_on_another_design(
other = await _add_design(db_session, "Other")
target = await _add_design(db_session, "Network Topology")
# Device is on `other` already (its global status is "approved").
- db_session.add(_node(other, ieee="0x00158d0005292b83"))
device = InventoryDevice(
id=str(uuid.uuid4()), ieee_address="0x00158d0005292b83",
suggested_type="zigbee_enddevice", status="approved",
discovery_source="zigbee",
)
db_session.add(device)
+ await db_session.flush()
+ db_session.add(Node(
+ label="sensor", type="zigbee_enddevice", device_id=device.id, design_id=other,
+ ))
await db_session.commit()
res = await client.post(
@@ -292,7 +292,7 @@ async def test_approve_device_places_already_approved_on_another_design(
# A node now exists on the target design too (one per canvas).
nodes = (
await db_session.execute(
- select(Node).where(Node.ieee_address == "0x00158d0005292b83")
+ select(Node).join(InventoryDevice, Node.device_id == InventoryDevice.id).where(InventoryDevice.ieee_address == "0x00158d0005292b83")
)
).scalars().all()
assert {n.design_id for n in nodes} == {other, target}
@@ -464,7 +464,7 @@ async def test_bulk_approve_skips_device_already_on_target_design(
ids = [d.id for d in two_device_inventory]
design = await _add_design(db_session, "Canvas")
# First device already sits on the canvas (matched by ip).
- db_session.add(_node(design, ip="192.168.1.10"))
+ await _node_for(db_session, design, ip="192.168.1.10")
await db_session.commit()
res = await client.post(
@@ -480,9 +480,10 @@ async def test_bulk_approve_skips_device_already_on_target_design(
nodes = (
await db_session.execute(select(NodeModel).where(NodeModel.design_id == design))
).scalars().all()
+ devices = [await db_session.get(InventoryDevice, n.device_id) for n in nodes]
# The pre-existing node plus the one newly approved — no duplicate for .10.
assert len(nodes) == 2
- assert sorted(n.ip for n in nodes) == ["192.168.1.10", "192.168.1.11"]
+ assert sorted(d.ip for d in devices) == ["192.168.1.10", "192.168.1.11"]
@pytest.mark.asyncio
@@ -493,7 +494,7 @@ async def test_bulk_approve_skips_device_matching_ip_in_comma_list(
as the plain IPv4 is still recognised as already placed (issue #258)."""
ids = [d.id for d in two_device_inventory]
design = await _add_design(db_session, "Canvas")
- db_session.add(_node(design, ip="fe80::1, 192.168.1.10"))
+ await _node_for(db_session, design, ip="fe80::1, 192.168.1.10")
await db_session.commit()
res = await client.post(
@@ -515,8 +516,7 @@ async def test_bulk_approve_reports_skipped_devices(
(with the existing node id) instead of silently dropping it."""
ids = [d.id for d in two_device_inventory]
design = await _add_design(db_session, "Canvas")
- existing = _node(design, ip="192.168.1.10")
- db_session.add(existing)
+ existing = await _node_for(db_session, design, ip="192.168.1.10")
await db_session.commit()
res = await client.post(
@@ -551,10 +551,14 @@ async def test_approve_device_copies_mac_to_node_and_properties(
)
assert res.status_code == 200
node = (
- await db_session.execute(select(NodeModel).where(NodeModel.ip == "192.168.1.100"))
- ).scalar_one()
- assert node.mac == "aa:bb:cc:dd:ee:ff"
- mac_props = [p for p in node.properties if p["key"] == "MAC"]
+ await db_session.execute(select(NodeModel).where(NodeModel.device_id.is_not(None)))
+ ).scalars().first()
+ assert node is not None
+ # The device facts belong to the inventory row the node draws.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ assert device.mac == "aa:bb:cc:dd:ee:ff"
+ mac_props = [p for p in device.properties if p["key"] == "MAC"]
assert mac_props == [
{"key": "MAC", "value": "aa:bb:cc:dd:ee:ff", "icon": None, "visible": False}
]
@@ -587,10 +591,14 @@ async def test_bulk_approve_copies_mac_to_node_and_properties(
)
assert res.status_code == 200
node = (
- await db_session.execute(select(NodeModel).where(NodeModel.ip == "192.168.1.55"))
- ).scalar_one()
- assert node.mac == "11:22:33:44:55:66"
- mac_props = [p for p in node.properties if p["key"] == "MAC"]
+ await db_session.execute(select(NodeModel).where(NodeModel.device_id.is_not(None)))
+ ).scalars().first()
+ assert node is not None
+ # The device facts belong to the inventory row the node draws.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ assert device.mac == "11:22:33:44:55:66"
+ mac_props = [p for p in device.properties if p["key"] == "MAC"]
assert mac_props == [
{"key": "MAC", "value": "11:22:33:44:55:66", "icon": None, "visible": False}
]
@@ -607,8 +615,10 @@ async def test_bulk_approve_sets_default_check_method(client: AsyncClient, heade
assert res.status_code == 200
nodes = (await db_session.execute(select(NodeModel))).scalars().all()
for n in nodes:
- if n.ip:
- assert n.check_method == "ping", f"node {n.id} created without check_method"
+ device = await db_session.get(InventoryDevice, n.device_id)
+ assert device is not None
+ if device.ip:
+ assert device.check_method == "ping", f"device {device.id} has no check_method"
@pytest.mark.asyncio
@@ -624,7 +634,9 @@ async def test_approve_device_sets_default_check_method(client: AsyncClient, hea
assert res.status_code == 200
node = (await db_session.execute(select(NodeModel))).scalars().first()
assert node is not None
- assert node.check_method == "ping"
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ assert device.check_method == "ping"
@pytest.mark.asyncio
@@ -713,11 +725,14 @@ async def test_bulk_approve_targets_requested_design(client, headers, db_session
assert res.json()["approved"] == 1
node = (
- await db_session.execute(select(Node).where(Node.ieee_address == "zwave-H-2"))
+ await db_session.execute(select(Node).join(InventoryDevice, Node.device_id == InventoryDevice.id).where(InventoryDevice.ieee_address == "zwave-H-2"))
).scalar_one()
assert node.design_id == active
assert node.design_id != first
- # Z-Wave device → online + Z-Wave property rows, no ICMP check.
- assert node.status == "online"
- assert node.check_method == "none"
- assert {p["key"] for p in node.properties} == {"Z-Wave ID", "Vendor", "Model"}
+ # Z-Wave device → online + Z-Wave property rows, no ICMP check. All of that
+ # is the device's, so it is read off the row the node draws.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ assert device.status_live == "online"
+ assert device.check_method == "none"
+ assert {p["key"] for p in device.properties} == {"Z-Wave ID", "Vendor", "Model"}
diff --git a/backend/tests/scan/test_scan_properties.py b/backend/tests/scan/test_scan_properties.py
index 323aa6f..234f6c3 100644
--- a/backend/tests/scan/test_scan_properties.py
+++ b/backend/tests/scan/test_scan_properties.py
@@ -31,9 +31,13 @@ async def test_approve_zigbee_device_populates_properties(
)
assert res.status_code == 200
node = (
- await db_session.execute(select(NodeModel).where(NodeModel.ieee_address == "0xABCDEF"))
- ).scalar_one()
- keys = {p["key"]: p["value"] for p in node.properties}
+ await db_session.execute(select(NodeModel).where(NodeModel.device_id.is_not(None)))
+ ).scalars().first()
+ assert node is not None
+ # The device facts belong to the inventory row the node draws.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ keys = {p["key"]: p["value"] for p in device.properties}
assert keys == {
"IEEE": "0xABCDEF",
"Vendor": "IKEA",
@@ -56,14 +60,18 @@ async def test_bulk_approve_zigbee_populates_properties(
)
assert res.status_code == 200
node = (
- await db_session.execute(select(NodeModel).where(NodeModel.ieee_address == "0xABCDEF"))
- ).scalar_one()
- keys = {p["key"]: p["value"] for p in node.properties}
+ await db_session.execute(select(NodeModel).where(NodeModel.device_id.is_not(None)))
+ ).scalars().first()
+ assert node is not None
+ # The device facts belong to the inventory row the node draws.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ keys = {p["key"]: p["value"] for p in device.properties}
assert keys["IEEE"] == "0xABCDEF"
assert keys["Vendor"] == "IKEA"
assert keys["Model"] == "TRADFRI"
assert keys["LQI"] == "180"
- assert node.check_method == "none"
+ assert device.check_method == "none"
def test_build_mac_property_returns_hidden_row():
@@ -133,9 +141,13 @@ async def test_approve_device_does_not_duplicate_mac_property(
)
assert res.status_code == 200
node = (
- await db_session.execute(select(NodeModel).where(NodeModel.ip == "192.168.1.100"))
- ).scalar_one()
- mac_props = [p for p in node.properties if p["key"] == "MAC"]
+ await db_session.execute(select(NodeModel).where(NodeModel.device_id.is_not(None)))
+ ).scalars().first()
+ assert node is not None
+ # The device facts belong to the inventory row the node draws.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ mac_props = [p for p in device.properties if p["key"] == "MAC"]
assert len(mac_props) == 1
# User's visibility choice is preserved.
assert mac_props[0]["visible"] is True
@@ -328,9 +340,13 @@ async def test_single_approve_zwave_sets_wireless_fields(client, headers, db_ses
assert res.status_code == 200
node = (
- await db_session.execute(select(Node).where(Node.ieee_address == "zwave-H-9"))
+ await db_session.execute(select(Node).join(InventoryDevice, Node.device_id == InventoryDevice.id).where(InventoryDevice.ieee_address == "zwave-H-9"))
).scalar_one()
assert node.design_id == active
- assert node.status == "online"
- assert node.check_method == "none"
- assert any(p["key"] == "Z-Wave ID" for p in node.properties)
+ node = (await db_session.execute(select(Node))).scalars().first()
+ assert node is not None
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ assert device.status_live == "online"
+ assert device.check_method == "none"
+ assert any(p["key"] == "Z-Wave ID" for p in device.properties)
diff --git a/backend/tests/scan/test_scan_routes.py b/backend/tests/scan/test_scan_routes.py
index 4c3c19f..1854bee 100644
--- a/backend/tests/scan/test_scan_routes.py
+++ b/backend/tests/scan/test_scan_routes.py
@@ -7,7 +7,7 @@ import pytest
from httpx import AsyncClient
from app.db.models import InventoryDevice
-from tests.scan.helpers import _add_design, _node
+from tests.scan.helpers import _add_design, _node_for
@pytest.mark.asyncio
@@ -55,7 +55,7 @@ async def test_canvas_count_matches_ip_in_comma_list(client, headers, db_session
# Node.ip holds several comma-separated addresses (IPv6 added first). The
# device scanned as the plain IPv4 must still correlate (issue #258).
d1 = await _add_design(db_session, "Home")
- db_session.add(_node(d1, ip="fe80::1, 192.168.1.100"))
+ await _node_for(db_session, d1, ip="fe80::1, 192.168.1.100")
await db_session.commit()
data = (await client.get("/api/v1/scan/pending", headers=headers)).json()
@@ -67,7 +67,7 @@ async def test_canvas_count_correlates_by_mac(client, headers, db_session, pendi
# Node's ip differs entirely (user edited it) but the MAC still matches:
# the device is on the canvas (issue #258, MAC is the stable identifier).
d1 = await _add_design(db_session, "Home")
- db_session.add(_node(d1, ip="10.9.9.9", mac="aa:bb:cc:dd:ee:ff"))
+ await _node_for(db_session, d1, ip="10.9.9.9", mac="aa:bb:cc:dd:ee:ff")
await db_session.commit()
data = (await client.get("/api/v1/scan/pending", headers=headers)).json()
@@ -79,8 +79,8 @@ async def test_canvas_count_counts_distinct_designs_by_ip(client, headers, db_se
# Same IP placed on two different canvases → canvas_count == 2.
d1 = await _add_design(db_session, "Home")
d2 = await _add_design(db_session, "Lab")
- db_session.add(_node(d1, ip="192.168.1.100"))
- db_session.add(_node(d2, ip="192.168.1.100"))
+ await _node_for(db_session, d1, ip="192.168.1.100")
+ await _node_for(db_session, d2, ip="192.168.1.100")
await db_session.commit()
res = await client.get("/api/v1/scan/pending", headers=headers)
@@ -97,7 +97,7 @@ async def test_canvas_count_correlates_by_ieee(client, headers, db_session):
)
db_session.add(device)
d1 = await _add_design(db_session, "Zigbee")
- db_session.add(_node(d1, ieee="0x00124b001"))
+ await _node_for(db_session, d1, ieee="0x00124b001")
await db_session.commit()
res = await client.get("/api/v1/scan/pending", headers=headers)
@@ -120,10 +120,13 @@ async def test_pending_device_without_node_has_null_node_timestamps(client, head
@pytest.mark.asyncio
async def test_pending_device_exposes_linked_node_timestamps(client, headers, db_session, pending_device):
d1 = await _add_design(db_session, "Home")
- node = _node(d1, ip="192.168.1.100")
- node.last_scan = datetime(2026, 6, 1, 8, 30, tzinfo=timezone.utc)
- node.last_seen = datetime(2026, 6, 25, 9, 15, tzinfo=timezone.utc)
- db_session.add(node)
+ node = await _node_for(db_session, d1, ip="192.168.1.100")
+ # When the scanner last saw the device, and when it last answered, are facts
+ # about the device — they live on its row, not on any one drawing of it.
+ device = await db_session.get(InventoryDevice, node.device_id)
+ assert device is not None
+ device.last_scan = datetime(2026, 6, 1, 8, 30, tzinfo=timezone.utc)
+ device.last_seen = datetime(2026, 6, 25, 9, 15, tzinfo=timezone.utc)
await db_session.commit()
data = (await client.get("/api/v1/scan/pending", headers=headers)).json()[0]
@@ -139,18 +142,21 @@ async def test_node_timestamps_aggregate_across_matches(client, headers, db_sess
# last_scan takes the NEWEST.
d1 = await _add_design(db_session, "Home")
d2 = await _add_design(db_session, "Lab")
- older = _node(d1, ip="192.168.1.100")
+ older = await _node_for(db_session, d1, ip="192.168.1.100")
older.created_at = datetime(2026, 1, 1, 0, 0, tzinfo=timezone.utc)
- older.last_scan = datetime(2026, 3, 1, 0, 0, tzinfo=timezone.utc)
- newer = _node(d2, ip="192.168.1.100")
+ newer = await _node_for(db_session, d2, ip="192.168.1.100")
newer.created_at = datetime(2026, 5, 1, 0, 0, tzinfo=timezone.utc)
- newer.last_scan = datetime(2026, 6, 1, 0, 0, tzinfo=timezone.utc)
+ # Both nodes draw the one row, which carries the scan observation.
+ assert older.device_id == newer.device_id
+ device = await db_session.get(InventoryDevice, older.device_id)
+ assert device is not None
+ device.last_scan = datetime(2026, 6, 1, 0, 0, tzinfo=timezone.utc)
db_session.add_all([older, newer])
await db_session.commit()
data = (await client.get("/api/v1/scan/pending", headers=headers)).json()[0]
- assert data["node_created_at"].startswith("2026-01-01") # oldest
- assert data["node_last_scan"].startswith("2026-06-01") # newest
+ assert data["node_created_at"].startswith("2026-01-01") # oldest node
+ assert data["node_last_scan"].startswith("2026-06-01") # off the device row
@pytest.mark.asyncio
diff --git a/backend/tests/scan/test_scan_run.py b/backend/tests/scan/test_scan_run.py
index 96d3925..5fa9108 100644
--- a/backend/tests/scan/test_scan_run.py
+++ b/backend/tests/scan/test_scan_run.py
@@ -131,16 +131,6 @@ async def test_run_scan_creates_new_pending_device(db_session: AsyncSession):
async def test_run_scan_keeps_stale_pending_for_canvas_nodes(db_session: AsyncSession):
"""Pending devices whose IP is already on a canvas are NOT purged — they stay
in the inventory and are surfaced with an "In N canvas" badge."""
- node = Node(
- id=str(uuid.uuid4()),
- label="Existing Server",
- type="server",
- ip="192.168.1.50",
- status="online",
- services=[],
- pos_x=0.0,
- pos_y=0.0,
- )
stale = InventoryDevice(
id=str(uuid.uuid4()),
ip="192.168.1.50",
@@ -151,8 +141,17 @@ async def test_run_scan_keeps_stale_pending_for_canvas_nodes(db_session: AsyncSe
suggested_type="generic",
status="pending",
)
- db_session.add(node)
db_session.add(stale)
+ await db_session.flush()
+ # A canvas already draws that device.
+ db_session.add(Node(
+ id=str(uuid.uuid4()),
+ label="Existing Server",
+ type="server",
+ device_id=stale.id,
+ pos_x=0.0,
+ pos_y=0.0,
+ ))
await db_session.commit()
run_id = str(uuid.uuid4())
@@ -174,19 +173,23 @@ async def test_run_scan_keeps_stale_pending_for_canvas_nodes(db_session: AsyncSe
@pytest.mark.asyncio
async def test_run_scan_records_ip_already_in_canvas(db_session: AsyncSession):
- """A scanned IP that already exists as a canvas Node still produces a pending
- device (no longer suppressed)."""
- node = Node(
+ """A scanned IP already drawn on a canvas refreshes the one inventory row.
+
+ The device is never suppressed, and never duplicated: the node draws that
+ row, so a second row for the same host would split the device in two.
+ """
+ existing = InventoryDevice(id=str(uuid.uuid4()), ip="192.168.1.50", status="approved")
+ db_session.add(existing)
+ await db_session.flush()
+ existing_id = existing.id
+ db_session.add(Node(
id=str(uuid.uuid4()),
label="Existing Server",
type="server",
- ip="192.168.1.50",
- status="online",
- services=[],
+ device_id=existing.id,
pos_x=0.0,
pos_y=0.0,
- )
- db_session.add(node)
+ ))
await db_session.commit()
run_id = str(uuid.uuid4())
@@ -200,12 +203,16 @@ async def test_run_scan_records_ip_already_in_canvas(db_session: AsyncSession):
):
await run_scan(["192.168.1.0/24"], db_session, run_id)
- result = await db_session.execute(
- select(InventoryDevice).where(InventoryDevice.ip == "192.168.1.50")
- )
- device = result.scalar_one_or_none()
- assert device is not None
- assert device.status == "pending"
+ rows = (
+ await db_session.execute(
+ select(InventoryDevice).where(InventoryDevice.ip == "192.168.1.50")
+ )
+ ).scalars().all()
+ assert len(rows) == 1
+ assert rows[0].id == existing_id
+ # The scan refreshed it; it did not reset the lifecycle.
+ assert rows[0].status == "approved"
+ assert rows[0].hostname == MOCK_HOST["hostname"]
@pytest.mark.asyncio
diff --git a/backend/tests/test_inventory_sync.py b/backend/tests/test_inventory_sync.py
index 9515f6b..552aa49 100644
--- a/backend/tests/test_inventory_sync.py
+++ b/backend/tests/test_inventory_sync.py
@@ -4,12 +4,13 @@ The inventory row owns the device facts; a node owns how the device is drawn.
These tests pin the rules that make one device end up as one row even when it
was drawn on several canvases.
"""
+import json
import uuid
from datetime import datetime, timedelta, timezone
import pytest
from httpx import AsyncClient
-from sqlalchemy import select
+from sqlalchemy import select, text
from app.db.models import Design, InventoryDevice, Node
from app.services.inventory_sync import (
@@ -36,9 +37,6 @@ def _node(design_id: str, **kwargs) -> Node:
"id": str(uuid.uuid4()),
"label": "n",
"type": "server",
- "status": "unknown",
- "services": [],
- "properties": [],
"pos_x": 0.0,
"pos_y": 0.0,
"design_id": design_id,
@@ -126,31 +124,92 @@ class TestFindDeviceFor:
class TestBackfill:
+ """The 3.3.0 migration path.
+
+ A pre-3.3.0 database still has the device columns on `nodes`; the backfill
+ reads them with raw SQL (the model no longer declares them) and folds each
+ node's view into an inventory row. These tests recreate that shape.
+ """
+
+ async def _legacy_nodes_table(self, db_session) -> None:
+ """Re-add the pre-3.3.0 device columns to `nodes`."""
+ for column, sql_type in (
+ ("hostname", "VARCHAR"), ("ip", "VARCHAR"), ("mac", "VARCHAR"), ("os", "VARCHAR"),
+ ("status", "VARCHAR"), ("check_method", "VARCHAR"), ("check_target", "VARCHAR"),
+ ("services", "JSON"), ("notes", "TEXT"), ("cpu_count", "INTEGER"),
+ ("cpu_model", "VARCHAR"), ("ram_gb", "FLOAT"), ("disk_gb", "FLOAT"),
+ ("show_hardware", "BOOLEAN"), ("properties", "JSON"), ("ieee_address", "VARCHAR"),
+ ("last_seen", "DATETIME"), ("last_scan", "DATETIME"), ("response_time_ms", "INTEGER"),
+ ):
+ await db_session.execute(text(f"ALTER TABLE nodes ADD COLUMN {column} {sql_type}"))
+ await db_session.commit()
+
+ async def _legacy_node(self, db_session, design_id: str, *, updated_at=None, **facts) -> str:
+ node_id = str(uuid.uuid4())
+ columns = {
+ "id": node_id,
+ "label": facts.pop("label", "n"),
+ "type": facts.pop("type", "server"),
+ "design_id": design_id,
+ "pos_x": 0.0,
+ "pos_y": 0.0,
+ "container_mode": 0,
+ "show_port_numbers": 0,
+ "bottom_handles": 1,
+ "top_handles": 1,
+ "left_handles": 0,
+ "right_handles": 0,
+ "created_at": _now(0),
+ "updated_at": updated_at or _now(0),
+ }
+ for key in ("services", "properties"):
+ if key in facts:
+ facts[key] = json.dumps(facts[key])
+ columns.update(facts)
+ names = ", ".join(columns)
+ binds = ", ".join(f":{c}" for c in columns)
+ await db_session.execute(text(f"INSERT INTO nodes ({names}) VALUES ({binds})"), columns)
+ await db_session.commit()
+ return node_id
+
+ @pytest.mark.asyncio
+ async def test_does_nothing_when_the_columns_are_already_gone(self, db_session):
+ """A second boot: there is no legacy data left to read."""
+ design = await _design(db_session)
+ db_session.add(_node(design))
+ await db_session.commit()
+
+ assert await backfill_node_devices(db_session) == {"linked": 0, "created": 0, "merged": 0}
+
@pytest.mark.asyncio
async def test_links_a_node_to_its_existing_row(self, db_session):
+ await self._legacy_nodes_table(db_session)
design = await _design(db_session)
db_session.add(InventoryDevice(id="d-1", ip="10.0.0.5", hostname="nas"))
- node = _node(design, ip="10.0.0.5", label="NAS")
- db_session.add(node)
await db_session.commit()
+ node_id = await self._legacy_node(db_session, design, ip="10.0.0.5", label="NAS")
stats = await backfill_node_devices(db_session)
await db_session.commit()
assert stats == {"linked": 1, "created": 0, "merged": 1}
- assert node.device_id == "d-1"
+ node = await db_session.get(Node, node_id)
+ assert node is not None and node.device_id == "d-1"
@pytest.mark.asyncio
async def test_creates_a_row_for_a_node_no_scan_ever_saw(self, db_session):
+ await self._legacy_nodes_table(db_session)
design = await _design(db_session)
- node = _node(design, label="Dumb switch", type="switch", notes="under the desk")
- db_session.add(node)
- await db_session.commit()
+ node_id = await self._legacy_node(
+ db_session, design, label="Dumb switch", type="switch", notes="under the desk"
+ )
stats = await backfill_node_devices(db_session)
await db_session.commit()
assert stats == {"linked": 1, "created": 1, "merged": 0}
+ node = await db_session.get(Node, node_id)
+ assert node is not None
device = await db_session.get(InventoryDevice, node.device_id)
assert device is not None
assert device.label == "Dumb switch"
@@ -162,36 +221,33 @@ class TestBackfill:
@pytest.mark.asyncio
async def test_two_canvases_one_device_merge_into_one_row(self, db_session):
"""The whole point: the same host drawn twice becomes one inventory row."""
+ await self._legacy_nodes_table(db_session)
design_a = await _design(db_session, "A")
design_b = await _design(db_session, "B")
- older = _node(
- design_a,
- ip="10.0.0.5",
- label="nas-old",
- hostname="nas.lan",
- notes="older note",
+ older = await self._legacy_node(
+ db_session, design_a,
+ ip="10.0.0.5", label="nas-old", hostname="nas.lan", notes="older note",
properties=[{"key": "Rack", "value": "A1", "icon": None, "visible": True}],
services=[{"port": 22, "protocol": "tcp", "service_name": "ssh"}],
updated_at=_now(0),
)
- newer = _node(
- design_b,
- ip="10.0.0.5",
- label="nas-new",
- os="TrueNAS",
+ newer = await self._legacy_node(
+ db_session, design_b,
+ ip="10.0.0.5", label="nas-new", os="TrueNAS",
properties=[{"key": "Owner", "value": "me", "icon": None, "visible": True}],
services=[{"port": 80, "protocol": "tcp", "service_name": "http"}],
updated_at=_now(30),
)
- db_session.add_all([older, newer])
- await db_session.commit()
stats = await backfill_node_devices(db_session)
await db_session.commit()
assert stats["linked"] == 2
- assert older.device_id == newer.device_id
- device = await db_session.get(InventoryDevice, older.device_id)
+ node_a = await db_session.get(Node, older)
+ node_b = await db_session.get(Node, newer)
+ assert node_a is not None and node_b is not None
+ assert node_a.device_id == node_b.device_id
+ device = await db_session.get(InventoryDevice, node_a.device_id)
assert device is not None
# Scalars: most recent edit wins, blanks never wipe an established value.
assert device.label == "nas-new"
@@ -204,10 +260,10 @@ class TestBackfill:
@pytest.mark.asyncio
async def test_leaves_canvas_furniture_alone(self, db_session):
+ await self._legacy_nodes_table(db_session)
design = await _design(db_session)
for kind in ("group", "groupRect", "text"):
- db_session.add(_node(design, type=kind, label=kind))
- await db_session.commit()
+ await self._legacy_node(db_session, design, type=kind, label=kind)
stats = await backfill_node_devices(db_session)
await db_session.commit()
@@ -217,9 +273,9 @@ class TestBackfill:
@pytest.mark.asyncio
async def test_is_a_no_op_on_a_second_run(self, db_session):
+ await self._legacy_nodes_table(db_session)
design = await _design(db_session)
- db_session.add(_node(design, ip="10.0.0.5"))
- await db_session.commit()
+ await self._legacy_node(db_session, design, ip="10.0.0.5")
first = await backfill_node_devices(db_session)
await db_session.commit()
diff --git a/backend/tests/test_node_dedupe.py b/backend/tests/test_node_dedupe.py
index be702f1..65fb8a3 100644
--- a/backend/tests/test_node_dedupe.py
+++ b/backend/tests/test_node_dedupe.py
@@ -1,10 +1,16 @@
-"""Tests for the same-canvas node dedupe repair (app.services.node_dedupe)."""
+"""Tests for the same-canvas node dedupe repair (app.services.node_dedupe).
+
+Identity is the device link now: two nodes are duplicates when they draw the
+same `device_inventory` row on the same design. The device facts live on that
+row, so nothing has to be merged out of the extras before they go — only edges
+and parent links are re-pointed.
+"""
import pytest
from sqlalchemy import select
-from app.db.models import Design, Edge, Node
-from app.services.node_dedupe import dedupe_nodes_by_ieee
+from app.db.models import Design, Edge, InventoryDevice, Node
+from app.services.node_dedupe import dedupe_nodes_by_device
async def _design(db, name="d1"):
@@ -14,61 +20,80 @@ async def _design(db, name="d1"):
return d
+async def _device(db, ieee="0xAAA", **kwargs):
+ device = InventoryDevice(ieee_address=ieee, status="approved", **kwargs)
+ db.add(device)
+ await db.flush()
+ return device
+
+
@pytest.mark.asyncio
-async def test_collapses_same_ieee_same_design(db_session):
+async def test_collapses_same_device_same_design(db_session):
d = await _design(db_session)
+ device = await _device(db_session)
keep = Node(
label="Sensor", type="zigbee_enddevice", design_id=d.id,
- ieee_address="0xAAA", properties=[{"key": "IEEE", "value": "0xAAA", "visible": True}],
- pos_x=100, pos_y=200,
+ device_id=device.id, pos_x=100, pos_y=200,
)
db_session.add(keep)
await db_session.flush()
- dup = Node(
- label="Sensor", type="zigbee_enddevice", design_id=d.id,
- ieee_address="0xAAA", ip="10.0.0.5",
- properties=[{"key": "LQI", "value": "88", "visible": False}],
- )
+ dup = Node(label="Sensor", type="zigbee_enddevice", design_id=d.id, device_id=device.id)
db_session.add(dup)
await db_session.flush()
- removed = await dedupe_nodes_by_ieee(db_session)
+ removed = await dedupe_nodes_by_device(db_session)
assert removed == 1
- nodes = (await db_session.execute(select(Node).where(Node.ieee_address == "0xAAA"))).scalars().all()
+ nodes = (
+ await db_session.execute(select(Node).where(Node.device_id == device.id))
+ ).scalars().all()
assert len(nodes) == 1
survivor = nodes[0]
assert survivor.id == keep.id # oldest kept
assert survivor.pos_x == 100 # canvas position preserved
- assert survivor.ip == "10.0.0.5" # missing field filled from dup
- keys = {p["key"] for p in survivor.properties}
- assert keys == {"IEEE", "LQI"} # properties merged
+ # The device facts were never on the node, so none can be lost here.
+ assert await db_session.get(InventoryDevice, device.id) is not None
@pytest.mark.asyncio
-async def test_preserves_same_ieee_across_designs(db_session):
+async def test_preserves_same_device_across_designs(db_session):
"""Same device on two canvases is valid — must NOT be merged."""
d1 = await _design(db_session, "d1")
d2 = await _design(db_session, "d2")
+ device = await _device(db_session, ieee="0xBBB")
for d in (d1, d2):
- db_session.add(Node(label="S", type="zigbee_enddevice", design_id=d.id, ieee_address="0xBBB"))
+ db_session.add(Node(label="S", type="zigbee_enddevice", design_id=d.id, device_id=device.id))
await db_session.flush()
- removed = await dedupe_nodes_by_ieee(db_session)
+ removed = await dedupe_nodes_by_device(db_session)
assert removed == 0
- nodes = (await db_session.execute(select(Node).where(Node.ieee_address == "0xBBB"))).scalars().all()
+ nodes = (
+ await db_session.execute(select(Node).where(Node.device_id == device.id))
+ ).scalars().all()
assert len(nodes) == 2
+async def test_leaves_furniture_alone(db_session):
+ """Zones and text carry no device link, so they are never duplicates."""
+ d = await _design(db_session)
+ for _ in range(2):
+ db_session.add(Node(label="Zone", type="groupRect", design_id=d.id))
+ await db_session.flush()
+
+ assert await dedupe_nodes_by_device(db_session) == 0
+
+
@pytest.mark.asyncio
async def test_repoints_edges_and_drops_dupes(db_session):
d = await _design(db_session)
- keep = Node(label="A", type="server", design_id=d.id, ieee_address="0xCCC")
- dup = Node(label="A", type="server", design_id=d.id, ieee_address="0xCCC")
+ device = await _device(db_session, ieee="0xCCC")
+ keep = Node(label="A", type="server", design_id=d.id, device_id=device.id)
other = Node(label="B", type="server", design_id=d.id)
db_session.add_all([keep, other])
await db_session.flush()
+ dup = Node(label="A", type="server", design_id=d.id, device_id=device.id)
db_session.add(dup)
await db_session.flush()
@@ -80,7 +105,7 @@ async def test_repoints_edges_and_drops_dupes(db_session):
])
await db_session.flush()
- removed = await dedupe_nodes_by_ieee(db_session)
+ removed = await dedupe_nodes_by_device(db_session)
assert removed == 1
edges = (await db_session.execute(select(Edge))).scalars().all()
@@ -93,17 +118,18 @@ async def test_repoints_edges_and_drops_dupes(db_session):
@pytest.mark.asyncio
async def test_repoints_child_parent(db_session):
d = await _design(db_session)
- keep = Node(label="Host", type="proxmox", design_id=d.id, ieee_address="0xDDD")
- dup = Node(label="Host", type="proxmox", design_id=d.id, ieee_address="0xDDD")
+ device = await _device(db_session, ieee="0xDDD")
+ keep = Node(label="Host", type="proxmox", design_id=d.id, device_id=device.id)
db_session.add(keep)
await db_session.flush()
+ dup = Node(label="Host", type="proxmox", design_id=d.id, device_id=device.id)
db_session.add(dup)
await db_session.flush()
child = Node(label="VM", type="vm", design_id=d.id, parent_id=dup.id)
db_session.add(child)
await db_session.flush()
- await dedupe_nodes_by_ieee(db_session)
+ await dedupe_nodes_by_device(db_session)
await db_session.refresh(child)
assert child.parent_id == keep.id
@@ -111,7 +137,8 @@ async def test_repoints_child_parent(db_session):
@pytest.mark.asyncio
async def test_idempotent_noop_when_unique(db_session):
d = await _design(db_session)
- db_session.add(Node(label="X", type="server", design_id=d.id, ieee_address="0xEEE"))
+ device = await _device(db_session, ieee="0xEEE")
+ db_session.add(Node(label="X", type="server", design_id=d.id, device_id=device.id))
await db_session.flush()
- assert await dedupe_nodes_by_ieee(db_session) == 0
- assert await dedupe_nodes_by_ieee(db_session) == 0
+ assert await dedupe_nodes_by_device(db_session) == 0
+ assert await dedupe_nodes_by_device(db_session) == 0
diff --git a/backend/tests/test_proxmox_router.py b/backend/tests/test_proxmox_router.py
index de616e3..3ecae70 100644
--- a/backend/tests/test_proxmox_router.py
+++ b/backend/tests/test_proxmox_router.py
@@ -222,25 +222,33 @@ async def test_persist_creates_pending(db_session) -> None:
@pytest.mark.asyncio
async def test_persist_merges_existing_scanned_node_by_ip(db_session) -> None:
# A device previously found by an IP scan (no ieee, no specs).
+ # The scanned host: one inventory row, drawn by one node.
+ scanned_device = InventoryDevice(
+ id=str(uuid.uuid4()), ip="10.0.0.5", suggested_type="generic",
+ status="approved", discovery_source="arp", discovery_sources=["arp"],
+ )
+ db_session.add(scanned_device)
+ await db_session.flush()
scanned = Node(
id=str(uuid.uuid4()), type="generic", label="10.0.0.5",
- ip="10.0.0.5", status="online", pos_x=0, pos_y=0,
+ device_id=scanned_device.id, pos_x=0, pos_y=0,
)
db_session.add(scanned)
await db_session.commit()
await _persist_pending_import(db_session, [_guest_node(101, "10.0.0.5")], [])
- # No duplicate node; identity + specs merged onto the existing one.
- nodes = (await db_session.execute(select(Node).where(Node.ip == "10.0.0.5"))).scalars().all()
+ # No duplicate node, and no second row: the import merged into the row the
+ # node already draws.
+ nodes = (await db_session.execute(select(Node))).scalars().all()
assert len(nodes) == 1
- merged = nodes[0]
+ merged = await db_session.get(InventoryDevice, nodes[0].device_id)
+ assert merged is not None
assert merged.ieee_address == "pve-pve1-101"
assert merged.cpu_count == 2
assert any(p["key"] == "CPU Cores" for p in (merged.properties or []))
- # Inventory row exists as approved (already on canvas).
- inv = (await db_session.execute(select(InventoryDevice).where(InventoryDevice.ieee_address == "pve-pve1-101"))).scalar_one()
- assert inv.status == "approved"
+ # Still approved — it is on a canvas.
+ assert merged.status == "approved"
@pytest.mark.asyncio
@@ -290,17 +298,17 @@ async def test_persist_preserves_ip_tag_for_legacy_null_source_row(db_session) -
async def test_persist_preserves_ip_tag_on_canvas_merge_legacy_row(db_session) -> None:
# The immich case: an on-canvas node from an old scan, with a legacy
# inventory row (NULL source). Merge must keep the IP tag on the row.
- node = Node(
- id=str(uuid.uuid4()), type="lxc", label="immich",
- ip="192.168.1.108", mac="bc:24:11:6c:96:52",
- status="online", pos_x=0, pos_y=0,
- )
inv = InventoryDevice(
id=str(uuid.uuid4()), ip="192.168.1.108", mac="bc:24:11:6c:96:52",
suggested_type="lxc", status="approved",
discovery_source=None, discovery_sources=[],
)
- db_session.add_all([node, inv])
+ db_session.add(inv)
+ await db_session.flush()
+ db_session.add(Node(
+ id=str(uuid.uuid4()), type="lxc", label="immich",
+ device_id=inv.id, pos_x=0, pos_y=0,
+ ))
await db_session.commit()
await _persist_pending_import(
@@ -328,9 +336,15 @@ async def test_persist_does_not_add_ip_tag_to_pure_proxmox_guest(db_session) ->
@pytest.mark.asyncio
async def test_persist_merges_canvas_node_by_mac(db_session) -> None:
# A scanned canvas node with a MAC but no IP recorded for the guest.
+ scanned_device = InventoryDevice(
+ id=str(uuid.uuid4()), mac="bc:24:11:aa:bb:cc", suggested_type="generic",
+ status="approved", discovery_source="arp", discovery_sources=["arp"],
+ )
+ db_session.add(scanned_device)
+ await db_session.flush()
scanned = Node(
id=str(uuid.uuid4()), type="generic", label="box",
- mac="bc:24:11:aa:bb:cc", status="online", pos_x=0, pos_y=0,
+ device_id=scanned_device.id, pos_x=0, pos_y=0,
)
db_session.add(scanned)
await db_session.commit()
@@ -339,7 +353,8 @@ async def test_persist_merges_canvas_node_by_mac(db_session) -> None:
nodes = (await db_session.execute(select(Node))).scalars().all()
assert len(nodes) == 1 # no duplicate node
- merged = nodes[0]
+ merged = await db_session.get(InventoryDevice, nodes[0].device_id)
+ assert merged is not None
assert merged.ieee_address == "pve-pve1-101"
assert merged.mac == "bc:24:11:aa:bb:cc"
assert merged.cpu_count == 2 # specs backfilled
@@ -420,12 +435,14 @@ async def test_cluster_link_resolves_to_cluster_edge(db_session) -> None:
# Two host nodes already on a canvas + a pending cluster link between them.
design = Design(id=str(uuid.uuid4()), name="d")
db_session.add(design)
- a = Node(id=str(uuid.uuid4()), type="proxmox", label="a", ieee_address="pve-node-a",
- status="online", pos_x=0, pos_y=0, design_id=design.id,
- left_handles=1, right_handles=1)
- b = Node(id=str(uuid.uuid4()), type="proxmox", label="b", ieee_address="pve-node-b",
- status="online", pos_x=0, pos_y=0, design_id=design.id,
- left_handles=1, right_handles=1)
+ dev_a = InventoryDevice(id=str(uuid.uuid4()), ieee_address="pve-node-a", status="approved")
+ dev_b = InventoryDevice(id=str(uuid.uuid4()), ieee_address="pve-node-b", status="approved")
+ db_session.add_all([dev_a, dev_b])
+ await db_session.flush()
+ a = Node(id=str(uuid.uuid4()), type="proxmox", label="a", device_id=dev_a.id,
+ pos_x=0, pos_y=0, design_id=design.id, left_handles=1, right_handles=1)
+ b = Node(id=str(uuid.uuid4()), type="proxmox", label="b", device_id=dev_b.id,
+ pos_x=0, pos_y=0, design_id=design.id, left_handles=1, right_handles=1)
db_session.add_all([a, b])
db_session.add(InventoryDeviceLink(
id=str(uuid.uuid4()), source_ieee="pve-node-a", target_ieee="pve-node-b",
@@ -451,13 +468,18 @@ async def test_link_survives_and_resolves_onto_second_design(db_session) -> None
da = Design(id=str(uuid.uuid4()), name="a")
db_ = Design(id=str(uuid.uuid4()), name="b")
db_session.add_all([da, db_])
+ # One row per device, whatever the canvas count.
+ dev_x = InventoryDevice(id=str(uuid.uuid4()), ieee_address="0xAAAA", status="approved")
+ dev_y = InventoryDevice(id=str(uuid.uuid4()), ieee_address="0xBBBB", status="approved")
+ db_session.add_all([dev_x, dev_y])
+ await db_session.flush()
# Same two devices placed on BOTH designs (one Node per canvas).
for d in (da, db_):
db_session.add_all([
- Node(id=str(uuid.uuid4()), type="iot", label="x", ieee_address="0xAAAA",
- status="online", pos_x=0, pos_y=0, design_id=d.id),
- Node(id=str(uuid.uuid4()), type="iot", label="y", ieee_address="0xBBBB",
- status="online", pos_x=0, pos_y=0, design_id=d.id),
+ Node(id=str(uuid.uuid4()), type="iot", label="x", device_id=dev_x.id,
+ pos_x=0, pos_y=0, design_id=d.id),
+ Node(id=str(uuid.uuid4()), type="iot", label="y", device_id=dev_y.id,
+ pos_x=0, pos_y=0, design_id=d.id),
])
db_session.add(InventoryDeviceLink(
id=str(uuid.uuid4()), source_ieee="0xAAAA", target_ieee="0xBBBB",
diff --git a/backend/tests/test_racks.py b/backend/tests/test_racks.py
index 2d8084d..fe49030 100644
--- a/backend/tests/test_racks.py
+++ b/backend/tests/test_racks.py
@@ -508,8 +508,10 @@ class TestInventory:
assert entry["services"] == [{"port": 445, "name": "smb"}]
assert entry["node_label"] == "nas-truenas"
assert entry["node_type"] == "nas"
- assert entry["node_hostname"] == "nas.lan"
- assert entry["node_os"] == "TrueNAS SCALE"
+ # The node no longer keeps its own copy: both views read the row, which
+ # the node create filled in without overwriting what the scan found.
+ assert entry["node_hostname"] == "nas"
+ assert entry["node_os"] == "TrueNAS"
assert entry["node_check_method"] == "http"
assert entry["node_design_id"] == network
assert entry["node_design_name"] == "Network"
@@ -526,17 +528,16 @@ class TestInventory:
headers=headers,
)
).json()
- db_session.add(InventoryDevice(id="pd-seen", ip="192.168.1.77", suggested_type="server"))
await db_session.execute(
- text("UPDATE nodes SET last_seen = :ts WHERE id = :id"),
- {"ts": "2026-08-08 10:00:00.000000", "id": node["id"]},
+ text("UPDATE device_inventory SET last_seen = :ts WHERE id = :id"),
+ {"ts": "2026-08-08 10:00:00.000000", "id": node["device_id"]},
)
await db_session.commit()
items = (
await client.get(f"/api/v1/racks/inventory?design_id={design_id}", headers=headers)
).json()["items"]
- entry = next(i for i in items if i["id"] == "pd-seen")
+ entry = next(i for i in items if i["id"] == node["device_id"])
assert entry["node_last_seen"].endswith(("Z", "+00:00"))
async def test_prefers_the_node_the_mount_names(self, client: AsyncClient, headers):
@@ -631,9 +632,17 @@ class TestInventory:
assert entry["node_design_name"] is None
assert entry["services"] == []
- async def test_falls_back_to_the_node_services(self, client: AsyncClient, headers, db_session):
- """An approved device keeps its fingerprint on the node, not the inventory row."""
+ async def test_reports_the_services_a_canvas_added(self, client: AsyncClient, headers, db_session):
+ """Services documented on a canvas reach the rack — they are the device's.
+
+ The scan found the host but no services; the user typed one on the
+ logical canvas, and the rack prints it without the node being consulted.
+ """
design_id = await _design(client, headers)
+ db_session.add(
+ InventoryDevice(id="pd-noservices", ip="192.168.1.63", suggested_type="server")
+ )
+ await db_session.commit()
await client.post(
"/api/v1/nodes",
json={
@@ -644,10 +653,6 @@ class TestInventory:
},
headers=headers,
)
- db_session.add(
- InventoryDevice(id="pd-noservices", ip="192.168.1.63", suggested_type="server")
- )
- await db_session.commit()
items = (
await client.get(f"/api/v1/racks/inventory?design_id={design_id}", headers=headers)
diff --git a/backend/tests/test_scanner.py b/backend/tests/test_scanner.py
index ab3210c..eb431bf 100644
--- a/backend/tests/test_scanner.py
+++ b/backend/tests/test_scanner.py
@@ -596,13 +596,20 @@ async def test_run_scan_adds_nmap_devices_as_pending(mem_db):
@pytest.mark.asyncio
async def test_run_scan_stamps_last_scan_on_matching_node_by_ip(mem_db):
- """A scan that sees a device matching a canvas node (by IP) stamps last_scan."""
+ """A scan that sees a known device stamps last_scan on its inventory row.
+
+ The stamp is a fact about the device, so it lands once on the row every
+ canvas draws — not once per node.
+ """
from app.services.scanner import run_scan
run_id = _make_run_id()
async with mem_db() as session:
session.add(_make_scan_run(run_id))
- session.add(Node(id="n1", type="server", label="NAS", ip="192.168.1.5"))
+ device = InventoryDevice(id="d1", ip="192.168.1.5", status="approved")
+ session.add(device)
+ await session.flush()
+ session.add(Node(id="n1", type="server", label="NAS", device_id="d1"))
await session.commit()
nmap_hosts = [{"ip": "192.168.1.5", "hostname": "nas.lan", "mac": None, "os": None, "open_ports": []}]
@@ -614,21 +621,24 @@ async def test_run_scan_stamps_last_scan_on_matching_node_by_ip(mem_db):
await run_scan(["192.168.1.0/24"], session, run_id)
async with mem_db() as session:
- node = await session.get(Node, "n1")
+ device = await session.get(InventoryDevice, "d1")
- assert node is not None
- assert node.last_scan is not None
+ assert device is not None
+ assert device.last_scan is not None
@pytest.mark.asyncio
async def test_run_scan_stamps_last_scan_on_matching_node_by_mac(mem_db):
- """A node with no IP but a matching MAC still gets last_scan stamped."""
+ """A device with no IP but a matching MAC still gets last_scan stamped."""
from app.services.scanner import run_scan
run_id = _make_run_id()
async with mem_db() as session:
session.add(_make_scan_run(run_id))
- session.add(Node(id="n2", type="iot", label="Sensor", mac="AA:BB:CC:DD:EE:FF"))
+ device = InventoryDevice(id="d2", mac="aa:bb:cc:dd:ee:ff", status="approved")
+ session.add(device)
+ await session.flush()
+ session.add(Node(id="n2", type="iot", label="Sensor", device_id="d2"))
await session.commit()
nmap_hosts = [{"ip": "192.168.1.9", "hostname": None, "mac": "AA:BB:CC:DD:EE:FF", "os": None, "open_ports": []}]
@@ -640,21 +650,24 @@ async def test_run_scan_stamps_last_scan_on_matching_node_by_mac(mem_db):
await run_scan(["192.168.1.0/24"], session, run_id)
async with mem_db() as session:
- node = await session.get(Node, "n2")
+ device = await session.get(InventoryDevice, "d2")
- assert node is not None
- assert node.last_scan is not None
+ assert device is not None
+ assert device.last_scan is not None
@pytest.mark.asyncio
async def test_run_scan_leaves_last_scan_untouched_on_unmatched_node(mem_db):
- """A node whose IP/MAC is not seen by the scan keeps last_scan = None."""
+ """A device whose IP/MAC the scan never saw keeps last_scan = None."""
from app.services.scanner import run_scan
run_id = _make_run_id()
async with mem_db() as session:
session.add(_make_scan_run(run_id))
- session.add(Node(id="n3", type="server", label="Other", ip="10.0.0.99"))
+ device = InventoryDevice(id="d3", ip="10.0.0.99", status="approved")
+ session.add(device)
+ await session.flush()
+ session.add(Node(id="n3", type="server", label="Other", device_id="d3"))
await session.commit()
nmap_hosts = [{"ip": "192.168.1.5", "hostname": None, "mac": None, "os": None, "open_ports": []}]
@@ -666,10 +679,10 @@ async def test_run_scan_leaves_last_scan_untouched_on_unmatched_node(mem_db):
await run_scan(["192.168.1.0/24"], session, run_id)
async with mem_db() as session:
- node = await session.get(Node, "n3")
+ device = await session.get(InventoryDevice, "d3")
- assert node is not None
- assert node.last_scan is None
+ assert device is not None
+ assert device.last_scan is None
@pytest.mark.asyncio
@@ -771,11 +784,12 @@ async def test_run_scan_keeps_canvas_nodes(mem_db):
run_id = _make_run_id()
async with mem_db() as session:
session.add(_make_scan_run(run_id))
- canvas_node = Node(
- id=str(uuid.uuid4()), label="PVE", type="proxmox",
- ip="192.168.1.100", status="online",
- )
- session.add(canvas_node)
+ drawn = InventoryDevice(id="d-pve", ip="192.168.1.100", status="pending")
+ session.add(drawn)
+ await session.flush()
+ session.add(Node(
+ id=str(uuid.uuid4()), label="PVE", type="proxmox", device_id="d-pve",
+ ))
await session.commit()
nmap_hosts = [{"ip": "192.168.1.100", "hostname": "pve.lan", "mac": None, "os": None, "open_ports": []}]
diff --git a/backend/tests/test_scheduler.py b/backend/tests/test_scheduler.py
index 4cc18a3..b8caf2e 100644
--- a/backend/tests/test_scheduler.py
+++ b/backend/tests/test_scheduler.py
@@ -24,18 +24,25 @@ from app.core.scheduler import (
stop_scheduler,
)
from app.db.database import Base
-from app.db.models import Node
+from app.db.models import InventoryDevice, Node
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_node(**kwargs) -> Node:
+def _make_device(**kwargs) -> InventoryDevice:
+ """An inventory row — what the checker iterates now that a device is checked
+ once rather than once per canvas."""
+ defaults = dict(id=str(uuid.uuid4()), status="approved", status_live="unknown")
+ return InventoryDevice(**{**defaults, **kwargs})
+
+
+def _make_node(device_id: str | None = None, **kwargs) -> Node:
defaults = dict(
id=str(uuid.uuid4()),
type="server",
label="Test",
- status="unknown",
+ device_id=device_id,
pos_x=0.0,
pos_y=0.0,
)
@@ -58,11 +65,10 @@ async def mem_db():
@pytest.mark.asyncio
-async def test_run_status_checks_skips_nodes_without_check_method(mem_db):
- """Nodes with no check_method are skipped; check_node is never called."""
+async def test_run_status_checks_skips_devices_without_check_method(mem_db):
+ """Devices with no check_method are skipped; check_node is never called."""
async with mem_db() as session:
- node = _make_node(check_method=None, ip="10.0.0.1")
- session.add(node)
+ session.add(_make_device(check_method=None, ip="10.0.0.1"))
await session.commit()
with patch("app.core.scheduler.AsyncSessionLocal", mem_db), \
@@ -72,13 +78,16 @@ async def test_run_status_checks_skips_nodes_without_check_method(mem_db):
@pytest.mark.asyncio
-async def test_run_status_checks_updates_node_status(mem_db):
- """check_node result is persisted to the DB and broadcast via WebSocket."""
+async def test_run_status_checks_updates_device_status(mem_db):
+ """check_node result lands on the device row and is broadcast to its nodes."""
async with mem_db() as session:
- node = _make_node(check_method="ping", ip="10.0.0.1")
+ device = _make_device(check_method="ping", ip="10.0.0.1")
+ session.add(device)
+ await session.flush()
+ node = _make_node(device_id=device.id)
session.add(node)
await session.commit()
- node_id = node.id
+ device_id, node_id = device.id, node.id
check_result = {"status": "online", "response_time_ms": 5}
@@ -87,17 +96,20 @@ async def test_run_status_checks_updates_node_status(mem_db):
patch("app.api.routes.status.broadcast_status", new_callable=AsyncMock) as mock_broadcast:
await _run_status_checks()
- # Verify DB updated
+ # Verify the row updated — `status_live` is reachability, `status` is the
+ # pending/approved/hidden lifecycle and must not be touched.
async with mem_db() as session:
- updated = await session.get(Node, node_id)
+ updated = await session.get(InventoryDevice, device_id)
assert updated is not None
- assert updated.status == "online"
+ assert updated.status_live == "online"
+ assert updated.status == "approved"
assert updated.response_time_ms == 5
- # Verify WebSocket broadcast
+ # Verify WebSocket broadcast, addressed by device and carrying its nodes
mock_broadcast.assert_awaited_once()
_, kwargs = mock_broadcast.call_args
- assert kwargs["node_id"] == node_id
+ assert kwargs["device_id"] == device_id
+ assert kwargs["node_ids"] == [node_id]
assert kwargs["status"] == "online"
@@ -105,10 +117,10 @@ async def test_run_status_checks_updates_node_status(mem_db):
async def test_run_status_checks_sets_last_seen_only_when_online(mem_db):
"""last_seen is updated only when status is 'online'."""
async with mem_db() as session:
- node = _make_node(check_method="ping", ip="10.0.0.1", last_seen=None)
- session.add(node)
+ device = _make_device(check_method="ping", ip="10.0.0.1", last_seen=None)
+ session.add(device)
await session.commit()
- node_id = node.id
+ device_id = device.id
check_result = {"status": "offline", "response_time_ms": None}
@@ -118,18 +130,19 @@ async def test_run_status_checks_sets_last_seen_only_when_online(mem_db):
await _run_status_checks()
async with mem_db() as session:
- updated = await session.get(Node, node_id)
+ updated = await session.get(InventoryDevice, device_id)
assert updated is not None
assert updated.last_seen is None # not set for offline
@pytest.mark.asyncio
async def test_run_status_checks_handles_check_error_gracefully(mem_db):
- """An exception from check_node is logged and does not abort other nodes."""
+ """An exception from check_node is logged and does not abort other devices."""
async with mem_db() as session:
- n1 = _make_node(check_method="ping", ip="10.0.0.1")
- n2 = _make_node(check_method="ping", ip="10.0.0.2")
- session.add_all([n1, n2])
+ session.add_all([
+ _make_device(check_method="ping", ip="10.0.0.1"),
+ _make_device(check_method="ping", ip="10.0.0.2"),
+ ])
await session.commit()
call_count = 0
@@ -191,7 +204,7 @@ def test_start_and_stop_scheduler():
@pytest.mark.asyncio
async def test_run_service_checks_disabled_does_nothing(mem_db):
async with mem_db() as session:
- session.add(_make_node(services=[{"port": 80, "protocol": "tcp", "service_name": "http"}]))
+ session.add(_make_device(services=[{"port": 80, "protocol": "tcp", "service_name": "http"}]))
await session.commit()
with patch("app.core.scheduler.settings") as mock_settings, \
@@ -203,15 +216,18 @@ async def test_run_service_checks_disabled_does_nothing(mem_db):
@pytest.mark.asyncio
-async def test_run_service_checks_broadcasts_per_node(mem_db):
+async def test_run_service_checks_broadcasts_per_device(mem_db):
async with mem_db() as session:
- node = _make_node(
+ device = _make_device(
ip="10.0.0.5",
services=[{"port": 80, "protocol": "tcp", "service_name": "http"}],
)
+ session.add(device)
+ await session.flush()
+ node = _make_node(device_id=device.id)
session.add(node)
await session.commit()
- node_id = node.id
+ device_id, node_id = device.id, node.id
statuses = [{"port": 80, "protocol": "tcp", "status": "offline"}]
@@ -224,14 +240,15 @@ async def test_run_service_checks_broadcasts_per_node(mem_db):
mock_bcast.assert_awaited_once()
_, kwargs = mock_bcast.call_args
- assert kwargs["node_id"] == node_id
+ assert kwargs["device_id"] == device_id
+ assert kwargs["node_ids"] == [node_id]
assert kwargs["services"] == statuses
@pytest.mark.asyncio
-async def test_run_service_checks_skips_nodes_without_services(mem_db):
+async def test_run_service_checks_skips_devices_without_services(mem_db):
async with mem_db() as session:
- session.add(_make_node(ip="10.0.0.6", services=[]))
+ session.add(_make_device(ip="10.0.0.6", services=[]))
await session.commit()
with patch("app.core.scheduler.settings") as mock_settings, \
diff --git a/backend/tests/test_stats.py b/backend/tests/test_stats.py
index f9d009e..853166e 100644
--- a/backend/tests/test_stats.py
+++ b/backend/tests/test_stats.py
@@ -68,13 +68,21 @@ async def test_summary_aggregates_counts(
) -> None:
settings.homepage_api_key = "topsecret"
finished = datetime(2026, 5, 14, 10, 0, tzinfo=timezone.utc)
+ # Reachability and the ieee live on the inventory row each node draws.
+ drawn = [
+ ("A", "server", "online", None),
+ ("B", "server", "online", None),
+ ("C", "server", "offline", None),
+ ("D", "server", "unknown", None),
+ ("Z1", "iot", "online", "0x1"),
+ ("Z2", "iot", "online", "0x2"),
+ ]
+ for label, node_type, status_live, ieee in drawn:
+ device = InventoryDevice(status="approved", status_live=status_live, ieee_address=ieee)
+ db_session.add(device)
+ await db_session.flush()
+ db_session.add(Node(type=node_type, label=label, device_id=device.id))
db_session.add_all([
- Node(type="server", label="A", status="online"),
- Node(type="server", label="B", status="online"),
- Node(type="server", label="C", status="offline"),
- Node(type="server", label="D", status="unknown"),
- Node(type="iot", label="Z1", status="online", ieee_address="0x1"),
- Node(type="iot", label="Z2", status="online", ieee_address="0x2"),
InventoryDevice(ip="10.0.0.1", status="pending"),
InventoryDevice(ip="10.0.0.2", status="pending"),
InventoryDevice(ip="10.0.0.3", status="hidden"), # excluded
diff --git a/backend/tests/test_status.py b/backend/tests/test_status.py
index 74a6da9..29a3011 100644
--- a/backend/tests/test_status.py
+++ b/backend/tests/test_status.py
@@ -74,7 +74,8 @@ async def test_broadcast_status_sends_to_connected_clients():
_connections.append(fake)
try:
await broadcast_status(
- node_id="node-1",
+ device_id="dev-1",
+ node_ids=["node-1", "node-2"],
status="online",
checked_at="2024-01-01T00:00:00",
response_time_ms=42,
@@ -85,7 +86,9 @@ async def test_broadcast_status_sends_to_connected_clients():
assert len(received) == 1
msg = json.loads(received[0])
assert msg["type"] == "status"
- assert msg["node_id"] == "node-1"
+ # One check, addressed by device, lighting up every canvas drawing it.
+ assert msg["device_id"] == "dev-1"
+ assert msg["node_ids"] == ["node-1", "node-2"]
assert msg["status"] == "online"
assert msg["response_time_ms"] == 42
@@ -102,7 +105,7 @@ async def test_broadcast_status_no_response_time():
fake = FakeWS()
_connections.append(fake)
try:
- await broadcast_status(node_id="n", status="offline", checked_at="t")
+ await broadcast_status(device_id="d", node_ids=["n"], status="offline", checked_at="t")
finally:
_connections.remove(fake)
@@ -122,7 +125,7 @@ async def test_broadcast_status_removes_dead_connection():
_connections.append(dead)
initial_len = len(_connections)
- await broadcast_status(node_id="n", status="online", checked_at="t")
+ await broadcast_status(device_id="d", node_ids=["n"], status="online", checked_at="t")
assert dead not in _connections
assert len(_connections) == initial_len - 1
@@ -159,7 +162,7 @@ async def test_broadcast_scan_update():
async def test_broadcast_no_connections():
"""broadcast_* with no connections must not raise."""
assert len(_connections) == 0
- await broadcast_status(node_id="n", status="online", checked_at="t")
+ await broadcast_status(device_id="d", node_ids=["n"], status="online", checked_at="t")
await broadcast_scan_update(run_id="r", devices_found=0)
@@ -179,7 +182,8 @@ async def test_broadcast_service_status_payload():
_connections.append(fake)
try:
await broadcast_service_status(
- node_id="node-7",
+ device_id="dev-7",
+ node_ids=["node-7"],
services=[{"port": 80, "protocol": "tcp", "status": "offline"}],
checked_at="2024-01-01T00:00:00",
)
@@ -188,7 +192,8 @@ async def test_broadcast_service_status_payload():
msg = json.loads(received[0])
assert msg["type"] == "service_status"
- assert msg["node_id"] == "node-7"
+ assert msg["device_id"] == "dev-7"
+ assert msg["node_ids"] == ["node-7"]
assert msg["services"] == [{"port": 80, "protocol": "tcp", "status": "offline"}]
@@ -217,7 +222,7 @@ async def test_broadcast_dead_connection_dropped_once_safely():
dead = DeadWS()
_connections.append(dead)
- await broadcast_status(node_id="n", status="online", checked_at="t")
+ await broadcast_status(device_id="d", node_ids=["n"], status="online", checked_at="t")
# A second broadcast must not raise even though dead is already gone.
- await broadcast_status(node_id="n", status="online", checked_at="t")
+ await broadcast_status(device_id="d", node_ids=["n"], status="online", checked_at="t")
assert dead not in _connections
diff --git a/backend/tests/test_zigbee_router.py b/backend/tests/test_zigbee_router.py
index fc533cc..a439896 100644
--- a/backend/tests/test_zigbee_router.py
+++ b/backend/tests/test_zigbee_router.py
@@ -436,15 +436,18 @@ async def test_persist_pending_import_backfills_inventory_for_approved_node(
# Simulate: router was approved earlier → exists as a canvas Node, but with
# no matching device_inventory row (e.g. a legacy auto-placed device).
- approved = Node(
- label="router_1",
- type="zigbee_router",
- status="online",
- check_method="none",
+ device = InventoryDevice(
ieee_address="0xR1",
- services=[],
+ friendly_name="router_1",
+ suggested_type="zigbee_router",
+ status="approved",
+ status_live="online",
+ check_method="none",
properties=[],
)
+ db_session.add(device)
+ await db_session.flush()
+ approved = Node(label="router_1", type="zigbee_router", device_id=device.id)
db_session.add(approved)
await db_session.commit()
@@ -464,7 +467,9 @@ async def test_persist_pending_import_backfills_inventory_for_approved_node(
# Node properties got refreshed.
refreshed = (
- await db_session.execute(select(Node).where(Node.ieee_address == "0xR1"))
+ await db_session.execute(
+ select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
+ )
).scalar_one()
keys = {p["key"]: p["value"] for p in refreshed.properties}
assert keys == {"IEEE": "0xR1", "Vendor": "TI", "Model": "CC2530", "LQI": "250"}
@@ -483,14 +488,15 @@ async def test_persist_pending_import_preserves_hidden_inventory_for_approved_no
from app.api.routes.zigbee import _persist_pending_import
from app.db.models import InventoryDevice, Node
- db_session.add(Node(
- label="router_1", type="zigbee_router", status="online",
- check_method="none", ieee_address="0xR1", services=[], properties=[],
- ))
- db_session.add(InventoryDevice(
+ # One row, hidden by the user, and a canvas still draws it.
+ drawn = InventoryDevice(
ieee_address="0xR1", friendly_name="router_1", suggested_type="zigbee_router",
- device_subtype="Router", status="hidden", discovery_source="zigbee",
- ))
+ device_subtype="Router", status="hidden", status_live="online",
+ check_method="none", discovery_source="zigbee",
+ )
+ db_session.add(drawn)
+ await db_session.flush()
+ db_session.add(Node(label="router_1", type="zigbee_router", device_id=drawn.id))
await db_session.commit()
await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
@@ -595,21 +601,24 @@ async def test_persist_pending_import_preserves_user_visibility(db_session) -> N
from sqlalchemy import select
from app.api.routes.zigbee import _persist_pending_import
- from app.db.models import Node
+ from app.db.models import InventoryDevice, Node
- approved = Node(
- label="router_1",
- type="zigbee_router",
- status="online",
- check_method="none",
+ device = InventoryDevice(
ieee_address="0xR1",
- services=[],
+ friendly_name="router_1",
+ suggested_type="zigbee_router",
+ status="approved",
+ status_live="online",
+ check_method="none",
properties=[
{"key": "IEEE", "value": "0xR1", "icon": None, "visible": True},
{"key": "Vendor", "value": "TI", "icon": None, "visible": True},
{"key": "Custom", "value": "kept", "icon": None, "visible": True},
],
)
+ db_session.add(device)
+ await db_session.flush()
+ approved = Node(label="router_1", type="zigbee_router", device_id=device.id)
db_session.add(approved)
await db_session.commit()
@@ -619,7 +628,9 @@ async def test_persist_pending_import_preserves_user_visibility(db_session) -> N
await _persist_pending_import(db_session, bumped, _PENDING_EDGES)
refreshed = (
- await db_session.execute(select(Node).where(Node.ieee_address == "0xR1"))
+ await db_session.execute(
+ select(InventoryDevice).where(InventoryDevice.ieee_address == "0xR1")
+ )
).scalar_one()
by_key = {p["key"]: p for p in refreshed.properties}
# Existing keys keep their visibility (True).
@@ -647,10 +658,14 @@ async def test_persist_pending_import_refreshes_approved_coordinator_node(
from app.db.models import InventoryDevice, Node
# Legacy: coordinator auto-placed as a canvas Node, no device_inventory row.
- db_session.add(Node(
- label="Coordinator", type="zigbee_coordinator", status="online",
- check_method="none", ieee_address="0xCOORD", services=[], properties=[],
- ))
+ drawn = InventoryDevice(
+ ieee_address="0xCOORD", friendly_name="Coordinator",
+ suggested_type="zigbee_coordinator", status="approved", status_live="online",
+ check_method="none",
+ )
+ db_session.add(drawn)
+ await db_session.flush()
+ db_session.add(Node(label="Coordinator", type="zigbee_coordinator", device_id=drawn.id))
await db_session.commit()
bumped = [dict(n) for n in _PENDING_NODES]
@@ -659,7 +674,9 @@ async def test_persist_pending_import_refreshes_approved_coordinator_node(
await _persist_pending_import(db_session, bumped, _PENDING_EDGES)
coord = (
- await db_session.execute(select(Node).where(Node.ieee_address == "0xCOORD"))
+ await db_session.execute(
+ select(InventoryDevice).where(InventoryDevice.ieee_address == "0xCOORD")
+ )
).scalar_one()
keys = {p["key"]: p["value"] for p in coord.properties}
assert keys["Vendor"] == "TI"
@@ -685,17 +702,19 @@ async def test_persist_pending_import_device_on_multiple_canvases(
from sqlalchemy import select
from app.api.routes.zigbee import _persist_pending_import
- from app.db.models import Design, Node
+ from app.db.models import Design, InventoryDevice, Node
d1 = Design(name="d1")
d2 = Design(name="d2")
- db_session.add_all([d1, d2])
+ drawn = InventoryDevice(
+ ieee_address="0xR1", friendly_name="router_1", suggested_type="zigbee_router",
+ status="approved", status_live="online", check_method="none",
+ )
+ db_session.add_all([d1, d2, drawn])
await db_session.flush()
for d in (d1, d2):
db_session.add(Node(
- label="router_1", type="zigbee_router", status="online",
- check_method="none", ieee_address="0xR1", services=[],
- properties=[], design_id=d.id,
+ label="router_1", type="zigbee_router", device_id=drawn.id, design_id=d.id,
))
await db_session.commit()
@@ -705,10 +724,12 @@ async def test_persist_pending_import_device_on_multiple_canvases(
await _persist_pending_import(db_session, bumped, _PENDING_EDGES)
nodes = (
- await db_session.execute(select(Node).where(Node.ieee_address == "0xR1"))
+ await db_session.execute(select(Node).where(Node.device_id == drawn.id))
).scalars().all()
assert len(nodes) == 2 # both canvas placements preserved
- for n in nodes:
+ # One row behind them, so one refresh serves both canvases.
+ await db_session.refresh(drawn)
+ for n in [drawn]:
lqi = {p["key"]: p["value"] for p in n.properties}.get("LQI")
assert lqi == "240" # refreshed on every canvas
diff --git a/backend/tests/test_zwave_router.py b/backend/tests/test_zwave_router.py
index c26c866..e7bf962 100644
--- a/backend/tests/test_zwave_router.py
+++ b/backend/tests/test_zwave_router.py
@@ -374,16 +374,14 @@ async def test_persist_backfills_inventory_for_approved_node(db_session) -> None
from app.api.routes.zwave import _persist_pending_import
from app.db.models import InventoryDevice, Node
- approved = Node(
- label="Wall Plug",
- type="zwave_router",
- status="online",
+ drawn = InventoryDevice(
+ ieee_address="zwave-0xh-2", friendly_name="Wall Plug",
+ suggested_type="zwave_router", status="approved", status_live="online",
check_method="none",
- ieee_address="zwave-0xh-2",
- services=[],
- properties=[],
)
- db_session.add(approved)
+ db_session.add(drawn)
+ await db_session.flush()
+ db_session.add(Node(label="Wall Plug", type="zwave_router", device_id=drawn.id))
await db_session.commit()
await _persist_pending_import(db_session, _PENDING_NODES, _PENDING_EDGES)
@@ -396,7 +394,9 @@ async def test_persist_backfills_inventory_for_approved_node(db_session) -> None
assert inv.status == "approved"
assert inv.suggested_type == "zwave_router"
refreshed = (
- await db_session.execute(select(Node).where(Node.ieee_address == "zwave-0xh-2"))
+ await db_session.execute(
+ select(InventoryDevice).where(InventoryDevice.ieee_address == "zwave-0xh-2")
+ )
).scalar_one()
keys = {p["key"]: p["value"] for p in refreshed.properties}
assert keys == {"Z-Wave ID": "zwave-0xh-2", "Vendor": "Aeotec", "Model": "ZW100"}
@@ -466,17 +466,20 @@ async def test_persist_device_on_multiple_canvases(db_session) -> None:
from sqlalchemy import select
from app.api.routes.zwave import _persist_pending_import
- from app.db.models import Design, Node
+ from app.db.models import Design, InventoryDevice, Node
d1 = Design(name="d1")
d2 = Design(name="d2")
- db_session.add_all([d1, d2])
+ drawn = InventoryDevice(
+ ieee_address="zwave-0xh-2", friendly_name="Wall Plug",
+ suggested_type="zwave_router", status="approved", status_live="online",
+ check_method="none",
+ )
+ db_session.add_all([d1, d2, drawn])
await db_session.flush()
for d in (d1, d2):
db_session.add(Node(
- label="Wall Plug", type="zwave_router", status="online",
- check_method="none", ieee_address="zwave-0xh-2", services=[],
- properties=[], design_id=d.id,
+ label="Wall Plug", type="zwave_router", device_id=drawn.id, design_id=d.id,
))
await db_session.commit()
@@ -486,12 +489,13 @@ async def test_persist_device_on_multiple_canvases(db_session) -> None:
await _persist_pending_import(db_session, bumped, _PENDING_EDGES)
nodes = (
- await db_session.execute(select(Node).where(Node.ieee_address == "zwave-0xh-2"))
+ await db_session.execute(select(Node).where(Node.device_id == drawn.id))
).scalars().all()
assert len(nodes) == 2 # both canvas placements preserved
- for n in nodes:
- model = {p["key"]: p["value"] for p in n.properties}.get("Model")
- assert model == "ZW200" # refreshed on every canvas
+ # One row behind them, so one refresh serves every canvas.
+ await db_session.refresh(drawn)
+ model = {p["key"]: p["value"] for p in drawn.properties}.get("Model")
+ assert model == "ZW200"
# ---------------------------------------------------------------------------
diff --git a/frontend/src/hooks/__tests__/useStatusPolling.test.ts b/frontend/src/hooks/__tests__/useStatusPolling.test.ts
index a558b42..4478153 100644
--- a/frontend/src/hooks/__tests__/useStatusPolling.test.ts
+++ b/frontend/src/hooks/__tests__/useStatusPolling.test.ts
@@ -113,22 +113,25 @@ describe('useStatusPolling', () => {
expect(ws.send).toHaveBeenCalledWith(JSON.stringify({ token: 'test-token' }))
})
- it('calls setNodeStatus with correct data on status message', () => {
+ it('lights up every node drawing the checked device', () => {
renderHook(() => useStatusPolling())
const ws = MockWebSocket.instances[0]
ws.onmessage?.({
data: JSON.stringify({
- node_id: 'node-1',
+ device_id: 'dev-1',
+ node_ids: ['node-1', 'node-2'],
status: 'online',
checked_at: '2024-01-01T12:00:00Z',
response_time_ms: 42,
}),
})
- expect(mockSetNodeStatus).toHaveBeenCalledWith('node-1', {
+ const expected = {
status: 'online',
response_time_ms: 42,
last_seen: '2024-01-01T12:00:00Z',
- })
+ }
+ expect(mockSetNodeStatus).toHaveBeenCalledWith('node-1', expected)
+ expect(mockSetNodeStatus).toHaveBeenCalledWith('node-2', expected)
})
it('sets last_seen to undefined when status is offline', () => {
@@ -136,7 +139,8 @@ describe('useStatusPolling', () => {
const ws = MockWebSocket.instances[0]
ws.onmessage?.({
data: JSON.stringify({
- node_id: 'node-1',
+ device_id: 'dev-1',
+ node_ids: ['node-1'],
status: 'offline',
checked_at: '2024-01-01T12:00:00Z',
}),
@@ -152,7 +156,9 @@ describe('useStatusPolling', () => {
renderHook(() => useStatusPolling())
const ws = MockWebSocket.instances[0]
ws.onmessage?.({
- data: JSON.stringify({ node_id: 'node-1', status: 'online', response_time_ms: null }),
+ data: JSON.stringify({
+ device_id: 'dev-1', node_ids: ['node-1'], status: 'online', response_time_ms: null,
+ }),
})
expect(mockSetNodeStatus).toHaveBeenCalledWith(
'node-1',
@@ -173,7 +179,9 @@ describe('useStatusPolling', () => {
const ws = MockWebSocket.instances[0]
const services = [{ port: 80, protocol: 'tcp', status: 'offline' }]
ws.onmessage?.({
- data: JSON.stringify({ type: 'service_status', node_id: 'node-9', services }),
+ data: JSON.stringify({
+ type: 'service_status', device_id: 'dev-9', node_ids: ['node-9'], services,
+ }),
})
expect(mockSetServiceStatuses).toHaveBeenCalledWith('node-9', services)
expect(mockSetNodeStatus).not.toHaveBeenCalled()
diff --git a/frontend/src/hooks/useStatusPolling.ts b/frontend/src/hooks/useStatusPolling.ts
index e6ccff4..f08c8dd 100644
--- a/frontend/src/hooks/useStatusPolling.ts
+++ b/frontend/src/hooks/useStatusPolling.ts
@@ -14,7 +14,11 @@ interface ServiceStatusEntry {
interface StatusMessage {
type?: string
- node_id?: string
+ /** The device that was checked. A device is checked once, however many
+ * canvases draw it. */
+ device_id?: string
+ /** The nodes drawing that device, as the backend knew them at check time. */
+ node_ids?: string[]
status?: 'online' | 'offline' | 'pending' | 'unknown'
checked_at?: string
response_time_ms?: number | null
@@ -25,6 +29,25 @@ interface StatusMessage {
const STANDALONE = import.meta.env.VITE_STANDALONE === 'true'
+/**
+ * The nodes a device-scoped message applies to.
+ *
+ * The backend sends the node ids it knew at check time; a node placed since
+ * then is resolved from the store by `device_id`, so a freshly approved device
+ * lights up on the first cycle rather than the second.
+ */
+function targets(msg: StatusMessage): string[] {
+ const known = msg.node_ids ?? []
+ if (!msg.device_id) return known
+ // `getState` is absent when the store is mocked; the message's own list is
+ // then the whole answer.
+ const nodes = useCanvasStore.getState?.()?.nodes ?? []
+ const fromStore = nodes
+ .filter((n) => n.data.device_id === msg.device_id)
+ .map((n) => n.id)
+ return [...new Set([...known, ...fromStore])]
+}
+
export function useStatusPolling() {
const wsRef = useRef<WebSocket | null>(null)
const { setNodeStatus, notifyScanDeviceFound, setServiceStatuses } = useCanvasStore()
@@ -54,16 +77,19 @@ export function useStatusPolling() {
const msg: StatusMessage = JSON.parse(event.data)
if (msg.type === 'scan_device_found') {
notifyScanDeviceFound()
- } else if (msg.type === 'service_status' && msg.node_id && msg.services) {
- setServiceStatuses(msg.node_id, msg.services)
- } else if (msg.node_id && msg.status) {
+ } else if (msg.type === 'service_status' && msg.services) {
+ for (const nodeId of targets(msg)) setServiceStatuses(nodeId, msg.services)
+ } else if (msg.status) {
// Live status is monitoring data, not a user edit — must not dirty the
// canvas (otherwise autosave rewrites an untouched canvas every cycle).
- setNodeStatus(msg.node_id, {
- status: msg.status,
- response_time_ms: msg.response_time_ms ?? undefined,
- last_seen: msg.status === 'online' ? msg.checked_at : undefined,
- })
+ // One check lights up every node drawing that device.
+ for (const nodeId of targets(msg)) {
+ setNodeStatus(nodeId, {
+ status: msg.status,
+ response_time_ms: msg.response_time_ms ?? undefined,
+ last_seen: msg.status === 'online' ? msg.checked_at : undefined,
+ })
+ }
}
} catch {
// ignore malformed messages
diff --git a/frontend/src/utils/__tests__/standaloneStorageDevices.test.ts b/frontend/src/utils/__tests__/standaloneStorageDevices.test.ts
new file mode 100644
index 0000000..ab6b29f
--- /dev/null
+++ b/frontend/src/utils/__tests__/standaloneStorageDevices.test.ts
@@ -0,0 +1,103 @@
+/**
+ * Standalone keeps the same split as the API: one inventory row per device, a
+ * node holding only the id of the row it draws. The blob carries both, and a
+ * load puts the facts back on the nodes so the canvas renders unchanged.
+ */
+import { describe, it, expect, beforeEach } from 'vitest'
+import type { Node } from '@xyflow/react'
+import type { NodeData } from '@/types'
+import { saveCanvas, loadCanvas, type StandaloneCanvas } from '@/utils/standaloneStorage'
+
+function node(id: string, data: Partial<NodeData> = {}): Node<NodeData> {
+ return {
+ id,
+ type: data.type ?? 'server',
+ position: { x: 0, y: 0 },
+ data: { label: id, type: 'server', status: 'unknown', services: [], ...data },
+ }
+}
+
+function stored(designId: string): StandaloneCanvas {
+ return JSON.parse(localStorage.getItem(`homelable_canvas:${designId}`) as string)
+}
+
+beforeEach(() => localStorage.clear())
+
+describe('standalone device rows', () => {
+ it('moves the device facts off the node and into a row', () => {
+ saveCanvas('d1', {
+ nodes: [node('n1', { ip: '10.0.0.5', services: [{ port: 22, protocol: 'tcp', service_name: 'ssh' }] })],
+ edges: [],
+ })
+
+ const blob = stored('d1')
+ expect(blob.devices).toHaveLength(1)
+ const [device] = blob.devices!
+ expect(device.ip).toBe('10.0.0.5')
+ expect(device.services).toHaveLength(1)
+ expect(blob.nodes[0].data.device_id).toBe(device.id)
+ })
+
+ it('puts them back on load', () => {
+ saveCanvas('d1', { nodes: [node('n1', { ip: '10.0.0.5', notes: 'garage' })], edges: [] })
+
+ const loaded = loadCanvas('d1')
+ expect(loaded?.nodes[0].data.ip).toBe('10.0.0.5')
+ expect(loaded?.nodes[0].data.notes).toBe('garage')
+ })
+
+ it('keeps one row for a device drawn twice', () => {
+ saveCanvas('d1', { nodes: [node('n1', { ip: '10.0.0.5' })], edges: [] })
+ const loaded = loadCanvas('d1')!
+ // Same device placed again on this canvas, pointing at the same row.
+ const deviceId = loaded.nodes[0].data.device_id
+ saveCanvas('d1', {
+ ...loaded,
+ nodes: [...loaded.nodes, node('n2', { ip: '10.0.0.5', device_id: deviceId })],
+ })
+
+ const blob = stored('d1')
+ expect(blob.devices).toHaveLength(1)
+ expect(blob.nodes.map((n) => n.data.device_id)).toEqual([deviceId, deviceId])
+ })
+
+ it('propagates an edit to every node drawing that device', () => {
+ saveCanvas('d1', { nodes: [node('n1', { ip: '10.0.0.5' })], edges: [] })
+ const loaded = loadCanvas('d1')!
+ const deviceId = loaded.nodes[0].data.device_id as string
+ saveCanvas('d1', {
+ ...loaded,
+ nodes: [
+ { ...loaded.nodes[0], data: { ...loaded.nodes[0].data, ip: '10.0.0.9' } },
+ node('n2', { ip: '10.0.0.5', device_id: deviceId }),
+ ],
+ })
+
+ // n2 was saved with the stale address; the row holds the last write and both
+ // nodes read it back.
+ const reloaded = loadCanvas('d1')!
+ expect(stored('d1').devices).toHaveLength(1)
+ expect(reloaded.nodes.every((n) => n.data.ip === reloaded.nodes[0].data.ip)).toBe(true)
+ })
+
+ it('gives canvas furniture no row', () => {
+ saveCanvas('d1', {
+ nodes: [node('z1', { type: 'groupRect', label: 'Zone' }), node('t1', { type: 'text' })],
+ edges: [],
+ })
+
+ const blob = stored('d1')
+ expect(blob.devices).toEqual([])
+ expect(blob.nodes.every((n) => n.data.device_id === undefined)).toBe(true)
+ })
+
+ it('reads a canvas saved before the split, whose nodes carry the facts inline', () => {
+ localStorage.setItem(
+ 'homelable_canvas:d1',
+ JSON.stringify({ nodes: [node('n1', { ip: '10.0.0.7' })], edges: [] }),
+ )
+
+ const loaded = loadCanvas('d1')
+ expect(loaded?.nodes[0].data.ip).toBe('10.0.0.7')
+ })
+})
diff --git a/frontend/src/utils/standaloneStorage.ts b/frontend/src/utils/standaloneStorage.ts
index 31b1e76..f3ad936 100644
--- a/frontend/src/utils/standaloneStorage.ts
+++ b/frontend/src/utils/standaloneStorage.ts
@@ -19,6 +19,7 @@ import type {
DesignType,
EdgeData,
InventoryDevice,
+ InventoryEntry,
NodeData,
Rack,
RackDevice,
@@ -49,10 +50,82 @@ export interface StandaloneCanvas {
edges: Edge<EdgeData>[]
theme_id?: ThemeId
custom_style?: CustomStyleDef | null
+ /**
+ * The Device Inventory for this canvas. Standalone has no backend, so the
+ * rows live with the canvas the way `StandaloneRackCanvas.inventory` does —
+ * but the split is the same one the API makes: a node points at a row through
+ * `device_id`, and the row owns the device facts. Absent on canvases saved
+ * before the split, whose nodes still carry those facts inline.
+ */
+ devices?: InventoryEntry[]
// NOTE: no floor plan here — floor plans need a backend to upload/serve the
// image, so they are disabled in standalone mode (see homelable/CLAUDE.md ADR).
}
+/** Node types that draw nothing physical, so they never get an inventory row. */
+const FURNITURE_TYPES = new Set(['group', 'groupRect', 'text'])
+
+/** Device fields the inventory row owns — everything a node no longer keeps. */
+const DEVICE_FIELDS = [
+ 'hostname', 'ip', 'mac', 'os', 'status', 'check_method', 'check_target',
+ 'services', 'notes', 'cpu_count', 'cpu_model', 'ram_gb', 'disk_gb',
+ 'show_hardware', 'properties', 'last_seen', 'last_scan', 'response_time_ms',
+] as const
+
+/**
+ * Split a canvas into nodes-plus-rows on the way to storage.
+ *
+ * Mirrors what the API does server-side, so a standalone canvas and a
+ * backed one carry the same shape: one row per device, the node keeping only
+ * the id of the row it draws.
+ */
+function splitDevices(data: StandaloneCanvas): StandaloneCanvas {
+ const devices: InventoryEntry[] = [...(data.devices ?? [])]
+ const byId = new Map(devices.map((d) => [d.id, d]))
+
+ const nodes = data.nodes.map((n) => {
+ if (FURNITURE_TYPES.has(n.data.type)) return n
+ const deviceId = n.data.device_id ?? generateUUID()
+ const previous = byId.get(deviceId)
+ const facts = Object.fromEntries(
+ DEVICE_FIELDS.filter((f) => n.data[f] !== undefined).map((f) => [f, n.data[f]]),
+ )
+ const row = {
+ ...(previous ?? { id: deviceId, discovered_at: new Date().toISOString(), status: 'approved' }),
+ ...facts,
+ id: deviceId,
+ label: n.data.label,
+ type: n.data.type,
+ } as InventoryEntry
+ if (previous) devices[devices.indexOf(previous)] = row
+ else devices.push(row)
+ byId.set(deviceId, row)
+ return { ...n, data: { ...n.data, device_id: deviceId } }
+ })
+
+ return { ...data, nodes, devices }
+}
+
+/** Put the device facts back on the nodes, so the canvas renders unchanged. */
+function hydrateDevices(data: StandaloneCanvas): StandaloneCanvas {
+ if (!data.devices?.length) return data
+ const byId = new Map(data.devices.map((d) => [d.id, d]))
+ return {
+ ...data,
+ nodes: data.nodes.map((n) => {
+ const row = n.data.device_id ? byId.get(n.data.device_id) : undefined
+ if (!row) return n
+ const facts = Object.fromEntries(
+ DEVICE_FIELDS.filter((f) => row[f] != null).map((f) => [f, row[f]]),
+ )
+ return {
+ ...n,
+ data: { ...n.data, ...facts, label: row.label ?? n.data.label, type: row.type ?? n.data.type },
+ } as Node<NodeData>
+ }),
+ }
+}
+
function readJSON<T>(key: string): T | null {
try {
const raw = localStorage.getItem(key)
@@ -182,9 +255,10 @@ export function saveRackCanvas(designId: string, data: StandaloneRackCanvas): vo
/** Load a design's canvas. Returns null when the design has never been saved. */
export function loadCanvas(designId: string): StandaloneCanvas | null {
- return readJSON<StandaloneCanvas>(canvasKey(designId))
+ const stored = readJSON<StandaloneCanvas>(canvasKey(designId))
+ return stored ? hydrateDevices(stored) : null
}
export function saveCanvas(designId: string, data: StandaloneCanvas): void {
- localStorage.setItem(canvasKey(designId), JSON.stringify(data))
+ localStorage.setItem(canvasKey(designId), JSON.stringify(splitDevices(data)))
}