Files
HealthLog/src/lib/export/paged-measurements.ts
T
Marc-André Bombeck 9c63d3ce20 chore(release): v1.28.25 — every integration held to the same standard
Platform-wide hardening: the failure classes found live this week hunted down
across every integration.

Fitbit carried the same tombstone wedge fixed for Google — probe now matches
soft-deleted rows and the update resurrects them; the comment describing a
partial unique index that never existed is corrected. Sleep segment ids are
stable across re-scoring for Withings (session+segment-start), WHOOP/Polar
(one segment per stage, no positional index) and Oura (run-start keyed), each
with a session-scoped sweep that soft-deletes rows a re-score orphaned — so
legacy and affected nights heal as they are re-read.

One platform rule for deleted rows: source-owned readings resurrect on
re-import (Withings, WHOOP, Oura, Polar, Nightscout, mood webhook, CSV
re-imports, Apple stats day-totals); Apple sample-grain deletions stick per
the paired-client contract, and the mood bulk route's tombstone match is now
a true no-op reported as duplicate. Import counts reconcile against actual
createMany counts.

Google lifecycle: a dead token registers on the cycle's hard-fail ledger, so
an expired connection parks instead of stamping success hourly; backfill and
sleep-repair markers stamp only after a clean pass (pg-boss retries are now
real); failed measurement writes hold the watermark; no-op updates are
skipped; mapping.md tells the current truth.

Scale: doctor-report reads only consumed columns and enabled sections;
exports and backups keyset-paginate; achievements tally via SQL buckets;
series charts day-bucket dense kinds beyond 90 days; BP pairing is a linear
two-pointer; distinct-type scans run server-side; the graded-series cold
fallback is bounded; the fitbit defer tracker loops instead of spreading.
2026-07-11 01:54:12 +02:00

54 lines
2.3 KiB
TypeScript

/**
* Keyset-paginated measurement reader for the export surfaces.
*
* The legacy exports (`GET /api/export`, `GET /api/export/measurements`, the
* full-backup builder) read the user's ENTIRE measurement table in one
* `findMany`. On a CGM + per-sample-HR account that is a six-figure row set
* materialised by a single query — the v1.28.2x incident class. This helper
* keeps the output row set (and its `measuredAt desc` order) identical while
* bounding each database round-trip to `MEASUREMENT_EXPORT_PAGE_SIZE` rows:
* the driver never buffers one giant result, and the event loop yields
* between chunks.
*
* Ordering: `measuredAt desc, id desc`. The exports always sorted
* `measuredAt desc`; the `id` tie-breaker makes the keyset cursor total (two
* rows can share a timestamp) — for equal-timestamp rows the previous order
* was unspecified heap order, so pinning it is not a behaviour change any
* consumer could have relied on.
*
* The `select` MUST include `id` (the cursor key); the generic constraint
* enforces it at compile time.
*/
import type { Prisma, PrismaClient } from "@/generated/prisma/client";
export const MEASUREMENT_EXPORT_PAGE_SIZE = 10_000;
export async function findMeasurementsPaged<
S extends Prisma.MeasurementSelect & { id: true },
>(
db: PrismaClient,
where: Prisma.MeasurementWhereInput,
select: S,
pageSize: number = MEASUREMENT_EXPORT_PAGE_SIZE,
): Promise<Array<Prisma.MeasurementGetPayload<{ select: S }>>> {
const rows: Array<Prisma.MeasurementGetPayload<{ select: S }>> = [];
let cursorId: string | null = null;
for (;;) {
const batch: Array<Prisma.MeasurementGetPayload<{ select: S }>> =
(await db.measurement.findMany({
where,
orderBy: [{ measuredAt: "desc" }, { id: "desc" }],
take: pageSize,
...(cursorId !== null ? { cursor: { id: cursorId }, skip: 1 } : {}),
select,
})) as Array<Prisma.MeasurementGetPayload<{ select: S }>>;
// Loop, not `push(...batch)` — a 10k-element spread is fine today, but
// this file exists because of stack-overflow-by-spread regressions on
// large arrays (v1.28.22 class); keep the pattern boring.
for (const row of batch) rows.push(row);
if (batch.length < pageSize) break;
cursorId = (batch[batch.length - 1] as { id: string }).id;
}
return rows;
}