Notification routing rules, per-browser push wishes, escalation of unacknowledged alerts

- Routing: rules pick channels by kind, severity and server tags (first match wins, none = nobody); mixed batches split per route; recoveries follow their alert; quiet-hours digest keeps each notification's audience
- Push: each subscribed browser can ask for a minimum severity and some kinds
- Escalation: up to three timed steps re-notify alerts nobody has acknowledged, through chosen channels and an optional extra email; an acknowledgement stops them
- Fix ntfy: titles with an em dash made every ntfy send fail (header not Latin-1); now RFC 2047 encoded

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-18 19:44:45 +02:00
1 parent 9caa414829
commit 43fc5e0272
21 files changed
+4769 -55

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
/**
* Who hears what. By default every notification goes to every channel that is switched on. Routing rules narrow that: the first rule a
* notification matches decides which channels get it (possibly none — it is then only on the Alerts page), and each browser that has asked
* for push notifications may further say it only wants some severities or kinds. This file is the pure part: what a notification is about,
* which rule matches and what a browser accepts. Sending is in notify.ts.
*/
import type { AlertCategory, AlertSeverity } from "./alertTypes.js";
export const CHANNEL_IDS = ["gotify", "ntfy", "smtp", "webhook", "webPush"] as const;
export type ChannelId = (typeof CHANNEL_IDS)[number];
export const CATEGORIES = ["offline", "disk", "services", "backup", "updates", "expiry", "automation", "integration", "monitoring", "tickets"] as const satisfies readonly AlertCategory[];
/** What a notification is about, as far as routing cares. Anything left out matches only rules that don't ask about it. */
export interface NotifyMeta {
category?: AlertCategory | null;
severity?: AlertSeverity;
/** The server it is about, whose tags a rule may ask for. */
serverId?: number | null;
/** Filled in from the server before matching. */
tags?: string[];
}
export interface RoutingRule {
id: string;
name: string;
enabled: boolean;
/** Empty = any kind. */
categories: AlertCategory[];
/** null = any severity. */
minSeverity: "warning" | "critical" | null;
/** Empty = any server. Otherwise the notification must be about a server that has at least one of these tags. */
tags: string[];
/** The channels that get it. Empty = none (the Alerts page still shows it). */
channels: ChannelId[];
}
export interface RoutingSettings {
rules: RoutingRule[];
}
export interface EscalationStep {
/** Minutes an alert must have been raised, with nobody on it, before this step fires. */
afterMinutes: number;
minSeverity: "warning" | "critical";
channels: ChannelId[];
/** An address to email as well (someone else), through the email channel's server. Blank = none. */
extraEmail: string;
/** Send even during quiet hours — the point of an escalation can be to wake someone. */
evenInQuietHours: boolean;
}
export interface EscalationSettings {
enabled: boolean;
steps: EscalationStep[];
}
const RANK: Record<string, number> = { info: 0, warning: 1, critical: 2 };
export const severityRank = (s: string | undefined | null) => RANK[s ?? "warning"] ?? 1;
/** Whether a rule applies to a notification. A rule that asks about something the notification doesn't say doesn't match. */
export function ruleMatches(rule: RoutingRule, meta: NotifyMeta): boolean {
if (!rule.enabled) return false;
if (rule.categories.length > 0 && !(meta.category && rule.categories.includes(meta.category))) return false;
if (rule.minSeverity && severityRank(meta.severity) < severityRank(rule.minSeverity)) return false;
if (rule.tags.length > 0) {
const have = new Set((meta.tags ?? []).map((t) => t.toLowerCase()));
if (!rule.tags.some((t) => have.has(t.toLowerCase()))) return false;
}
return true;
}
/** The first rule that applies, or null (every channel that is on). */
export function firstMatchingRule(rules: RoutingRule[], meta: NotifyMeta): RoutingRule | null {
return rules.find((r) => ruleMatches(r, meta)) ?? null;
}
/** A browser's own wishes, as stored. */
export interface PushPrefs {
minSeverity: string | null;
categories: string | null;
}
export function parseCategories(stored: string | null): AlertCategory[] | null {
if (!stored) return null;
try {
const v = JSON.parse(stored);
if (!Array.isArray(v)) return null;
const list = v.filter((c): c is AlertCategory => (CATEGORIES as readonly string[]).includes(c));
return list.length > 0 ? list : null;
} catch {
return null;
}
}
/** Whether a browser wants this notification. What the notification doesn't say can't exclude it. */
export function pushAccepts(prefs: PushPrefs, meta: NotifyMeta | undefined): boolean {
if (!meta) return true;
if (prefs.minSeverity && meta.severity && severityRank(meta.severity) < severityRank(prefs.minSeverity)) return false;
const wanted = parseCategories(prefs.categories);
if (wanted && meta.category && !wanted.includes(meta.category)) return false;
return true;
}
/** What kind of problem a health or automation condition key is about, and which server (for its tags). */
export function metaOfConditionKey(key: string, severity?: string): NotifyMeta {
let category: AlertCategory | null = null;
if (key.startsWith("offline:")) category = "offline";
else if (key.startsWith("failed-units:")) category = "services";
else if (/^(disk|smart|zfs|synology-volume|synology-disk):/.test(key)) category = "disk";
else if (key.startsWith("automation:")) category = "automation";
const server = /^(?:offline|disk|failed-units|smart|zfs):server:(\d+)/.exec(key);
return { category, severity: severity === "critical" ? "critical" : "warning", serverId: server ? Number(server[1]) : null };
}