Acknowledge and hand over alerts, incident timelines, and built-in database backups
- Alerts can be acknowledged (note, assignee, duration); acknowledged alerts are left out of what needs attention (sidebar, dashboard strip, cards) but stay raised and counted by /metrics - Incidents: each time an alert is raised and goes away is recorded (alert_acks, alert_incidents, incident_notes, migration 0032), with a timeline of acks, maintenance, changes, update runs, related alerts and notes; Incidents page and a digest section - Backups of Homelab Manager's own database: daily checked VACUUM INTO copies, retention, download, check, upload, delete, and restore staged and applied at start-up before the database opens (pre-restore copy kept); alerts and /metrics for stale or failing backups - Docs: README, BACKUP, DATABASE, NOTIFICATIONS, ROLES, API; tests Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a5b623444
commit
2744a84719
41 files changed
+6573
-26
No files matched your search
@@ -1,6 +1,10 @@
|
||||
import { Router } from "express";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { z } from "zod";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { getAlerts } from "../services/alerts.js";
|
||||
import { AckError, assignableUsers, attentionCounts, clearAck, loadAcks, MAX_HOURS, MAX_NOTE, setAck } from "../services/alertAcks.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { openIncidentIds } from "../services/incidents.js";
|
||||
import { ticketsForAlerts, type AlertTicketInfo } from "../services/alertTickets.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
@@ -19,5 +23,68 @@ alertsRouter.get("/", asyncHandler(async (req, res) => {
|
||||
} catch (err) {
|
||||
console.error("[alerts] couldn't look up tickets:", err);
|
||||
}
|
||||
res.json({ ...report, alerts: report.alerts.map((a) => ({ ...a, ticket: tickets.get(a.id) ?? null })) });
|
||||
// Who has said they are on it, and the incident record each alert is part of. Neither may stop the page from loading.
|
||||
let acks = new Map<string, Awaited<ReturnType<typeof loadAcks>> extends Map<string, infer V> ? V : never>();
|
||||
let incidents = new Map<string, number>();
|
||||
try {
|
||||
acks = await loadAcks();
|
||||
incidents = await openIncidentIds(report.alerts.map((a) => a.id));
|
||||
} catch (err) {
|
||||
console.error("[alerts] couldn't look up acknowledgements:", err);
|
||||
}
|
||||
const attention = attentionCounts(report.alerts, acks);
|
||||
res.json({
|
||||
...report,
|
||||
// `counts` still counts everything, as /metrics and the wall do; `attention` leaves out what somebody has said they are dealing with.
|
||||
counts: { ...report.counts, acknowledged: attention.acknowledged, attention: { critical: attention.critical, warning: attention.warning } },
|
||||
alerts: report.alerts.map((a) => ({ ...a, ticket: tickets.get(a.id) ?? null, ack: acks.get(a.id) ?? null, incidentId: incidents.get(a.id) ?? null })),
|
||||
});
|
||||
}));
|
||||
|
||||
// Who an alert can be handed to.
|
||||
alertsRouter.get("/assignees", requireRole("operator"), asyncHandler(async (_req, res) => {
|
||||
res.json({ users: await assignableUsers() });
|
||||
}));
|
||||
|
||||
const ackSchema = z.object({
|
||||
alertId: z.string().min(1).max(600),
|
||||
note: z.string().max(MAX_NOTE).default(""),
|
||||
assigneeId: z.number().int().positive().nullable().default(null),
|
||||
/** How long it lasts; null (or left out) is until the alert is gone. */
|
||||
hours: z.number().int().min(1).max(MAX_HOURS).nullable().default(null),
|
||||
});
|
||||
|
||||
const auditTargetOf = (alert: { target: { type: string; id: number } | null }) => (alert.target ? { targetType: alert.target.type, targetId: alert.target.id } : {});
|
||||
|
||||
// "I'm on it": acknowledge an alert, say why, hand it to someone, and say how long it should be left alone.
|
||||
alertsRouter.put("/ack", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = ackSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "Say which alert, and optionally why, who has it and for how long.", details: parsed.error.flatten() });
|
||||
const alert = (await getAlerts(false)).alerts.find((a) => a.id === parsed.data.alertId);
|
||||
if (!alert) return res.status(404).json({ error: "not_found", message: "That alert isn't there any more." });
|
||||
const actor = req.currentUser!;
|
||||
try {
|
||||
const { ack, before } = await setAck({ ...parsed.data, actor: actor.name ?? actor.email ?? actor.oidcSub });
|
||||
await recordAudit({
|
||||
actor,
|
||||
category: "alert",
|
||||
action: "ack",
|
||||
...auditTargetOf(alert),
|
||||
detail: { alertId: alert.id, message: alert.message, note: ack.note, assignee: ack.assignee?.name ?? null, hours: parsed.data.hours, changed: before !== null },
|
||||
});
|
||||
res.json({ ack });
|
||||
} catch (err) {
|
||||
if (err instanceof AckError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
alertsRouter.delete("/ack", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const alertId = typeof req.query.alertId === "string" ? req.query.alertId : "";
|
||||
if (!alertId) return res.status(400).json({ error: "invalid_id" });
|
||||
const removed = await clearAck(alertId);
|
||||
if (!removed) return res.status(404).json({ error: "not_found" });
|
||||
const alert = (await getAlerts(false)).alerts.find((a) => a.id === alertId);
|
||||
await recordAudit({ actor: req.currentUser!, category: "alert", action: "unack", ...(alert ? auditTargetOf(alert) : {}), detail: { alertId, message: alert?.message ?? null } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
Reference in new issue
Block a user