swarm-ui: test which alert instances the alerts page lists
The firing-only filter and the helpers beside it move out of the page component into a plain module, with tests for the state match, the never-active time and the row key.
This commit is contained in:
parent
6da252aa20
commit
b39e095aa8
3 changed files with 104 additions and 26 deletions
34
frontend/packages/swarm-ui/src/pages/alerts.ts
Normal file
34
frontend/packages/swarm-ui/src/pages/alerts.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// 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);
|
||||
}
|
||||
|
||||
// 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());
|
||||
}
|
||||
Loading…
Reference in a new issue