Say who was told in an alert's timeline

Notifications are written down with the alerts they were about, and the
incident timeline lists each one: which channels took it, which refused and
what they said, or why it was held or not sent. New alerts, recovery notices,
reminders and escalations are all covered.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-10 21:14:58 +01:00
1 parent f546971000
commit 461219844b
13 files changed
+9443 -18

No files matched your search

+26 -3
View File
@@ -3,7 +3,7 @@
* for quiet hours, a routing rule said nobody should hear it, or no channel was switched on to send it to. Kept 30 days and at most 1000 entries. Nothing here can make sending fail: writing
* it down is best-effort, like the channels themselves. An error's text is cleaned of every token, key and password in the settings before it is kept, since a channel may say them back.
*/
import { and, desc, eq, like, lt, notInArray, or } from "drizzle-orm";
import { and, desc, eq, gte, like, lt, lte, notInArray, or } from "drizzle-orm";
import { db } from "../db/client.js";
import { notificationLog } from "../db/schema.js";
import { getSettings } from "./settingsStore.js";
@@ -62,13 +62,15 @@ async function trim(now: Date): Promise<void> {
}
/** Writes down one notification: what it said, and how it went (or why it was held or muted). Returns its id, or null when it could not be written — never throws. */
export async function recordNotification(entry: { title: string; message: string; results: ChannelResult[]; status?: LogStatus; note?: string | null; resentOf?: number | null }, now: Date = new Date()): Promise<number | null> {
export const MAX_ALERT_KEYS = 50;
export async function recordNotification(entry: { title: string; message: string; results: ChannelResult[]; status?: LogStatus; note?: string | null; resentOf?: number | null; keys?: string[] }, now: Date = new Date()): Promise<number | null> {
try {
const secrets = await secretsNow();
const results = entry.results.map((r) => ({ channel: r.channel, ok: r.ok, error: r.error === null ? null : clip(scrub(r.error.replace(/\s+/g, " ").trim(), secrets), MAX_ERROR) }));
const [row] = await db
.insert(notificationLog)
.values({ at: now.toISOString(), title: clip(entry.title, 300), message: clip(entry.message, MAX_MESSAGE), status: entry.status ?? statusOf(results), results: JSON.stringify(results), note: entry.note ?? null, resentOf: entry.resentOf ?? null })
.values({ at: now.toISOString(), title: clip(entry.title, 300), message: clip(entry.message, MAX_MESSAGE), status: entry.status ?? statusOf(results), results: JSON.stringify(results), note: entry.note ?? null, alertKeys: entry.keys && entry.keys.length > 0 ? JSON.stringify([...new Set(entry.keys.filter((k) => typeof k === "string" && k !== ""))].slice(0, MAX_ALERT_KEYS)) : null, resentOf: entry.resentOf ?? null })
.returning({ id: notificationLog.id });
await trim(now);
return row.id;
@@ -87,6 +89,27 @@ export function resultsOf(row: Pick<LogRow, "results">): ChannelResult[] {
}
}
/** Pure: the keys a row says it was about. */
export function keysOf(row: Pick<LogRow, "alertKeys">): string[] {
try {
const v = row.alertKeys ? JSON.parse(row.alertKeys) : [];
return Array.isArray(v) ? v.filter((k): k is string => typeof k === "string") : [];
} catch {
return [];
}
}
/** The notifications that were about one alert between two moments, oldest first — what its timeline says about who was told. */
export async function loggedForAlert(alertKey: string, fromIso: string, toIso: string): Promise<(Omit<LogRow, "results"> & { results: ChannelResult[] })[]> {
// The key is looked for as the JSON writes it; a character that LIKE treats as a wildcard can match too much, so what is found is checked again.
const rows = await db
.select()
.from(notificationLog)
.where(and(like(notificationLog.alertKeys, `%${JSON.stringify(alertKey)}%`), gte(notificationLog.at, fromIso), lte(notificationLog.at, toIso)))
.orderBy(notificationLog.at, notificationLog.id);
return rows.filter((r) => keysOf(r).includes(alertKey)).map((r) => ({ ...r, results: resultsOf(r) }));
}
export async function getLogged(id: number): Promise<LogRow | null> {
return (await db.select().from(notificationLog).where(eq(notificationLog.id, id)).limit(1))[0] ?? null;
}