An agent card shows its freshness (stale with the last report's age, never reported, not in swarm identity) when it is not fresh, so the status filter has a visible reason. A failed state change shows its reason in a panel under the card, and a failed cancel shows its reason in the badge, instead of a hover title. Pages can set their title (usePageTitle) and the flow editor does, so several editor tabs differ; the 404 links home. The claude login dialog offers try again after a failure, announces its wait, and has a per-agent field id. The create-agent tabs go forward again as far as the operator has been, but not past a step that is invalid now (lastReachableStep, tested), and the disabled next and create buttons say why in a hint they are described by. usePolled reports when the last live answer arrived, and StaleNote says how old a list is under a failed refresh, or that it is the answer saved from the last visit, on the alerts, builds, review runs, hives, schedules and flows lists.
153 lines
4.4 KiB
TypeScript
153 lines
4.4 KiB
TypeScript
// <AlertsCard> — a card of the reports tab: the alerts the swarm's Grafana holds
|
|
// now, from swarm-controller's `GET /api/alerts`. Only the firing ones show at
|
|
// first; a button in the header adds the pending, no-data and errored ones.
|
|
// Same shape as `HivesCard`: one fetch on a `RefreshIntervalPicker` cadence,
|
|
// rendered as a `Table`.
|
|
import { useState } from "preact/hooks";
|
|
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
|
import { Badge, type BadgeTone } from "@hive/shared/badge.js";
|
|
import { Button } from "../ui/button/Button.js";
|
|
import { ExternalLink } from "../ui/external-link/ExternalLink.js";
|
|
import { LazyDisclosure } from "../ui/disclosure/LazyDisclosure.js";
|
|
import { RelativeTime } from "../ui/relative-time/RelativeTime.js";
|
|
import {
|
|
RefreshIntervalPicker,
|
|
type RefreshIntervalMs,
|
|
} from "../ui/refresh-interval/RefreshInterval.js";
|
|
import { fetchJson, usePolled } from "../ui/polled/usePolled.js";
|
|
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";
|
|
|
|
const STATE_TONE: Record<string, BadgeTone> = {
|
|
Alerting: "negative",
|
|
Error: "negative",
|
|
NoData: "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>[] = [
|
|
{
|
|
key: "name",
|
|
header: "alert",
|
|
render: (a) => a.name,
|
|
sortBy: (a) => a.name,
|
|
filterValue: (a) => a.name,
|
|
},
|
|
{
|
|
key: "state",
|
|
header: "state",
|
|
render: (a) => {
|
|
const since = activeSinceMs(a);
|
|
return (
|
|
<Badge
|
|
tone={STATE_TONE[a.state] ?? "neutral"}
|
|
value={
|
|
since !== null ? (
|
|
<>
|
|
{a.state} (<RelativeTime epochMs={since} />)
|
|
</>
|
|
) : (
|
|
a.state
|
|
)
|
|
}
|
|
/>
|
|
);
|
|
},
|
|
sortBy: (a) => a.state,
|
|
filterValues: (a) => [a.state],
|
|
filterMode: "multiselect",
|
|
},
|
|
{
|
|
key: "summary",
|
|
header: "summary",
|
|
render: (a) => a.summary ?? "—",
|
|
sortBy: (a) => a.summary ?? "",
|
|
filterValue: (a) => a.summary ?? "",
|
|
},
|
|
];
|
|
|
|
const DEFAULT_INTERVAL_MS: RefreshIntervalMs = 30_000;
|
|
|
|
function AlertsBody() {
|
|
const [showAll, setShowAll] = useState(false);
|
|
const [intervalMs, setIntervalMs] =
|
|
useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
|
|
const {
|
|
data: alerts,
|
|
error,
|
|
updatedAt,
|
|
refreshing,
|
|
} = usePolled(
|
|
intervalMs,
|
|
(signal) => fetchJson<Alert[]>("/api/alerts", signal),
|
|
{ cacheKey: "alerts" },
|
|
);
|
|
const listed = alerts ? listedAlerts(alerts, showAll) : null;
|
|
const hidden = alerts && listed ? alerts.length - listed.length : 0;
|
|
|
|
return (
|
|
<>
|
|
<Toolbar>
|
|
<ExternalLink href={GRAFANA_ALERTS_URL}>
|
|
Grafana's alert list
|
|
</ExternalLink>
|
|
{showAll || hidden > 0 ? (
|
|
<Button onClick={() => setShowAll(!showAll)}>
|
|
{showAll ? "only firing" : `show ${hidden} more`}
|
|
</Button>
|
|
) : null}
|
|
<RefreshIntervalPicker
|
|
id="alerts-refresh"
|
|
value={intervalMs}
|
|
onChange={setIntervalMs}
|
|
refreshing={refreshing}
|
|
/>
|
|
</Toolbar>
|
|
{error ? (
|
|
<ApiErrorPanel
|
|
context="failed to load Grafana's alerts"
|
|
problem={error}
|
|
/>
|
|
) : null}
|
|
<StaleNote
|
|
updatedAt={updatedAt}
|
|
failed={error !== null}
|
|
hasData={alerts !== null}
|
|
/>
|
|
{!error && alerts === null ? (
|
|
<TableSkeleton columns={COLUMNS} label="the alerts" />
|
|
) : null}
|
|
{listed && listed.length === 0 ? (
|
|
<p>{showAll ? "no alerts." : "nothing is alerting."}</p>
|
|
) : null}
|
|
{listed && listed.length > 0 ? (
|
|
<Table
|
|
columns={COLUMNS}
|
|
rows={listed}
|
|
rowKey={alertKey}
|
|
storageKey="swarm-ui:alerts:table-filters"
|
|
/>
|
|
) : null}
|
|
</>
|
|
);
|
|
}
|
|
|
|
// A folded card on the reports tab: read and polled only once it is opened.
|
|
export function AlertsCard() {
|
|
return (
|
|
<LazyDisclosure
|
|
title="alerts"
|
|
icon="🚨"
|
|
hint="what the swarm's Grafana holds now"
|
|
>
|
|
<AlertsBody />
|
|
</LazyDisclosure>
|
|
);
|
|
}
|