perf: consolidate integration status query, add mood-insights SWR, defer below-fold charts

Settings → Integrations fired five status round-trips on every mount —
the consolidated /api/integrations/status alongside the four per-provider
/api/<provider>/status queries. Extend the consolidated endpoint to carry
every field the four cards render (Withings activity scope, WHOOP/Fitbit
backfill state, moodLog webhook secret + entry count) and drive all four
cards off the single envelope. The legacy per-provider routes stay for the
iOS/test callers; the web cards no longer hit them.

Mood-insights cache was a plain 60 s TTL that was hard-evicted on every
mood write, so an active logger re-paid the multi-second cold compute on
every entry. Add stale-while-revalidate to ServerCache (wrapSwr / cachedSwr
+ a markStaleByPrefix that collapses the fresh TTL without dropping the
value), give the moodInsights bucket a 10-minute stale window, route the
read through cachedSwr, and switch the mood-write invalidation from a hard
evict to a mark-stale so the prior aggregate serves immediately while a
single background recompute warms a fresh one.

Defer the eager Recharts on the mood-insights surface. The trajectory
forecast card pulled Recharts into the initial chunk of every
trajectory-eligible metric page though the main chart is already deferred;
split its fan band into trajectory-fan.tsx behind next/dynamic. The three
below-fold mood mini-charts (distribution / weekday / time-of-day) were
static imports under an already-dynamic hero line chart; defer them with
next/dynamic. Each loader paints a skeleton sized to the chart's own band
so the chunk arrives without a layout shift; the charts stay Recharts and
visually identical.

(cherry picked from commit 2b627893ab6b4d7bc457fc18e6e5585b048190e1)
This commit is contained in:
Marc-André Bombeck committed 2026-06-05 03:25:22 +02:00
1 parent 05cff531e4
commit 53e87ba345
11 files changed
+632 -254

No files matched your search

