A year of history, one chart, and a read API with personal tokens

History: server load, memory and disks are also kept as hourly (four months) and
daily (over two years) averages with lowest and highest; endpoint answers as
hourly (four months) beside the daily figures. Charts ask for 24 hours to a year
and are drawn from whichever is fine enough. A disk that two weeks call flat is
looked at again over ninety days for the forecast. Built once from what is kept.

One chart: TimeChart replaces the sparkline and the multi-line chart: hover and
arrow-key readout, a lowest/highest band, gaps shown as gaps, the same ranges
everywhere and the choice remembered. Used for server trends, comparing servers,
a check's availability and answer times (Endpoints > History) and a server's
availability.

Read API: /api/v1 (servers, metrics, alerts, incidents, inventory, network,
endpoints and their history, maintenance) with personal read-only tokens made
under the user menu: shown once, hashed, 30/90/365 days or none, ten at most,
removed with their owner. GET only. Documented in API.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-20 20:22:58 +02:00
1 parent 5e26e77340
commit 68b41b5586
47 files changed
+12360 -247

No files matched your search

+38 -38
View File
@@ -1,13 +1,9 @@
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { api, type ServerMetricSeries } from "../api/client";
import Sparkline from "./Sparkline";
import TimeChart from "./TimeChart";
import RangeSelect from "./RangeSelect";
import { BlockSkeleton } from "./Skeleton";
const RANGES = [
{ days: 1, label: "24 hours" },
{ days: 7, label: "7 days" },
{ days: 30, label: "30 days" },
];
import { CHART_RANGES, useChartRange } from "../utils/chartRanges";
function levelColor(series: ServerMetricSeries): string {
const v = series.latest ?? 0;
@@ -26,40 +22,48 @@ function describeTrend(series: ServerMetricSeries): string {
if (series.kind === "disk") return "not enough history for a trend yet";
const values = series.points.map((p) => p.v);
if (values.length === 0) return "";
return `${Math.round(Math.min(...values))}–${Math.round(Math.max(...values))}% over this time`;
// With hourly or daily figures the line is an average: the real lowest and highest are in the band.
const lo = Math.min(...series.points.map((p) => p.lo ?? p.v));
const hi = Math.max(...series.points.map((p) => p.hi ?? p.v));
return `${Math.round(lo)}–${Math.round(hi)}% over this time`;
}
/** How an agent-reported server's load, memory and disks have moved lately, with a forecast for a filling disk. */
/** How an agent-reported server's load, memory and disks have moved over a day to a year, with a forecast for a filling disk. */
export default function ServerTrends({ serverId }: { serverId: number }) {
const [days, setDays] = useState(7);
const [days, setDays] = useChartRange(7);
const [series, setSeries] = useState<ServerMetricSeries[] | null>(null);
const [failed, setFailed] = useState(false);
// 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(() => {
let alive = true;
setFailed(false);
api.servers
.metrics(serverId, days)
.then((res) => alive && setSeries(res.series))
.then((res) => {
if (!alive) return;
setSeries(res.series);
setTo(Date.now());
})
.catch(() => alive && setFailed(true));
return () => {
alive = false;
};
}, [serverId, days]);
const from = to - days * 86_400_000;
const rangeLabel = CHART_RANGES.find((r) => r.days === days)?.label ?? `${days} days`;
const charts = useMemo(
() => (series ?? []).map((s) => ({ s, words: describeTrend(s), series: [{ name: s.label, color: levelColor(s), points: s.points }] })),
[series],
);
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Trends</h3>
<div className="card-actions">
<select className="form-select form-select-sm" value={days} onChange={(e) => setDays(Number(e.target.value))} aria-label="Time range">
{RANGES.map((r) => (
<option key={r.days} value={r.days}>
Last {r.label}
</option>
))}
</select>
</div>
<div className="card-header d-flex flex-wrap gap-2">
<h3 className="card-title me-auto">Trends</h3>
<RangeSelect value={days} onChange={setDays} />
</div>
<div className="card-body">
{failed ? (
@@ -68,25 +72,21 @@ export default function ServerTrends({ serverId }: { serverId: number }) {
<BlockSkeleton lines={4} />
) : series.length === 0 ? (
<div className="text-secondary">
No history yet. A sample is kept about every ten minutes once the agent has reported, so lines appear here after a short while.
No history in this stretch yet. A sample is kept about every ten minutes once the agent has reported, and its hours and days after that, so lines appear here after a short while.
</div>
) : (
<div className="trend-grid">
{series.map((s) => {
const words = describeTrend(s);
return (
<div key={s.metric} className="trend-row">
<div className="trend-label">{s.label}</div>
<Sparkline
points={s.points}
color={levelColor(s)}
label={`${s.label}, ${s.latest !== null ? `${Math.round(s.latest)}% now` : "no reading"}${words ? `, ${words}` : ""}`}
/>
<div className="trend-now">{s.latest !== null ? `${Math.round(s.latest)}%` : "—"}</div>
<div className="trend-words text-secondary small">{words}</div>
{charts.map(({ s, words, series: lines }) => (
<div key={s.metric} className="trend-block mb-3">
<div className="d-flex flex-wrap align-items-baseline gap-2 mb-1">
<strong>{s.label}</strong>
<span>{s.latest !== null ? `${Math.round(s.latest)}%` : "—"}</span>
<span className="text-secondary small">{words}</span>
</div>
);
})}
<TimeChart series={lines} from={from} to={to} compact label={`${s.label} over the last ${rangeLabel}, ${s.latest !== null ? `${Math.round(s.latest)}% now` : "no reading"}`} />
</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>