swarm-ui: a header bell with the count of firing alerts opens a list of them and a link to Grafana
This commit is contained in:
parent
ffcba2b026
commit
70e5de5571
8 changed files with 218 additions and 5 deletions
|
|
@ -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
|
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.
|
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
|
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
|
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
|
current browser. Open it with a press, or with the arrow keys, **Enter** or
|
||||||
|
|
|
||||||
|
|
@ -260,3 +260,23 @@ export function ArrowDownIcon() {
|
||||||
</svg>
|
</svg>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A bell, for the header's alert button. Same size and stroke as `LinkIcon`.
|
||||||
|
export function BellIcon() {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="1.3em"
|
||||||
|
height="1.3em"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
stroke-width="2"
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
aria-hidden="true"
|
||||||
|
>
|
||||||
|
<path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" />
|
||||||
|
<path d="M13.73 21a2 2 0 0 1-3.46 0" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,13 @@ import { StaleNote } from "../ui/polled/StaleNote.js";
|
||||||
import { Table, type TableColumn } from "../ui/table/Table.js";
|
import { Table, type TableColumn } from "../ui/table/Table.js";
|
||||||
import { TableSkeleton } from "../ui/table/TableSkeleton.js";
|
import { TableSkeleton } from "../ui/table/TableSkeleton.js";
|
||||||
import { Toolbar } from "../ui/toolbar/Toolbar.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<string, BadgeTone> = {
|
const STATE_TONE: Record<string, BadgeTone> = {
|
||||||
Alerting: "negative",
|
Alerting: "negative",
|
||||||
|
|
@ -28,10 +34,6 @@ const STATE_TONE: Record<string, BadgeTone> = {
|
||||||
Pending: "warning",
|
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<Alert>[] = [
|
const COLUMNS: TableColumn<Alert>[] = [
|
||||||
{
|
{
|
||||||
key: "name",
|
key: "name",
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,15 @@
|
||||||
// instance from swarm-controller's `GET /api/alerts` and which of them count
|
// instance from swarm-controller's `GET /api/alerts` and which of them count
|
||||||
// as firing.
|
// 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 {
|
export interface Alert {
|
||||||
name: string;
|
name: string;
|
||||||
// Grafana's state for the instance: `Alerting`, `Pending`, `NoData` or
|
// Grafana's state for the instance: `Alerting`, `Pending`, `NoData` or
|
||||||
|
|
|
||||||
77
frontend/packages/swarm-ui/src/shell/AlertsMenu.css
Normal file
77
frontend/packages/swarm-ui/src/shell/AlertsMenu.css
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
/* <AlertsMenu> — 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);
|
||||||
|
}
|
||||||
91
frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx
Normal file
91
frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
// <AlertsMenu> — 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<Alert[]>("/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 : <AlertsButton firing={firing} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function AlertsButton({ firing }: { firing: Alert[] }) {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const rootRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useDismiss({
|
||||||
|
open,
|
||||||
|
onDismiss: () => setOpen(false),
|
||||||
|
inside: [rootRef],
|
||||||
|
focusOnEscape: () => rootRef.current?.querySelector("button") ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const label = alertsLabel(firing.length);
|
||||||
|
return (
|
||||||
|
<div class="alerts-menu" ref={rootRef}>
|
||||||
|
<IconButton
|
||||||
|
label={label}
|
||||||
|
onClick={() => setOpen((v) => !v)}
|
||||||
|
expanded={open}
|
||||||
|
>
|
||||||
|
<BellIcon />
|
||||||
|
<span class="alerts-menu-count" aria-hidden="true">
|
||||||
|
{firing.length}
|
||||||
|
</span>
|
||||||
|
</IconButton>
|
||||||
|
{open ? (
|
||||||
|
<div class="alerts-menu-popover" role="group" aria-label={label}>
|
||||||
|
<ul class="alerts-menu-list">
|
||||||
|
{firing.map((a) => {
|
||||||
|
const since = activeSinceMs(a);
|
||||||
|
return (
|
||||||
|
<li key={alertKey(a)} class="alerts-menu-item">
|
||||||
|
<span class="alerts-menu-name">{a.name}</span>
|
||||||
|
{since !== null ? (
|
||||||
|
<span class="alerts-menu-since">
|
||||||
|
<RelativeTime epochMs={since} />
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
{a.summary ? (
|
||||||
|
<span class="alerts-menu-summary">{a.summary}</span>
|
||||||
|
) : null}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
<ExternalLink href={GRAFANA_ALERTS_URL} class="alerts-menu-link">
|
||||||
|
open Grafana's alert list
|
||||||
|
</ExternalLink>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ import type { ComponentChildren } from "preact";
|
||||||
import { Link, useLocation } from "wouter-preact";
|
import { Link, useLocation } from "wouter-preact";
|
||||||
import { AppShell } from "@hive/shared/app-shell.js";
|
import { AppShell } from "@hive/shared/app-shell.js";
|
||||||
import { usePageTitleValue } from "./page-title.js";
|
import { usePageTitleValue } from "./page-title.js";
|
||||||
|
import { AlertsMenu } from "./AlertsMenu.js";
|
||||||
import { LinksMenu } from "./LinksMenu.js";
|
import { LinksMenu } from "./LinksMenu.js";
|
||||||
import { UserMenu } from "./UserMenu.js";
|
import { UserMenu } from "./UserMenu.js";
|
||||||
import { useHideDebug } from "./useHideDebug.js";
|
import { useHideDebug } from "./useHideDebug.js";
|
||||||
|
|
@ -253,6 +254,7 @@ export function Shell({ children }: { children: ComponentChildren }) {
|
||||||
}
|
}
|
||||||
menus={
|
menus={
|
||||||
<>
|
<>
|
||||||
|
<AlertsMenu />
|
||||||
<LinksMenu />
|
<LinksMenu />
|
||||||
<UserMenu />
|
<UserMenu />
|
||||||
</>
|
</>
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import { test } from "node:test";
|
||||||
import {
|
import {
|
||||||
activeSinceMs,
|
activeSinceMs,
|
||||||
alertKey,
|
alertKey,
|
||||||
|
alertsLabel,
|
||||||
firingAlerts,
|
firingAlerts,
|
||||||
listedAlerts,
|
listedAlerts,
|
||||||
} from "../src/pages/alerts.ts";
|
} 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.equal(alertKey(a), alertKey(b));
|
||||||
assert.notEqual(alertKey(a), alertKey(c));
|
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");
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue