Files
Homelab-manager/server/src/services/serverSummary.ts
T
bobbanandClaude Sonnet 5.5 7a5b623444 Stale-backup alerts, per-server alert limits, and update runs from the Updates page
- Backups: a guest whose newest backup is older than its limit is an alert (backup kind),
  in the daily backup message, the digest and /metrics (homelab_backup_guests, ..._last_age_seconds)
- Per-server thresholds (servers.thresholds, migration 0030): offline minutes, disk percent,
  forecast window, backup age, and per-disk limits or "leave alone"; applied everywhere the
  global ones were; saving clears the alerts cache
- Update runs (update_runs, migration 0031): Update... on the Updates page puts the server under
  maintenance, runs a Semaphore playbook limited to it, waits for the host to report, and
  reports what is left and whether a restart is due; poller survives restarts
- Notification toggle for update-run results; tests and docs

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-18 01:25:09 +02:00

89 lines
3.7 KiB
TypeScript

import { evaluateHealth, type Thresholds } from "./healthMonitor.js";
import { parseThresholds } from "./serverThresholds.js";
/** The columns of a server row the summary reads. */
export interface SummaryRow {
id: number;
name: string;
osType: string;
lastSeenAt: string | null;
disks: string | null; // JSON, as the agent's report is stored
/** This server's own thresholds, as stored. */
thresholds?: string | null;
}
export interface ServerSummary {
total: number;
/** Reported within the "server offline after" threshold. */
online: number;
/** Has reported before, but not recently enough. */
offline: { id: number; name: string; lastSeenAt: string; inMaintenance: boolean }[];
/** Never reported: no agent, or not yet — a server tracked only through Proxmox lands here too. */
noData: number;
byOs: { linux: number; windows: number };
/** Reporting servers with a disk at or above the usage threshold. */
fullDisks: { id: number; name: string; mount: string; percent: number }[];
thresholds: { offlineMinutes: number; diskPercent: number };
}
function parseDisks(text: string | null): { mount: string; sizeBytes: number; usedBytes: number }[] {
if (!text) return [];
try {
const value = JSON.parse(text);
return Array.isArray(value) ? value : [];
} catch {
return []; // a garbled report shouldn't blank the whole widget
}
}
/**
* What the dashboard's Servers widget shows. "Offline" and "disk full" are decided by the same function the alerts
* use (evaluateHealth), so the widget can never say a server is fine while an alert says it isn't, or the reverse —
* and it follows the thresholds set in Settings, which viewers can't read themselves.
*/
export function buildServerSummary(rows: SummaryRow[], thresholds: Thresholds, now: number, silencedSubjects: Set<string> = new Set()): ServerSummary {
const snapshot = {
servers: rows.map((r) => ({ id: r.id, name: r.name, lastSeenAt: r.lastSeenAt, disks: parseDisks(r.disks), thresholds: parseThresholds(r.thresholds) })),
proxmox: [],
synology: [],
};
const conditions = evaluateHealth(snapshot, thresholds, now);
const byId = new Map(rows.map((r) => [r.id, r]));
const offlineIds = new Set<number>();
const fullDisks: ServerSummary["fullDisks"] = [];
for (const c of conditions) {
let m = /^offline:server:(\d+)$/.exec(c.key);
if (m) {
offlineIds.add(Number(m[1]));
continue;
}
// The mount can contain a colon ("C:"), so take everything after the id.
m = /^disk:server:(\d+):(.*)$/.exec(c.key);
if (m) {
const row = byId.get(Number(m[1]));
const disk = row ? parseDisks(row.disks).find((d) => d.mount === m![2]) : undefined;
if (row && disk && disk.sizeBytes > 0) {
fullDisks.push({ id: row.id, name: row.name, mount: disk.mount, percent: Math.round((disk.usedBytes / disk.sizeBytes) * 100) });
}
}
}
fullDisks.sort((a, b) => b.percent - a.percent || a.name.localeCompare(b.name));
const offline = rows
.filter((r) => offlineIds.has(r.id) && r.lastSeenAt)
.map((r) => ({ id: r.id, name: r.name, lastSeenAt: r.lastSeenAt!, inMaintenance: silencedSubjects.has(`server:${r.id}`) }))
.sort((a, b) => a.lastSeenAt.localeCompare(b.lastSeenAt)); // longest silent first
const noData = rows.filter((r) => !r.lastSeenAt).length;
return {
total: rows.length,
online: rows.length - noData - offline.length,
offline,
noData,
byOs: { linux: rows.filter((r) => r.osType !== "windows").length, windows: rows.filter((r) => r.osType === "windows").length },
fullDisks,
thresholds: { offlineMinutes: thresholds.serverOfflineMinutes, diskPercent: thresholds.diskUsagePercent },
};
}