Add a status strip to the server page: status, maintenance, alerts, updates, disk, load and uptime at a glance

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-06 23:21:57 +01:00
1 parent f9991a49af
commit 87e07b2209
5 files changed
+369

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
import type { AlertItem, HealthCheckSettings, MaintenanceWindow, ServerHardware, ServerRecord } from "../api/client";
import { formatAgo, formatRemaining } from "./duration";
/** How a figure reads: fine, to be watched, wrong, merely information, or not known. */
export type StripTone = "ok" | "warn" | "bad" | "info" | "muted";
export interface StripTile {
id: "status" | "maintenance" | "alerts" | "updates" | "disk" | "load" | "uptime";
label: string;
value: string;
sub: string | null;
tone: StripTone;
/** A page it leads to. */
to?: string;
}
export interface StripInput {
server: ServerRecord;
hardware: ServerHardware;
/** The limits in Settings, which the server follows wherever it has none of its own. */
defaults: HealthCheckSettings;
/** Null while they have not been loaded (or could not be). */
alerts: AlertItem[] | null;
windows: MaintenanceWindow[] | null;
now?: number;
}
const plural = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
/** "12d 4h", "3h 20m". */
export function uptimeText(seconds: number): string {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
if (days > 0) return `${days}d ${hours}h`;
return `${hours}h ${Math.floor((seconds % 3600) / 60)}m`;
}
/** Pure: what is wrong with a disk's fullness, against its own limit (a mount may have its own, or be ignored): the worst of them, as "87% on /". */
export function worstDisk(hardware: ServerHardware, server: Pick<ServerRecord, "thresholds">, defaults: Pick<HealthCheckSettings, "diskUsagePercent">): { mount: string; percent: number; limit: number; of: number } | null {
const base = server.thresholds?.diskUsagePercent ?? defaults.diskUsagePercent;
let worst: { mount: string; percent: number; limit: number; margin: number } | null = null;
let counted = 0;
for (const d of hardware.disks ?? []) {
if (!(d.sizeBytes > 0)) continue;
const rule = server.thresholds?.mounts?.[d.mount];
if (rule?.ignore) continue;
counted++;
const percent = Math.round((d.usedBytes / d.sizeBytes) * 100);
const limit = rule?.percent ?? base;
const margin = percent - limit;
if (worst === null || margin > worst.margin) worst = { mount: d.mount, percent, limit, margin };
}
return worst && { mount: worst.mount, percent: worst.percent, limit: worst.limit, of: counted };
}
/**
* Pure: the figures that say at a glance how a server is — whether it reports, whether it is in maintenance, what is open about it, what is waiting to be installed, how full its disk is, how busy it is
* and how long it has run — each with a tone and, where there is one, the page to go to. A figure that cannot be told (nothing reported, nothing loaded) is left out or says so, never made up.
*/
export function serverStrip(i: StripInput): StripTile[] {
const now = i.now ?? Date.now();
const { server, hardware, defaults } = i;
const tiles: StripTile[] = [];
// Whether it reports.
const offlineMinutes = server.thresholds?.serverOfflineMinutes ?? defaults.serverOfflineMinutes;
const viaProxmox = hardware.source === "proxmox" && hardware.status ? hardware.status : null;
if (server.lastSeenAt) {
const age = (now - Date.parse(server.lastSeenAt)) / 60_000;
if (age > offlineMinutes) {
tiles.push({ id: "status", label: "Status", value: "Offline", sub: `last report ${formatAgo(server.lastSeenAt, now)}${viaProxmox === "running" ? " — Proxmox says it is running" : ""}`, tone: "bad" });
} else {
tiles.push({ id: "status", label: "Status", value: "Online", sub: `reported ${formatAgo(server.lastSeenAt, now)}`, tone: "ok" });
}
} else if (viaProxmox) {
tiles.push({ id: "status", label: "Status", value: viaProxmox.charAt(0).toUpperCase() + viaProxmox.slice(1), sub: "from Proxmox — no agent report", tone: viaProxmox === "running" ? "ok" : "warn" });
} else {
tiles.push({ id: "status", label: "Status", value: "No report yet", sub: "its agent has not reported", tone: "muted" });
}
// A maintenance window that is on now.
const active = (i.windows ?? [])
.filter((w) => w.targetType === "server" && w.targetId === server.id && Date.parse(w.startedAt) <= now && Date.parse(w.endsAt) > now)
.sort((a, b) => Date.parse(a.endsAt) - Date.parse(b.endsAt))[0];
if (active) tiles.push({ id: "maintenance", label: "Maintenance", value: "On now", sub: `ends in ${formatRemaining(active.endsAt, now)}${active.reason ? ` — ${active.reason}` : ""}`, tone: "info", to: "/maintenance" });
// What is open about it (what a window or a snooze has put aside is not counted, but said).
if (i.alerts === null) {
tiles.push({ id: "alerts", label: "Alerts", value: "—", sub: "not loaded", tone: "muted", to: "/alerts" });
} else {
const mine = i.alerts.filter((a) => a.target?.type === "server" && a.target.id === server.id);
const open = mine.filter((a) => !a.silenced && !a.snoozed && !a.closed);
const critical = open.filter((a) => a.severity === "critical").length;
const warning = open.filter((a) => a.severity === "warning").length;
const aside = mine.length - open.length;
const parts = [critical > 0 ? plural(critical, "critical", "critical") : null, warning > 0 ? plural(warning, "warning") : null].filter(Boolean) as string[];
tiles.push({
id: "alerts",
label: "Alerts",
value: critical + warning === 0 ? "None" : String(critical + warning),
sub: [parts.join(" · "), aside > 0 ? `${aside} set aside` : ""].filter(Boolean).join(" · ") || "nothing open",
tone: critical > 0 ? "bad" : warning > 0 ? "warn" : "ok",
to: "/alerts",
});
}
// What is waiting to be installed.
const u = server.pendingUpdates;
if (u === null) {
tiles.push({ id: "updates", label: "Updates", value: "Not reported", sub: "the agent has not said", tone: "muted", to: "/updates" });
} else {
const security = u.security !== null && u.security > 0;
tiles.push({
id: "updates",
label: "Updates",
value: u.count === 0 ? "Up to date" : `${u.count} waiting`,
sub: [u.count > 0 && u.security !== null ? `${u.security} security` : "", u.rebootRequired ? "reboot needed" : ""].filter(Boolean).join(" · ") || null,
tone: security || u.rebootRequired ? "warn" : u.count > 0 ? "info" : "ok",
to: "/updates",
});
}
// The fullest disk, against its own limit.
const disk = worstDisk(hardware, server, defaults);
if (disk) {
tiles.push({ id: "disk", label: "Disk", value: `${disk.percent}%`, sub: `${disk.mount}${disk.of > 1 ? ` — fullest of ${disk.of}` : ""}`, tone: disk.percent >= disk.limit ? "bad" : disk.percent >= disk.limit - 10 ? "warn" : "ok" });
}
// How busy it is.
const cpu = hardware.cpuUsagePercent ?? hardware.cpuLoadPercent ?? null;
const mem = hardware.memTotalBytes ? Math.round(((hardware.memUsedBytes ?? 0) / hardware.memTotalBytes) * 100) : null;
if (cpu !== null || mem !== null) {
const busy = (cpu ?? 0) >= 90 || (mem ?? 0) >= 90;
tiles.push({ id: "load", label: "CPU · memory", value: [cpu !== null ? `${Math.round(cpu)}%` : "—", mem !== null ? `${mem}%` : "—"].join(" · "), sub: null, tone: busy ? "warn" : "muted" });
}
if (typeof hardware.uptime === "number" && hardware.uptime > 0) tiles.push({ id: "uptime", label: "Up for", value: uptimeText(hardware.uptime), sub: null, tone: "muted" });
return tiles;
}