import { Router } from "express"; import { eq } from "drizzle-orm"; import { z } from "zod"; import { db } from "../db/client.js"; import { dependencies, networkDevices, servers } from "../db/schema.js"; import { requireAuth, requireRole } from "../auth/middleware.js"; import { recordAudit } from "../services/audit.js"; import { invalidateAlertsCache } from "../services/alerts.js"; import { dependentsOf, impactOf, loadGraph, nodeKey, nodeTypes, wouldLoop } from "../services/dependencies.js"; import { getSettings } from "../services/settingsStore.js"; import { loadDependencySuggestions, loadDismissedDependencies, saveDismissedDependencies } from "../services/dependencySuggestions.js"; import { getSourceStatus } from "../services/integrationHealthMonitor.js"; import { serverStatus, type MapStatus } from "../services/topology.js"; import { asyncHandler } from "../utils/asyncHandler.js"; export const dependenciesRouter = Router(); dependenciesRouter.use(requireAuth); const edgeSchema = z.object({ fromType: z.enum(nodeTypes), fromId: z.number().int().positive(), toType: z.enum(nodeTypes), toId: z.number().int().positive(), }); // Everything signed in people may see: what needs what, with the "runs on" links of servers alongside (those are set on the server itself). dependenciesRouter.get("/", asyncHandler(async (_req, res) => { const graph = await loadGraph(); const written = new Set(graph.rows.map((r) => `${nodeKey(r.fromType, r.fromId)}>${nodeKey(r.toType, r.toId)}`)); const runsOn: { from: string; to: string }[] = []; for (const [from, tos] of graph.edges) for (const to of tos) if (!written.has(`${from}>${to}`)) runsOn.push({ from, to }); const nodes = [...graph.names.entries()] .map(([key, name]) => { const [type, id] = key.split(":"); return { key, type, id: Number(id), name }; }) .sort((a, b) => a.type.localeCompare(b.type) || a.name.localeCompare(b.name)); res.json({ dependencies: graph.rows .filter((r) => graph.names.has(nodeKey(r.fromType, r.fromId)) && graph.names.has(nodeKey(r.toType, r.toId))) .map((r) => ({ id: r.id, from: { type: r.fromType, id: r.fromId, name: graph.names.get(nodeKey(r.fromType, r.fromId))! }, to: { type: r.toType, id: r.toId, name: graph.names.get(nodeKey(r.toType, r.toId))! }, createdAt: r.createdAt })), runsOn: runsOn.map((r) => ({ from: { key: r.from, name: graph.names.get(r.from) ?? r.from }, to: { key: r.to, name: graph.names.get(r.to) ?? r.to } })), nodes, }); })); // Dependencies worth writing, from what the app already knows (an integration reached at a server's own address). Everyone signed in can see them; operators // add or dismiss them, as they would any dependency. dependenciesRouter.get("/suggestions", asyncHandler(async (_req, res) => { res.json(await loadDependencySuggestions()); })); const keysSchema = z.object({ keys: z.array(z.string().min(1).max(100)).min(1).max(200) }); dependenciesRouter.post("/suggestions/add", requireRole("operator"), asyncHandler(async (req, res) => { const parsed = keysSchema.safeParse(req.body); if (!parsed.success) return res.status(400).json({ error: "invalid_body" }); const { suggestions } = await loadDependencySuggestions(); const wanted = new Set(parsed.data.keys); const added: { id: number; from: string; to: string }[] = []; for (const s of suggestions.filter((x) => wanted.has(x.key))) { const [row] = await db.insert(dependencies).values({ fromType: "integration", fromId: s.from.id, toType: "server", toId: s.to.id }).returning(); await recordAudit({ actor: req.currentUser!, category: "dependency", action: "create", targetType: "dependency", targetId: row.id, detail: { from: s.from.name, to: s.to.name, fromKey: `integration:${s.from.id}`, toKey: `server:${s.to.id}`, suggested: true } }); added.push({ id: row.id, from: s.from.name, to: s.to.name }); } if (added.length > 0) invalidateAlertsCache(); res.json({ added, ...(await loadDependencySuggestions()) }); })); dependenciesRouter.post("/suggestions/dismiss", requireRole("operator"), asyncHandler(async (req, res) => { const parsed = keysSchema.safeParse(req.body); if (!parsed.success) return res.status(400).json({ error: "invalid_body" }); const dismissed = await loadDismissedDependencies(); for (const k of parsed.data.keys) dismissed.add(k); await saveDismissedDependencies(dismissed); res.json(await loadDependencySuggestions()); })); dependenciesRouter.post("/suggestions/restore", requireRole("operator"), asyncHandler(async (_req, res) => { await saveDismissedDependencies(new Set()); res.json(await loadDependencySuggestions()); })); // What a thing would take with it, and what it needs itself: for the maintenance and update pages, which say so before it is done. dependenciesRouter.get("/impact", asyncHandler(async (req, res) => { const type = nodeTypes.find((t) => t === req.query.type); const id = typeof req.query.id === "string" && /^\d+$/.test(req.query.id) ? Number(req.query.id) : NaN; if (!type || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_query", message: "Say which server or integration." }); const impact = await impactOf(type, id); if (!impact) return res.status(404).json({ error: "not_found" }); res.json(impact); })); // The graph for the Map page: the servers and integrations that take part in any dependency, how each is doing, and who needs whom. An edge runs // from the thing that is needed to the thing that needs it, so the foundations come first. dependenciesRouter.get("/graph", asyncHandler(async (_req, res) => { const graph = await loadGraph(); const written = new Set(graph.rows.map((r) => `${nodeKey(r.fromType, r.fromId)}>${nodeKey(r.toType, r.toId)}`)); const edges: { from: string; to: string; kind: "written" | "runs-on" | "uplink" }[] = []; const taking = new Set(); for (const [needing, needed] of graph.edges) { for (const n of needed) { // A device connected through another is an uplink (the controller says so); a server's "runs on" is the other kind of link nobody wrote. edges.push({ from: n, to: needing, kind: written.has(`${needing}>${n}`) ? "written" : n.startsWith("device:") ? "uplink" : "runs-on" }); taking.add(needing); taking.add(n); } } const { healthChecks } = await getSettings(); const now = new Date(); const serverRows = await db.select({ id: servers.id, lastSeenAt: servers.lastSeenAt, thresholds: servers.thresholds }).from(servers); const serverById = new Map(serverRows.map((s) => [s.id, s])); const intById = new Map(graph.intRows.map((i) => [i.id, i])); const deviceById = new Map((await db.select({ id: networkDevices.id, state: networkDevices.state }).from(networkDevices)).map((d) => [d.id, d])); const nodes = [...taking] .map((key) => { const [type, rawId] = key.split(":"); const id = Number(rawId); let status: MapStatus = "unknown"; if (type === "server") { const s = serverById.get(id); if (s) status = serverStatus(s, healthChecks.serverOfflineMinutes, now); } else if (type === "device") { const d = deviceById.get(id); if (d) status = d.state === "online" ? "ok" : d.state === "offline" ? "bad" : d.state === "updating" ? "warn" : "unknown"; } else { const i = intById.get(id); if (i?.enabled) { const st = getSourceStatus(i.type).status; status = st === "failing" ? "bad" : st === "ok" ? "ok" : "unknown"; } } return { key, type: type as "server" | "integration" | "device", id, name: graph.names.get(key) ?? key, status, href: type === "server" ? `/servers/${id}` : type === "device" ? "/network/devices" : "/integrations" }; }) .sort((a, b) => a.name.localeCompare(b.name)); res.json({ nodes, edges }); })); dependenciesRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => { const parsed = edgeSchema.safeParse(req.body); if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say what needs what: a server or an integration each.", details: parsed.error.flatten() }); const d = parsed.data; const graph = await loadGraph(); const from = nodeKey(d.fromType, d.fromId); const to = nodeKey(d.toType, d.toId); if (!graph.names.has(from) || !graph.names.has(to)) return res.status(400).json({ error: "unknown_node", message: "One of those doesn't exist." }); if (from === to) return res.status(400).json({ error: "self", message: "Something can't depend on itself." }); if (graph.edges.get(from)?.includes(to)) return res.status(409).json({ error: "duplicate", message: "That is already so." }); if (wouldLoop(graph.edges, from, to)) { return res.status(409).json({ error: "loop", message: `${graph.names.get(to)} already depends on ${graph.names.get(from)}, so this would make them wait for each other.` }); } const [row] = await db.insert(dependencies).values(d).returning(); await recordAudit({ actor: req.currentUser!, category: "dependency", action: "create", targetType: "dependency", targetId: row.id, detail: { from: graph.names.get(from), to: graph.names.get(to), fromKey: from, toKey: to } }); invalidateAlertsCache(); res.status(201).json({ id: row.id }); })); dependenciesRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => { const id = /^\d+$/.test(req.params.id) ? Number(req.params.id) : NaN; if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" }); const [before] = await db.select().from(dependencies).where(eq(dependencies.id, id)).limit(1); if (!before) return res.status(404).json({ error: "not_found" }); const graph = await loadGraph(); await db.delete(dependencies).where(eq(dependencies.id, id)); await recordAudit({ actor: req.currentUser!, category: "dependency", action: "delete", targetType: "dependency", targetId: id, detail: { from: graph.names.get(nodeKey(before.fromType as "server", before.fromId)) ?? `${before.fromType} #${before.fromId}`, to: graph.names.get(nodeKey(before.toType as "server", before.toId)) ?? `${before.toType} #${before.toId}`, fromKey: `${before.fromType}:${before.fromId}`, toKey: `${before.toType}:${before.toId}` }, }); invalidateAlertsCache(); res.status(204).end(); }));