Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/pages/alerts.ts
iris f0a6d6c042 swarm-ui: add a button to the alerts page that shows the pending, no-data and error instances
The page opens on the firing ones. The button lists every instance again,
with the state filter and the tones back, so the table's filtering is
visible. The saved-filters key goes back to its original name, since the
state column is a filter again.
2026-10-18 13:29:52 +02:00

40 lines
1.4 KiB
TypeScript

// 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<string, string>;
}
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());
}