+18
View File
@@ -59,6 +59,10 @@ async function primeAllCaches(): Promise<void> {
await cached(caches.moodAnalytics, USER_A, async () => ({ mood: 1 }));
await cached(caches.moodAnalytics, USER_B, async () => ({ mood: 2 }));
// v1.12.1 — mood-insights aggregate cache (SWR bucket).
await cached(caches.moodInsights, USER_A, async () => ({ mi: 1 }));
await cached(caches.moodInsights, USER_B, async () => ({ mi: 2 }));
await cached(caches.dashboardWidgets, USER_A, async () => ({ d: 1 }));
await cached(caches.dashboardWidgets, USER_B, async () => ({ d: 2 }));
@@ -114,6 +118,20 @@ describe("invalidateUserMood", () => {
expect(caches.analytics.get(dashboardSnapshotCacheKey(USER_A))).toBeNull();
expect(caches.moodAnalytics.get(USER_B)).not.toBeNull();
expect(caches.analytics.get(`${USER_B}|default`)).not.toBeNull();
// v1.12.1 — mood-insights is marked stale, not hard-evicted: the
// prior value is still serveable within the SWR window so the next
// reader pays no cold compute. (A plain `get()` would treat the
// collapsed TTL as expired and drop it, so assert via getAllowStale
// first — it must not consume the entry.) User B stays fresh.
expect(caches.moodInsights.getAllowStale(USER_A)).toEqual({
value: { mi: 1 },
stale: true,
});
expect(caches.moodInsights.getAllowStale(USER_B)).toEqual({
value: { mi: 2 },
stale: false,
});
});
});
+178
View File
@@ -10,6 +10,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import {
__resetAllCachesForTests,
cached,
cachedSwr,
caches,
hashCacheKey,
ServerCache,
@@ -222,6 +223,183 @@ describe("cached() observability wrapper", () => {
});
});
describe("ServerCache stale-while-revalidate (wrapSwr / cachedSwr)", () => {
it("serves a fresh hit without rebuilding", async () => {
const cache = new ServerCache<string>({
maxEntries: 4,
ttlMs: 60_000,
staleTtlMs: 600_000,
});
const builder = vi.fn(async () => "v");
const first = await cache.wrapSwr("k", builder);
expect(first.outcome).toBe("miss");
const second = await cache.wrapSwr("k", builder);
expect(second.outcome).toBe("hit");
expect(builder).toHaveBeenCalledTimes(1);
});
it("serves the stale value immediately and revalidates in the background", async () => {
vi.useFakeTimers();
const cache = new ServerCache<string>({
maxEntries: 4,
ttlMs: 1000,
staleTtlMs: 600_000,
});
let calls = 0;
const builder = vi.fn(async () => `v${++calls}`);
// Cold compute → v1.
const cold = await cache.wrapSwr("k", builder);
expect(cold.value).toBe("v1");
expect(cold.outcome).toBe("miss");
// Past the fresh TTL but inside the stale window.
vi.advanceTimersByTime(1500);
const stale = await cache.wrapSwr("k", builder);
// The active caller gets the prior value immediately — it did NOT
// pay the cold compute.
expect(stale.value).toBe("v1");
expect(stale.outcome).toBe("stale");
// The background recompute settles to a fresh value.
await vi.runAllTimersAsync();
expect(builder).toHaveBeenCalledTimes(2);
const warm = await cache.wrapSwr("k", builder);
expect(warm.value).toBe("v2");
expect(warm.outcome).toBe("hit");
});
it("coalesces a burst of stale reads into a single background rebuild", async () => {
vi.useFakeTimers();
const cache = new ServerCache<string>({
maxEntries: 4,
ttlMs: 1000,
staleTtlMs: 600_000,
});
// The cold build resolves immediately; the rebuild is gated on a
// manual promise so all three stale reads fire before it settles —
// proving they coalesce onto one in-flight rebuild.
let releaseRebuild: (() => void) | null = null;
let calls = 0;
const builder = vi.fn(async () => {
calls += 1;
if (calls === 1) return "v1";
await new Promise<void>((r) => {
releaseRebuild = r;
});
return "v2";
});
await cache.wrapSwr("k", builder);
vi.advanceTimersByTime(1500);
const a = await cache.wrapSwr("k", builder);
const b = await cache.wrapSwr("k", builder);
const c = await cache.wrapSwr("k", builder);
expect([a.outcome, b.outcome, c.outcome]).toEqual([
"stale",
"stale",
"stale",
]);
// The single in-flight rebuild is still pending across the burst.
expect(builder).toHaveBeenCalledTimes(2);
releaseRebuild!();
await vi.runAllTimersAsync();
// Still exactly one cold build + one background rebuild.
expect(builder).toHaveBeenCalledTimes(2);
});
it("treats an entry past the stale window as a hard miss", async () => {
vi.useFakeTimers();
const cache = new ServerCache<string>({
maxEntries: 4,
ttlMs: 1000,
staleTtlMs: 2000,
});
let calls = 0;
const builder = vi.fn(async () => `v${++calls}`);
await cache.wrapSwr("k", builder);
// Past ttl + staleTtl → the stale window is closed.
vi.advanceTimersByTime(3500);
const miss = await cache.wrapSwr("k", builder);
expect(miss.outcome).toBe("miss");
expect(miss.value).toBe("v2");
});
it("markStaleByPrefix keeps serving the prior value under SWR (no cold re-pay)", async () => {
const cache = new ServerCache<string>({
maxEntries: 4,
ttlMs: 60_000,
staleTtlMs: 600_000,
});
let calls = 0;
const builder = vi.fn(async () => `v${++calls}`);
await cache.wrapSwr("u1|k", builder);
// Simulate a mood write marking the bucket stale rather than evicting.
expect(cache.markStaleByPrefix("u1|")).toBe(1);
// The next read still serves the prior value immediately (stale),
// not a hard miss — so the active logger doesn't re-pay the compute.
const afterMark = await cache.wrapSwr("u1|k", builder);
expect(afterMark.outcome).toBe("stale");
expect(afterMark.value).toBe("v1");
});
it("a rejected background rebuild keeps the stale value and retries next read", async () => {
vi.useFakeTimers();
const cache = new ServerCache<string>({
maxEntries: 4,
ttlMs: 1000,
staleTtlMs: 600_000,
});
let calls = 0;
const builder = vi.fn(async () => {
calls += 1;
if (calls === 2) throw new Error("transient");
return `v${calls}`;
});
await cache.wrapSwr("k", builder);
vi.advanceTimersByTime(1500);
// Stale read triggers a background rebuild that rejects.
const stale = await cache.wrapSwr("k", builder);
expect(stale.value).toBe("v1");
await vi.runAllTimersAsync();
// The prior value still serves; the next read retries the build.
const retry = await cache.wrapSwr("k", builder);
expect(retry.outcome).toBe("stale");
expect(retry.value).toBe("v1");
await vi.runAllTimersAsync();
const warm = await cache.wrapSwr("k", builder);
expect(warm.value).toBe("v3");
});
it("cachedSwr annotates the outcome (hit / stale / miss)", async () => {
vi.useFakeTimers();
const cache = new ServerCache<string>({
name: "moodInsights",
maxEntries: 4,
ttlMs: 1000,
staleTtlMs: 600_000,
});
const annotate = vi.fn();
const builder = vi.fn(async () => "v");
await cachedSwr(cache, "u1", builder, annotate);
expect(annotate).toHaveBeenLastCalledWith({
meta: expect.objectContaining({ "cache.moodInsights.outcome": "miss" }),
});
vi.advanceTimersByTime(1500);
annotate.mockClear();
await cachedSwr(cache, "u1", builder, annotate);
expect(annotate).toHaveBeenLastCalledWith({
meta: expect.objectContaining({ "cache.moodInsights.outcome": "stale" }),
});
});
});
describe("global cache registry", () => {
it("exposes the eight blueprint caches as module-scope singletons", () => {
expect(caches.analytics).toBeDefined();
+6 -1
View File
@@ -78,7 +78,12 @@ export function invalidateUserMood(userId: string): void {
caches.insightsTargets.deleteByPrefix(userId);
// v1.8.5 — mood writes dirty every dimension of the mood-insights
// aggregate (heatmap cell, distribution, weekday, tag breakdown).
caches.moodInsights.deleteByPrefix(userId);
// v1.12.1 — mark stale rather than hard-evict: the mood-insights read
// is a multi-second cold compute, so an active logger writing back to
// back used to re-pay it on every entry. Marking stale keeps serving
// the prior aggregate (within the SWR window) while a single
// background recompute warms a fresh one.
caches.moodInsights.markStaleByPrefix(userId);
}
/**
+160 -4
View File
@@ -41,6 +41,15 @@ export interface ServerCacheOptions {
readonly maxEntries: number;
/** Time-to-live in milliseconds. Expired entries miss on read. */
readonly ttlMs: number;
/**
* v1.12.1 — stale-while-revalidate window in milliseconds. When set,
* an entry whose `ttlMs` has lapsed but is still inside
* `ttlMs + staleTtlMs` is served immediately (stale) by `wrapSwr()`,
* which kicks off a single background recompute to refresh it. Past
* the stale window the entry is a hard miss like any other. Leave
* unset for the legacy hard-TTL behaviour.
*/
readonly staleTtlMs?: number;
/**
* Optional name for observability — surfaced in `stats().name` so the
* wide-event meta keys can carry the cache identity without the
@@ -59,7 +68,10 @@ export interface ServerCacheStats {
}
export class ServerCache<T> {
private readonly map = new Map<string, { expiresAt: number; value: T }>();
private readonly map = new Map<
string,
{ expiresAt: number; staleUntil: number; value: T }
>();
private readonly pending = new Map<string, Promise<T>>();
private hits = 0;
private misses = 0;
@@ -114,12 +126,61 @@ export class ServerCache<T> {
this.evictions += 1;
}
}
const now = Date.now();
const expiresAt = now + (ttlMsOverride ?? this.opts.ttlMs);
this.map.set(key, {
expiresAt: Date.now() + (ttlMsOverride ?? this.opts.ttlMs),
expiresAt,
// The stale window extends past the fresh TTL only when the
// bucket opted into SWR; otherwise `staleUntil === expiresAt`
// so `getAllowStale` behaves exactly like `get`.
staleUntil: expiresAt + (this.opts.staleTtlMs ?? 0),
value,
});
}
/**
* v1.12.1 — read a key allowing a stale entry inside the SWR window.
* Returns the value plus a `stale` flag (true when the fresh TTL has
* lapsed but the entry is still inside `staleTtlMs`). Past the stale
* window the entry is removed and `null` is returned. LRU-touches a
* live or stale entry so a hot key never falls out under pressure.
*/
getAllowStale(key: string): { value: T; stale: boolean } | null {
const entry = this.map.get(key);
if (!entry) return null;
const now = Date.now();
if (entry.staleUntil <= now) {
this.map.delete(key);
return null;
}
// LRU touch.
this.map.delete(key);
this.map.set(key, entry);
return { value: entry.value, stale: entry.expiresAt <= now };
}
/**
* v1.12.1 — mark every entry under `prefix` stale without dropping it.
* Collapses the fresh TTL to "now" so the next `wrapSwr` read serves
* the prior value immediately (within the stale window) while a single
* coalesced recompute warms a fresh value. This replaces the
* hard-evict on a mood write so an active logger no longer re-pays the
* cold compute on every entry. No-op for buckets without `staleTtlMs`
* (the entry is already at/over its hard TTL after this, so the next
* read is a clean miss — identical to the old evict behaviour).
*/
markStaleByPrefix(prefix: string): number {
let marked = 0;
const now = Date.now();
for (const [key, entry] of this.map) {
if (key.startsWith(prefix)) {
entry.expiresAt = now;
marked += 1;
}
}
return marked;
}
/** Single-key eviction. Returns true when the key was present. */
delete(key: string): boolean {
return this.map.delete(key);
@@ -187,6 +248,64 @@ export class ServerCache<T> {
return { value, outcome: "miss" };
}
/**
* v1.12.1 — stale-while-revalidate read. On a fresh hit it returns the
* cached value like `wrap`. On a stale hit (entry past its fresh TTL
* but inside the SWR window) it returns the prior value IMMEDIATELY
* and kicks off a single background recompute — the active caller
* never pays the cold compute. On a hard miss / cold key it falls back
* to `wrap`'s single-flight builder and awaits it.
*
* Background revalidation reuses the `pending` single-flight slot so a
* burst of stale reads coalesces into one recompute. A rejected
* background build is swallowed (the stale value already served) and
* clears `pending` so the next read retries.
*/
async wrapSwr(
key: string,
builder: () => Promise<T>,
ttlMsOverride?: number,
): Promise<{
value: T;
outcome: "hit" | "stale" | "miss" | "stampede";
}> {
const cachedEntry = this.getAllowStale(key);
if (cachedEntry && !cachedEntry.stale) {
this.hits += 1;
return { value: cachedEntry.value, outcome: "hit" };
}
if (cachedEntry && cachedEntry.stale) {
// Serve stale now; revalidate in the background under the
// single-flight slot so concurrent stale reads share one rebuild.
this.hits += 1;
if (!this.pending.has(key)) {
const refresh = builder()
.then((value) => {
this.set(key, value, ttlMsOverride);
return value;
})
.catch((err) => {
// The stale value already went out; don't crash the worker.
// Re-throw so the join path of any awaiting caller sees it.
throw err;
})
.finally(() => {
this.pending.delete(key);
});
this.pending.set(key, refresh);
// Detach: the foreground caller does not await the rebuild.
// Swallow the rejection on the detached handle so an
// unhandled-rejection warning never fires.
refresh.catch(() => {});
}
return { value: cachedEntry.value, outcome: "stale" };
}
// Hard miss / cold key — fall back to the awaited single-flight path.
return this.wrap(key, builder, ttlMsOverride);
}
/** Snapshot of per-instance counters for the wide-event annotation. */
stats(): ServerCacheStats {
return {
@@ -264,13 +383,22 @@ export const caches = {
* v1.8.5 — pre-computed mood-insights aggregates (heatmap, distribution,
* weekday, tags, cross-metric correlations) for the Mood Insights page.
* Read on every `/insights/mood` mount; bounded 365-day live read behind
* the cache. 60 s TTL matches `moodAnalytics`; invalidated alongside it
* on a mood write via `invalidateUserMood`.
* the cache. 60 s fresh TTL matches `moodAnalytics`.
*
* v1.12.1 — stale-while-revalidate. The read route uses `cachedSwr` and
* a mood write marks the bucket stale (not a hard evict) via
* `invalidateUserMood`. An active logger now serves the prior aggregate
* immediately while a single background recompute warms a fresh one,
* instead of re-paying the multi-second cold compute on every entry
* (the v1.8.7 "sync evicting the cache all day" class, on mood). The
* 10-minute stale window bounds how old a served aggregate can be when
* no reader has triggered a revalidation.
*/
moodInsights: new ServerCache<unknown>({
name: "moodInsights",
maxEntries: 1000,
ttlMs: 60_000,
staleTtlMs: 600_000,
}),
/**
* v1.4.36 W1 — `/api/insights/targets` is read on every Insights
@@ -342,6 +470,34 @@ export async function cached<T>(
return value;
}
/**
* v1.12.1 — stale-while-revalidate read-through. Same observability
* contract as `cached` (annotates the wide-event with the outcome +
* key hash) but routes through `wrapSwr`: a stale entry inside the
* bucket's `staleTtlMs` window is served immediately while a single
* background recompute refreshes it, so an active caller never pays
* the cold compute on an expired or freshly-marked-stale bucket.
*/
export async function cachedSwr<T>(
cache: ServerCache<T>,
key: string,
builder: () => Promise<T>,
annotateFn?: (fields: { meta: Record<string, unknown> }) => void,
ttlMsOverride?: number,
): Promise<T> {
const { value, outcome } = await cache.wrapSwr(key, builder, ttlMsOverride);
if (annotateFn) {
const name = cache.stats().name;
annotateFn({
meta: {
[`cache.${name}.outcome`]: outcome,
[`cache.${name}.key_hash`]: hashCacheKey(key),
},
});
}
return value;
}
/** Test helper — reset every cache in the registry. */
export function __resetAllCachesForTests(): void {
for (const cache of Object.values(caches)) {