// What the alerts page lists, apart from how it draws it: the shape of one // instance from swarm-controller's `GET /api/alerts` and which of them count // as firing. export interface Alert { name: string; // Grafana's state for the instance: `Alerting`, `Pending`, `NoData` or // `Error`. state: string; active_at: string | null; summary: string | null; labels: Record; } const FIRING = "Alerting"; // The instances the page shows: the ones Grafana says are firing now. Pending, // no-data and errored ones are left to Grafana's own list. export function firingAlerts(alerts: Alert[]): Alert[] { return alerts.filter((a) => a.state === FIRING); } // What the page lists: the firing ones, or every instance once the operator // asks for the rest. export function listedAlerts(alerts: Alert[], showAll: boolean): Alert[] { return showAll ? alerts : firingAlerts(alerts); } // Grafana reports an instance that never became active with the zero time, // which `Date.parse` reads as year 1. export function activeSinceMs(a: Alert): number | null { if (a.active_at === null) return null; const ms = Date.parse(a.active_at); return Number.isNaN(ms) || ms <= 0 ? null : ms; } // A row needs a key that is unique among instances of the same rule. export function alertKey(a: Alert): string { return JSON.stringify(Object.entries(a.labels).sort()); }