Skip to content

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

fix(docs): read the hardware table out of the device properties

Pouzor079098cdda22
diff --git a/backend/app/services/doc_template.py b/backend/app/services/doc_template.py
index 7513f4c..2c5e3ae 100644
--- a/backend/app/services/doc_template.py
+++ b/backend/app/services/doc_template.py
@@ -162,20 +162,76 @@ def block_device_info(device: Any, **_: Any) -> str:
return "\n".join(lines)
+# What the hardware table looks for in `properties`, in order of preference.
+# The scanner and the Proxmox import write these keys ("CPU Cores", "RAM",
+# "Disk"); a user typing their own is likely to reach for one of the synonyms.
+# Matched case-insensitively, so "ram" and "RAM" are the same key.
+_HARDWARE_KEYS: dict[str, tuple[str, ...]] = {
+ "cpu_model": ("cpu model", "cpu", "processor"),
+ "cpu_count": ("cpu cores", "cores", "vcpu", "vcpus", "cpu count"),
+ "ram": ("ram", "memory", "mem"),
+ "disk": ("disk", "storage", "disk size", "capacity"),
+}
+
+
+def property_map(device: Any) -> dict[str, str]:
+ """A device's properties as a lowercased key -> value lookup.
+
+ The first occurrence of a key wins, matching how the tables read: a device
+ carrying two "RAM" rows is showing the first one.
+ """
+ found: dict[str, str] = {}
+ for entry in getattr(device, "properties", None) or []:
+ if not isinstance(entry, dict):
+ continue
+ key = entry.get("key")
+ value = entry.get("value")
+ if not key or value in (None, ""):
+ continue
+ found.setdefault(str(key).strip().lower(), str(value).strip())
+ return found
+
+
+def _hardware_property(properties: dict[str, str], field: str) -> str | None:
+ for candidate in _HARDWARE_KEYS[field]:
+ if candidate in properties:
+ return properties[candidate]
+ return None
+
+
def block_hardware(device: Any, **_: Any) -> str:
- cpu_model = getattr(device, "cpu_model", None)
- cpu_count = getattr(device, "cpu_count", None)
+ """CPU / RAM / Disk, read from `properties` first.
+
+ The `cpu_count` / `cpu_model` / `ram_gb` / `disk_gb` columns are barely
+ written any more — the scanner, the Proxmox import and the inventory modal
+ all put hardware in `properties` instead — so a document generated from the
+ columns alone printed an empty table beside a properties table listing the
+ very same facts. Properties win; the columns are the fallback for a row that
+ still carries them.
+
+ A property value is taken verbatim: it is already formatted the way the user
+ or the importer wrote it ("15.5 GB"), and guessing a unit onto a bare number
+ would be inventing a fact.
+ """
+ properties = property_map(device)
+
+ cpu_model = _hardware_property(properties, "cpu_model") or getattr(device, "cpu_model", None)
+ cpu_count = _hardware_property(properties, "cpu_count") or getattr(device, "cpu_count", None)
if cpu_model and cpu_count:
cpu: Any = f"{cpu_model} ×{cpu_count}"
else:
cpu = cpu_model or (f"{cpu_count} cores" if cpu_count else None)
- ram = getattr(device, "ram_gb", None)
- disk = getattr(device, "disk_gb", None)
+
+ ram_gb = getattr(device, "ram_gb", None)
+ ram = _hardware_property(properties, "ram") or (f"{ram_gb:g} GB" if ram_gb else None)
+ disk_gb = getattr(device, "disk_gb", None)
+ disk = _hardware_property(properties, "disk") or (f"{disk_gb:g} GB" if disk_gb else None)
+
return "\n".join(
[
"| CPU | RAM | Disk |",
"|---|---|---|",
- f"| {cell(cpu)} | {cell(f'{ram:g} GB' if ram else None)} | {cell(f'{disk:g} GB' if disk else None)} |",
+ f"| {cell(cpu)} | {cell(ram)} | {cell(disk)} |",
]
)
@@ -303,6 +359,15 @@ def facts_snapshot(device: Any) -> dict[str, Any]:
"cpu_model": getattr(device, "cpu_model", None),
"ram_gb": getattr(device, "ram_gb", None),
"disk_gb": getattr(device, "disk_gb", None),
+ # The properties table prints these, and the hardware table now reads
+ # CPU / RAM / Disk out of them — so an edit to one is a real change to
+ # what the document says. A plain dict, because the snapshot is compared
+ # against its own JSON round-trip and a tuple would come back a list.
+ "properties": {
+ str(entry["key"]): str(entry.get("value") or "")
+ for entry in reversed(getattr(device, "properties", None) or [])
+ if isinstance(entry, dict) and entry.get("key")
+ },
"check_method": getattr(device, "check_method", None),
"check_target": getattr(device, "check_target", None),
"services": sorted(service_key(s) for s in services if isinstance(s, dict)),
diff --git a/backend/tests/test_doc_template.py b/backend/tests/test_doc_template.py
index c70af8d..023c95c 100644
--- a/backend/tests/test_doc_template.py
+++ b/backend/tests/test_doc_template.py
@@ -95,6 +95,128 @@ def test_hardware_block_handles_a_count_without_a_model():
assert "4 cores" in t.block_hardware(_device(cpu_model=None))
+# ── hardware comes out of `properties` first ────────────────────────────────
+
+# What the Proxmox import and the scanner actually write.
+_PROXMOX_PROPERTIES = [
+ {"key": "VMID", "value": "108"},
+ {"key": "Kind", "value": "LXC"},
+ {"key": "CPU Cores", "value": "4"},
+ {"key": "RAM", "value": "6.1 GB"},
+ {"key": "Disk", "value": "50.0 GB"},
+]
+
+
+def _from_properties(**overrides):
+ """A row as the importers leave it: hardware in properties, columns empty."""
+ return _device(
+ **{
+ "properties": _PROXMOX_PROPERTIES,
+ "cpu_count": None,
+ "cpu_model": None,
+ "ram_gb": None,
+ "disk_gb": None,
+ **overrides,
+ }
+ )
+
+
+def test_hardware_block_reads_the_properties_when_the_columns_are_empty():
+ """The regression: an empty hardware table beside a full properties one.
+
+ `cpu_count` / `ram_gb` / `disk_gb` are barely written any more — the same
+ facts arrive as properties — so the table printed three em dashes next to a
+ properties table listing all three.
+ """
+ md = t.block_hardware(_from_properties())
+ assert "| 4 cores | 6.1 GB | 50.0 GB |" in md
+ assert "—" not in md
+
+
+def test_hardware_block_takes_a_property_value_verbatim():
+ # "6.1 GB" is already formatted; appending a unit would say "6.1 GB GB", and
+ # guessing one onto a bare number would be inventing a fact.
+ md = t.block_hardware(_from_properties(properties=[{"key": "RAM", "value": "12288 MB"}]))
+ assert "| 12288 MB |" in md
+
+
+def test_hardware_block_matches_property_keys_whatever_their_case():
+ md = t.block_hardware(_from_properties(properties=[{"key": "ram", "value": "8 GB"}]))
+ assert "8 GB" in md
+
+
+def test_hardware_block_accepts_the_common_synonyms():
+ md = t.block_hardware(
+ _from_properties(
+ properties=[
+ {"key": "Processor", "value": "Xeon E3-1220"},
+ {"key": "vCPU", "value": "8"},
+ {"key": "Memory", "value": "64 GB"},
+ {"key": "Storage", "value": "4 TB"},
+ ]
+ )
+ )
+ assert "| Xeon E3-1220 ×8 | 64 GB | 4 TB |" in md
+
+
+def test_hardware_block_prefers_a_property_over_the_legacy_column():
+ md = t.block_hardware(_device(properties=[{"key": "RAM", "value": "64 GB"}]))
+ assert "64 GB" in md
+ assert "20 GB" not in md
+
+
+def test_hardware_block_still_falls_back_to_the_columns():
+ # A row that predates the move, or one typed into the inventory modal.
+ md = t.block_hardware(_device(properties=[]))
+ assert "Intel Celeron J4125 ×4" in md
+ assert "20 GB" in md
+
+
+def test_hardware_block_is_empty_when_neither_source_has_anything():
+ md = t.block_hardware(
+ _device(properties=[], cpu_count=None, cpu_model=None, ram_gb=None, disk_gb=None)
+ )
+ assert "| — | — | — |" in md
+
+
+def test_hardware_block_ignores_a_property_with_no_value():
+ md = t.block_hardware(_from_properties(properties=[{"key": "RAM", "value": ""}]))
+ assert "| — | — | — |" in md
+
+
+def test_property_map_keeps_the_first_of_a_repeated_key():
+ device = _device(properties=[{"key": "RAM", "value": "first"}, {"key": "RAM", "value": "second"}])
+ assert t.property_map(device)["ram"] == "first"
+
+
+# ── the snapshot follows what the tables print ──────────────────────────────
+
+
+def test_the_snapshot_carries_the_properties():
+ snapshot = t.facts_snapshot(_from_properties())
+ assert snapshot["properties"]["RAM"] == "6.1 GB"
+
+
+def test_editing_a_property_is_drift():
+ before = t.facts_snapshot(_from_properties())
+ after = t.facts_snapshot(
+ _from_properties(properties=[*_PROXMOX_PROPERTIES[:3], {"key": "RAM", "value": "12 GB"}])
+ )
+ assert before != after
+
+
+def test_the_snapshot_survives_its_json_round_trip():
+ """It is stored as JSON and compared against a freshly computed one.
+
+ A tuple or a set would come back a list and every document would read as
+ drifted forever, so the shape has to be JSON-native on both sides.
+ """
+ import json
+
+ snapshot = t.facts_snapshot(_from_properties())
+ assert json.loads(json.dumps(snapshot)) == snapshot
+
+
def test_services_block_writes_one_section_per_service():
md = t.block_services(_device())
assert "### Synology DSM — `5000/tcp`" in md