Keep a history of integration figures and draw it on each integration's page

The numbers integrations report (queries and blocked share, queue and missing
items, speeds, photos, documents, failed runs, firing rules, targets down,
monthly cost and more) are kept about every quarter of an hour for five weeks,
then as hours and days for up to two years, taken from each integration's
stored reading. Every card on the 14 integration pages gets a Show the
history button with a chart per figure, how it changed and its lowest and
highest. A stretch when the integration could not be read is a gap.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-12 21:12:47 +01:00
1 parent e4f3a54189
commit e7f23ba13f
30 files changed
+10532 -1

No files matched your search

+16
View File
@@ -848,6 +848,20 @@ export interface AgentDiagnosticNote {
detail: string | null;
}
/** One figure an integration reports, over a stretch: where it stands, and what it was. */
export interface IntegrationFigure {
key: string;
label: string;
/** count, percent, ms, bytes, bytes_per_s or money:<currency>. */
unit: string;
latest: number;
readAt: string;
points: { t: string; v: number; lo?: number; hi?: number }[];
first: number | null;
low: number | null;
high: number | null;
}
export interface UpdateRun {
id: number;
serverId: number;
@@ -4391,6 +4405,8 @@ export const api = {
integration: { id: number; type: IntegrationType; name: string; enabled: boolean };
config: Record<string, string | boolean>;
}>(`/api/integrations/${id}/config`),
/** How the figures it reports have moved over the last `days` (a day to a year); `kept` is false for a kind that keeps none. */
figures: (id: number, days: number) => request<{ kept: boolean; days: number; figures: IntegrationFigure[] }>(`/api/integrations/${id}/figures?days=${days}`),
inspect: (id: number) => request<IntegrationInspection>(`/api/integrations/${id}/inspect`, { method: "POST" }),
/** Reads it now instead of at its next turn. A kind with a stored reading is read and judged again; a kind that is read live is asked whether it answers. */
readNow: (id: number) => request<{ kind: "stored" | "live"; ok: boolean; error: string | null; at: string; latencyMs: number | null }>(`/api/integrations/${id}/read-now`, { method: "POST" }),
+89
View File
@@ -0,0 +1,89 @@
import { useEffect, useMemo, useState } from "react";
import { api, type IntegrationFigure } from "../api/client";
import { CHART_RANGES, useChartRange } from "../utils/chartRanges";
import { readableError } from "../utils/errors";
import { axisFigure, changeWords, formatFigure } from "../utils/figureUnits";
import { ErrorNotice } from "./Notice";
import RangeSelect from "./RangeSelect";
import { BlockSkeleton } from "./Skeleton";
import TimeChart from "./TimeChart";
/**
* How the figures an integration reports have moved, a day to a year: one small chart for each figure with where it stands, how it has changed over the stretch and its lowest and highest.
* Folded away until it is opened (so a page with many integrations asks for nothing it was not asked to show). A kind of integration that keeps none says so rather than showing an empty box.
*/
export default function FigureHistory({ integrationId, name }: { integrationId: number; name: string }) {
const [open, setOpen] = useState(false);
const [days, setDays] = useChartRange(7);
const [data, setData] = useState<{ kept: boolean; figures: IntegrationFigure[] } | null>(null);
const [error, setError] = useState<string | null>(null);
// Fixed per load, so the right-hand end of a chart is when it was read and does not creep as the page re-renders.
const [to, setTo] = useState(() => Date.now());
useEffect(() => {
if (!open) return;
let alive = true;
setError(null);
api.integrations
.figures(integrationId, days)
.then((res) => {
if (!alive) return;
setData(res);
setTo(Date.now());
})
.catch((err) => alive && setError(readableError(err)));
return () => {
alive = false;
};
}, [open, integrationId, days]);
const from = to - days * 86_400_000;
const rangeLabel = CHART_RANGES.find((r) => r.days === days)?.label ?? `${days} days`;
const charts = useMemo(
() =>
(data?.figures ?? []).map((f) => ({
f,
words: changeWords(f.unit, f.first, f.points[f.points.length - 1]?.v ?? null),
series: [{ name: f.label, color: "#4299e1", points: f.points }],
})),
[data],
);
return (
<div className="mt-3">
<button type="button" className="btn btn-sm btn-outline-secondary" aria-expanded={open} onClick={() => setOpen((o) => !o)}>
{open ? "Hide the history" : "Show the history"}
</button>
{open && (
<div className="mt-3">
<div className="d-flex flex-wrap gap-2 align-items-center mb-2">
<h4 className="mb-0 me-auto">History</h4>
<RangeSelect value={days} onChange={setDays} />
</div>
<ErrorNotice error={error} />
{!data && !error && <BlockSkeleton lines={3} />}
{data && !data.kept && <div className="text-secondary small">No history is kept for this kind of integration: it is read when a page is opened, and nothing is stored to draw from.</div>}
{data && data.kept && data.figures.length === 0 && <div className="text-secondary small">Nothing has been kept in this stretch yet. A reading is kept about every quarter of an hour once the integration has been read, so lines appear after a short while.</div>}
{data && data.figures.length > 0 && (
<div className="trend-grid">
{charts.map(({ f, words, series }) => (
<div key={f.key} className="trend-block mb-3">
<div className="d-flex flex-wrap align-items-baseline gap-2 mb-1">
<strong>{f.label}</strong>
<span>{formatFigure(f.unit, f.latest)}</span>
<span className="text-secondary small">
{words}
{f.low !== null && f.high !== null && f.low !== f.high ? ` · ${formatFigure(f.unit, f.low)} to ${formatFigure(f.unit, f.high)}` : ""}
</span>
</div>
<TimeChart series={series} from={from} to={to} compact unit={f.unit === "percent" ? "%" : "value"} format={(v) => formatFigure(f.unit, v)} axisFormat={(v) => axisFigure(f.unit, v)} label={`${name}: ${f.label} over the last ${rangeLabel}, ${formatFigure(f.unit, f.latest)} now`} />
</div>
))}
{days > 35 && <div className="text-secondary small">Over {days > 120 ? "more than four months" : "more than five weeks"} each point is {days > 120 ? "a day" : "an hour"}&rsquo;s average, and the shaded band is its lowest and highest.</div>}
</div>
)}
</div>
)}
</div>
);
}
+4 -1
View File
@@ -50,6 +50,7 @@ export default function TimeChart({
bars = false,
barColor,
format,
axisFormat,
empty = "No history in this stretch yet.",
}: {
series: ChartSeries[];
@@ -64,6 +65,8 @@ export default function TimeChart({
bars?: boolean;
barColor?: (v: number) => string;
format?: (v: number) => string;
/** How the figures down the left edge are written, where the readout's own (`format`) is too long for the margin ("340 GB" reads, "340000000000" does not). */
axisFormat?: (v: number) => string;
empty?: string;
}) {
const H = compact ? 116 : 250;
@@ -161,7 +164,7 @@ export default function TimeChart({
<g key={v}>
<line x1={PAD.left} x2={W - PAD.right} y1={y(v)} y2={y(v)} stroke="currentColor" strokeOpacity={v === 0 ? 0.4 : 0.15} />
<text x={PAD.left - 6} y={y(v) + 4} textAnchor="end" fontSize="11" fill="currentColor" fillOpacity="0.7">
{unit === "%" ? `${v}%` : unit === "value" ? `${Math.round(v * 10) / 10}` : `${Math.round(v)}`}
{axisFormat ? axisFormat(v) : unit === "%" ? `${v}%` : unit === "value" ? `${Math.round(v * 10) / 10}` : `${Math.round(v)}`}
</text>
</g>
))}
+2
View File
@@ -9,6 +9,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatAgo } from "../utils/duration";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ a }: { a: AdguardInstance }) {
if (!a.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -140,6 +141,7 @@ function Card({ a, canRead, onRead }: { a: AdguardInstance; canRead: boolean; on
)}
</>
)}
<FigureHistory integrationId={a.id} name={a.name} />
</div>
</div>
);
+2
View File
@@ -11,6 +11,7 @@ import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ a }: { a: AuthentikInfo }) {
if (!a.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -131,6 +132,7 @@ function Card({ a }: { a: AuthentikInfo }) {
{s.lastConfigError && <div className="text-secondary small">Latest configuration error: {s.lastConfigError}</div>}
</>
)}
<FigureHistory integrationId={a.id} name={a.name} />
</div>
</div>
);
+2
View File
@@ -9,6 +9,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatBytes } from "./settings/StorageSettings";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
const KIND_NAMES: Record<DownloadClient["kind"], string> = { qbittorrent: "qBittorrent", sabnzbd: "SABnzbd" };
const SHOWN = 25;
@@ -234,6 +235,7 @@ function Card({ c, canRead, onRead }: { c: DownloadClient; canRead: boolean; onR
)}
</>
)}
<FigureHistory integrationId={c.id} name={c.name} />
</div>
</div>
);
+2
View File
@@ -8,6 +8,7 @@ import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ g }: { g: GrafanaInstance }) {
if (!g.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -123,6 +124,7 @@ function Card({ g, canRead, onRead }: { g: GrafanaInstance; canRead: boolean; on
)}
</>
)}
<FigureHistory integrationId={g.id} name={g.name} />
</div>
</div>
);
+2
View File
@@ -9,6 +9,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatBytes } from "./settings/StorageSettings";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ p }: { p: HetznerProject }) {
if (!p.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -326,6 +327,7 @@ function Card({ p, canRead, onRead }: { p: HetznerProject; canRead: boolean; onR
)}
</>
)}
<FigureHistory integrationId={p.id} name={p.name} />
</div>
</div>
);
+2
View File
@@ -8,6 +8,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatBytes } from "./settings/StorageSettings";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ i }: { i: ImmichInfo }) {
if (!i.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -124,6 +125,7 @@ function Card({ i }: { i: ImmichInfo }) {
)}
</>
)}
<FigureHistory integrationId={i.id} name={i.name} />
</div>
</div>
);
+2
View File
@@ -10,6 +10,7 @@ import { ErrorNotice } from "../components/Notice";
import { useChartRange } from "../utils/chartRanges";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ j }: { j: JellyfinInfo }) {
if (!j.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -156,6 +157,7 @@ function Card({ j }: { j: JellyfinInfo }) {
</>
)}
{j.enabled && s && <History id={j.id} name={j.name} />}
<FigureHistory integrationId={j.id} name={j.name} />
</div>
</div>
);
+2
View File
@@ -9,6 +9,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatBytes } from "./settings/StorageSettings";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
const KIND_NAMES: Record<ArrApp["kind"], string> = { sonarr: "Sonarr", radarr: "Radarr", prowlarr: "Prowlarr", lidarr: "Lidarr", readarr: "Readarr" };
const QUEUE_SHOWN = 25;
@@ -193,6 +194,7 @@ function Card({ a, canRead, onRead }: { a: ArrApp; canRead: boolean; onRead: ()
)}
</>
)}
<FigureHistory integrationId={a.id} name={a.name} />
</div>
</div>
);
+2
View File
@@ -7,6 +7,7 @@ import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ n }: { n: N8nInfo }) {
if (!n.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -84,6 +85,7 @@ function Card({ n }: { n: N8nInfo }) {
)}
</>
)}
<FigureHistory integrationId={n.id} name={n.name} />
</div>
</div>
);
+2
View File
@@ -7,6 +7,7 @@ import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ n }: { n: NetBoxInfo }) {
if (!n.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -81,6 +82,7 @@ function Card({ n }: { n: NetBoxInfo }) {
)}
</>
)}
<FigureHistory integrationId={n.id} name={n.name} />
</div>
</div>
);
+2
View File
@@ -8,6 +8,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatBytes } from "./settings/StorageSettings";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ n }: { n: NextcloudInfo }) {
if (!n.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -111,6 +112,7 @@ function Card({ n }: { n: NextcloudInfo }) {
)}
</>
)}
<FigureHistory integrationId={n.id} name={n.name} />
</div>
</div>
);
+2
View File
@@ -8,6 +8,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatBytes } from "./settings/StorageSettings";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
const NAMES: Record<string, string> = { database: "Database", redis: "Redis", celery: "Document worker", index: "Search index", classifier: "Classifier", sanity_check: "Sanity check" };
@@ -125,6 +126,7 @@ function Card({ p }: { p: PaperlessInfo }) {
)}
</>
)}
<FigureHistory integrationId={p.id} name={p.name} />
</div>
</div>
);
+2
View File
@@ -7,6 +7,7 @@ import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
/** Alerts every pipeline carries to prove it works: they are always firing, so they are not shown as firing. */
const BY_DESIGN = /^(watchdog|deadmansswitch|deadmanssnitch|infoinhibitor)$/i;
@@ -112,6 +113,7 @@ function Card({ p }: { p: PrometheusInfo }) {
)}
</>
)}
<FigureHistory integrationId={p.id} name={p.name} />
</div>
</div>
);
+2
View File
@@ -9,6 +9,7 @@ import { ErrorNotice } from "../components/Notice";
import { formatAgo } from "../utils/duration";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import FigureHistory from "../components/FigureHistory";
function Badge({ a }: { a: TechnitiumInstance }) {
if (!a.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
@@ -289,6 +290,7 @@ function Card({ a, canRead, onRead }: { a: TechnitiumInstance; canRead: boolean;
</ul>
</>
)}
<FigureHistory integrationId={a.id} name={a.name} />
</div>
</div>
);
+57
View File
@@ -0,0 +1,57 @@
/** How the figures an integration reports are written. Pure, so it can be tested without a browser. */
export type FigureUnit = "count" | "percent" | "ms" | "bytes" | "bytes_per_s" | `money:${string}`;
const BYTE_STEPS = ["B", "kB", "MB", "GB", "TB", "PB"];
/** 1 536 000 → "1.5 MB": powers of 1000, one decimal under 10 and none above, as disk makers count. */
export function formatBytes(n: number): string {
if (!Number.isFinite(n)) return "—";
const sign = n < 0 ? "-" : "";
let v = Math.abs(n);
let i = 0;
while (v >= 1000 && i < BYTE_STEPS.length - 1) {
v /= 1000;
i++;
}
return `${sign}${i === 0 || v >= 10 ? Math.round(v) : Math.round(v * 10) / 10} ${BYTE_STEPS[i]}`;
}
/** A number of any unit, as it is read: 12,345, 12.5%, 8 ms, 1.5 GB, 5 MB/s, €42. */
export function formatFigure(unit: string, v: number): string {
if (!Number.isFinite(v)) return "—";
if (unit === "percent") return `${Math.round(v * 10) / 10}%`;
if (unit === "ms") return `${Math.round(v * 10) / 10} ms`;
if (unit === "bytes") return formatBytes(v);
if (unit === "bytes_per_s") return `${formatBytes(v)}/s`;
if (unit.startsWith("money:")) {
const currency = unit.slice("money:".length);
try {
return new Intl.NumberFormat("en", { style: "currency", currency, maximumFractionDigits: 0 }).format(v);
} catch {
return `${Math.round(v)} ${currency}`;
}
}
// A count has no decimals once it is more than a handful (an average of counts can come out as 1146.5).
return new Intl.NumberFormat("en").format(Math.abs(v) < 10 ? Math.round(v * 10) / 10 : Math.round(v));
}
/** A figure short enough for a chart's margin: 12.5k, 3.2M, 340 GB, 5 MB/s, EUR 1.2k. */
export function axisFigure(unit: string, v: number): string {
if (!Number.isFinite(v)) return "";
if (unit === "count" || unit.startsWith("money:")) {
const short = new Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 1 }).format(v);
return unit === "count" ? short : `${unit.slice("money:".length)} ${short}`;
}
if (unit === "percent") return `${Math.round(v)}%`;
return formatFigure(unit, v);
}
/** How a figure moved over a stretch, in words: "up 120", "down 3.5 points", "unchanged". Percentages move in points. */
export function changeWords(unit: string, first: number | null, last: number | null): string {
if (first === null || last === null) return "";
const d = last - first;
const tiny = unit === "percent" || unit === "ms" ? 0.05 : unit === "count" ? 0.5 : Math.max(Math.abs(first) * 0.005, 1);
if (Math.abs(d) < tiny) return "unchanged";
const amount = unit === "percent" ? `${Math.round(Math.abs(d) * 10) / 10} points` : formatFigure(unit, Math.abs(d));
return `${d > 0 ? "up" : "down"} ${amount}`;
}