From 70e5de5571ea05cea010c7e431a016ce8bab3a93 Mon Sep 17 00:00:00 2001 From: iris Date: Sat, 21 Nov 2026 15:01:16 +0100 Subject: [PATCH] swarm-ui: a header bell with the count of firing alerts opens a list of them and a link to Grafana --- docs/4-implementation/web-ui/swarm-ui.md | 6 ++ frontend/packages/shared/src/icons.tsx | 20 ++++ .../swarm-ui/src/pages/AlertsPage.tsx | 12 ++- .../packages/swarm-ui/src/pages/alerts.ts | 9 ++ .../swarm-ui/src/shell/AlertsMenu.css | 77 ++++++++++++++++ .../swarm-ui/src/shell/AlertsMenu.tsx | 91 +++++++++++++++++++ .../packages/swarm-ui/src/shell/Shell.tsx | 2 + .../packages/swarm-ui/test/alerts.test.mjs | 6 ++ 8 files changed, 218 insertions(+), 5 deletions(-) create mode 100644 frontend/packages/swarm-ui/src/shell/AlertsMenu.css create mode 100644 frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index b5832ba98..a4a2ae10e 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -11,6 +11,12 @@ The signed-in user's avatar sits at the right end of the header on every page. It opens a menu with the user's name and links to authelia's settings and sign-out pages; the hive dashboard has the same menu. +While an alert is firing in Grafana, a bell with the number of firing alerts +sits left of the links button on every page. It opens a list of them, each with +its summary and how long it has been firing, and a link to Grafana's alert list. +The bell polls `GET /api/alerts` every 30 seconds and disappears while nothing +fires or the poll fails. + Every dropdown (a form's select, the refresh interval, the history window) opens a list in the swarm UI's own theme, not the browser's, and works the same in every current browser. Open it with a press, or with the arrow keys, **Enter** or diff --git a/frontend/packages/shared/src/icons.tsx b/frontend/packages/shared/src/icons.tsx index d01559f1b..2cd65badc 100644 --- a/frontend/packages/shared/src/icons.tsx +++ b/frontend/packages/shared/src/icons.tsx @@ -260,3 +260,23 @@ export function ArrowDownIcon() { ); } + +// A bell, for the header's alert button. Same size and stroke as `LinkIcon`. +export function BellIcon() { + return ( + + ); +} diff --git a/frontend/packages/swarm-ui/src/pages/AlertsPage.tsx b/frontend/packages/swarm-ui/src/pages/AlertsPage.tsx index 50e3a8cea..82fe2cf60 100644 --- a/frontend/packages/swarm-ui/src/pages/AlertsPage.tsx +++ b/frontend/packages/swarm-ui/src/pages/AlertsPage.tsx @@ -19,7 +19,13 @@ import { StaleNote } from "../ui/polled/StaleNote.js"; import { Table, type TableColumn } from "../ui/table/Table.js"; import { TableSkeleton } from "../ui/table/TableSkeleton.js"; import { Toolbar } from "../ui/toolbar/Toolbar.js"; -import { activeSinceMs, alertKey, listedAlerts, type Alert } from "./alerts.js"; +import { + GRAFANA_ALERTS_URL, + activeSinceMs, + alertKey, + listedAlerts, + type Alert, +} from "./alerts.js"; const STATE_TONE: Record = { Alerting: "negative", @@ -28,10 +34,6 @@ const STATE_TONE: Record = { Pending: "warning", }; -// Grafana's own alert list, for what this page does not show. -const GRAFANA_ALERTS_URL = - "https://grafana.constellation.darkest.space/alerting/groups"; - const COLUMNS: TableColumn[] = [ { key: "name", diff --git a/frontend/packages/swarm-ui/src/pages/alerts.ts b/frontend/packages/swarm-ui/src/pages/alerts.ts index 3161e2d0e..2db5f098b 100644 --- a/frontend/packages/swarm-ui/src/pages/alerts.ts +++ b/frontend/packages/swarm-ui/src/pages/alerts.ts @@ -2,6 +2,15 @@ // 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 diff --git a/frontend/packages/swarm-ui/src/shell/AlertsMenu.css b/frontend/packages/swarm-ui/src/shell/AlertsMenu.css new file mode 100644 index 000000000..b18e98566 --- /dev/null +++ b/frontend/packages/swarm-ui/src/shell/AlertsMenu.css @@ -0,0 +1,77 @@ +/* — the header bell (the shared icon button) with a count badge, + and a popover shaped like the links menu's. */ +.alerts-menu { + position: relative; +} +.alerts-menu .ui-icon-button { + position: relative; + color: var(--red); +} +.alerts-menu-count { + position: absolute; + top: 0.15em; + right: 0.1em; + min-width: 1.35em; + padding: 0.1em 0.3em; + border-radius: 1em; + background: var(--red); + color: var(--bg); + font-size: 0.7em; + font-weight: 700; + line-height: 1.2; + text-align: center; +} +.alerts-menu-popover { + position: absolute; + top: calc(100% + 0.4em); + right: 0; + z-index: 10; + display: flex; + flex-direction: column; + gap: 0.4em; + width: min(26em, calc(100vw - 2em)); + max-height: 70vh; + padding: 0.4em; + overflow-y: auto; + border: 1px solid var(--border); + border-radius: 0.5em; + background: var(--bg-elev); + box-shadow: 0 0.25em 0.75em rgba(0, 0, 0, 0.3); +} +.alerts-menu-list { + display: flex; + flex-direction: column; + margin: 0; + padding: 0; + list-style: none; +} +.alerts-menu-item { + display: flex; + flex-wrap: wrap; + align-items: baseline; + gap: 0.1em 0.6em; + padding: 0.4em 0.6em; + border-left: 3px solid var(--red); + border-radius: 0.35em; +} +.alerts-menu-name { + font-weight: 600; +} +.alerts-menu-since { + color: var(--muted); + font-size: 0.85em; +} +.alerts-menu-summary { + flex-basis: 100%; + overflow-wrap: anywhere; +} +.alerts-menu-link { + display: flex; + align-items: center; + min-height: 2.75em; + padding: 0.4em 0.6em; + border-radius: 0.35em; +} +.alerts-menu-link:hover { + background: var(--bg); +} diff --git a/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx b/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx new file mode 100644 index 000000000..03b56eee2 --- /dev/null +++ b/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx @@ -0,0 +1,91 @@ +// — a header bell with the count of firing alerts, opening a +// popover that lists them and links to Grafana. It draws nothing while no +// alert fires, and while the alerts can't be read (the swarm may have no +// Grafana): no button beats a dead one. It polls `GET /api/alerts`, the same +// list the reports tab shows. +import { useRef, useState } from "preact/hooks"; +import { BellIcon } from "@hive/shared/icons.js"; +import { IconButton } from "@hive/shared/icon-button.js"; +import { useDismiss } from "@hive/shared/use-dismiss.js"; +import "@hive/shared/icon-button.css"; +import { ExternalLink } from "../ui/external-link/ExternalLink.js"; +import { fetchJson, usePolled } from "../ui/polled/usePolled.js"; +import { RelativeTime } from "../ui/relative-time/RelativeTime.js"; +import { + GRAFANA_ALERTS_URL, + activeSinceMs, + alertKey, + alertsLabel, + firingAlerts, + type Alert, +} from "../pages/alerts.js"; +import "./AlertsMenu.css"; + +const POLL_MS = 30_000; + +export function AlertsMenu() { + const { data, error } = usePolled( + POLL_MS, + (signal) => fetchJson("/api/alerts", signal), + { cacheKey: "alerts" }, + ); + // A failed poll keeps the last answer in `data`, so the bell checks `error` + // too rather than show a list that may have gone stale. The controller + // answers 503 both for a swarm with no Grafana and for a Grafana that + // didn't answer, so a swarm without one costs an error response per poll. + const firing = data && !error ? firingAlerts(data) : []; + return firing.length === 0 ? null : ; +} + +function AlertsButton({ firing }: { firing: Alert[] }) { + const [open, setOpen] = useState(false); + const rootRef = useRef(null); + + useDismiss({ + open, + onDismiss: () => setOpen(false), + inside: [rootRef], + focusOnEscape: () => rootRef.current?.querySelector("button") ?? null, + }); + + const label = alertsLabel(firing.length); + return ( +
+ setOpen((v) => !v)} + expanded={open} + > + + + + {open ? ( +
+
    + {firing.map((a) => { + const since = activeSinceMs(a); + return ( +
  • + {a.name} + {since !== null ? ( + + + + ) : null} + {a.summary ? ( + {a.summary} + ) : null} +
  • + ); + })} +
+ + open Grafana's alert list + +
+ ) : null} +
+ ); +} diff --git a/frontend/packages/swarm-ui/src/shell/Shell.tsx b/frontend/packages/swarm-ui/src/shell/Shell.tsx index 85431fdf1..b023eb769 100644 --- a/frontend/packages/swarm-ui/src/shell/Shell.tsx +++ b/frontend/packages/swarm-ui/src/shell/Shell.tsx @@ -8,6 +8,7 @@ import type { ComponentChildren } from "preact"; import { Link, useLocation } from "wouter-preact"; import { AppShell } from "@hive/shared/app-shell.js"; import { usePageTitleValue } from "./page-title.js"; +import { AlertsMenu } from "./AlertsMenu.js"; import { LinksMenu } from "./LinksMenu.js"; import { UserMenu } from "./UserMenu.js"; import { useHideDebug } from "./useHideDebug.js"; @@ -253,6 +254,7 @@ export function Shell({ children }: { children: ComponentChildren }) { } menus={ <> + diff --git a/frontend/packages/swarm-ui/test/alerts.test.mjs b/frontend/packages/swarm-ui/test/alerts.test.mjs index e68e8e4d1..cd4b5ae6b 100644 --- a/frontend/packages/swarm-ui/test/alerts.test.mjs +++ b/frontend/packages/swarm-ui/test/alerts.test.mjs @@ -6,6 +6,7 @@ import { test } from "node:test"; import { activeSinceMs, alertKey, + alertsLabel, firingAlerts, listedAlerts, } from "../src/pages/alerts.ts"; @@ -87,3 +88,8 @@ test("instances of one rule get different keys, whatever the label order", () => assert.equal(alertKey(a), alertKey(b)); assert.notEqual(alertKey(a), alertKey(c)); }); + +test("the alert button's name counts the firing alerts", () => { + assert.equal(alertsLabel(1), "1 alert firing"); + assert.equal(alertsLabel(3), "3 alerts firing"); +});