perf(dashboard): batched rollup series endpoint + server-side target bands + gate-helper split

This commit is contained in:
Marc-André Bombeck committed 2026-06-18 15:33:53 +02:00
commit 82c6947633
16 files changed
+1648 -343

No files matched your search

+71 -12
View File
@@ -233,6 +233,23 @@ interface HealthChartProps {
* idempotent; non-dashboard mounts simply omit it.
*/
onDataReady?: () => void;
/**
* v1.18.6 — pre-fetched daily series, keyed by `MeasurementType`. When
* the dashboard batches every visible chart's series into ONE
* `/api/measurements/series-batch` round-trip (audit finding #2), it
* passes each chart its slice here. When supplied, the chart's queryFn
* consumes these rows for the covered types instead of issuing its own
* per-type `/api/measurements` fetch. Omitted on non-dashboard mounts
* (insights sub-pages, mini cards) which keep self-fetching.
* SLEEP_DURATION is never pre-fetched (it rides the per-night `/series`
* adapter), so a sleep chart self-fetches that branch regardless.
*
* The rows must already be scoped to the chart's active fetch window;
* the prop is keyed into the query cache so a range-tab change (wider
* window) re-fetches cleanly rather than blending a batched slice with
* a self-fetched one.
*/
preloadedSeries?: Record<string, MeasurementApiRow[]>;
}
interface ChartDataPoint {
@@ -251,8 +268,13 @@ interface MeasurementApiRow {
// path (non-cumulative metrics only). Drive the Apple-Health-style
// range band shaded around the mean line. Absent on the raw-row path
// (windows ≤ 7 days) and on cumulative metrics.
minValue?: number;
maxValue?: number;
// v1.18.6 — `null` accepted alongside `undefined` so the batched
// series endpoint's row shape (which carries `number | null`) feeds
// `preloadedSeries` directly. The bucketing loop already guards each
// with a `typeof … === "number"` check, so a null is ignored exactly
// like an absent value.
minValue?: number | null;
maxValue?: number | null;
// v1.4.43 W2-CHART-GATE — the rollup / daily-aggregate paths return
// the underlying raw-row count per bucket. Used downstream to gate
// the "more days needed" empty-state copy on the actual measurement
@@ -537,6 +559,7 @@ export function HealthChart({
onVisibleStats,
titleIcon,
onDataReady,
preloadedSeries,
}: HealthChartProps) {
const { isAuthenticated, user } = useAuth();
const { t, locale } = useTranslations();
@@ -620,6 +643,20 @@ export function HealthChart({
};
}, [rangePoints, effectiveCompareBaseline]);
// v1.18.6 — honour the dashboard's batched slice ONLY for the
// daily-aggregate window (`windowDays > 7`, the rollup path) and ONLY
// when EVERY non-sleep type the chart renders is present in the slice.
// SLEEP_DURATION is never batched (per-night `/series`), so a chart
// that includes it always self-fetches. A range-tab change to a ≤7-day
// window (raw rows) drops batched coverage and self-fetches.
const usePreloaded = useMemo(() => {
if (!preloadedSeries) return false;
if (fetchWindow.windowDays <= 7) return false;
return types.every(
(type) => type === "SLEEP_DURATION" || preloadedSeries[type] != null,
);
}, [preloadedSeries, fetchWindow.windowDays, types]);
const { data, isLoading, isError, refetch } = useQuery({
// v1.4.40 W-RSC — route the chart-data key through `queryKeys.chartData`
// so the `["chart-data"]` prefix lands in `measurementDependentKeys`
@@ -649,6 +686,14 @@ export function HealthChart({
// v1.7.0 — re-key when the display scale changes so a km/h chart
// never reads a m/s-scaled sibling out of the cache.
valueScale,
// v1.18.6 — discriminate a batched-slice render from a self-fetch
// ONLY on the dashboard mounts that pass `preloadedSeries`. When the
// prop is absent (insights sub-pages, mini cards) the empty string
// keeps the factory tuple byte-identical with the pre-v1.18.6
// 8-element key, so those callers' cache layout is unchanged. On a
// dashboard mount the discriminator flips "preloaded" → "fetch" when
// a range-tab change drops batched coverage, re-fetching cleanly.
preloadedSeries ? (usePreloaded ? "preloaded" : "fetch") : "",
),
// v1.4.28 FB-D2 — cache the bounded window for a minute so tab
// navigation between insights sub-pages does not re-fire every
@@ -760,16 +805,30 @@ export function HealthChart({
typeParams.set("source", "rollup");
}
// v1.16.8 — a failed per-type fetch rejects the whole query
// instead of silently dropping the series: the swallowed-catch
// variant cached the partial/empty result as fresh success, so
// an outage painted the empty state (or a half chart) with no
// retry path. Rejecting lets TanStack's retry semantics and the
// in-card error state below do their jobs.
const data = await apiGet<{ measurements?: MeasurementApiRow[] }>(
`/api/measurements?${typeParams}`,
);
const page = data?.measurements ?? [];
// v1.18.6 — when the dashboard handed this chart a batched slice
// for the type, fold those rows instead of a per-type fetch. The
// rows came from `/api/measurements/series-batch`, which reads
// through the SAME rollup path the per-type daily fetch hits, so
// the bucketed result is byte-identical. `usePreloaded` already
// gated the window (`> 7`) + per-type coverage above; default to
// an empty slice (paints the no-data card) rather than fetching,
// so a covered chart never issues a network request.
const page =
usePreloaded && preloadedSeries
? (preloadedSeries[type] ?? [])
: await (async () => {
// v1.16.8 — a failed per-type fetch rejects the whole
// query instead of silently dropping the series: the
// swallowed-catch variant cached the partial/empty result
// as fresh success, so an outage painted the empty state
// (or a half chart) with no retry path. Rejecting lets
// TanStack's retry semantics and the in-card error state
// below do their jobs.
const data = await apiGet<{
measurements?: MeasurementApiRow[];
}>(`/api/measurements?${typeParams}`);
return data?.measurements ?? [];
})();
for (const measurement of page) {
// v1.7.0 — fold the display-time scale into the raw value at
+119
View File
@@ -0,0 +1,119 @@
/**
* v1.18.6 — dashboard first-paint / skeleton-reservation gates.
*
* Extracted verbatim from `src/app/page.tsx` so the page component reads
* as a thinner orchestrator and these pure helpers (already unit-tested)
* live in one cohesive module. `page.tsx` re-exports them so the existing
* `../page` test imports stay valid.
*/
import type { DashboardLayout } from "@/lib/dashboard-layout";
/**
* v1.7.0 — first-paint gate for the dashboard tile strip.
*
* `primaryLoading` must come from whichever query actually drives the
* tiles: the snapshot cell by default, the slim analytics cell when
* `NEXT_PUBLIC_DASHBOARD_SNAPSHOT=false`. A disabled TanStack query reports
* `isLoading: false` (idle fetch status), so keying off the wrong
* source flashes the empty state for the whole fetch. Pure + exported
* so the gate has direct unit coverage without mounting the page.
*/
export function resolveDashboardFirstPaintGate(input: {
trendCardCount: number;
chartCount: number;
configuredTileCount: number;
primaryLoading: boolean;
}): { showTileStripSkeleton: boolean; showEmptyState: boolean } {
const showTileStripSkeleton =
input.trendCardCount === 0 &&
input.primaryLoading &&
input.configuredTileCount > 0;
const showEmptyState =
input.trendCardCount === 0 &&
input.chartCount === 0 &&
!showTileStripSkeleton &&
!input.primaryLoading;
return { showTileStripSkeleton, showEmptyState };
}
/**
* v1.16.8 — widget ids that actually paint a tile in the strip on the
* web dashboard, mirrored from the per-id render blocks below.
* `medications` / `recentWorkouts` / `achievements` carry NO strip tile
* (chart-row cards only), and the iOS-pin-only ids have no web render
* path at all — counting any of them over-reserved the loading
* silhouette and made the strip reshuffle when the data landed.
*/
const TILE_CAPABLE_WIDGET_IDS = new Set<string>([
"weight",
"bp",
"pulse",
"bodyFat",
"mood",
"sleep",
"steps",
"glucose",
"bpInTarget",
"vo2Max",
]);
/**
* v1.16.8 — silhouette count for the tile-strip skeleton: one card per
* tile-capable, tile-visible widget. `bp` paints TWO tiles (sys + dia,
* see the render block below) so it counts double; `glucose` can fan
* out to one tile per logged context, but the contexts are unknown
* until the snapshot lands, so it reserves one card (the sane floor).
* Pure + exported for direct unit coverage.
*/
export function resolveConfiguredTileCount(layout: DashboardLayout): number {
let count = 0;
for (const widget of layout.widgets) {
if (!TILE_CAPABLE_WIDGET_IDS.has(widget.id)) continue;
if (!(widget.tileVisible ?? widget.visible)) continue;
count += widget.id === "bp" ? 2 : 1;
}
return count;
}
/**
* v1.16.8 — widget ids with a chart-row surface on the web dashboard,
* mirrored from the `charts[]` entries below. The achievements +
* recent-workouts cards stay out: they self-skeleton, carry no
* chart-shaped footprint, and gate on `layoutResolved`.
*/
const CHART_CAPABLE_WIDGET_IDS = new Set<string>([
"weight",
"bp",
"pulse",
"bodyFat",
"mood",
"sleep",
"steps",
"medications",
// v1.18.2 — Vorsorge preventive-care summary card (chart-row only).
"vorsorge",
]);
/**
* v1.16.8 — expected chart-row card count while the snapshot is still
* in flight. Every chart gate below needs `count > 0` from snapshot
* data, so the cold page used to render NO chart row at all and then
* grow ~1000 px when the snapshot landed. The best available signal
* before data arrives is the layout config (the user's saved layout
* when cached, the default otherwise): one card per chart-visible
* widget, plus the BMI card that rides the weight gate when the
* profile carries a height. Pure + exported for direct unit coverage.
*/
export function resolveChartRowPlaceholderCount(
layout: DashboardLayout,
opts?: { hasHeightCm?: boolean },
): number {
let count = 0;
for (const widget of layout.widgets) {
if (!CHART_CAPABLE_WIDGET_IDS.has(widget.id)) continue;
if (!widget.visible) continue;
count += 1;
if (widget.id === "weight" && opts?.hasHeightCm) count += 1;
}
return count;
}
@@ -6,6 +6,7 @@ import { join } from "node:path";
import { I18nProvider } from "@/lib/i18n/context";
import { makeFormatters } from "@/lib/format-locale";
import type { DashboardSnapshot } from "@/lib/dashboard/snapshot";
import { buildTargetBands } from "@/lib/dashboard/snapshot";
import type { MedsTodayBlock } from "@/lib/dashboard/meds-today";
import type { DataSummary } from "@/lib/analytics/trends";
import { DashboardHero } from "../dashboard-hero";
@@ -92,6 +93,11 @@ function baseSnapshot(
layout: { version: 1, widgets: [] },
layoutCatalogue: [],
metricStates: {},
targetBands: buildTargetBands({
dateOfBirth: null,
gender: null,
heightCm: null,
}),
tiles: {
summaries: {},
lastSeenByType: {},