Files
HealthLog/src/components/admin/host-metrics-chart.tsx
T
Marc-André Bombeck 900a5c4f1f perf: stop keeping folded samples and re-reading duplicates
The nightly folds soft-deleted every raw Apple Health sample they folded
into an hourly or daily stats row and kept it for 75 days. Nothing ever
read those rows as deletions; they only held a re-upload of the sample
off, and they were most of the measurements table and its indexes.

Ingest now refuses such a re-upload directly: a raw sample of a dense or
mean type, old enough to have been folded, whose hour or day a live
stats row covers, is a duplicate with reason folded_window. The batch
route and the Apple Health export import apply the same rule. With that
in place the dense retention and the daily-mean consolidation delete the
raw rows outright, and a new compaction-tombstone-purge job removes the
backlog: 5,000 rows a batch, one account at a time under the restore
lock, a short pause between batches, resumable because the predicate is
idempotent, queued at every boot and continuing itself while it makes
progress. A person's own deletions are never in its predicate. The sync
feed leaves compaction tombstones out and a restore does not write them
back; all three classify through the same function.

The batch route answers an exact re-sent sample without a transaction:
one read over the batch's sample ids, live or deleted, decides what the
reconciler would have decided, at the same point in the loop, so every
status, reason, counter and the cross-source merge stay as they were. A
batch of duplicates only no longer queues PR detection, a reminder sweep
or the detection audit row. Caches are evicted only for a foreground or
manual sync or a batch that wrote a hand-entered reading, and marked
stale otherwise; the targets cache follows the same split.

Also:
- POST /api/admin/maintenance/measurements queues VACUUM (ANALYZE) and a
  REINDEX INDEX CONCURRENTLY per index, largest first, on a connection of
  its own; it refuses while the purge runs, drops invalid leftover copies
  and stops at the first failed rebuild. Runbook in docs/ops.
- The pure volume queues keep their finished jobs for 96 hours instead
  of seven days, never less than the failure window plus a day.
- work_mem is set per app connection from DATABASE_WORK_MEM (16MB), also
  when the statement timeout is disabled.
- The PR-detection fallback tick re-scans only accounts whose readings
  or workouts changed since the previous tick.
- Type discovery for the status refill walks the live-rows index as a
  loose index scan instead of grouping every live row.
- The host metrics chart polls every five minutes, like its sampler.
- The batch route records per-type HealthKit arrivals.
2026-10-08 08:41:25 +02:00

