Repository Public
1// Deterministic layout of the logical topology: infrastructure vertices (LAN gateway, WireGuard hub) in the2// centre column, nodes in a left and a right column. Same input → same picture: nothing depends on arrival order.3import type { Schemas } from "@/api/client";45type TNode = Schemas["TopologyNode"];6type TLink = Schemas["TopologyLink"];78export interface PlacedNode {9 id: string;10 label: string;11 kind: TNode["kind"];12 nodeId?: string;13 freshness?: Schemas["Freshness"];14 x: number;15 y: number;16 side: "left" | "right" | "center";17}18export interface PlacedLink {19 link: TLink;20 x1: number;21 y1: number;22 x2: number;23 y2: number;24 /** Point near the measuring (source) end where the latency / loss label is drawn. */25 lx: number;26 ly: number;27 anchor: "start" | "end" | "middle";28}29export interface TopologyLayout {30 width: number;31 height: number;32 nodes: PlacedNode[];33 links: PlacedLink[];34 /** Links whose source or target is not a known vertex (listed in the table, not drawn). */35 dangling: TLink[];36}3738export const LAYOUT = { width: 960, rowHeight: 34, padTop: 28, padBottom: 28, leftX: 170, rightX: 790, centerX: 480, minHeight: 220 } as const;3940const byLabel = (a: TNode, b: TNode) => a.label.localeCompare(b.label, "en", { numeric: true, sensitivity: "base" }) || a.id.localeCompare(b.id);41const HUB_ORDER: Record<string, number> = { lan_gateway: 0, wg_hub: 1 };4243export function layoutTopology(nodes: readonly TNode[], links: readonly TLink[]): TopologyLayout {44 const hubs = nodes.filter((n) => n.kind !== "node").sort((a, b) => (HUB_ORDER[a.kind] ?? 9) - (HUB_ORDER[b.kind] ?? 9) || byLabel(a, b));45 const leaves = nodes.filter((n) => n.kind === "node").sort(byLabel);46 const half = Math.ceil(leaves.length / 2);47 const left = leaves.slice(0, half);48 const right = leaves.slice(half);49 const rows = Math.max(left.length, right.length, hubs.length, 1);50 const inner = Math.max(LAYOUT.minHeight - LAYOUT.padTop - LAYOUT.padBottom, (rows - 1) * LAYOUT.rowHeight);51 const height = inner + LAYOUT.padTop + LAYOUT.padBottom;5253 // a column of k items is centred vertically; a single item sits in the middle54 const column = (count: number, i: number, spread: number) => LAYOUT.padTop + inner / 2 + (i - (count - 1) / 2) * spread;55 const hubSpread = hubs.length > 1 ? Math.min(inner / (hubs.length - 1), Math.max(LAYOUT.rowHeight * 3, inner / hubs.length)) : 0;5657 const place = (n: TNode, x: number, y: number, side: PlacedNode["side"]): PlacedNode => ({58 id: n.id, label: n.label, kind: n.kind, x, y: Math.round(y * 10) / 10, side,59 ...(n.node_id ? { nodeId: n.node_id } : {}),60 ...(n.freshness ? { freshness: n.freshness } : {}),61 });62 const placed: PlacedNode[] = [63 ...left.map((n, i) => place(n, LAYOUT.leftX, column(left.length, i, LAYOUT.rowHeight), "left")),64 ...hubs.map((n, i) => place(n, LAYOUT.centerX, column(hubs.length, i, hubSpread), "center")),65 ...right.map((n, i) => place(n, LAYOUT.rightX, column(right.length, i, LAYOUT.rowHeight), "right")),66 ];67 const at = new Map(placed.map((p) => [p.id, p]));6869 const drawn: PlacedLink[] = [];70 const dangling: TLink[] = [];71 for (const link of [...links].sort((a, b) => a.id.localeCompare(b.id))) {72 const s = at.get(link.source), t = at.get(link.target);73 if (!s || !t) {74 dangling.push(link);75 continue;76 }77 // labels sit a quarter of the way from the source so that the many links converging on a hub do not overlap78 const k = 0.24;79 drawn.push({80 link, x1: s.x, y1: s.y, x2: t.x, y2: t.y,81 lx: Math.round((s.x + (t.x - s.x) * k) * 10) / 10,82 ly: Math.round((s.y + (t.y - s.y) * k) * 10) / 10,83 anchor: s.x < t.x ? "start" : s.x > t.x ? "end" : "middle",84 });85 }86 return { width: LAYOUT.width, height, nodes: placed, links: drawn, dangling };87}88