import { useEffect, useMemo, useState } from "react"; import { Link, useSearchParams } from "react-router-dom"; import DependencyMap from "../components/DependencyMap"; import DependencyEditor from "../views/DependencyEditor"; import type { CurrentUser } from "../api/client"; import { api, type MapData, type MapNode } from "../api/client"; import { ErrorNotice } from "../components/Notice"; import { PageSkeleton } from "../components/Skeleton"; import { readableError } from "../utils/errors"; const NODE_W = 210; const NODE_H = 46; const GAP_Y = 12; const COL_GAP = 130; const STATUS_SYMBOL: Record = { ok: "✓", bad: "✕", warn: "▲", unknown: "·" }; const STATUS_WORD: Record = { ok: "working", bad: "problem", warn: "check", unknown: "no status" }; const KIND_WORD: Record = { host: "Proxmox host", server: "Server", dns: "DNS name", monitor: "Uptime Kuma monitor" }; interface Placed extends MapNode { x: number; y: number; } /** Places each node: hosts, then servers, then names and monitors, each column ordered so the lines cross as little as is easy. */ function layout(data: MapData): { placed: Map; width: number; height: number; columns: { x: number; title: string }[] } { const neighbours = new Map(); for (const e of data.edges) { neighbours.set(e.from, [...(neighbours.get(e.from) ?? []), e.to]); neighbours.set(e.to, [...(neighbours.get(e.to) ?? []), e.from]); } const byKind = (kinds: MapNode["kind"][]) => data.nodes.filter((n) => kinds.includes(n.kind)); const hosts = byKind(["host"]).sort((a, b) => a.label.localeCompare(b.label)); const serverNodes = byKind(["server"]); const rightNodes = byKind(["dns", "monitor"]); // Servers grouped under their host, then those with none. const hostOf = (s: MapNode) => (neighbours.get(s.id) ?? []).find((id) => id.startsWith("host:")); const servers: MapNode[] = []; for (const h of hosts) servers.push(...serverNodes.filter((s) => hostOf(s) === h.id).sort((a, b) => a.label.localeCompare(b.label))); servers.push(...serverNodes.filter((s) => !hostOf(s)).sort((a, b) => a.label.localeCompare(b.label))); const step = NODE_H + GAP_Y; const placed = new Map(); const hasHosts = hosts.length > 0; const hasRight = rightNodes.length > 0; const colX = [0, hasHosts ? NODE_W + COL_GAP : 0, 0]; colX[2] = colX[1] + (servers.length > 0 ? NODE_W + COL_GAP : 0); servers.forEach((s, i) => placed.set(s.id, { ...s, x: colX[1], y: i * step })); // A column of nodes sits at the average height of the servers it connects to, pushed down as needed so none overlap. const stack = (nodes: MapNode[], x: number) => { const wanted = nodes.map((n) => { const ys = (neighbours.get(n.id) ?? []).map((id) => placed.get(id)?.y).filter((y): y is number => y !== undefined); return { n, y: ys.length ? ys.reduce((a, b) => a + b, 0) / ys.length : Number.MAX_SAFE_INTEGER / 2 }; }); wanted.sort((a, b) => a.y - b.y || a.n.label.localeCompare(b.n.label)); let next = 0; for (const { n, y } of wanted) { const at = Math.max(next, y >= Number.MAX_SAFE_INTEGER / 4 ? next : y); placed.set(n.id, { ...n, x, y: at }); next = at + step; } }; stack(hosts, colX[0]); stack(rightNodes, colX[2]); const all = [...placed.values()]; const height = all.length ? Math.max(...all.map((p) => p.y)) + NODE_H : 0; const width = Math.max(...all.map((p) => p.x), 0) + NODE_W; const columns = [ ...(hasHosts ? [{ x: colX[0], title: "Proxmox hosts" }] : []), ...(servers.length > 0 ? [{ x: colX[1], title: "Servers" }] : []), ...(hasRight ? [{ x: colX[2], title: "DNS names & monitors" }] : []), ]; return { placed, width, height, columns }; } /** The picture: how the servers, their Proxmox hosts, DNS names and monitors connect. Hover or tab to a box to light up its connections. */ export default function MapPage({ user }: { user: CurrentUser }) { const [params, setParams] = useSearchParams(); const view = params.get("view") === "dependencies" ? "dependencies" : "connections"; const choose = (v: "connections" | "dependencies") => setParams( (prev) => { const next = new URLSearchParams(prev); if (v === "connections") next.delete("view"); else next.set("view", v); return next; }, { replace: true }, ); return ( <>

Map

{view === "connections" ? ( ) : ( <>

A server that runs on another, a service that needs the server it is installed on. Pick a box to see what would go down with it before you reboot it.

Say what needs what

)} ); } function ConnectionsMap() { const [data, setData] = useState(null); const [error, setError] = useState(null); const [focus, setFocus] = useState(null); useEffect(() => { api.map .get() .then(setData) .catch((err) => setError(readableError(err))); }, []); const drawing = useMemo(() => (data ? layout(data) : null), [data]); const lit = useMemo(() => { if (!data || !focus) return null; const set = new Set([focus]); for (const e of data.edges) { if (e.from === focus) set.add(e.to); if (e.to === focus) set.add(e.from); } return set; }, [data, focus]); return ( <>

How things connect: which Proxmox host a server runs on, which DNS names point at it, and which Uptime Kuma monitors watch it. Hover or tab to a box to light up what it is connected to.

{data?.notes.map((n) => (
{n}
))} {!data && !error && } {data && drawing && data.nodes.length === 0 &&
Nothing to draw yet — add a server first.
} {data && drawing && data.nodes.length > 0 && (
{drawing.columns.map((c) => (
{c.title}
))}
{[...drawing.placed.values()].map((n) => { const dim = lit && !lit.has(n.id); const body = ( <> {n.label} {n.sub && {n.sub}} ); const props = { className: `map-node map-node-${n.kind} ${dim ? "map-dim" : ""}`, style: { left: n.x, top: n.y, width: NODE_W, height: NODE_H }, onMouseEnter: () => setFocus(n.id), onMouseLeave: () => setFocus((f) => (f === n.id ? null : f)), onFocus: () => setFocus(n.id), onBlur: () => setFocus((f) => (f === n.id ? null : f)), "aria-label": `${KIND_WORD[n.kind]}: ${n.label}${n.sub ? `, ${n.sub}` : ""}, ${STATUS_WORD[n.status]}`, }; return n.href ? ( {body} ) : (
{body}
); })}
✓ working ✕ problem or not reporting ▲ needs a look · no status DNS names appear where a record holds a server's own address; monitors where their target is the server's.
)} ); }