371 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
/**
* `<HostMetricsChart>` — last-2h host-load chart that sits above the
* `/admin/system-status` facts grid (v1.4.16 phase B3).
*
* Three lines on a shared time axis:
* - load1 (yellow) os.loadavg() 1-minute average
* - memUsedPercent (cyan) 0..100, derived from totalmem - freemem
* - diskBusyPercent (purple) read-bps + write-bps as a fraction of
* the window's peak; only drawn when at
* least two diskBps samples are present
* (Linux only — macOS dev hosts hide the
* line entirely)
*
* The chart wrapper imports every Recharts primitive statically and
* lives inside `next/dynamic` at the consumer (the system-status
* section). Splitting at the wrapper boundary keeps Recharts' internal
* `findAllByType` happy. See
* `src/components/charts/scatter-correlation-chart.tsx` for the same
* pattern + rationale.
*
* Polling: TanStack Query with `refetchInterval: 300_000` so the chart
* surfaces a fresh sample as soon as the worker writes one (every five
* minutes since v1.42).
*/
import { useQuery } from "@tanstack/react-query";
import {
CartesianGrid,
Legend,
Line,
LineChart,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from "recharts";
import { useMemo } from "react";
import { useFormatters, useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { Skeleton } from "@/components/ui/skeleton";
import { QueryErrorRow } from "@/components/ui/query-error-row";
import { Activity } from "lucide-react";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { apiGet } from "@/lib/api/api-fetch";
import { readStoredTimezone } from "@/lib/timezone-mirror";
interface HostMetricsApiSample {
capturedAt: string;
loadAvg1: number;
memUsedPercent: number;
diskReadBps: number | null;
diskWriteBps: number | null;
}
interface HostMetricsApiResponse {
samples: HostMetricsApiSample[];
meta: {
since: string;
count: number;
memTotalBytes: number;
};
}
interface ChartRow {
/** ms since epoch — the X-axis. */
timestamp: number;
/** Display label "HH:mm" in the user's locale. */
label: string;
loadAvg1: number;
memUsedPercent: number;
/**
* Combined read+write BPS as a fraction (0..100) of the window's peak.
* Null when the API row had no diskReadBps/diskWriteBps (first sample,
* non-Linux host, or counter reset).
*/
diskBusyPercent: number | null;
}
const COLOR_LOAD = "var(--dracula-yellow)";
const COLOR_MEM = "var(--dracula-cyan)";
const COLOR_DISK = "var(--dracula-purple)";
/**
* Pure helper exposed for unit tests: turns API samples into chart rows
* + the absolute peak BPS used to scale `diskBusyPercent`. Kept as a
* separate export so the test suite doesn't need to render Recharts.
*/
export function buildChartRows(samples: HostMetricsApiSample[]): {
rows: ChartRow[];
peakDiskBps: number;
hasDiskData: boolean;
} {
let peakDiskBps = 0;
for (const sample of samples) {
const total = (sample.diskReadBps ?? 0) + (sample.diskWriteBps ?? 0);
if (total > peakDiskBps) peakDiskBps = total;
}
// The profile's timezone, not the browser's. These are real instants (host
// samples), and an operator reading their own instance should see the same
// clock here as on every other timestamp in the admin area — a spike at
// "14:20" must not be 14:20 here and 15:20 two screens away.
const formatter = new Intl.DateTimeFormat(undefined, {
hour: "2-digit",
minute: "2-digit",
hour12: false,
timeZone: readStoredTimezone() || undefined,
});
let hasDiskData = false;
const rows = samples.map((sample) => {
const ts = new Date(sample.capturedAt).getTime();
const total =
sample.diskReadBps !== null && sample.diskWriteBps !== null
? sample.diskReadBps + sample.diskWriteBps
: null;
let diskBusyPercent: number | null = null;
if (total !== null) {
hasDiskData = true;
diskBusyPercent =
peakDiskBps > 0
? Math.max(0, Math.min(100, (total / peakDiskBps) * 100))
: 0;
}
return {
timestamp: ts,
label: formatter.format(new Date(ts)),
loadAvg1: sample.loadAvg1,
memUsedPercent: sample.memUsedPercent,
diskBusyPercent,
};
});
return { rows, peakDiskBps, hasDiskData };
}
export function HostMetricsChart() {
const { t } = useTranslations();
const fmt = useFormatters();
const { data, isLoading, isError, refetch } = useQuery({
queryKey: queryKeys.adminHostMetrics("2h"),
queryFn: async () => {
return apiGet<HostMetricsApiResponse>("/api/admin/host-metrics?since=2h");
},
// Five minutes matches the sampler cadence (`host-metric-sample`, v1.42)
// — anything faster would just re-render the same data.
refetchInterval: 300_000,
refetchOnWindowFocus: false,
});
const { rows, peakDiskBps, hasDiskData } = useMemo(
() => buildChartRows(data?.samples ?? []),
[data?.samples],
);
// Total physical memory (latest sample) — lets the tooltip pair the
// percentage with an absolute "X.X GiB / Y.Y GiB" label. 0 ⇒ omit.
const memTotalGiB = (data?.meta.memTotalBytes ?? 0) / 1_073_741_824;
// Loading skeleton: same footprint as the chart so the panel doesn't
// jump when data lands. Animated only when motion isn't reduced.
if (isLoading) {
return (
<SettingsCard as="section" aria-labelledby="admin-host-metrics-heading">
<SettingsCardHeader
icon={Activity}
titleId="admin-host-metrics-heading"
title={t("admin.hostMetrics.title")}
status={
<span className="text-muted-foreground text-xs">
{t("admin.hostMetrics.last2hours")}
</span>
}
/>
<Skeleton className="bg-muted/40 h-[180px] w-full" />
</SettingsCard>
);
}
if (isError) {
return (
<SettingsCard as="section" aria-labelledby="admin-host-metrics-heading">
<SettingsCardHeader
icon={Activity}
titleId="admin-host-metrics-heading"
title={t("admin.hostMetrics.title")}
status={
<span className="text-muted-foreground text-xs">
{t("admin.hostMetrics.last2hours")}
</span>
}
/>
<QueryErrorRow
message={t("admin.hostMetrics.loadError")}
onRetry={() => void refetch()}
/>
</SettingsCard>
);
}
// Empty: the sampler runs every 60s, so a freshly-deployed instance
// takes 1–2 minutes to surface its first row. Show a friendly hint
// instead of an empty plot.
if (rows.length === 0) {
return (
<SettingsCard as="section" aria-labelledby="admin-host-metrics-heading">
<SettingsCardHeader
icon={Activity}
titleId="admin-host-metrics-heading"
title={t("admin.hostMetrics.title")}
status={
<span className="text-muted-foreground text-xs">
{t("admin.hostMetrics.last2hours")}
</span>
}
/>
<p className="text-muted-foreground text-xs">
{t("admin.hostMetrics.empty")}
</p>
</SettingsCard>
);
}
return (
<SettingsCard as="section" aria-labelledby="admin-host-metrics-heading">
<SettingsCardHeader
icon={Activity}
titleId="admin-host-metrics-heading"
title={t("admin.hostMetrics.title")}
status={
<span className="text-muted-foreground text-xs">
{t("admin.hostMetrics.last2hours")}
</span>
}
/>
<div className="h-[200px] w-full touch-pan-y">
<ResponsiveContainer width="100%" height="100%">
<LineChart
data={rows}
margin={{ top: 10, right: 12, bottom: 8, left: 8 }}
>
<CartesianGrid
strokeDasharray="3 3"
stroke="var(--border)"
opacity={0.5}
/>
<XAxis
dataKey="label"
tick={{ fontSize: 11, fill: "var(--muted-foreground)" }}
tickLine={false}
axisLine={false}
interval="preserveStartEnd"
minTickGap={32}
/>
{/* Left axis: load (raw number) — Linux scheduler convention,
typical idle host = 0..1, multi-core saturation > #cpu. */}
<YAxis
yAxisId="load"
orientation="left"
tick={{ fontSize: 11, fill: "var(--muted-foreground)" }}
tickLine={false}
axisLine={false}
width={36}
domain={[0, "auto"]}
/>
{/* Right axis: percentage — shared by memory + diskBusy. */}
<YAxis
yAxisId="percent"
orientation="right"
tick={{ fontSize: 11, fill: "var(--muted-foreground)" }}
tickLine={false}
axisLine={false}
width={40}
domain={[0, 100]}
tickFormatter={(value: number) => `${Math.round(value)}%`}
/>
<Tooltip
contentStyle={{
backgroundColor: "var(--card)",
border: "1px solid var(--border)",
borderRadius: "0.5rem",
fontSize: "0.75rem",
}}
labelStyle={{ color: "var(--foreground)" }}
itemStyle={{ color: "var(--foreground)" }}
formatter={(value, name) => {
if (typeof value !== "number") return [String(value), name];
if (name === t("admin.hostMetrics.load1")) {
return [fmt.number(value, 2), name];
}
if (name === t("admin.hostMetrics.memUsedPercent")) {
if (memTotalGiB > 0) {
const usedGiB = (value / 100) * memTotalGiB;
return [
`${fmt.number(value, 1)} % · ${fmt.number(usedGiB, 1)} / ${fmt.number(memTotalGiB, 1)} GiB`,
name,
];
}
return [`${fmt.number(value, 1)} %`, name];
}
if (name === t("admin.hostMetrics.diskBusyPercent")) {
// Show the actual MiB/s alongside the relative share —
// the percent alone is meaningless without the peak.
const peakMiB = peakDiskBps / 1_048_576;
const absMiB = (value / 100) * peakMiB;
return [
`${fmt.number(value, 1)} % · ${fmt.number(absMiB, 2)} MiB/s`,
name,
];
}
return [String(value), name];
}}
/>
<Legend
wrapperStyle={{
fontSize: "0.75rem",
fontFamily: "inherit",
fontWeight: "normal",
}}
/>
<Line
yAxisId="load"
type="monotone"
dataKey="loadAvg1"
name={t("admin.hostMetrics.load1")}
stroke={COLOR_LOAD}
strokeWidth={2}
dot={false}
activeDot={{ r: 4 }}
isAnimationActive={false}
/>
<Line
yAxisId="percent"
type="monotone"
dataKey="memUsedPercent"
name={t("admin.hostMetrics.memUsedPercent")}
stroke={COLOR_MEM}
strokeWidth={2}
dot={false}
activeDot={{ r: 4 }}
isAnimationActive={false}
/>
{hasDiskData && (
<Line
yAxisId="percent"
type="monotone"
dataKey="diskBusyPercent"
name={t("admin.hostMetrics.diskBusyPercent")}
stroke={COLOR_DISK}
strokeWidth={2}
dot={false}
activeDot={{ r: 4 }}
connectNulls
isAnimationActive={false}
/>
)}
</LineChart>
</ResponsiveContainer>
</div>
</SettingsCard>
);
}