mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
chore: remove the dashboard's pre-snapshot feed
NEXT_PUBLIC_DASHBOARD_SNAPSHOT=false built a dashboard that read its tiles from two analytics slices, the widget layout, the mood analytics and the thresholds instead of the one snapshot cell. It took a rebuilt image to reach, and the snapshot has been the only path the published image runs since it became the default. The flag, its readers, the legacy reads in the dashboard hook and tile context, the slim and thick merge helper, the build-time toggle notes in .env.production.example and the self-hosting guide, and the tests that pinned the legacy wiring go.
This commit is contained in:
1 parent
d99456138b
commit
9dc519dd85
26 files changed
+106
-729
No files matched your search
@@ -1,141 +0,0 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import { mergeSlimAndThickAnalytics } from "@/lib/analytics/merge-slim-thick";
|
||||
import type { DataSummary } from "@/lib/analytics/trends";
|
||||
|
||||
/**
|
||||
* v1.4.39.3 — dashboard slim/thick merge robustness.
|
||||
*
|
||||
* v1.4.39.2 split the dashboard's analytics consumption into a slim
|
||||
* `?slice=summaries` query and the thick default query. The original
|
||||
* inline merge used `slim?.summaries ?? thick?.summaries` which
|
||||
* short-circuited on a truthy-but-empty `{}` from the slim slice and
|
||||
* blanked the tile strip even when thick carried the full payload —
|
||||
* the regression the maintainer's v1.4.39.3 e2e CI flagged across eight
|
||||
* dashboard / chart specs (chart-overlay-controls, dashboard,
|
||||
* measurement-flow, charts-mobile). The pure helper extracted in
|
||||
* v1.4.39.3 uses object emptiness as the discriminator so a populated
|
||||
* thick payload survives an empty slim resolve.
|
||||
*/
|
||||
|
||||
const stubSummary = (): DataSummary => ({
|
||||
count: 30,
|
||||
latest: 78.5,
|
||||
min: 76,
|
||||
max: 80,
|
||||
mean: 78,
|
||||
median: 78,
|
||||
avg7: 78.2,
|
||||
avg30: 77.9,
|
||||
slope7: null,
|
||||
slope30: { slope: -0.05, direction: "down", confidence: 0.5 },
|
||||
anomalyCount: 0,
|
||||
});
|
||||
|
||||
describe("mergeSlimAndThickAnalytics", () => {
|
||||
it("returns undefined when neither slim nor thick has resolved", () => {
|
||||
expect(mergeSlimAndThickAnalytics(undefined, undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("prefers slim summaries when slim carries content (progressive paint)", () => {
|
||||
const slim = {
|
||||
summaries: {
|
||||
WEIGHT: stubSummary(),
|
||||
},
|
||||
};
|
||||
const thick = {
|
||||
summaries: {
|
||||
WEIGHT: { ...stubSummary(), latest: 1 },
|
||||
PULSE: stubSummary(),
|
||||
},
|
||||
bpInTargetPct: 78,
|
||||
};
|
||||
const merged = mergeSlimAndThickAnalytics(slim, thick);
|
||||
// Slim wins on overlapping field — the v1.4.39.2 progressive-paint
|
||||
// contract is preserved.
|
||||
expect(merged?.summaries.WEIGHT?.latest).toBe(78.5);
|
||||
// Slim does not carry PULSE; merge keeps slim's record as-is
|
||||
// (matches the "as soon as slim lands, paint" semantic).
|
||||
expect(merged?.summaries.PULSE).toBeUndefined();
|
||||
// Thick still wires the BD-Zielbereich + glucose fields.
|
||||
expect(merged?.bpInTargetPct).toBe(78);
|
||||
});
|
||||
|
||||
it("falls back to thick when slim resolves with an empty summaries record", () => {
|
||||
// The v1.4.39.3 regression: empty slim `{}` is truthy by JS
|
||||
// semantics so the old `??` short-circuited on it, blanking the
|
||||
// tile strip even when thick had data.
|
||||
const slim = { summaries: {} as Record<string, DataSummary> };
|
||||
const thick = {
|
||||
summaries: {
|
||||
WEIGHT: stubSummary(),
|
||||
},
|
||||
bpInTargetPct: 78,
|
||||
};
|
||||
const merged = mergeSlimAndThickAnalytics(slim, thick);
|
||||
expect(merged?.summaries.WEIGHT?.latest).toBe(78.5);
|
||||
expect(merged?.bpInTargetPct).toBe(78);
|
||||
});
|
||||
|
||||
it("falls back to thick when slim has not resolved yet", () => {
|
||||
const thick = {
|
||||
summaries: {
|
||||
WEIGHT: stubSummary(),
|
||||
},
|
||||
bpInTargetPct: 78,
|
||||
};
|
||||
const merged = mergeSlimAndThickAnalytics(undefined, thick);
|
||||
expect(merged?.summaries.WEIGHT?.latest).toBe(78.5);
|
||||
expect(merged?.bpInTargetPct).toBe(78);
|
||||
});
|
||||
|
||||
it("returns slim summaries when only slim has resolved (thick still loading)", () => {
|
||||
const slim = {
|
||||
summaries: { WEIGHT: stubSummary() },
|
||||
};
|
||||
const merged = mergeSlimAndThickAnalytics(slim, undefined);
|
||||
expect(merged?.summaries.WEIGHT?.latest).toBe(78.5);
|
||||
// Thick-only fields collapse to null/undefined so consumers stay
|
||||
// undefined-safe — the BD-Zielbereich tile self-gates on these.
|
||||
expect(merged?.bpInTargetPct).toBeNull();
|
||||
expect(merged?.glucoseByContext).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns empty summaries when both sides resolve empty (legit zero-data tenant)", () => {
|
||||
const merged = mergeSlimAndThickAnalytics(
|
||||
{ summaries: {} },
|
||||
{ summaries: {} },
|
||||
);
|
||||
expect(merged?.summaries).toEqual({});
|
||||
expect(merged?.bpInTargetPct).toBeNull();
|
||||
});
|
||||
|
||||
it("falls back to thick lastSeenByType when slim has none", () => {
|
||||
const slim = { summaries: { WEIGHT: stubSummary() } };
|
||||
const thick = {
|
||||
summaries: { WEIGHT: stubSummary() },
|
||||
lastSeenByType: {
|
||||
WEIGHT: { lastSeenAt: "2026-05-01T00:00:00.000Z", daysAgo: 3 },
|
||||
},
|
||||
};
|
||||
const merged = mergeSlimAndThickAnalytics(slim, thick);
|
||||
expect(merged?.lastSeenByType?.WEIGHT?.daysAgo).toBe(3);
|
||||
});
|
||||
|
||||
it("prefers slim lastSeenByType when slim carries one", () => {
|
||||
const slim = {
|
||||
summaries: { WEIGHT: stubSummary() },
|
||||
lastSeenByType: {
|
||||
WEIGHT: { lastSeenAt: "2026-05-10T00:00:00.000Z", daysAgo: 1 },
|
||||
},
|
||||
};
|
||||
const thick = {
|
||||
summaries: { WEIGHT: stubSummary() },
|
||||
lastSeenByType: {
|
||||
WEIGHT: { lastSeenAt: "2026-05-01T00:00:00.000Z", daysAgo: 10 },
|
||||
},
|
||||
};
|
||||
const merged = mergeSlimAndThickAnalytics(slim, thick);
|
||||
expect(merged?.lastSeenByType?.WEIGHT?.daysAgo).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -1,137 +0,0 @@
|
||||
/**
|
||||
* v1.4.39.3 — pure helper that merges the dashboard's slim
|
||||
* (`/api/analytics?slice=summaries`) and thick (`/api/analytics`)
|
||||
* payloads into the single shape the dashboard tile-strip + chart row
|
||||
* consumes.
|
||||
*
|
||||
* Background — v1.4.39.2 split `useAnalyticsQuery` into two parallel
|
||||
* mounts so the per-type tile strip paints as soon as the slim slice
|
||||
* lands and the BD-Zielbereich + glucose tiles stream in from the
|
||||
* thick slice afterwards. The inline `useMemo` that performed the
|
||||
* merge originally used `slim?.summaries ?? thick?.summaries` —
|
||||
* structurally correct on production where both routes agree, but
|
||||
* silently buggy when slim resolves first with an *empty* `{}` (e.g.
|
||||
* a freshly-seeded tenant whose rollup tier hasn't folded yet, or a
|
||||
* test environment that mocks thick but not slim). The empty object
|
||||
* is truthy by JS semantics so `??` short-circuited and the tile
|
||||
* strip painted blank even though thick carried the full payload —
|
||||
* exactly the regression the v1.4.39.3 e2e CI surfaced across eight
|
||||
* dashboard / chart specs.
|
||||
*
|
||||
* Post-fix: object emptiness is the discriminator. A non-empty slim
|
||||
* payload still wins on overlapping fields (the v1.4.39.2 progressive-
|
||||
* paint contract). An empty slim falls through to thick. When both
|
||||
* are empty, the caller receives an empty record and the dashboard's
|
||||
* data-floor gates render the appropriate empty state.
|
||||
*
|
||||
* Kept as a pure helper so the regression is verified with a tight
|
||||
* unit test (`merge-slim-thick.test.ts`) rather than a source-text
|
||||
* snapshot — string matching against `src/app/page.tsx` would not
|
||||
* have caught the empty-object short-circuit bug.
|
||||
*/
|
||||
import type { DataSummary } from "@/lib/analytics/trends";
|
||||
import type { SleepSourceDiscrepancy } from "@/lib/analytics/sleep-night";
|
||||
|
||||
export interface AnalyticsSlimLike {
|
||||
summaries?: Record<string, DataSummary> | null;
|
||||
lastSeenByType?: Record<
|
||||
string,
|
||||
{ lastSeenAt: string; daysAgo: number } | null
|
||||
> | null;
|
||||
/**
|
||||
* v1.28.x — latest-night source-discrepancy annotation, carried by
|
||||
* both slices (the slim post-pass computes it; the thick body passes
|
||||
* it through). Additive and null-safe.
|
||||
*/
|
||||
sleepSourceDiscrepancy?: SleepSourceDiscrepancy | null;
|
||||
}
|
||||
|
||||
export interface AnalyticsThickLike extends AnalyticsSlimLike {
|
||||
bpInTargetPct?: number | null;
|
||||
bpInTargetPct7d?: number | null;
|
||||
bpInTargetPct30d?: number | null;
|
||||
bpInTargetPctAllTime?: number | null;
|
||||
bpInTargetPctPriorMonth?: number | null;
|
||||
bpInTargetPctPriorYear?: number | null;
|
||||
bpInTargetCount90?: number | null;
|
||||
bpInTargetSpanDays90?: number | null;
|
||||
glucoseByContext?: Record<string, unknown> | null;
|
||||
}
|
||||
|
||||
export interface MergedDashboardAnalytics {
|
||||
summaries: Record<string, DataSummary>;
|
||||
lastSeenByType?:
|
||||
Record<string, { lastSeenAt: string; daysAgo: number } | null> | undefined;
|
||||
bpInTargetPct: number | null;
|
||||
bpInTargetPct7d: number | null;
|
||||
bpInTargetPct30d: number | null;
|
||||
bpInTargetPctAllTime: number | null;
|
||||
bpInTargetPctPriorMonth: number | null;
|
||||
bpInTargetPctPriorYear: number | null;
|
||||
bpInTargetCount90: number | null;
|
||||
bpInTargetSpanDays90: number | null;
|
||||
glucoseByContext: Record<string, unknown> | undefined;
|
||||
sleepSourceDiscrepancy: SleepSourceDiscrepancy | null;
|
||||
}
|
||||
|
||||
function hasContent(
|
||||
record: Record<string, unknown> | null | undefined,
|
||||
): boolean {
|
||||
return record != null && Object.keys(record).length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge a slim and a thick analytics payload into the shape the
|
||||
* dashboard renders against. Either input may be `undefined`
|
||||
* (TanStack query unresolved) or carry an empty record (zero-data
|
||||
* tenant); the helper falls back to whichever side has content
|
||||
* without ever returning a payload that ignores a populated source.
|
||||
*
|
||||
* Returns `undefined` when neither input has resolved yet so the
|
||||
* caller can keep its loading-state branch unchanged.
|
||||
*/
|
||||
export function mergeSlimAndThickAnalytics(
|
||||
slim: AnalyticsSlimLike | undefined,
|
||||
thick: AnalyticsThickLike | undefined,
|
||||
): MergedDashboardAnalytics | undefined {
|
||||
if (!slim && !thick) return undefined;
|
||||
|
||||
const slimSummariesHaveContent = hasContent(slim?.summaries ?? null);
|
||||
const summaries: Record<string, DataSummary> = slimSummariesHaveContent
|
||||
? (slim!.summaries as Record<string, DataSummary>)
|
||||
: ((thick?.summaries as Record<string, DataSummary> | null | undefined) ??
|
||||
(slim?.summaries as Record<string, DataSummary> | null | undefined) ??
|
||||
{});
|
||||
|
||||
const slimFreshnessHasContent = hasContent(slim?.lastSeenByType ?? null);
|
||||
const lastSeenByType:
|
||||
Record<string, { lastSeenAt: string; daysAgo: number } | null> | undefined =
|
||||
slimFreshnessHasContent
|
||||
? (slim!.lastSeenByType as Record<
|
||||
string,
|
||||
{ lastSeenAt: string; daysAgo: number } | null
|
||||
>)
|
||||
: (thick?.lastSeenByType ?? undefined);
|
||||
|
||||
return {
|
||||
summaries,
|
||||
lastSeenByType,
|
||||
bpInTargetPct: thick?.bpInTargetPct ?? null,
|
||||
bpInTargetPct7d: thick?.bpInTargetPct7d ?? null,
|
||||
bpInTargetPct30d: thick?.bpInTargetPct30d ?? null,
|
||||
bpInTargetPctAllTime: thick?.bpInTargetPctAllTime ?? null,
|
||||
bpInTargetPctPriorMonth: thick?.bpInTargetPctPriorMonth ?? null,
|
||||
bpInTargetPctPriorYear: thick?.bpInTargetPctPriorYear ?? null,
|
||||
bpInTargetCount90: thick?.bpInTargetCount90 ?? null,
|
||||
bpInTargetSpanDays90: thick?.bpInTargetSpanDays90 ?? null,
|
||||
glucoseByContext: thick?.glucoseByContext ?? undefined,
|
||||
// Rides the same discriminator as `summaries`: whichever slice
|
||||
// supplied the summaries also supplies the sleep annotation, so the
|
||||
// marker can never point at a night the rendered headline doesn't
|
||||
// show. `?? null` tolerates older cached payloads without the field.
|
||||
sleepSourceDiscrepancy:
|
||||
(slimSummariesHaveContent
|
||||
? slim!.sleepSourceDiscrepancy
|
||||
: thick?.sleepSourceDiscrepancy) ?? null,
|
||||
};
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* v1.7.2 — unit tests for `isDashboardSnapshotEnabled`.
|
||||
*
|
||||
* The flag flipped default-ON in v1.7.2: the dashboard reads the unified
|
||||
* `/api/dashboard/snapshot` cell UNLESS `NEXT_PUBLIC_DASHBOARD_SNAPSHOT`
|
||||
* is exactly `"false"`. Pin the boundary so a future edit can't quietly
|
||||
* regress to the old `=== "true"` opt-in semantics.
|
||||
*/
|
||||
import { describe, it, expect, afterEach } from "vitest";
|
||||
|
||||
import { isDashboardSnapshotEnabled } from "../snapshot-flag";
|
||||
|
||||
describe("isDashboardSnapshotEnabled", () => {
|
||||
const original = process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT;
|
||||
|
||||
afterEach(() => {
|
||||
if (original === undefined) {
|
||||
delete process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT;
|
||||
} else {
|
||||
process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT = original;
|
||||
}
|
||||
});
|
||||
|
||||
it("defaults ON when the var is unset", () => {
|
||||
delete process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT;
|
||||
expect(isDashboardSnapshotEnabled()).toBe(true);
|
||||
});
|
||||
|
||||
it("stays ON for an empty string", () => {
|
||||
process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT = "";
|
||||
expect(isDashboardSnapshotEnabled()).toBe(true);
|
||||
});
|
||||
|
||||
it('turns OFF only for the exact string "false"', () => {
|
||||
process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT = "false";
|
||||
expect(isDashboardSnapshotEnabled()).toBe(false);
|
||||
});
|
||||
|
||||
it('stays ON for "true"', () => {
|
||||
process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT = "true";
|
||||
expect(isDashboardSnapshotEnabled()).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
/**
|
||||
* Reversible rollout flag for the unified dashboard snapshot
|
||||
* consumption.
|
||||
*
|
||||
* Controls whether the dashboard page reads every above-the-fold tile
|
||||
* from the single `GET /api/dashboard/snapshot` cell (default) or falls
|
||||
* back to the legacy four independent cells (slim analytics + thick
|
||||
* analytics + mood + widget layout). The snapshot path is the desired
|
||||
* behaviour: one un-gated request hydrates the whole strip so every
|
||||
* tile shares one completion moment, instead of four parallel cells
|
||||
* where the mood cache resolves fastest and pops in ahead of the rest.
|
||||
*
|
||||
* Default ON. Set `NEXT_PUBLIC_DASHBOARD_SNAPSHOT=false` to fall back
|
||||
* to the legacy multi-cell path. Defaulting on (rather than gating on
|
||||
* `=== "true"`) avoids the `NEXT_PUBLIC_*` build-time-baking trap: the
|
||||
* snapshot path is exercised in dev / e2e without a special build
|
||||
* flag, and the production image gets it without a Dockerfile ARG.
|
||||
*/
|
||||
export function isDashboardSnapshotEnabled(): boolean {
|
||||
return process.env.NEXT_PUBLIC_DASHBOARD_SNAPSHOT !== "false";
|
||||
}
|
||||
@@ -99,13 +99,11 @@ export function disabledSummaryTypes(
|
||||
|
||||
/**
|
||||
* Strip disabled-module types from a summaries slice. Lifted out of
|
||||
* `@/lib/dashboard/snapshot`, where it was private, once a second
|
||||
* dashboard feed needed it: with `NEXT_PUBLIC_DASHBOARD_SNAPSHOT=false`
|
||||
* the tile strip hydrates from `/api/analytics` instead of the snapshot,
|
||||
* and that route filtered nothing — so a module the user had turned off
|
||||
* still reached the client, and the tile it owns still painted. The gate
|
||||
* has to sit on every feed that carries summaries, not just the default
|
||||
* one, or it is only as strong as a rollout flag.
|
||||
* `@/lib/dashboard/snapshot`, where it was private, once `/api/analytics`
|
||||
* needed it too: that route filtered nothing, so a module the user had
|
||||
* turned off still reached the client. The gate has to sit on every feed
|
||||
* that carries summaries (the snapshot, the analytics slice, the Insights
|
||||
* presence read), not just one.
|
||||
*
|
||||
* Pure over its inputs and browser-safe (string keys + the map above), so
|
||||
* the snapshot builder, the analytics route and any future feed share one
|
||||
|
||||
Reference in new issue
Block a user