Close an alert: dealt with or accepted, hidden until it gets worse

Close... on the Alerts page hides an alert with no end, with an optional reason
that is also written on its incident. It opens again only if it gets more
serious than when it was closed (said once) or the problem has been gone for
half an hour; Open again brings it back at once.

Built on the snooze table (kind, severity and note columns), so everything that
holds back snoozed alerts holds back closed ones. Audit log, change log and
/api/v1 alerts carry it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-25 20:55:39 +01:00
1 parent 4ff0b44943
commit a94899865e
24 files changed
+7982 -24

No files matched your search

+1 -1
View File
@@ -64,7 +64,7 @@ alertActionsRouter.get("/:token", asyncHandler(async (req, res) => {
continue;
}
const ack = await loadAck(alert.id, now);
const state = alert.snoozed ? `Snoozed until ${esc(new Date(alert.snoozed.until).toISOString().slice(0, 16).replace("T", " "))} UTC.` : ack?.active ? `Acknowledged by ${esc(ack.ackedBy)}.` : alert.silenced ? "Held back (under maintenance, or folded under something that is down)." : "Nobody has acknowledged it.";
const state = alert.closed ? `Closed by ${esc(alert.closed.by)}.` : alert.snoozed ?`Snoozed until ${esc(new Date(alert.snoozed.until).toISOString().slice(0, 16).replace("T", " "))} UTC.` : ack?.active ? `Acknowledged by ${esc(ack.ackedBy)}.` : alert.silenced ? "Held back (under maintenance, or folded under something that is down)." : "Nobody has acknowledged it.";
const form = (action: ActionId, label: string, secondary = false) =>
`<form method="post" action="/api/alert-actions/${esc(req.params.token)}/${action}"><input type="hidden" name="key" value="${esc(key)}"><button type="submit"${secondary ? ' class="secondary"' : ""}>${esc(label)}</button></form>`;
cards += `<div class="card"><div class="sev ${esc(alert.severity)}">${esc(alert.severity)}</div><div class="msg">${esc(alert.message)}</div><div class="state">${state}</div>${form("ack", "Acknowledge")}${alert.target ? form("silence", `Silence for ${SILENCE_MINUTES === 60 ? "an hour" : `${SILENCE_MINUTES} min`}`, true) : ""}${form("snooze", `Snooze until ${untilText}`, true)}<div class="small"><a href="${esc(base + (alert.link ?? "/alerts"))}">Open in Homelab Manager</a></div></div>`;
+45 -2
View File
@@ -2,11 +2,11 @@ import { Router } from "express";
import { z } from "zod";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { getAlerts, invalidateAlertsCache } from "../services/alerts.js";
import { clearSnooze, setSnooze, SnoozeError } from "../services/alertSnoozes.js";
import { clearSnooze, MAX_CLOSE_NOTE, setClosed, setSnooze, SnoozeError } from "../services/alertSnoozes.js";
import { keyOfAlertId } from "../services/flapGuard.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 { addNote, openIncidentIds } from "../services/incidents.js";
import { buildNoiseReport, NOISE_DAYS } from "../services/alertNoise.js";
import { ticketsForAlerts, type AlertTicketInfo } from "../services/alertTickets.js";
import { asyncHandler } from "../utils/asyncHandler.js";
@@ -133,3 +133,46 @@ alertsRouter.delete("/snooze", requireRole("operator"), asyncHandler(async (req,
invalidateAlertsCache();
res.status(204).end();
}));
const closeSchema = z.object({
alertId: z.string().min(1).max(600),
/** Why — shown on the alert and written into its incident. */
note: z.string().max(MAX_CLOSE_NOTE).nullable().optional(),
});
// "I have dealt with it, or I accept it": the alert is hidden with no end, until it gets worse or has gone for half an hour. The reason goes on the incident, so the history says why.
alertsRouter.put("/close", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = closeSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say which alert, and keep the note under " + MAX_CLOSE_NOTE + " characters." });
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!;
const who = actor.name ?? actor.email ?? actor.oidcSub;
try {
const closed = await setClosed({ alertId: alert.id, conditionKey: keyOfAlertId(alert.id, alert.category), message: alert.message, severity: alert.severity, note: parsed.data.note ?? null, actor: who });
// The incident it is part of says it was closed, by whom and why.
const incidentId = (await openIncidentIds([alert.id])).get(alert.id);
if (incidentId) await addNote(incidentId, who, closed.note ? "Closed: " + closed.note : "Closed without a note.");
await recordAudit({ actor, category: "alert", action: "close", ...auditTargetOf(alert), detail: { alertId: alert.id, message: alert.message, note: closed.note } });
invalidateAlertsCache();
res.json({ closed: { by: closed.snoozedBy, at: closed.snoozedAt, note: closed.note } });
} catch (err) {
if (err instanceof SnoozeError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;
}
}));
// Opens it again: it is shown, counted and announced like any other (and, if it is still there, is a new message to the people who get them).
alertsRouter.delete("/close", 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 clearSnooze(alertId);
if (!removed) return res.status(404).json({ error: "not_found" });
const actor = req.currentUser!;
const alert = (await getAlerts(false)).alerts.find((a) => a.id === alertId);
const incidentId = alert ? (await openIncidentIds([alertId])).get(alertId) : undefined;
if (incidentId) await addNote(incidentId, actor.name ?? actor.email ?? actor.oidcSub, "Opened again.");
await recordAudit({ actor, category: "alert", action: "reopen", ...(alert ? auditTargetOf(alert) : {}), detail: { alertId, message: removed.message } });
invalidateAlertsCache();
res.status(204).end();
}));
+1 -1
View File
@@ -103,7 +103,7 @@ const previousAck = z
})
.nullable();
const snoozeRecord = z.object({ conditionKey: z.string().max(700), message: z.string().max(500), until: z.string().max(40), snoozedBy: z.string().max(200), snoozedAt: z.string().max(40) });
const snoozeRecord = z.object({ conditionKey: z.string().max(700), message: z.string().max(500), until: z.string().max(40), snoozedBy: z.string().max(200), snoozedAt: z.string().max(40), kind: z.enum(["snooze", "closed"]).optional(), severity: z.string().max(20).nullable().optional(), note: z.string().max(500).nullable().optional() });
const alertsBody = z.discriminatedUnion("action", [
/** When they should come back, as a date and time with its zone. */