feat(insights): explain the shorter correlation list once

Since correlations are tested on seasonally adjusted residuals with an
effective sample size, an account with history sees far fewer
relationships, which reads as a defect without a word about it.

The correlations response now carries findingsBeforeSeasonalAdjustment:
true when the record kept a discovery pattern first stored before the
change, one indexed lookup on the pattern store. An account created since
can never qualify. The mood "What stands out" card shows a short note
for those records, closable, remembered per viewer like the AI setup
hint. Copy in all seven locales; OpenAPI regenerated.
This commit is contained in:
Marc-André Bombeck committed 2026-10-09 09:29:38 +02:00
1 parent 4c58274bed
commit 898729d1a8
17 files changed
+445 -19

No files matched your search

@@ -70,6 +70,7 @@ vi.mock("@/lib/insights/correlation-patterns", () => ({
},
syncAcceptedPatterns: vi.fn().mockResolvedValue(new Map()),
decisionForEvidence: vi.fn().mockReturnValue(null),
hadFindingsBeforeSeasonalAdjustment: vi.fn().mockResolvedValue(false),
}));
vi.mock("next/headers", () => ({
@@ -88,7 +89,10 @@ import { requireModuleEnabled, resolveModuleMap } from "@/lib/modules/gate";
import { apiError } from "@/lib/api-response";
import { checkAnalyticsReadRateLimit } from "@/lib/rate-limit";
import { __resetAllCachesForTests } from "@/lib/cache/server-cache";
import { syncAcceptedPatterns } from "@/lib/insights/correlation-patterns";
import {
hadFindingsBeforeSeasonalAdjustment,
syncAcceptedPatterns,
} from "@/lib/insights/correlation-patterns";
const SESSION_OK = {
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
@@ -155,6 +159,7 @@ beforeEach(() => {
).mockResolvedValue([]);
vi.mocked(prisma.customMetric.findMany).mockResolvedValue([] as never);
vi.mocked(prisma.symptomDefinition.findMany).mockResolvedValue([] as never);
vi.mocked(hadFindingsBeforeSeasonalAdjustment).mockResolvedValue(false);
});
const callGet = GET as unknown as (req: NextRequest) => Promise<Response>;
@@ -181,6 +186,18 @@ describe("GET /api/insights/correlations", () => {
expect(body.data.fdrQ).toBeGreaterThan(0);
});
it("reports whether the record had findings before the seasonal adjustment", async () => {
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(hadFindingsBeforeSeasonalAdjustment).mockResolvedValue(true);
const res = await callGet(makeReq());
expect(res.status).toBe(200);
const body = (await res.json()) as {
data: { findingsBeforeSeasonalAdjustment: boolean };
};
expect(body.data.findingsBeforeSeasonalAdjustment).toBe(true);
expect(hadFindingsBeforeSeasonalAdjustment).toHaveBeenCalledWith("user-1");
});
it("serves a cache hit without re-running the discovery reads or the pattern-sync writes", async () => {
// The SWR cell caches the built body for 60 s; the second GET inside that
// window must neither re-read the measurement window nor re-run the two
+19 -11
View File
@@ -36,6 +36,7 @@ import {
} from "@/lib/insights/correlation-discovery";
import {
decisionForEvidence,
hadFindingsBeforeSeasonalAdjustment,
PATTERN_FAMILIES,
syncAcceptedPatterns,
} from "@/lib/insights/correlation-patterns";
@@ -131,17 +132,23 @@ async function buildCorrelationsResponse(
//
// v1.22 — lab draws (for the labs ↔ outcome pass) fetch alongside; they feed
// a different pass over a different grain, so they are not matrix channels.
const [matrix, labDraws] = await Promise.all([
assembleDiscoveryMatrix(userId, {
tz,
since,
fetchMode: "tiered",
modules,
}),
isSurfaceVisible("correlation:LAB_DRAWS", modules)
? fetchLabDraws(userId, tz, since)
: Promise.resolve([]),
]);
//
// The earlier-findings probe is one indexed lookup on the pattern store; it
// tells the client whether this record had findings under the engine before
// the seasonal adjustment, which is what earns the one-time explanation.
const [matrix, labDraws, findingsBeforeSeasonalAdjustment] =
await Promise.all([
assembleDiscoveryMatrix(userId, {
tz,
since,
fetchMode: "tiered",
modules,
}),
isSurfaceVisible("correlation:LAB_DRAWS", modules)
? fetchLabDraws(userId, tz, since)
: Promise.resolve([]),
hadFindingsBeforeSeasonalAdjustment(userId),
]);
const { series, diagnostics } = matrix;
const result = discoverCorrelations(series, { locale });
@@ -246,5 +253,6 @@ async function buildCorrelationsResponse(
discovered,
emerging: emergingWithDecisions,
labCorrelations,
findingsBeforeSeasonalAdjustment,
};
}