UniFi: servers plugged into switches; server page in sections; My work; settings search and undo
UniFi: the controller's client list is matched to servers by IP address, so a server is connected through a switch or access point (and port). That is a dependency made by itself, shown on the server page, the Network devices page and the Connections map. IP Addresses can sync the devices' addresses. A server's page is split into Overview, Health, Network, History, Tasks and Settings, by ?view=; alerts link to the section they are about. My work lists what has your name on it: alerts you have, maintenance you started (extend, end with undo) and update runs you started, with a badge. Settings has a search box (and the command palette lists every setting). Removing a dependency, admin link, runbook, hand-made IP address, port opening or restore test is undone from the message instead of asked about first. Two backup tests used a fixed date and failed as days passed; they follow the real clock now. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
396e074108
commit
5e26e77340
45 files changed
+6224
-194
No files matched your search
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE `network_server_links` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`integration_id` integer NOT NULL,
|
||||
`server_id` integer NOT NULL,
|
||||
`device_external_id` text NOT NULL,
|
||||
`client_mac` text NOT NULL,
|
||||
`port` integer,
|
||||
`wired` integer DEFAULT true NOT NULL,
|
||||
`updated_at` text NOT NULL,
|
||||
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade,
|
||||
FOREIGN KEY (`server_id`) REFERENCES `servers`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `network_server_links_unique` ON `network_server_links` (`integration_id`,`server_id`,`client_mac`);
|
||||
File diff suppressed because it is too large.
Load diff
@@ -316,6 +316,13 @@
|
||||
"when": 1792447584218,
|
||||
"tag": "0044_sparkling_scourge",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 45,
|
||||
"version": "6",
|
||||
"when": 1792516305790,
|
||||
"tag": "0045_military_exodus",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -46,6 +46,7 @@ import { portsRouter } from "./routes/ports.js";
|
||||
import { alertsRouter } from "./routes/alerts.js";
|
||||
import { mapRouter } from "./routes/map.js";
|
||||
import { networkRouter } from "./routes/network.js";
|
||||
import { myWorkRouter } from "./routes/myWork.js";
|
||||
import { generatorRouter } from "./routes/generator.js";
|
||||
import { alertTicketsRouter } from "./routes/alertTickets.js";
|
||||
import { avatarsRouter } from "./routes/avatars.js";
|
||||
@@ -163,6 +164,7 @@ export function createApp() {
|
||||
app.use("/api/alerts", alertsRouter);
|
||||
app.use("/api/map", mapRouter);
|
||||
app.use("/api/network", networkRouter);
|
||||
app.use("/api/my-work", myWorkRouter);
|
||||
app.use("/api/generator", generatorRouter);
|
||||
app.use("/api/alert-tickets", alertTicketsRouter);
|
||||
|
||||
|
||||
@@ -564,6 +564,27 @@ export const networkDevices = sqliteTable(
|
||||
(t) => [uniqueIndex("network_devices_unique").on(t.integrationId, t.externalId)],
|
||||
);
|
||||
|
||||
// Which server is plugged into which network device, worked out on every read from what the controller says is connected (matched by the server's IP address).
|
||||
// A server needs the device it is connected through, as far as the dependency map is concerned.
|
||||
export const networkServerLinks = sqliteTable(
|
||||
"network_server_links",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
integrationId: integer("integration_id")
|
||||
.notNull()
|
||||
.references(() => integrations.id, { onDelete: "cascade" }),
|
||||
serverId: integer("server_id")
|
||||
.notNull()
|
||||
.references(() => servers.id, { onDelete: "cascade" }),
|
||||
deviceExternalId: text("device_external_id").notNull(), // the MAC of the switch or access point
|
||||
clientMac: text("client_mac").notNull(),
|
||||
port: integer("port"), // the switch port, when the controller says (the classic API does)
|
||||
wired: integer("wired", { mode: "boolean" }).notNull().default(true),
|
||||
updatedAt: text("updated_at").notNull(),
|
||||
},
|
||||
(t) => [uniqueIndex("network_server_links_unique").on(t.integrationId, t.serverId, t.clientMac)],
|
||||
);
|
||||
|
||||
// The networks (VLANs) and Wi-Fi names (SSIDs) a UniFi controller has set up, kept beside the devices. Passphrases are never read.
|
||||
export const networkSegments = sqliteTable(
|
||||
"network_segments",
|
||||
|
||||
@@ -75,12 +75,27 @@ export interface UnifiSsid {
|
||||
siteName: string;
|
||||
}
|
||||
|
||||
/** Something connected to the network (a computer, a phone, a server), and the device it is connected through. */
|
||||
export interface UnifiClient {
|
||||
mac: string;
|
||||
ip: string | null;
|
||||
name: string | null;
|
||||
wired: boolean;
|
||||
/** The MAC of the switch or access point it is on, when the controller says. */
|
||||
uplinkExternalId: string | null;
|
||||
/** The switch port, for a wired client on the classic API. */
|
||||
port: number | null;
|
||||
siteName: string;
|
||||
}
|
||||
|
||||
export interface UnifiInventory {
|
||||
mode: "api_key" | "login";
|
||||
sites: string[];
|
||||
devices: UnifiDevice[];
|
||||
networks: UnifiNetwork[];
|
||||
ssids: UnifiSsid[];
|
||||
/** Everything connected right now; [] when the controller would not say (an account or key that may not read clients). */
|
||||
clients: UnifiClient[];
|
||||
}
|
||||
|
||||
export interface UnifiAdapter {
|
||||
@@ -236,6 +251,22 @@ export function networkFromLegacy(raw: unknown, siteName: string): UnifiNetwork
|
||||
};
|
||||
}
|
||||
|
||||
export function clientFromApi(raw: unknown, macById: Map<string, string>, siteName: string): UnifiClient | null {
|
||||
const d = obj(raw);
|
||||
const mac = normaliseMac(d.macAddress);
|
||||
if (!mac) return null;
|
||||
const uplinkId = str(d.uplinkDeviceId);
|
||||
return { mac, ip: str(d.ipAddress), name: str(d.name), wired: String(d.type ?? "").toUpperCase() === "WIRED", uplinkExternalId: uplinkId ? (macById.get(uplinkId) ?? null) : null, port: null, siteName };
|
||||
}
|
||||
|
||||
export function clientFromLegacy(raw: unknown, siteName: string): UnifiClient | null {
|
||||
const d = obj(raw);
|
||||
const mac = normaliseMac(d.mac);
|
||||
if (!mac) return null;
|
||||
const wired = d.is_wired === true;
|
||||
return { mac, ip: str(d.ip), name: str(d.name) ?? str(d.hostname), wired, uplinkExternalId: normaliseMac(wired ? d.sw_mac : d.ap_mac), port: wired ? num(d.sw_port) : null, siteName };
|
||||
}
|
||||
|
||||
const API_SECURITY: Record<string, string> = { OPEN: "Open", WPA2_PERSONAL: "WPA2", WPA3_PERSONAL: "WPA3", WPA2_WPA3_PERSONAL: "WPA2/WPA3", WPA2_ENTERPRISE: "WPA2 Enterprise", WPA3_ENTERPRISE: "WPA3 Enterprise" };
|
||||
|
||||
export function ssidFromApi(raw: unknown, networkNames: Map<string, string>, siteName: string): UnifiSsid | null {
|
||||
@@ -406,7 +437,7 @@ export function createUnifiAdapter(config: UnifiConfig): UnifiAdapter {
|
||||
sites = sites.filter((s) => s.name.toLowerCase() === wantSite);
|
||||
if (sites.length === 0) throw new Error(`UniFi has no site called “${config.site}”`);
|
||||
}
|
||||
const inventory: UnifiInventory = { mode: "api_key", sites: sites.map((s) => s.name), devices: [], networks: [], ssids: [] };
|
||||
const inventory: UnifiInventory = { mode: "api_key", sites: sites.map((s) => s.name), devices: [], networks: [], ssids: [], clients: [] };
|
||||
for (const site of sites) {
|
||||
const base = `/sites/${site.id}`;
|
||||
const entries = await apiPaged(prefix, `${base}/devices`);
|
||||
@@ -428,6 +459,12 @@ export function createUnifiAdapter(config: UnifiConfig): UnifiAdapter {
|
||||
if (d) inventory.devices.push(d);
|
||||
});
|
||||
|
||||
// What is connected where. A key that may not read clients costs only the links from servers to the devices they are plugged into.
|
||||
for (const raw of await apiPaged(prefix, `${base}/clients`).catch(() => [])) {
|
||||
const c = clientFromApi(raw, macById, site.name);
|
||||
if (c) inventory.clients.push(c);
|
||||
}
|
||||
|
||||
const netEntries = await apiPaged(prefix, `${base}/networks`).catch(() => []);
|
||||
const netDetails = await pooled(netEntries, async (e) => {
|
||||
const id = str(obj(e).id);
|
||||
@@ -508,12 +545,16 @@ export function createUnifiAdapter(config: UnifiConfig): UnifiAdapter {
|
||||
sites = sites.filter((s) => s.name.toLowerCase() === wantSite || s.key.toLowerCase() === wantSite);
|
||||
if (sites.length === 0) throw new Error(`UniFi has no site called “${config.site}”`);
|
||||
}
|
||||
const inventory: UnifiInventory = { mode: "login", sites: sites.map((s) => s.name), devices: [], networks: [], ssids: [] };
|
||||
const inventory: UnifiInventory = { mode: "login", sites: sites.map((s) => s.name), devices: [], networks: [], ssids: [], clients: [] };
|
||||
for (const site of sites) {
|
||||
for (const raw of await legacyGet(session, `/api/s/${site.key}/stat/device`)) {
|
||||
const d = deviceFromLegacy(raw, site.name);
|
||||
if (d) inventory.devices.push(d);
|
||||
}
|
||||
for (const raw of await legacyGet(session, `/api/s/${site.key}/stat/sta`).catch(() => [])) {
|
||||
const c = clientFromLegacy(raw, site.name);
|
||||
if (c) inventory.clients.push(c);
|
||||
}
|
||||
const networkNames = new Map<string, string>();
|
||||
for (const raw of await legacyGet(session, `/api/s/${site.key}/rest/networkconf`).catch(() => [])) {
|
||||
const n = networkFromLegacy(raw, site.name);
|
||||
|
||||
@@ -64,7 +64,7 @@ dependenciesRouter.get("/graph", asyncHandler(async (_req, res) => {
|
||||
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" : needing.startsWith("device:") && n.startsWith("device:") ? "uplink" : "runs-on" });
|
||||
edges.push({ from: n, to: needing, kind: written.has(`${needing}>${n}`) ? "written" : n.startsWith("device:") ? "uplink" : "runs-on" });
|
||||
taking.add(needing);
|
||||
taking.add(n);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { isIP } from "node:net";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { ipamEntries, integrations, dnsRecordsCache } from "../db/schema.js";
|
||||
import { ipamEntries, integrations, dnsRecordsCache, networkDevices } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -290,6 +290,32 @@ ipamRouter.post("/sync-proxmox", requireRole("operator"), asyncHandler(async (re
|
||||
res.json({ added, updated, skipped, skippedIps, errors });
|
||||
}));
|
||||
|
||||
ipamRouter.post("/sync-unifi", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const controllers = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "unifi"), eq(integrations.enabled, true)));
|
||||
if (controllers.length === 0) {
|
||||
return res.status(400).json({ error: "no_unifi_integration" });
|
||||
}
|
||||
let added = 0;
|
||||
let updated = 0;
|
||||
let skipped = 0;
|
||||
const skippedIps: string[] = [];
|
||||
// What was last read from the controllers (the page's own copy), not a new call: a device is as current as the Network devices page says.
|
||||
for (const d of await db.select().from(networkDevices)) {
|
||||
if (!d.ip) continue;
|
||||
const kind = d.kind.replace("_", " ");
|
||||
const notes = [`${kind}${d.model ? `, ${d.model}` : ""}`, d.mac ? `MAC ${d.mac}` : null].filter((v): v is string => !!v).join(" — ");
|
||||
const result = await upsertSyncedEntry(d.ip, d.name, "UniFi", notes, d.siteName && d.siteName !== "Default" ? d.siteName : null, "unifi");
|
||||
if (result === "added") added++;
|
||||
else if (result === "updated") updated++;
|
||||
else {
|
||||
skipped++;
|
||||
skippedIps.push(d.ip);
|
||||
}
|
||||
}
|
||||
await recordAudit({ actor: req.currentUser!, category: "ipam", action: "sync_unifi", detail: { added, updated, skipped } });
|
||||
res.json({ added, updated, skipped, skippedIps, errors: [] });
|
||||
}));
|
||||
|
||||
ipamRouter.post("/sync-phpipam", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const phpIpamIntegrations = await db
|
||||
.select()
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Router } from "express";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { buildMyWork } from "../services/myWork.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const myWorkRouter = Router();
|
||||
|
||||
myWorkRouter.use(requireAuth);
|
||||
|
||||
// What the signed-in person has taken on. Only ever their own: the answer is worked out from who is asking.
|
||||
myWorkRouter.get("/", asyncHandler(async (req, res) => {
|
||||
const user = req.currentUser!;
|
||||
res.json(await buildMyWork({ id: user.id, name: user.name ?? null, email: user.email ?? null, oidcSub: user.oidcSub }));
|
||||
}));
|
||||
@@ -2,7 +2,7 @@ import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, networkDevices, networkSegments } from "../db/schema.js";
|
||||
import { integrations, networkDevices, networkSegments, networkServerLinks, servers } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { dependentsOf, loadGraph, needsOfNode, nodeKey } from "../services/dependencies.js";
|
||||
@@ -19,17 +19,31 @@ networkRouter.use(requireAuth);
|
||||
* names. Everyone signed in can see it (it is the same picture the dependency map and the alerts draw on); only starting a fresh read needs more.
|
||||
*/
|
||||
networkRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [intRows, devices, segments, statuses, graph, silenced] = await Promise.all([
|
||||
const [intRows, devices, segments, statuses, graph, silenced, linkRows, serverRows] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "unifi")),
|
||||
db.select().from(networkDevices),
|
||||
db.select().from(networkSegments),
|
||||
syncStatuses(),
|
||||
loadGraph(),
|
||||
activeSubjects(),
|
||||
db.select().from(networkServerLinks),
|
||||
db.select({ id: servers.id, name: servers.name }).from(servers),
|
||||
]);
|
||||
const serverName = new Map(serverRows.map((s) => [s.id, s.name]));
|
||||
const intName = new Map(intRows.map((i) => [i.id, i.name]));
|
||||
const byExternal = new Map(devices.map((d) => [`${d.integrationId}:${d.externalId}`, d]));
|
||||
// The servers plugged into each device, by what the controller says is connected (see unifiSync.matchClientsToServers).
|
||||
const plugged = new Map<number, { serverId: number; serverName: string; port: number | null; wired: boolean }[]>();
|
||||
const serverLinks: { serverId: number; serverName: string; deviceId: number; deviceName: string; port: number | null; wired: boolean }[] = [];
|
||||
for (const l of linkRows) {
|
||||
const device = byExternal.get(`${l.integrationId}:${l.deviceExternalId}`);
|
||||
const name = serverName.get(l.serverId);
|
||||
if (!device || !name) continue;
|
||||
plugged.set(device.id, [...(plugged.get(device.id) ?? []), { serverId: l.serverId, serverName: name, port: l.port, wired: l.wired }]);
|
||||
serverLinks.push({ serverId: l.serverId, serverName: name, deviceId: device.id, deviceName: device.name, port: l.port, wired: l.wired });
|
||||
}
|
||||
res.json({
|
||||
serverLinks,
|
||||
integrations: intRows.map((i) => ({ id: i.id, name: i.name, enabled: i.enabled, status: statuses[i.id] ?? null })),
|
||||
devices: devices
|
||||
.map((d) => {
|
||||
@@ -57,6 +71,7 @@ networkRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
dependents: dependentsOf(graph.edges, key).length,
|
||||
needs: needsOfNode(graph.edges, key).length,
|
||||
inMaintenance: silenced.has(key),
|
||||
servers: (plugged.get(d.id) ?? []).sort((a, b) => a.serverName.localeCompare(b.serverName)),
|
||||
};
|
||||
})
|
||||
.sort((a, b) => a.name.localeCompare(b.name)),
|
||||
|
||||
@@ -212,7 +212,7 @@ export async function collectAlerts(): Promise<AlertsReport> {
|
||||
type: origin.type,
|
||||
message: c.message,
|
||||
key: c.key,
|
||||
link: origin.type === "server" && origin.id !== null ? `/servers/${origin.id}` : origin.type === "unifi" ? "/network/devices" : undefined,
|
||||
link: origin.type === "server" && origin.id !== null ? `/servers/${origin.id}${/^(failed-units|smart|zfs):/.test(c.key) ? "?view=health" : c.key.startsWith("new-port:") ? "?view=network" : ""}` : origin.type === "unifi" ? "/network/devices" : undefined,
|
||||
silenced: subject !== null && subjects.has(subject), target: targetOfSubject(subject),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
*/
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { dependencies, integrations, networkDevices, servers } from "../db/schema.js";
|
||||
import { dependencies, integrations, networkDevices, networkServerLinks, servers } from "../db/schema.js";
|
||||
import { getInternalFlag } from "./settingsStore.js";
|
||||
import { listActiveWindows } from "./maintenance.js";
|
||||
import { loadParentRows, resolveParents, type ParentLink } from "./serverParents.js";
|
||||
@@ -129,12 +129,13 @@ export interface Graph {
|
||||
}
|
||||
|
||||
export async function loadGraph(): Promise<Graph> {
|
||||
const [serverRows, intRows, depRows, parentRows, deviceRows] = await Promise.all([
|
||||
const [serverRows, intRows, depRows, parentRows, deviceRows, linkRows] = await Promise.all([
|
||||
db.select({ id: servers.id, name: servers.name }).from(servers),
|
||||
db.select({ id: integrations.id, name: integrations.name, type: integrations.type, enabled: integrations.enabled }).from(integrations),
|
||||
db.select().from(dependencies),
|
||||
loadParentRows(),
|
||||
db.select({ id: networkDevices.id, name: networkDevices.name, integrationId: networkDevices.integrationId, externalId: networkDevices.externalId, uplinkExternalId: networkDevices.uplinkExternalId }).from(networkDevices),
|
||||
db.select({ integrationId: networkServerLinks.integrationId, serverId: networkServerLinks.serverId, deviceExternalId: networkServerLinks.deviceExternalId }).from(networkServerLinks),
|
||||
]);
|
||||
const names = new Map<string, string>();
|
||||
for (const s of serverRows) names.set(nodeKey("server", s.id), s.name);
|
||||
@@ -144,7 +145,8 @@ export async function loadGraph(): Promise<Graph> {
|
||||
const instances = new Map<string, number[]>();
|
||||
for (const i of intRows) if (i.enabled) instances.set(i.type, [...(instances.get(i.type) ?? []), i.id]);
|
||||
// A device needs the one it is connected through: the controller says so, and nobody has to write it.
|
||||
const uplinks = uplinkEdges(deviceRows);
|
||||
// And a server needs the device it is plugged into, as the controller says.
|
||||
const uplinks = [...uplinkEdges(deviceRows), ...serverLinkEdges(linkRows, deviceRows)];
|
||||
return { edges: buildEdges(resolveParents(parentRows), [...rows, ...uplinks], (k) => names.has(k)), names, rows, instances, intRows };
|
||||
}
|
||||
|
||||
@@ -160,6 +162,17 @@ export function uplinkEdges(devices: { id: number; integrationId: number; extern
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The edges from each server to the devices it is plugged into. */
|
||||
export function serverLinkEdges(links: { integrationId: number; serverId: number; deviceExternalId: string }[], devices: { id: number; integrationId: number; externalId: string }[]): DependencyEdge[] {
|
||||
const idOf = new Map(devices.map((d) => [`${d.integrationId}:${d.externalId}`, d.id]));
|
||||
const out: DependencyEdge[] = [];
|
||||
for (const l of links) {
|
||||
const to = idOf.get(`${l.integrationId}:${l.deviceExternalId}`);
|
||||
if (to !== undefined) out.push({ fromType: "server", fromId: l.serverId, toType: "device", toId: to });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The id of the alert about a node being down, when it has one (a server's "offline", a kind of service's "failing"). */
|
||||
export function alertIdOfNode(key: string, graph: Graph): string | null {
|
||||
const m = /^(server|integration|device):(\d+)$/.exec(key);
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* "My work": what one person has taken on, in one place — the alerts handed to them, the acknowledgements about to run out, the maintenance windows they
|
||||
* started that are about to end, and the update runs they started that are still going (or just finished). Everything here already exists elsewhere in the app;
|
||||
* this only gathers the parts that have this person's name on them, and says which of them need a look soon.
|
||||
*
|
||||
* Who did what is recorded as a label — the person's name, or their email, or their sign-in id — so those three are what are matched.
|
||||
*/
|
||||
import { and, eq, gte, inArray } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { servers, updateRuns } from "../db/schema.js";
|
||||
import { getAlerts } from "./alerts.js";
|
||||
import { loadAcks } from "./alertAcks.js";
|
||||
import { describeTarget, listActiveWindows } from "./maintenance.js";
|
||||
|
||||
/** An acknowledgement that runs out within this long is flagged. */
|
||||
export const ACK_ENDS_SOON_MS = 24 * 3600_000;
|
||||
/** A maintenance window that ends within this long is flagged. */
|
||||
export const WINDOW_ENDS_SOON_MS = 2 * 3600_000;
|
||||
/** A finished update run stays on the list this long, so the result is seen. */
|
||||
export const RUN_KEPT_MS = 24 * 3600_000;
|
||||
|
||||
export interface MyWorkUser {
|
||||
id: number;
|
||||
name: string | null;
|
||||
email: string | null;
|
||||
oidcSub: string;
|
||||
}
|
||||
|
||||
export interface MyWork {
|
||||
/** Alerts that are on the Alerts page now and that this person has acknowledged or been handed, worst-first-ending first. */
|
||||
alerts: {
|
||||
alertId: string;
|
||||
message: string;
|
||||
severity: string;
|
||||
source: string;
|
||||
link: string | null;
|
||||
note: string;
|
||||
/** "handed" = somebody gave it to them (or they gave it to themselves); "acked" = they acknowledged it for nobody in particular. */
|
||||
role: "handed" | "acked";
|
||||
ackedBy: string;
|
||||
ackedAt: string;
|
||||
until: string | null;
|
||||
endsSoon: boolean;
|
||||
}[];
|
||||
windows: { id: number; targetType: string; targetId: number; targetName: string; targetKind: string; reason: string | null; startedAt: string; endsAt: string; endsSoon: boolean }[];
|
||||
updateRuns: { id: number; serverId: number; serverName: string; templateName: string; status: string; message: string | null; startedAt: string; finishedAt: string | null; running: boolean; updatesBefore: number | null; updatesAfter: number | null; rebootRequired: boolean }[];
|
||||
/** How many of the above want a look: acknowledgements and windows that end soon, alerts handed to them, update runs still going. */
|
||||
needsAttention: number;
|
||||
}
|
||||
|
||||
/** The labels an action by this person may have been recorded under. */
|
||||
export function labelsOf(user: MyWorkUser): string[] {
|
||||
return [...new Set([user.name, user.email, user.oidcSub].filter((v): v is string => typeof v === "string" && v !== ""))];
|
||||
}
|
||||
|
||||
export async function buildMyWork(user: MyWorkUser, now: Date = new Date()): Promise<MyWork> {
|
||||
const labels = new Set(labelsOf(user));
|
||||
const nowMs = now.getTime();
|
||||
|
||||
const report = await getAlerts(false);
|
||||
const acks = await loadAcks(now);
|
||||
const alerts: MyWork["alerts"] = [];
|
||||
for (const a of report.alerts) {
|
||||
const ack = acks.get(a.id);
|
||||
if (!ack || !ack.active) continue;
|
||||
const handed = ack.assignee?.id === user.id;
|
||||
const acked = labels.has(ack.ackedBy);
|
||||
if (!handed && !acked) continue;
|
||||
alerts.push({
|
||||
alertId: a.id,
|
||||
message: a.message,
|
||||
severity: a.severity,
|
||||
source: a.source,
|
||||
link: a.link,
|
||||
note: ack.note,
|
||||
role: handed ? "handed" : "acked",
|
||||
ackedBy: ack.ackedBy,
|
||||
ackedAt: ack.ackedAt,
|
||||
until: ack.until,
|
||||
endsSoon: ack.until !== null && Date.parse(ack.until) - nowMs <= ACK_ENDS_SOON_MS,
|
||||
});
|
||||
}
|
||||
// What runs out first, first; then what has no end, worst first.
|
||||
const SEV: Record<string, number> = { critical: 0, warning: 1, info: 2 };
|
||||
alerts.sort((a, b) => (a.until === null ? Infinity : Date.parse(a.until)) - (b.until === null ? Infinity : Date.parse(b.until)) || (SEV[a.severity] ?? 3) - (SEV[b.severity] ?? 3) || a.message.localeCompare(b.message));
|
||||
|
||||
const windows: MyWork["windows"] = [];
|
||||
for (const w of await listActiveWindows(now)) {
|
||||
if (!w.createdBy || !labels.has(w.createdBy)) continue;
|
||||
const target = await describeTarget(w.targetType, w.targetId);
|
||||
if (!target) continue;
|
||||
windows.push({ id: w.id, targetType: w.targetType, targetId: w.targetId, targetName: target.name, targetKind: target.kind, reason: w.reason, startedAt: w.startedAt, endsAt: w.endsAt, endsSoon: Date.parse(w.endsAt) - nowMs <= WINDOW_ENDS_SOON_MS });
|
||||
}
|
||||
windows.sort((a, b) => a.endsAt.localeCompare(b.endsAt));
|
||||
|
||||
const labelList = [...labels];
|
||||
const runRows =
|
||||
labelList.length === 0
|
||||
? []
|
||||
: await db
|
||||
.select({ run: updateRuns, serverName: servers.name })
|
||||
.from(updateRuns)
|
||||
.innerJoin(servers, eq(servers.id, updateRuns.serverId))
|
||||
.where(and(inArray(updateRuns.startedBy, labelList), gte(updateRuns.startedAt, new Date(nowMs - 7 * 86_400_000).toISOString())));
|
||||
const runs: MyWork["updateRuns"] = [];
|
||||
for (const { run, serverName } of runRows) {
|
||||
const running = run.finishedAt === null;
|
||||
// Still going, or finished recently enough that its result is news.
|
||||
if (!running && nowMs - Date.parse(run.finishedAt!) > RUN_KEPT_MS) continue;
|
||||
runs.push({ id: run.id, serverId: run.serverId, serverName, templateName: run.templateName, status: run.status, message: run.message, startedAt: run.startedAt, finishedAt: run.finishedAt, running, updatesBefore: run.updatesBefore, updatesAfter: run.updatesAfter, rebootRequired: run.rebootRequired });
|
||||
}
|
||||
runs.sort((a, b) => Number(b.running) - Number(a.running) || b.startedAt.localeCompare(a.startedAt));
|
||||
|
||||
const needsAttention = alerts.filter((a) => a.role === "handed" || a.endsSoon).length + windows.filter((w) => w.endsSoon).length + runs.filter((r) => r.running).length;
|
||||
return { alerts, windows, updateRuns: runs, needsAttention };
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { dnsRecordsCache, dnsZonesCache, integrations, servers } from "../db/schema.js";
|
||||
import { dnsRecordsCache, dnsZonesCache, integrations, networkDevices, networkServerLinks, servers } from "../db/schema.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createUptimeKumaAdapter } from "../integrations/uptimekuma/adapter.js";
|
||||
import { getSettings } from "./settingsStore.js";
|
||||
@@ -17,7 +17,7 @@ export type MapStatus = "ok" | "bad" | "warn" | "unknown";
|
||||
|
||||
export interface MapNode {
|
||||
id: string;
|
||||
kind: "host" | "server" | "dns" | "monitor";
|
||||
kind: "host" | "server" | "dns" | "monitor" | "device";
|
||||
label: string;
|
||||
sub: string | null;
|
||||
status: MapStatus;
|
||||
@@ -91,6 +91,26 @@ export async function buildTopology(now: Date = new Date()): Promise<MapData> {
|
||||
}
|
||||
}
|
||||
|
||||
// The switches and access points servers are plugged into, as a UniFi controller says. Only devices with a server on them are drawn: the rest of the
|
||||
// network is on the dependency map.
|
||||
const links = await db.select().from(networkServerLinks);
|
||||
if (links.length > 0) {
|
||||
const devices = await db.select().from(networkDevices);
|
||||
const deviceOf = new Map(devices.map((d) => [`${d.integrationId}:${d.externalId}`, d]));
|
||||
const serverIds = new Set(serverRows.map((s) => s.id));
|
||||
const drawn = new Set<number>();
|
||||
for (const l of links) {
|
||||
const d = deviceOf.get(`${l.integrationId}:${l.deviceExternalId}`);
|
||||
if (!d || !serverIds.has(l.serverId)) continue;
|
||||
if (!drawn.has(d.id)) {
|
||||
drawn.add(d.id);
|
||||
const status: MapStatus = d.state === "online" ? "ok" : d.state === "offline" ? "bad" : d.state === "updating" ? "warn" : "unknown";
|
||||
nodes.push({ id: `device:${d.id}`, kind: "device", label: d.name, sub: [d.kind.replace("_", " "), d.model].filter(Boolean).join(" · "), status, href: "/network/devices" });
|
||||
}
|
||||
if (!edges.some((e) => e.from === `device:${d.id}` && e.to === `server:${l.serverId}`)) edges.push({ from: `device:${d.id}`, to: `server:${l.serverId}` });
|
||||
}
|
||||
}
|
||||
|
||||
// DNS names that point at a server's own address.
|
||||
const serversByIp = new Map<string, number[]>();
|
||||
for (const s of serverRows) for (const ip of parseIps(s.ipAddresses)) serversByIp.set(ip, [...(serversByIp.get(ip) ?? []), s.id]);
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
*/
|
||||
import { and, eq, inArray, or } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { dependencies, integrations, networkDevices, networkSegments } from "../db/schema.js";
|
||||
import { dependencies, integrations, networkDevices, networkSegments, networkServerLinks, servers } from "../db/schema.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createUnifiAdapter, type UnifiDevice, type UnifiInventory } from "../integrations/unifi/adapter.js";
|
||||
import { createUnifiAdapter, type UnifiClient, type UnifiDevice, type UnifiInventory } from "../integrations/unifi/adapter.js";
|
||||
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
|
||||
|
||||
export const SYNC_INTERVAL_MS = 5 * 60_000;
|
||||
@@ -39,6 +39,43 @@ export function trackOffline(previous: { offlineSince: string | null; lastOnline
|
||||
return { offlineSince: previous?.offlineSince ?? null, lastOnlineAt };
|
||||
}
|
||||
|
||||
export interface ServerLink {
|
||||
serverId: number;
|
||||
deviceExternalId: string;
|
||||
clientMac: string;
|
||||
port: number | null;
|
||||
wired: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Which servers are plugged into which device: a client the controller lists whose IP address is one of a server's own. Only an exact, unambiguous address counts
|
||||
* (an address two servers both report matches neither), and only a client whose switch or access point is a device that is known. Pure.
|
||||
*/
|
||||
export function matchClientsToServers(clients: UnifiClient[], serverRows: { id: number; ipAddresses: string | null }[], deviceMacs: Set<string>): ServerLink[] {
|
||||
const owners = new Map<string, number[]>();
|
||||
for (const s of serverRows) {
|
||||
let ips: unknown = [];
|
||||
try {
|
||||
ips = s.ipAddresses ? JSON.parse(s.ipAddresses) : [];
|
||||
} catch {
|
||||
// a malformed list matches nothing
|
||||
}
|
||||
for (const ip of Array.isArray(ips) ? ips : []) if (typeof ip === "string") owners.set(ip, [...new Set([...(owners.get(ip) ?? []), s.id])]);
|
||||
}
|
||||
const out: ServerLink[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const c of clients) {
|
||||
if (!c.ip || !c.uplinkExternalId || !deviceMacs.has(c.uplinkExternalId)) continue;
|
||||
const who = owners.get(c.ip);
|
||||
if (!who || who.length !== 1) continue;
|
||||
const key = `${who[0]}|${c.mac}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
out.push({ serverId: who[0], deviceExternalId: c.uplinkExternalId, clientMac: c.mac, port: c.port, wired: c.wired });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function readStatuses(): Promise<Record<string, SyncStatus>> {
|
||||
try {
|
||||
const raw = await getInternalFlag(STATUS_FLAG);
|
||||
@@ -91,6 +128,13 @@ export async function storeInventory(integrationId: number, inventory: UnifiInve
|
||||
const gone = existing.filter((r) => !seen.has(r.externalId)).map((r) => r.id);
|
||||
if (gone.length > 0) await db.delete(networkDevices).where(inArray(networkDevices.id, gone));
|
||||
|
||||
// Which servers are plugged into which device. A controller that listed no clients at all (a key that may not read them) says nothing, so what was known stays.
|
||||
if (inventory.clients.length > 0) {
|
||||
const links = matchClientsToServers(inventory.clients, await db.select({ id: servers.id, ipAddresses: servers.ipAddresses }).from(servers), new Set(inventory.devices.map((d) => d.externalId)));
|
||||
await db.delete(networkServerLinks).where(eq(networkServerLinks.integrationId, integrationId));
|
||||
if (links.length > 0) await db.insert(networkServerLinks).values(links.map((l) => ({ integrationId, ...l, updatedAt: nowIso })));
|
||||
}
|
||||
|
||||
const segments: (typeof networkSegments.$inferInsert)[] = [
|
||||
...inventory.networks.map((n) => ({ integrationId, kind: "network" as const, externalId: n.externalId, siteName: n.siteName, name: n.name, enabled: n.enabled, vlanId: n.vlanId, subnet: n.subnet, purpose: n.purpose, security: null, networkName: null, updatedAt: nowIso })),
|
||||
...inventory.ssids.map((s) => ({ integrationId, kind: "ssid" as const, externalId: s.externalId, siteName: s.siteName, name: s.name, enabled: s.enabled, vlanId: s.vlanId, subnet: null, purpose: null, security: s.security, networkName: s.networkName, updatedAt: nowIso })),
|
||||
@@ -140,6 +184,7 @@ export async function removeIntegrationData(integrationId: number): Promise<void
|
||||
await db.delete(networkDevices).where(inArray(networkDevices.id, ids));
|
||||
}
|
||||
await db.delete(networkSegments).where(eq(networkSegments.integrationId, integrationId));
|
||||
await db.delete(networkServerLinks).where(eq(networkServerLinks.integrationId, integrationId));
|
||||
const all = await readStatuses();
|
||||
if (String(integrationId) in all) {
|
||||
delete all[String(integrationId)];
|
||||
|
||||
@@ -27,7 +27,8 @@ const { buildDigest } = await import("../src/services/weeklyDigest.js");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const NOW = new Date("2026-10-17T12:00:00Z");
|
||||
// The routes read the real clock, so the fixtures are laid out from it: a fixed date made these tests fail once enough days had passed.
|
||||
const NOW = new Date(Math.floor(Date.now() / 1000) * 1000); // whole seconds, as snapshot times are
|
||||
const HOUR = 3600_000;
|
||||
const guest = (vmid: number, name: string, type: "qemu" | "lxc" = "qemu", node = "pve1") => ({ vmid, name, node, type, status: "running", cpu: null, maxmem: null, mem: null, uptime: null });
|
||||
const allJob = { id: "j1", enabled: true, schedule: "02:00", storage: "pbs", node: null, allGuests: true, vmids: null, exclude: null };
|
||||
|
||||
@@ -168,7 +168,7 @@ describe("through the report, the alerts, the figures and the page", () => {
|
||||
const services = alerts.find((a) => a.id.startsWith("services:failed-units"));
|
||||
assert.ok(services, JSON.stringify(alerts.map((a) => a.id)));
|
||||
assert.equal(services.category, "services");
|
||||
assert.equal(services.link, `/servers/${id}`);
|
||||
assert.equal(services.link, `/servers/${id}?view=health`, "it opens the Health section of the server page");
|
||||
assert.equal(services.silenced, false);
|
||||
const disk = alerts.find((a) => a.id.startsWith("disk:smart"));
|
||||
assert.ok(disk && disk.severity === "critical", "a failing disk is critical, in the disk category");
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
/**
|
||||
* "My work": what has one person's name on it — alerts handed to them or acknowledged by them, their maintenance windows, their update runs — and which of those
|
||||
* are about to run out.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import type { Server } from "node:http";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-mywork-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, updateRuns } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { setAck } = await import("../src/services/alertAcks.js");
|
||||
const { startOrExtendWindow } = await import("../src/services/maintenance.js");
|
||||
const { invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { buildMyWork, labelsOf } = await import("../src/services/myWork.js");
|
||||
const { myWorkRouter } = await import("../src/routes/myWork.js");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const HOUR = 3600_000;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "olga";
|
||||
const ids: Record<string, number> = {};
|
||||
const olga = { id: 0, name: "Olga", email: "olga@example.lan", oidcSub: "olga" };
|
||||
|
||||
before(async () => {
|
||||
const rows = await db
|
||||
.insert(users)
|
||||
.values([
|
||||
{ oidcSub: "ada", name: "Ada", role: "admin" },
|
||||
{ oidcSub: "olga", name: "Olga", email: "olga@example.lan", role: "operator" },
|
||||
{ oidcSub: "vic", name: "Vic", role: "viewer" },
|
||||
])
|
||||
.returning({ id: users.id, oidcSub: users.oidcSub });
|
||||
for (const r of rows) ids[r.oidcSub] = r.id;
|
||||
olga.id = ids.olga;
|
||||
// four servers that have stopped reporting: four standing alerts
|
||||
for (const name of ["web", "db", "nas", "pi"]) {
|
||||
ids[name] = (await db.insert(servers).values({ name, apiTokenHash: name, apiTokenPrefix: name, lastSeenAt: new Date(Date.now() - 6 * HOUR).toISOString() }).returning({ id: servers.id }))[0].id;
|
||||
}
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: who } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/my-work", myWorkRouter);
|
||||
app = await new Promise<Server>((resolve) => {
|
||||
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/my-work`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows may still hold the database file
|
||||
}
|
||||
});
|
||||
|
||||
const alertOf = (name: string) => `offline:offline:server:${ids[name]}`;
|
||||
|
||||
describe("my work", () => {
|
||||
test("the labels a person's actions are recorded under are their name, email and sign-in id", () => {
|
||||
assert.deepEqual(labelsOf(olga).sort(), ["Olga", "olga", "olga@example.lan"].sort());
|
||||
assert.deepEqual(labelsOf({ id: 1, name: null, email: null, oidcSub: "x" }), ["x"]);
|
||||
});
|
||||
|
||||
test("nothing has their name on it: nothing is listed", async () => {
|
||||
invalidateAlertsCache();
|
||||
const mine = await buildMyWork(olga);
|
||||
assert.deepEqual([mine.alerts.length, mine.windows.length, mine.updateRuns.length, mine.needsAttention], [0, 0, 0, 0]);
|
||||
});
|
||||
|
||||
test("alerts handed to them, or acknowledged by them, and nobody else's; the one that runs out first is first", async () => {
|
||||
await setAck({ alertId: alertOf("web"), note: "Looking at the PSU", assigneeId: ids.olga, hours: 72, actor: "Ada" });
|
||||
await setAck({ alertId: alertOf("db"), note: "Waiting for the disk", assigneeId: null, hours: 4, actor: "Olga" });
|
||||
await setAck({ alertId: alertOf("nas"), note: "Not mine", assigneeId: ids.ada, hours: 2, actor: "Ada" });
|
||||
await setAck({ alertId: alertOf("pi"), note: "", assigneeId: ids.olga, hours: null, actor: "Olga" });
|
||||
invalidateAlertsCache();
|
||||
const mine = await buildMyWork(olga);
|
||||
assert.deepEqual(
|
||||
mine.alerts.map((a) => [a.alertId, a.role, a.endsSoon]),
|
||||
[
|
||||
[alertOf("db"), "acked", true],
|
||||
[alertOf("web"), "handed", false],
|
||||
[alertOf("pi"), "handed", false],
|
||||
],
|
||||
"ending in 4 hours, in 3 days, and never; the one for Ada is not here",
|
||||
);
|
||||
assert.equal(mine.alerts[0].note, "Waiting for the disk");
|
||||
// handed to me (2) + one that runs out within a day (1, which is not also handed to me)
|
||||
assert.equal(mine.needsAttention, 3);
|
||||
});
|
||||
|
||||
test("a maintenance window they started, that ends soon; one that was started by somebody else is not theirs", async () => {
|
||||
await startOrExtendWindow("server", ids.web, 45, "PSU swap", "Olga");
|
||||
await startOrExtendWindow("server", ids.db, 600, "Disk replacement", "Olga");
|
||||
await startOrExtendWindow("server", ids.nas, 30, "Somebody else's", "Ada");
|
||||
invalidateAlertsCache();
|
||||
const mine = await buildMyWork(olga);
|
||||
assert.deepEqual(mine.windows.map((w) => [w.targetName, w.endsSoon, w.reason]), [["web", true, "PSU swap"], ["db", false, "Disk replacement"]], "the one that ends first first");
|
||||
assert.equal(mine.needsAttention, 4);
|
||||
});
|
||||
|
||||
test("update runs they started that are still going, or finished within a day; an older one, and somebody else's, are not listed", async () => {
|
||||
const run = (serverId: number, startedBy: string, startedAt: Date, finishedAt: Date | null, status: string) =>
|
||||
db.insert(updateRuns).values({ serverId, integrationId: 1, projectId: 1, templateId: 1, templateName: "Apply updates", hostLimit: "x", status, startedBy, startedAt: startedAt.toISOString(), finishedAt: finishedAt ? finishedAt.toISOString() : null, updatesBefore: 6, updatesAfter: finishedAt ? 0 : null });
|
||||
const now = Date.now();
|
||||
await run(ids.web, "Olga", new Date(now - 10 * 60_000), null, "running");
|
||||
await run(ids.db, "Olga", new Date(now - 5 * HOUR), new Date(now - 4 * HOUR), "done");
|
||||
await run(ids.nas, "Olga", new Date(now - 3 * 24 * HOUR), new Date(now - 3 * 24 * HOUR + HOUR), "done");
|
||||
await run(ids.pi, "Ada", new Date(now - 10 * 60_000), null, "running");
|
||||
const mine = await buildMyWork(olga);
|
||||
assert.deepEqual(mine.updateRuns.map((r) => [r.serverName, r.running]), [["web", true], ["db", false]], "the running one first");
|
||||
assert.equal(mine.needsAttention, 5);
|
||||
});
|
||||
|
||||
test("over the route: only the signed-in person's own", async () => {
|
||||
who = "olga";
|
||||
const body = (await (await fetch(base)).json()) as any;
|
||||
assert.equal(body.alerts.length, 3);
|
||||
assert.equal(body.windows.length, 2);
|
||||
who = "vic";
|
||||
const vic = (await (await fetch(base)).json()) as any;
|
||||
assert.deepEqual([vic.alerts.length, vic.windows.length, vic.updateRuns.length, vic.needsAttention], [0, 0, 0, 0]);
|
||||
});
|
||||
});
|
||||
@@ -42,7 +42,7 @@ const px = (guests: ReturnType<typeof guest>[]) => [{ integrationId: 1, integrat
|
||||
const snapAgo = (now: number, vmid: number, hours: number) => ({ backupType: "vm", backupId: String(vmid), backupTime: Math.floor((now - hours * HOUR) / 1000), verification: "ok" as const });
|
||||
|
||||
describe("when a guest is stale", () => {
|
||||
const NOW = new Date("2026-10-17T12:00:00Z");
|
||||
const NOW = new Date(); // the real clock: the routes under test read it too
|
||||
const now = NOW.getTime();
|
||||
const input = (hoursAgo: number, extra: { servers?: Parameters<typeof buildCoverage>[0]["servers"]; staleHours?: number } = {}) => ({
|
||||
proxmox: px([guest(100, "web")]),
|
||||
|
||||
+103
-5
@@ -20,12 +20,14 @@ process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, integrations, integrationCredentials, networkDevices, networkSegments, dependencies, maintenanceWindows } = await import("../src/db/schema.js");
|
||||
const { users, servers, integrations, integrationCredentials, networkDevices, networkSegments, networkServerLinks, ipamEntries, dependencies, maintenanceWindows } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { encryptSecret } = await import("../src/crypto.js");
|
||||
const adapterModule = await import("../src/integrations/unifi/adapter.js");
|
||||
const { createUnifiAdapter, normaliseMac, deviceKind, stateFromApi, stateFromLegacy, deviceFromApi, deviceFromLegacy, ssidFromLegacy } = adapterModule;
|
||||
const { syncUnifiIntegration, trackOffline, isFresh, removeIntegrationData, syncStatuses } = await import("../src/services/unifiSync.js");
|
||||
const { createUnifiAdapter, normaliseMac, deviceKind, stateFromApi, stateFromLegacy, deviceFromApi, deviceFromLegacy, ssidFromLegacy, clientFromApi, clientFromLegacy } = adapterModule;
|
||||
const { syncUnifiIntegration, trackOffline, isFresh, removeIntegrationData, syncStatuses, matchClientsToServers } = await import("../src/services/unifiSync.js");
|
||||
const { buildTopology } = await import("../src/services/topology.js");
|
||||
const { ipamRouter } = await import("../src/routes/ipam.js");
|
||||
const { evaluateHealth } = await import("../src/services/healthMonitor.js");
|
||||
const { dependencyCover, uplinkEdges, loadGraph, alertIdOfNode } = await import("../src/services/dependencies.js");
|
||||
const { subjectOfConditionKey, describeTarget } = await import("../src/services/maintenance.js");
|
||||
@@ -69,6 +71,21 @@ const wifiList = {
|
||||
],
|
||||
};
|
||||
|
||||
const clientList = {
|
||||
offset: 0,
|
||||
limit: 200,
|
||||
count: 3,
|
||||
totalCount: 3,
|
||||
data: [
|
||||
{ id: "c1", type: "WIRED", name: "web", ipAddress: "192.168.1.50", macAddress: "aa:bb:cc:00:00:50", uplinkDeviceId: "d-sw" },
|
||||
{ id: "c2", type: "WIRELESS", name: "phone", ipAddress: "192.168.1.60", macAddress: "aa:bb:cc:00:00:60", uplinkDeviceId: "d-ap" },
|
||||
{ id: "c3", type: "WIRED", name: "stranger", ipAddress: "192.168.1.99", macAddress: "aa:bb:cc:00:00:99", uplinkDeviceId: "d-gone" },
|
||||
],
|
||||
};
|
||||
const legacyClients = [
|
||||
{ mac: "aa:bb:cc:00:00:50", ip: "192.168.1.50", hostname: "web", is_wired: true, sw_mac: "00:11:22:33:44:02", sw_port: 7 },
|
||||
{ mac: "aa:bb:cc:00:00:60", ip: "192.168.1.60", name: "phone", is_wired: false, ap_mac: "00:11:22:33:44:03" },
|
||||
];
|
||||
const legacyDevices = [
|
||||
{ _id: "x1", mac: "00:11:22:33:44:01", ip: "192.168.1.1", name: "Gateway", model: "UDMPRO", type: "udm", state: 1, uptime: 1000, version: "4.0.6", upgradable: false },
|
||||
{ _id: "x2", mac: "00:11:22:33:44:02", ip: "192.168.1.2", name: "Rack switch", model: "US24PRO", type: "usw", state: 1, uptime: 500, version: "7.0.50", upgradable: true, uplink: { uplink_mac: "00:11:22:33:44:01", type: "wire" } },
|
||||
@@ -93,6 +110,7 @@ function handleApi(url: URL, res: ServerResponse, strip: string) {
|
||||
const path = url.pathname.slice(strip.length);
|
||||
const m = /^\/sites\/site-1\/(devices|networks|wifi\/broadcasts)(?:\/([^/]+?)(\/statistics\/latest)?)?$/.exec(path);
|
||||
if (path === "/sites") return reply(res, 200, siteList);
|
||||
if (path === "/sites/site-1/clients") return reply(res, 200, clientList);
|
||||
if (!m) return reply(res, 404, { message: "no such endpoint" });
|
||||
if (m[1] === "devices") return reply(res, 200, m[2] ? (m[3] ? (stats[m[2]] ?? {}) : (detail[m[2]] ?? {})) : deviceList);
|
||||
if (m[1] === "networks") return reply(res, 200, m[2] ? (networkDetail[m[2]] ?? {}) : networkList);
|
||||
@@ -103,6 +121,7 @@ function handleLegacy(url: URL, res: ServerResponse, strip: string) {
|
||||
const path = url.pathname.slice(strip.length);
|
||||
if (path === "/api/self/sites") return reply(res, 200, { data: [{ name: "default", desc: "Default" }] });
|
||||
if (path === "/api/s/default/stat/device") return reply(res, 200, { data: legacyDevices });
|
||||
if (path === "/api/s/default/stat/sta") return reply(res, 200, { data: legacyClients });
|
||||
if (path === "/api/s/default/rest/networkconf") return reply(res, 200, { data: legacyNetworks });
|
||||
if (path === "/api/s/default/rest/wlanconf") return reply(res, 200, { data: legacyWlans });
|
||||
return reply(res, 404, {});
|
||||
@@ -206,6 +225,23 @@ describe("normalising what a controller says", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is connected where", () => {
|
||||
test("a client from either API: its address, whether it is wired, and the device it is on (and the port, on the classic one)", () => {
|
||||
const macById = new Map([["d-sw", MAC.sw]]);
|
||||
assert.deepEqual(clientFromApi(clientList.data[0], macById, "Default"), { mac: "aa:bb:cc:00:00:50", ip: "192.168.1.50", name: "web", wired: true, uplinkExternalId: MAC.sw, port: null, siteName: "Default" });
|
||||
assert.equal(clientFromApi(clientList.data[2], macById, "Default")!.uplinkExternalId, null, "an unknown device is no uplink");
|
||||
assert.deepEqual(clientFromLegacy(legacyClients[0], "Default"), { mac: "aa:bb:cc:00:00:50", ip: "192.168.1.50", name: "web", wired: true, uplinkExternalId: MAC.sw, port: 7, siteName: "Default" });
|
||||
assert.deepEqual([clientFromLegacy(legacyClients[1], "Default")!.wired, clientFromLegacy(legacyClients[1], "Default")!.uplinkExternalId], [false, MAC.ap]);
|
||||
});
|
||||
|
||||
test("a server is plugged into a device only by an exact, unambiguous address on a device that is known", () => {
|
||||
const servers = [{ id: 1, ipAddresses: JSON.stringify(["10.0.0.1", "10.0.0.2"]) }, { id: 2, ipAddresses: JSON.stringify(["10.0.0.2", "10.0.0.3"]) }, { id: 3, ipAddresses: "not json" }, { id: 4, ipAddresses: null }];
|
||||
const c = (mac: string, ip: string | null, up: string | null) => ({ mac, ip, name: null, wired: true, uplinkExternalId: up, port: 3, siteName: "Default" });
|
||||
const links = matchClientsToServers([c("m1", "10.0.0.1", MAC.sw), c("m2", "10.0.0.2", MAC.sw), c("m3", "10.0.0.3", MAC.ap), c("m4", "10.0.0.1", "ff:ff:ff:ff:ff:ff"), c("m5", null, MAC.sw), c("m1", "10.0.0.1", MAC.sw)], servers, new Set([MAC.sw, MAC.ap]));
|
||||
assert.deepEqual(links.map((l) => [l.serverId, l.deviceExternalId, l.clientMac]), [[1, MAC.sw, "m1"], [2, MAC.ap, "m3"]], "10.0.0.2 belongs to two servers, so to neither; an unknown device and no address count for nothing; a repeat is one link");
|
||||
});
|
||||
});
|
||||
|
||||
describe("reading a controller", () => {
|
||||
const cfg = (extra: Record<string, unknown>) => ({ url: controllerUrl, ...extra }) as never;
|
||||
|
||||
@@ -321,7 +357,7 @@ describe("a controller in the database", () => {
|
||||
const [cred] = await db.insert(integrationCredentials).values({ name: "unifi:Home", encryptedSecret: encryptSecret(JSON.stringify({ apiKey: "good-key" })) }).returning();
|
||||
const [row] = await db.insert(integrations).values({ type: "unifi", name: "Home UniFi", baseUrl: controllerUrl, credentialId: cred.id, config: JSON.stringify({ url: controllerUrl }) }).returning();
|
||||
integrationId = row.id;
|
||||
webId = (await db.insert(servers).values({ name: "web", apiTokenHash: "w", apiTokenPrefix: "w", lastSeenAt: new Date().toISOString() }).returning({ id: servers.id }))[0].id;
|
||||
webId = (await db.insert(servers).values({ name: "web", apiTokenHash: "w", apiTokenPrefix: "w", lastSeenAt: new Date().toISOString(), ipAddresses: JSON.stringify(["192.168.1.50"]) }).returning({ id: servers.id }))[0].id;
|
||||
});
|
||||
|
||||
test("the first read stores the devices, the networks and the Wi-Fi names, and says how it went", async () => {
|
||||
@@ -361,6 +397,33 @@ describe("a controller in the database", () => {
|
||||
assert.equal((await syncUnifiIntegration(integrationId)).ok, true);
|
||||
});
|
||||
|
||||
test("the server whose address is on the controller's client list is plugged into the switch the controller says, and needs it", async () => {
|
||||
const links = await db.select().from(networkServerLinks);
|
||||
assert.equal(links.length, 1, "the phone and the stranger match no server");
|
||||
assert.deepEqual([links[0].serverId, links[0].deviceExternalId, links[0].wired, links[0].clientMac], [webId, MAC.sw, true, "aa:bb:cc:00:00:50"]);
|
||||
const graph = await loadGraph();
|
||||
assert.ok(graph.edges.get(`server:${webId}`)?.includes(`device:${swId}`));
|
||||
// the switch is down: the server (offline too) is explained by it, with nothing written by hand
|
||||
const cover = await dependencyCover([webId], [], [swId]);
|
||||
assert.equal(cover.cause.get(`server:${webId}`), `device:${swId}`);
|
||||
});
|
||||
|
||||
test("a controller that lists no clients says nothing, so what was known stays", async () => {
|
||||
const [row] = await db.select().from(integrations);
|
||||
const before = (await db.select().from(networkServerLinks)).length;
|
||||
const { storeInventory } = await import("../src/services/unifiSync.js");
|
||||
await storeInventory(row.id, { mode: "api_key", sites: ["Default"], devices: [], networks: [], ssids: [], clients: [] });
|
||||
assert.equal((await db.select().from(networkServerLinks)).length, before);
|
||||
assert.equal((await db.select().from(networkDevices)).length, 0, "the devices, on the other hand, are gone from it");
|
||||
assert.equal((await syncUnifiIntegration(row.id)).ok, true, "and come back on the next read");
|
||||
// device ids are new now: look them up again
|
||||
const rows = await db.select().from(networkDevices);
|
||||
gwId = rows.find((r) => r.name === "Gateway")!.id;
|
||||
swId = rows.find((r) => r.name === "Rack switch")!.id;
|
||||
apId = rows.find((r) => r.name === "Hallway AP")!.id;
|
||||
await db.delete(dependencies);
|
||||
});
|
||||
|
||||
test("uplinks are dependencies nobody had to write: the access point needs the switch, which needs the gateway", async () => {
|
||||
const graph = await loadGraph();
|
||||
assert.deepEqual(graph.edges.get(`device:${apId}`), [`device:${swId}`]);
|
||||
@@ -433,6 +496,15 @@ describe("a controller in the database", () => {
|
||||
invalidateAlertsCache();
|
||||
});
|
||||
|
||||
test("the Connections map draws a device that has a server on it, with its state", async () => {
|
||||
const map = await buildTopology();
|
||||
const sw = map.nodes.find((n) => n.kind === "device");
|
||||
assert.ok(sw, "the switch is on the map");
|
||||
assert.deepEqual([sw!.label, sw!.status, sw!.href], ["Rack switch", "ok", "/network/devices"]);
|
||||
assert.ok(map.edges.some((e) => e.from === sw!.id && e.to === `server:${webId}`));
|
||||
assert.equal(map.nodes.filter((n) => n.kind === "device").length, 1, "the access point and gateway have no server on them");
|
||||
});
|
||||
|
||||
describe("the page's data", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
@@ -459,7 +531,9 @@ describe("a controller in the database", () => {
|
||||
const by = Object.fromEntries(body.devices.map((d: any) => [d.name, d]));
|
||||
assert.deepEqual([by["Rack switch"].ip, by["Rack switch"].uptimeSeconds, by["Rack switch"].uplink.name, by["Rack switch"].firmwareUpdatable], ["192.168.1.2", 3600, "Gateway", true]);
|
||||
assert.equal(by["Hallway AP"].state, "offline");
|
||||
assert.equal(by["Gateway"].dependents, 3, "the switch, the access point and the server behind them");
|
||||
assert.equal(by["Gateway"].dependents, 3, "the switch, the access point and the server behind it");
|
||||
assert.deepEqual(by["Rack switch"].servers.map((x: any) => [x.serverName, x.port, x.wired]), [["web", null, true]]);
|
||||
assert.deepEqual(body.serverLinks.map((l: any) => [l.serverName, l.deviceName]), [["web", "Rack switch"]]);
|
||||
assert.equal(by["Hallway AP"].needs, 2);
|
||||
assert.equal(by["Rack switch"].inMaintenance, true);
|
||||
assert.deepEqual(body.networks.map((n: any) => [n.name, n.vlanId, n.subnet]), [["Default", 1, "192.168.1.1/24"], ["IoT", 20, "192.168.20.1/24"]]);
|
||||
@@ -468,6 +542,29 @@ describe("a controller in the database", () => {
|
||||
assert.ok(!JSON.stringify(body).includes(SECRET_PASSPHRASE));
|
||||
});
|
||||
|
||||
test("the devices' addresses can be added to the IP addresses, and a hand-made entry is left alone", async () => {
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "operator" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/ipam", ipamRouter);
|
||||
const s = await new Promise<Server>((resolve) => {
|
||||
const x = a.listen(0, "127.0.0.1", () => resolve(x));
|
||||
});
|
||||
const ipam = `http://127.0.0.1:${(s.address() as AddressInfo).port}/api/ipam/sync-unifi`;
|
||||
await db.insert(ipamEntries).values({ ipAddress: "192.168.1.3", label: "mine" });
|
||||
const res = await fetch(ipam, { method: "POST" });
|
||||
const body = (await res.json()) as any;
|
||||
assert.deepEqual([body.added, body.skipped, body.skippedIps], [2, 1, ["192.168.1.3"]]);
|
||||
const rows = await db.select().from(ipamEntries);
|
||||
assert.deepEqual(rows.find((r) => r.ipAddress === "192.168.1.2")!.label, "Rack switch");
|
||||
assert.equal(rows.find((r) => r.ipAddress === "192.168.1.3")!.label, "mine");
|
||||
assert.equal(rows.find((r) => r.ipAddress === "192.168.1.1")!.source, "unifi");
|
||||
s.close();
|
||||
});
|
||||
|
||||
test("reading again needs an operator", async () => {
|
||||
who = "viewer";
|
||||
assert.equal((await fetch(`${base}/sync`, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" })).status, 403);
|
||||
@@ -484,6 +581,7 @@ describe("a controller in the database", () => {
|
||||
await removeIntegrationData(integrationId);
|
||||
assert.equal((await db.select().from(networkDevices)).length, 0);
|
||||
assert.equal((await db.select().from(networkSegments)).length, 0);
|
||||
assert.equal((await db.select().from(networkServerLinks)).length, 0);
|
||||
assert.equal((await db.select().from(dependencies)).length, 0);
|
||||
assert.equal((await syncStatuses())[integrationId], undefined);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user