Internet as a dependency: a node that can be needed, never judged down
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fca2dc0338
commit
7e625e7f42
11 files changed
+173
-22
No files matched your search
@@ -6,7 +6,7 @@ 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 { dependentsOf, impactOf, INTERNET_ID, 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";
|
||||
@@ -121,6 +121,8 @@ dependenciesRouter.get("/graph", asyncHandler(async (_req, res) => {
|
||||
if (type === "server") {
|
||||
const s = serverById.get(id);
|
||||
if (s) status = serverStatus(s, healthChecks.serverOfflineMinutes, now);
|
||||
} else if (type === "internet") {
|
||||
status = "unknown"; // not watched: nothing here judges the internet down
|
||||
} 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";
|
||||
@@ -131,7 +133,7 @@ dependenciesRouter.get("/graph", asyncHandler(async (_req, res) => {
|
||||
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" };
|
||||
return { key, type: type as "server" | "integration" | "device" | "internet", id, name: graph.names.get(key) ?? key, status, href: type === "server" ? `/servers/${id}` : type === "device" ? "/network/devices" : type === "internet" ? "/map?view=dependencies" : "/integrations" };
|
||||
})
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
res.json({ nodes, edges });
|
||||
@@ -141,6 +143,7 @@ dependenciesRouter.post("/", requireRole("operator"), asyncHandler(async (req, r
|
||||
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;
|
||||
if (d.fromType === "internet") return res.status(400).json({ error: "internet_needs_nothing", message: "The internet can't be made to need something; pick it as what a server, service or device needs." });
|
||||
const graph = await loadGraph();
|
||||
const from = nodeKey(d.fromType, d.fromId);
|
||||
const to = nodeKey(d.toType, d.toId);
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
* that can be down and be needed. (DNS providers raise no alerts of their own, so there is
|
||||
* nothing to fold.) An integration is judged per kind of service, as its health is: it is folded only if every enabled instance of that
|
||||
* kind is explained. A UPS is down while it runs on battery (services/upsSync.ts).
|
||||
*
|
||||
* "The internet" is a node too, so that what needs it (a cloud service, a VPS monitor, the router) can be drawn and found with "what depends on …?".
|
||||
* Nothing here judges it down — it is the one node that is never down — so it explains no alert; it is a thing to point at, not a thing that is watched.
|
||||
*/
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
@@ -20,9 +23,13 @@ import { upsOnBatteryIds } from "./upsSync.js";
|
||||
/** Where the health monitor keeps the problems it is announcing; read here so a service's alert can tell a server it needs is down. */
|
||||
export const HEALTH_STATE_FLAG = "healthActiveConditions";
|
||||
|
||||
export const nodeTypes = ["server", "integration", "device"] as const;
|
||||
export const nodeTypes = ["server", "integration", "device", "internet"] as const;
|
||||
export type NodeType = (typeof nodeTypes)[number];
|
||||
|
||||
/** There is one internet; its node is `internet:1`. */
|
||||
export const INTERNET_ID = 1;
|
||||
export const INTERNET_NAME = "Internet";
|
||||
|
||||
export const nodeKey = (type: NodeType, id: number) => `${type}:${id}`;
|
||||
|
||||
export interface DependencyEdge {
|
||||
@@ -142,6 +149,7 @@ export async function loadGraph(): Promise<Graph> {
|
||||
for (const s of serverRows) names.set(nodeKey("server", s.id), s.name);
|
||||
for (const i of intRows) names.set(nodeKey("integration", i.id), i.name);
|
||||
for (const d of deviceRows) names.set(nodeKey("device", d.id), d.name);
|
||||
names.set(nodeKey("internet", INTERNET_ID), INTERNET_NAME);
|
||||
const rows = depRows.map((r) => ({ id: r.id, fromType: r.fromType as NodeType, fromId: r.fromId, toType: r.toType as NodeType, toId: r.toId, createdAt: r.createdAt }));
|
||||
const instances = new Map<string, number[]>();
|
||||
for (const i of intRows) if (i.enabled) instances.set(i.type, [...(instances.get(i.type) ?? []), i.id]);
|
||||
@@ -197,8 +205,11 @@ export interface ImpactItem {
|
||||
silenced: boolean;
|
||||
}
|
||||
|
||||
/** What needs something is never the internet (it is only ever needed), so what is affected is one of the three that can be put in maintenance. */
|
||||
export type DependentItem = ImpactItem & { type: Exclude<NodeType, "internet"> };
|
||||
|
||||
/** What would be affected if `type`/`id` went down (everything that needs it) and what it needs itself. Null if it doesn't exist. */
|
||||
export async function impactOf(type: NodeType, id: number, now: Date = new Date()): Promise<{ target: { key: string; type: NodeType; id: number; name: string }; dependents: ImpactItem[]; needs: ImpactItem[] } | null> {
|
||||
export async function impactOf(type: NodeType, id: number, now: Date = new Date()): Promise<{ target: { key: string; type: NodeType; id: number; name: string }; dependents: DependentItem[]; needs: ImpactItem[] } | null> {
|
||||
const graph = await loadGraph();
|
||||
const key = nodeKey(type, id);
|
||||
const name = graph.names.get(key);
|
||||
@@ -211,7 +222,7 @@ export async function impactOf(type: NodeType, id: number, now: Date = new Date(
|
||||
};
|
||||
return {
|
||||
target: { key, type, id, name },
|
||||
dependents: dependentsOf(graph.edges, key).map((d) => item(d.key, d.distance)),
|
||||
dependents: dependentsOf(graph.edges, key).map((d) => item(d.key, d.distance) as DependentItem),
|
||||
needs: needsOfNode(graph.edges, key).map((k, i) => item(k, i + 1)),
|
||||
};
|
||||
}
|
||||
@@ -294,6 +305,7 @@ export async function integrationCausedBy(type: string, failingTypes: string[]):
|
||||
}
|
||||
|
||||
export async function nodeExists(type: NodeType, id: number): Promise<boolean> {
|
||||
if (type === "internet") return id === INTERNET_ID;
|
||||
if (type === "device") return (await db.select({ id: networkDevices.id }).from(networkDevices).where(eq(networkDevices.id, id)).limit(1)).length > 0;
|
||||
if (type === "server") return (await db.select({ id: servers.id }).from(servers).where(eq(servers.id, id)).limit(1)).length > 0;
|
||||
return (await db.select({ id: integrations.id }).from(integrations).where(eq(integrations.id, id)).limit(1)).length > 0;
|
||||
|
||||
Reference in new issue
Block a user