49 lines
1.7 KiB
TypeScript
49 lines
1.7 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.
|
|
|
|
// Grafana's own alert list, for what the swarm UI does not show.
|
|
export const GRAFANA_ALERTS_URL =
|
|
"https://grafana.constellation.darkest.space/alerting/groups";
|
|
|
|
// What the header's alert button is called for `count` firing alerts.
|
|
export function alertsLabel(count: number): string {
|
|
return count === 1 ? "1 alert firing" : `${count} alerts 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());
|
|
}
|