/** * Snooze: "remind me tomorrow at 09:00". An alert that is snoozed is hidden — on the Alerts page it is held back like one under maintenance, so it is not counted in the * badges, the dashboard or the digest — and the messages about it from the health and automation checks are not sent. It is not maintenance: nothing is promised about * the thing, only that nobody wants to hear about it until then. When the time comes, an alert that is still there is announced again, once, as a reminder; one that * went away in the meantime is simply forgotten. * * A snooze belongs to the alert's id and goes away, like an acknowledgement, once the alert has been gone for half an hour — the same problem coming back later is new. */ import { eq, inArray } from "drizzle-orm"; import { db } from "../db/client.js"; import { alertSnoozes } from "../db/schema.js"; export const MAX_DAYS = 90; /** At least this far ahead, so a snooze cannot end before it has begun. */ export const MIN_AHEAD_MS = 60_000; /** A snoozed alert that has been absent this long loses its snooze. */ export const GONE_AFTER_MS = 30 * 60_000; export class SnoozeError extends Error { constructor( readonly status: number, readonly code: string, message: string, ) { super(message); } } export interface SnoozeInfo { until: string; snoozedBy: string; snoozedAt: string; } const shape = (r: typeof alertSnoozes.$inferSelect): SnoozeInfo => ({ until: r.until, snoozedBy: r.snoozedBy, snoozedAt: r.snoozedAt }); /** The snoozes still running, by alert id. */ export async function loadSnoozes(now: Date = new Date()): Promise> { return new Map((await db.select().from(alertSnoozes)).filter((r) => Date.parse(r.until) > now.getTime()).map((r) => [r.alertId, shape(r)])); } /** Pure: whether a notification about this condition key is held because it is snoozed. */ export function isSnoozedKey(rows: { conditionKey: string; until: string }[], key: string | undefined, now: Date): boolean { return key !== undefined && rows.some((r) => r.conditionKey === key && Date.parse(r.until) > now.getTime()); } /** Drops from a list of messages the ones about alerts that are snoozed. */ export async function withoutSnoozed(items: T[], now: Date = new Date()): Promise { if (!items.some((i) => i.key)) return items; const rows = await db.select({ conditionKey: alertSnoozes.conditionKey, until: alertSnoozes.until }).from(alertSnoozes); if (rows.length === 0) return items; return items.filter((i) => !isSnoozedKey(rows, i.key, now)); } export async function setSnooze(input: { alertId: string; conditionKey: string; message: string; until: Date; actor: string }, now: Date = new Date()): Promise { if (!(input.until.getTime() >= now.getTime() + MIN_AHEAD_MS)) throw new SnoozeError(400, "invalid_until", "Choose a time that has not come yet."); if (input.until.getTime() > now.getTime() + MAX_DAYS * 86_400_000) throw new SnoozeError(400, "invalid_until", `A snooze can be at most ${MAX_DAYS} days.`); const values = { conditionKey: input.conditionKey, message: input.message.slice(0, 500), until: input.until.toISOString(), snoozedBy: input.actor, snoozedAt: now.toISOString(), lastSeenAt: now.toISOString() }; await db.insert(alertSnoozes).values({ alertId: input.alertId, ...values }).onConflictDoUpdate({ target: alertSnoozes.alertId, set: values }); return { until: values.until, snoozedBy: values.snoozedBy, snoozedAt: values.snoozedAt }; } export async function clearSnooze(alertId: string): Promise<{ message: string } | null> { const [row] = await db.select().from(alertSnoozes).where(eq(alertSnoozes.alertId, alertId)).limit(1); if (!row) return null; await db.delete(alertSnoozes).where(eq(alertSnoozes.id, row.id)); return { message: row.message }; } export interface Reminder { key: string; message: string; severity?: string; } /** * One look, with the alerts that are there now: snoozes that have run out are removed, and each whose alert is still there becomes a reminder; the others' alerts are * marked as seen, and those that have been gone for half an hour are dropped. Returns the reminders to send. */ export async function settleSnoozes(present: { id: string; message: string; severity: string; key: string }[], now: Date = new Date()): Promise { const rows = await db.select().from(alertSnoozes); if (rows.length === 0) return []; const byId = new Map(present.map((a) => [a.id, a])); const reminders: Reminder[] = []; const done: number[] = []; const seen: number[] = []; const cutoff = new Date(now.getTime() - GONE_AFTER_MS).toISOString(); for (const r of rows) { const there = byId.get(r.alertId); if (Date.parse(r.until) <= now.getTime()) { if (there) reminders.push({ key: r.conditionKey, message: there.message, severity: there.severity }); done.push(r.id); } else if (there) seen.push(r.id); else if (r.lastSeenAt < cutoff) done.push(r.id); } if (seen.length > 0) await db.update(alertSnoozes).set({ lastSeenAt: now.toISOString() }).where(inArray(alertSnoozes.id, seen)); if (done.length > 0) await db.delete(alertSnoozes).where(inArray(alertSnoozes.id, done)); return reminders; }