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:
bobbanandClaude Sonnet 5.5 committed 2026-10-20 19:35:45 +02:00
1 parent 396e074108
commit 5e26e77340
45 files changed
+6224 -194

No files matched your search

+116
View File
@@ -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 };
}