import { useEffect, useMemo, useState } from "react"; import { api, type BaselineView, type ServerMetricSeries } from "../api/client"; import { hourOfWeek } from "../utils/hourOfWeek"; import TimeChart from "./TimeChart"; import RangeSelect from "./RangeSelect"; import { BlockSkeleton } from "./Skeleton"; import { CHART_RANGES, useChartRange } from "../utils/chartRanges"; function levelColor(series: ServerMetricSeries): string { const v = series.latest ?? 0; if (series.kind === "disk") return v >= 90 ? "#f03e3e" : v >= 80 ? "#f59f00" : "#4299e1"; return "#4299e1"; } /** One line of words about where a series is heading: a disk's rate and when it fills, or a load/memory range. */ function describeTrend(series: ServerMetricSeries): string { if (series.kind === "disk" && series.trend) { const { perDay, daysToFull } = series.trend; if (daysToFull !== null) return `rising ${perDay.toFixed(1)} points a day โ€” full in about ${daysToFull} day${daysToFull === 1 ? "" : "s"}`; if (perDay <= -0.05) return `falling ${Math.abs(perDay).toFixed(1)} points a day`; return "steady"; } 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 ""; // 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 over a day to a year, with a forecast for a filling disk. */ export default function ServerTrends({ serverId }: { serverId: number }) { const [days, setDays] = useChartRange(7); const [series, setSeries] = useState(null); const [failed, setFailed] = useState(false); const [baseline, setBaseline] = useState(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(() => { let alive = true; setFailed(false); api.servers .metrics(serverId, days) .then((res) => { if (!alive) return; setSeries(res.series); setTo(Date.now()); }) .catch(() => alive && setFailed(true)); return () => { alive = false; }; }, [serverId, days]); // What is usual for each hour of the week, loaded once; it only matters while the points are single samples or hours (the weekday means nothing for a day's average). useEffect(() => { let alive = true; api.servers .baseline(serverId) .then((res) => alive && setBaseline(res)) .catch(() => alive && setBaseline(null)); return () => { alive = false; }; }, [serverId]); 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) => { const lines = [{ name: s.label, color: levelColor(s), points: s.points as { t: string; v: number; lo?: number; hi?: number }[] }]; const slots = s.kind === "disk" || !baseline ? null : baseline.slots[s.kind]; if (slots && days <= 30) { // The usual for the hour each point falls in, as a line with a band around it: the median and two spreads either side. const usual = s.points.flatMap((p) => { const h = hourOfWeek(Date.parse(p.t), baseline!.timezone); const slot = slots[h.dow * 24 + h.hour]; if (!slot) return []; const room = 2 * Math.max(slot.spread, s.kind === "cpu" ? 4 : 2); return [{ t: p.t, v: slot.median, lo: Math.max(0, slot.median - room), hi: Math.min(100, slot.median + room) }]; }); if (usual.length > 0) lines.push({ name: "Usual for the time", color: "#868e96", points: usual }); } const here = s.kind === "disk" || !baseline ? null : baseline.slots[s.kind][baseline.now.dow * 24 + baseline.now.hour]; return { s, words: describeTrend(s), series: lines, usualNow: here }; }), [series, baseline, days], ); return (

Trends

{failed ? (
Couldn't load the history.
) : series === null ? ( ) : series.length === 0 ? (
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.
) : (
{charts.map(({ s, words, series: lines, usualNow }) => (
{s.label} {s.latest !== null ? `${Math.round(s.latest)}%` : "โ€”"} {words} {usualNow && ( ยท usually about {Math.round(usualNow.median)}% now )}
))} {charts.some((c) => c.series.length > 1) &&
The grey band is what is usual for that hour of the week, from this server’s own history (the median, two spreads either side).
} {days > 35 &&
Over {days > 120 ? "more than four months" : "more than five weeks"} each point is {days > 120 ? "a day" : "an hour"}’s average, and the shaded band is its lowest and highest.
}
)}
); }