mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 06:40:50 +00:00
chore(release): v1.28.46 — server performance on large instances
Deferred perf fixes from the campaign's verified perf-correctness audit,
correctness-first (each with an output-equivalence test):
- comprehensive-generate fan-out: replaced per-type 400d findMany+JS-average
with DB prisma.aggregate(_avg) over the exact half-open windows the JS
reducer used (current 30d, lastMonth (60d,30d], lastYear (395d,365d]), same
rounding + null-on-empty, pLimit(4). SLEEP_DURATION stays on the raw path
(per-night reconstruction dedup). Byte-equality test vs the old algorithm.
- boot-discovery: replaced Prisma in-memory distinct with DB SELECT DISTINCT
in the 4 backfill discovery scans + migration 0243 (7 additive
partial indexes matching each predicate). Integration test asserts identical
discovered set against real Postgres.
- ChatBubble wrapped in React.memo with a comparator (onRegenerate compared by
presence — the thread rebuilds it each render), so only the streaming bubble
re-renders per token.
- doctor-report mood read narrowed to {score, tags} (the only fields the
collector uses); the 730d main measurement read left as-is — a blind bound
would corrupt clinical stats (documented deferred follow-up).
Migration 0243 additive-only.
This commit is contained in:
1 parent
a59a5d75e1
commit
656e96f72b
16 files changed
+812
-216
No files matched your search
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* v1.28.46 perf (H4) — the boot-time converging-backfill discovery scans now
|
||||
* run a DB-level `SELECT DISTINCT user_id` (with a join / anti-join where the
|
||||
* owner is not a direct column) instead of Prisma's in-memory `distinct`, and
|
||||
* ride the migration-0243 partial indexes. This suite runs the REAL discovery
|
||||
* functions against real Postgres to prove the raw SQL is correct (table +
|
||||
* column names, the medication join, the thumbnail anti-join, the mime IN
|
||||
* filter) and that each returns EXACTLY the set of users still holding
|
||||
* un-migrated rows — no more, no fewer.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { setGlobalBoss } from "@/lib/jobs/boss-instance";
|
||||
import { nativeCanvasSupported } from "@/lib/documents/native-canvas-support";
|
||||
import { enqueueBootTimeNoteEncryptionBackfill } from "@/lib/jobs/note-encryption-backfill";
|
||||
import { enqueueBootTimeMedNotesEncryptionBackfill } from "@/lib/jobs/med-notes-encryption-backfill";
|
||||
import { enqueueBootTimeLabBiomarkerBackfill } from "@/lib/jobs/lab-biomarker-backfill";
|
||||
import { enqueueBootTimeThumbnailBackfill } from "@/lib/jobs/document-thumbnail-backfill";
|
||||
import { getPrismaClient, truncateAllTables } from "./setup";
|
||||
|
||||
/**
|
||||
* Minimal fake pg-boss that records which (queue, userId) pairs were sent.
|
||||
* The discovery functions call `boss.send(queue, payload, opts)` and expect a
|
||||
* truthy job id; capturing the payload's userId lets us assert the discovered
|
||||
* set without a real queue.
|
||||
*/
|
||||
function makeCapturingBoss() {
|
||||
const sent: { queue: string; userId: string }[] = [];
|
||||
const boss = {
|
||||
send: async (
|
||||
queue: string,
|
||||
payload: { userId?: string },
|
||||
_opts?: unknown,
|
||||
) => {
|
||||
if (payload.userId) sent.push({ queue, userId: payload.userId });
|
||||
return `job-${sent.length}`;
|
||||
},
|
||||
};
|
||||
return { boss, sent };
|
||||
}
|
||||
|
||||
function usersFor(sent: { userId: string }[]): string[] {
|
||||
return [...new Set(sent.map((s) => s.userId))].sort();
|
||||
}
|
||||
|
||||
async function seedUser(id: string) {
|
||||
await getPrismaClient().user.create({
|
||||
data: {
|
||||
id,
|
||||
username: id,
|
||||
email: `${id}@example.test`,
|
||||
timezone: "Europe/Berlin",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await truncateAllTables(getPrismaClient());
|
||||
});
|
||||
|
||||
describe("boot-discovery scans (real Postgres)", () => {
|
||||
it("note-encryption discovery returns exactly the users with un-migrated notes", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
await Promise.all([
|
||||
seedUser("disc-a"),
|
||||
seedUser("disc-b"),
|
||||
seedUser("disc-c"),
|
||||
]);
|
||||
// disc-a: a measurement with a legacy plaintext note (discoverable).
|
||||
await prisma.measurement.create({
|
||||
data: {
|
||||
userId: "disc-a",
|
||||
type: "WEIGHT",
|
||||
value: 80,
|
||||
unit: "kg",
|
||||
measuredAt: new Date(),
|
||||
notes: "legacy plaintext",
|
||||
},
|
||||
});
|
||||
// disc-b: a mood entry with a legacy plaintext note (discoverable).
|
||||
await prisma.moodEntry.create({
|
||||
data: {
|
||||
userId: "disc-b",
|
||||
date: "2026-01-01",
|
||||
mood: "GUT",
|
||||
score: 4,
|
||||
moodLoggedAt: new Date(),
|
||||
note: "legacy plaintext mood note",
|
||||
},
|
||||
});
|
||||
// disc-c: a measurement with NO note (must NOT be discovered).
|
||||
await prisma.measurement.create({
|
||||
data: {
|
||||
userId: "disc-c",
|
||||
type: "WEIGHT",
|
||||
value: 70,
|
||||
unit: "kg",
|
||||
measuredAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
const { boss, sent } = makeCapturingBoss();
|
||||
setGlobalBoss(boss as never);
|
||||
const res = await enqueueBootTimeNoteEncryptionBackfill();
|
||||
expect(res.error).toBeNull();
|
||||
expect(usersFor(sent)).toEqual(["disc-a", "disc-b"]);
|
||||
});
|
||||
|
||||
it("lab-biomarker discovery returns only users with unlinked live results", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
await Promise.all([seedUser("lab-a"), seedUser("lab-b")]);
|
||||
// lab-a: unlinked (biomarkerId null), live → discoverable.
|
||||
await prisma.labResult.create({
|
||||
data: {
|
||||
userId: "lab-a",
|
||||
analyte: "LDL",
|
||||
unit: "mg/dL",
|
||||
takenAt: new Date(),
|
||||
},
|
||||
});
|
||||
// lab-b: unlinked but soft-deleted → excluded by `deleted_at IS NULL`.
|
||||
await prisma.labResult.create({
|
||||
data: {
|
||||
userId: "lab-b",
|
||||
analyte: "HbA1c",
|
||||
unit: "%",
|
||||
takenAt: new Date(),
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
const { boss, sent } = makeCapturingBoss();
|
||||
setGlobalBoss(boss as never);
|
||||
const res = await enqueueBootTimeLabBiomarkerBackfill();
|
||||
expect(res.error).toBeNull();
|
||||
expect(usersFor(sent)).toEqual(["lab-a"]);
|
||||
});
|
||||
|
||||
it("med-notes discovery runs its join SQL cleanly (empty tables → no enqueues)", async () => {
|
||||
// Exercises the three raw statements incl. the dose-changes → medications
|
||||
// JOIN. Empty tables prove the SQL parses + executes against the real
|
||||
// schema (a wrong column/join name would throw here).
|
||||
const { boss, sent } = makeCapturingBoss();
|
||||
setGlobalBoss(boss as never);
|
||||
const res = await enqueueBootTimeMedNotesEncryptionBackfill();
|
||||
expect(res.error).toBeNull();
|
||||
expect(sent).toEqual([]);
|
||||
});
|
||||
|
||||
it("thumbnail discovery applies the mime filter + thumbnail anti-join", async () => {
|
||||
if (!nativeCanvasSupported()) {
|
||||
// The gate returns before the query on a non-AVX2 host; the SQL is still
|
||||
// covered by the other cases' schema, so skip the data assertion here.
|
||||
return;
|
||||
}
|
||||
const prisma = getPrismaClient();
|
||||
await Promise.all([seedUser("doc-a"), seedUser("doc-c")]);
|
||||
// doc-a: a thumbnailable PDF with no thumbnail row → discoverable.
|
||||
await prisma.inboundDocument.create({
|
||||
data: {
|
||||
userId: "doc-a",
|
||||
mimeType: "application/pdf",
|
||||
byteSize: 10,
|
||||
contentEncrypted: Buffer.from("x"),
|
||||
},
|
||||
});
|
||||
// doc-c: a non-thumbnailable text document → excluded by the mime IN filter.
|
||||
await prisma.inboundDocument.create({
|
||||
data: {
|
||||
userId: "doc-c",
|
||||
mimeType: "text/plain",
|
||||
byteSize: 10,
|
||||
contentEncrypted: Buffer.from("x"),
|
||||
},
|
||||
});
|
||||
|
||||
const { boss, sent } = makeCapturingBoss();
|
||||
setGlobalBoss(boss as never);
|
||||
await enqueueBootTimeThumbnailBackfill();
|
||||
expect(usersFor(sent)).toEqual(["doc-a"]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user