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
@@ -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