mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
release: v1.32.0 — hardening and scalability (#580)
* release: prepare v1.32.0 * test: stabilize water capture re-entry
This commit is contained in:
1 parent
b01822be48
commit
62eb431d66
372 files changed
+25178
-8561
No files matched your search
@@ -42,12 +42,7 @@ describe("dedupeWorkoutBatch", () => {
|
||||
expect(dedupeWorkoutBatch([])).toEqual([]);
|
||||
});
|
||||
|
||||
it("collapses exact duplicates from the same source", () => {
|
||||
// Two rows from the SAME source at the SAME instant: a re-posted
|
||||
// batch with the externalId differing (e.g. the iOS app split the
|
||||
// workout into two HKWorkout entries by mistake) would otherwise
|
||||
// double-count. The helper keeps one survivor — the higher-cal /
|
||||
// earliest-createdAt row.
|
||||
it("preserves overlapping workouts from the same source", () => {
|
||||
const a = row({
|
||||
id: "a",
|
||||
source: "APPLE_HEALTH",
|
||||
@@ -61,10 +56,10 @@ describe("dedupeWorkoutBatch", () => {
|
||||
createdAt: new Date("2026-05-21T08:31:00Z"),
|
||||
});
|
||||
|
||||
const out = dedupeWorkoutBatch([a, b]);
|
||||
expect(out).toHaveLength(1);
|
||||
// Same calories → earliest createdAt wins (`a`).
|
||||
expect(out[0]!.id).toBe("a");
|
||||
expect(dedupeWorkoutBatch([a, b]).map((item) => item.id)).toEqual([
|
||||
"a",
|
||||
"b",
|
||||
]);
|
||||
});
|
||||
|
||||
it("prefers the higher-priority source on a cross-source overlap inside the window", () => {
|
||||
@@ -93,6 +88,30 @@ describe("dedupeWorkoutBatch", () => {
|
||||
expect(out[0]!.id).toBe("apple");
|
||||
});
|
||||
|
||||
it("retains every row from the winning source in a cross-source overlap", () => {
|
||||
const appleFirst = row({
|
||||
id: "apple-first",
|
||||
source: "APPLE_HEALTH",
|
||||
startedAt: new Date("2026-05-21T08:00:00Z"),
|
||||
});
|
||||
const withings = row({
|
||||
id: "withings",
|
||||
source: "WITHINGS",
|
||||
startedAt: new Date("2026-05-21T08:00:30Z"),
|
||||
});
|
||||
const appleSecond = row({
|
||||
id: "apple-second",
|
||||
source: "APPLE_HEALTH",
|
||||
startedAt: new Date("2026-05-21T08:01:00Z"),
|
||||
});
|
||||
|
||||
expect(
|
||||
dedupeWorkoutBatch([appleFirst, withings, appleSecond]).map(
|
||||
(item) => item.id,
|
||||
),
|
||||
).toEqual(["apple-first", "apple-second"]);
|
||||
});
|
||||
|
||||
it("returns every row unchanged when no overlap exists", () => {
|
||||
// Two runs 10 min apart from the SAME source — well outside the
|
||||
// 90 s window. Both must survive; the helper is not a generic
|
||||
@@ -148,10 +167,7 @@ describe("dedupeWorkoutBatch", () => {
|
||||
expect(out.map((r) => r.id).sort()).toEqual(["strength", "walk"]);
|
||||
});
|
||||
|
||||
it("uses caloriesKcal as the tie-breaker when sources tie", () => {
|
||||
// Apple Watch in the bag (no HR contact, calories=200) vs Apple
|
||||
// Watch on the wrist (full HR series, calories=500). Same source,
|
||||
// same instant, same activityType — calories wins.
|
||||
it("preserves same-source workouts when calories differ", () => {
|
||||
const lo = row({
|
||||
id: "lo",
|
||||
source: "APPLE_HEALTH",
|
||||
@@ -161,30 +177,16 @@ describe("dedupeWorkoutBatch", () => {
|
||||
id: "hi",
|
||||
source: "APPLE_HEALTH",
|
||||
caloriesKcal: 500,
|
||||
// Stamp a later createdAt to prove the calories axis wins
|
||||
// over the createdAt fallback.
|
||||
createdAt: new Date("2026-05-21T09:00:00Z"),
|
||||
});
|
||||
const loWithEarlierCreated = row({
|
||||
id: "lo-early",
|
||||
source: "APPLE_HEALTH",
|
||||
caloriesKcal: 200,
|
||||
createdAt: new Date("2026-05-21T08:30:00Z"),
|
||||
});
|
||||
|
||||
// Pair 1: hi vs lo → hi wins on calories.
|
||||
expect(dedupeWorkoutBatch([lo, hi]).map((r) => r.id)).toEqual(["hi"]);
|
||||
|
||||
// Pair 2: hi vs lo-early → hi STILL wins because calories trump
|
||||
// createdAt.
|
||||
expect(
|
||||
dedupeWorkoutBatch([loWithEarlierCreated, hi]).map((r) => r.id),
|
||||
).toEqual(["hi"]);
|
||||
expect(dedupeWorkoutBatch([lo, hi]).map((item) => item.id)).toEqual([
|
||||
"lo",
|
||||
"hi",
|
||||
]);
|
||||
});
|
||||
|
||||
it("falls back to earliest createdAt when source + calories both tie", () => {
|
||||
// The maintainer's ScanWatch on the wrist + ScanWatch in a desk drawer:
|
||||
// unrealistic, but the case isolates the tie-breaker hierarchy.
|
||||
it("preserves same-source input order when creation times differ", () => {
|
||||
const early = row({
|
||||
id: "early",
|
||||
source: "WITHINGS",
|
||||
@@ -198,28 +200,24 @@ describe("dedupeWorkoutBatch", () => {
|
||||
createdAt: new Date("2026-05-21T08:45:00Z"),
|
||||
});
|
||||
|
||||
expect(dedupeWorkoutBatch([late, early]).map((r) => r.id)).toEqual([
|
||||
expect(dedupeWorkoutBatch([late, early]).map((item) => item.id)).toEqual([
|
||||
"late",
|
||||
"early",
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps determinism via input order when every field ties", () => {
|
||||
// Two manually-entered "running" workouts at the same instant
|
||||
// with no calories and no createdAt — the helper falls back to
|
||||
// the original input ordinal so re-running over the same input
|
||||
// always picks the same survivor.
|
||||
it("preserves same-source input order when every field ties", () => {
|
||||
const first = row({ id: "first", source: "MANUAL" });
|
||||
const second = row({ id: "second", source: "MANUAL" });
|
||||
|
||||
expect(dedupeWorkoutBatch([first, second]).map((r) => r.id)).toEqual([
|
||||
expect(dedupeWorkoutBatch([first, second]).map((item) => item.id)).toEqual([
|
||||
"first",
|
||||
]);
|
||||
// Reversed input → reversed survivor. Confirms the tie-breaker
|
||||
// genuinely consults input order rather than a hidden field
|
||||
// (e.g. `id` lexicographic).
|
||||
expect(dedupeWorkoutBatch([second, first]).map((r) => r.id)).toEqual([
|
||||
"second",
|
||||
]);
|
||||
expect(dedupeWorkoutBatch([second, first]).map((item) => item.id)).toEqual([
|
||||
"second",
|
||||
"first",
|
||||
]);
|
||||
});
|
||||
|
||||
it("treats a row exactly at the ±90 s boundary as in-window", () => {
|
||||
@@ -255,6 +253,58 @@ describe("dedupeWorkoutBatch", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("anchors overlap groups to the first row instead of chaining neighbours", () => {
|
||||
const anchor = row({
|
||||
id: "anchor",
|
||||
source: "APPLE_HEALTH",
|
||||
startedAt: new Date("2026-05-21T08:00:00Z"),
|
||||
});
|
||||
const neighbour = row({
|
||||
id: "neighbour",
|
||||
source: "WITHINGS",
|
||||
startedAt: new Date(
|
||||
anchor.startedAt.getTime() + WORKOUT_DEDUP_WINDOW_MS - 1_000,
|
||||
),
|
||||
});
|
||||
const chained = row({
|
||||
id: "chained",
|
||||
source: "WITHINGS",
|
||||
startedAt: new Date(
|
||||
anchor.startedAt.getTime() + 2 * (WORKOUT_DEDUP_WINDOW_MS - 1_000),
|
||||
),
|
||||
});
|
||||
|
||||
expect(
|
||||
dedupeWorkoutBatch([anchor, neighbour, chained]).map((item) => item.id),
|
||||
).toEqual(["anchor", "chained"]);
|
||||
});
|
||||
|
||||
it("keeps anchor windows stable when the batch arrives out of order", () => {
|
||||
const anchor = row({
|
||||
id: "anchor",
|
||||
source: "APPLE_HEALTH",
|
||||
startedAt: new Date("2026-05-21T08:00:00Z"),
|
||||
});
|
||||
const neighbour = row({
|
||||
id: "neighbour",
|
||||
source: "WITHINGS",
|
||||
startedAt: new Date(
|
||||
anchor.startedAt.getTime() + WORKOUT_DEDUP_WINDOW_MS - 1_000,
|
||||
),
|
||||
});
|
||||
const chained = row({
|
||||
id: "chained",
|
||||
source: "WITHINGS",
|
||||
startedAt: new Date(
|
||||
anchor.startedAt.getTime() + 2 * (WORKOUT_DEDUP_WINDOW_MS - 1_000),
|
||||
),
|
||||
});
|
||||
|
||||
expect(
|
||||
dedupeWorkoutBatch([neighbour, chained, anchor]).map((item) => item.id),
|
||||
).toEqual(["chained", "anchor"]);
|
||||
});
|
||||
|
||||
it("walks the full source ladder (APPLE_HEALTH > WITHINGS > MANUAL > IMPORT)", () => {
|
||||
const sources: MeasurementSource[] = [
|
||||
"IMPORT",
|
||||
|
||||
@@ -47,20 +47,16 @@
|
||||
* with HealthKit's `startDate`, which Apple aggregates to the
|
||||
* same instant across paired sensors. A 90-second cushion
|
||||
* covers HK's documented ±60s smoothing plus a small buffer.
|
||||
* 2. Within each group, prefer in order:
|
||||
* `APPLE_HEALTH ≻ WITHINGS ≻ MANUAL ≻ IMPORT`. The literal
|
||||
* source ladder is sourced from `DEFAULT_WORKOUT_SOURCE_PRIORITY`
|
||||
* in `src/lib/sources/pick-canonical-workout.ts` (the v1.4.25
|
||||
* W16a scaffolding) so both pickers consult a single source of
|
||||
* truth — adding a new source enum value is a one-place change.
|
||||
* Per the scope memo the legacy `STRAVA_IMPORT` reference maps
|
||||
* to the actual `IMPORT` enum value (the codebase has no
|
||||
* separate Strava source today; Strava XML imports ride the
|
||||
* generic `IMPORT` bucket).
|
||||
* 3. Tie-breaker: higher `caloriesKcal` (more sensor data — a row
|
||||
* with HR-derived calories beats a row with a zero / null
|
||||
* total). Then earliest `createdAt` so re-running the picker
|
||||
* with the same inputs picks the same survivor every time.
|
||||
* 2. Groups containing one source are not duplicates and remain
|
||||
* unchanged. For a cross-source group, select the winning source
|
||||
* in order `APPLE_HEALTH ≻ WITHINGS ≻ MANUAL ≻ IMPORT`, then keep
|
||||
* every row from that source. The literal source ladder comes from
|
||||
* `DEFAULT_WORKOUT_SOURCE_PRIORITY` so both pickers share one source
|
||||
* of truth.
|
||||
* 3. When two sources share a priority tier, prefer the source whose
|
||||
* best candidate has higher `caloriesKcal`, then earliest `createdAt`,
|
||||
* then input order. These tie-breakers choose a source; they never
|
||||
* collapse multiple workouts recorded by that winning source.
|
||||
*
|
||||
* Purity contract
|
||||
* ───────────────
|
||||
@@ -213,9 +209,9 @@ function compareCandidates<T extends WorkoutRow>(
|
||||
/**
|
||||
* Pick canonical rows across sources for an inbound ingest batch.
|
||||
*
|
||||
* Group by `(userId, activityType, startedAt ± 90 s)`; within each
|
||||
* group the comparator selects one survivor (the rest drop). No
|
||||
* cross-source overlap → the row passes through. The returned
|
||||
* Group by `(userId, activityType, startedAt ± 90 s)`; a group with
|
||||
* multiple sources keeps every row from its highest-priority source.
|
||||
* Single-source overlaps pass through unchanged. The returned
|
||||
* array preserves the input order of the surviving rows so a
|
||||
* caller that pre-sorted by `startedAt ASC` keeps that ordering
|
||||
* downstream.
|
||||
@@ -257,7 +253,10 @@ export function dedupeWorkoutBatch<T extends WorkoutRow>(
|
||||
// anchor row sets the window comparison reference.
|
||||
const groups: Tagged[][] = [];
|
||||
|
||||
for (const entry of tagged) {
|
||||
for (const entry of [...tagged].sort((a, b) => {
|
||||
const delta = a.row.startedAt.getTime() - b.row.startedAt.getTime();
|
||||
return delta || a.origIndex - b.origIndex;
|
||||
})) {
|
||||
let placed = false;
|
||||
for (const group of groups) {
|
||||
const anchor = group[0]!.row;
|
||||
@@ -277,21 +276,19 @@ export function dedupeWorkoutBatch<T extends WorkoutRow>(
|
||||
}
|
||||
}
|
||||
|
||||
// Pick one survivor per group, then re-emit in original input
|
||||
// order. The comparator sees `index` via the original input
|
||||
// ordinal (overrides any caller-supplied `index` so the helper
|
||||
// is self-contained — but a caller who set `index` explicitly
|
||||
// still wins because we layer the explicit value on top below).
|
||||
// Preserve single-source groups. For genuine cross-source overlaps,
|
||||
// select the winning source and retain every row it contributed.
|
||||
const survivors: Tagged[] = [];
|
||||
for (const group of groups) {
|
||||
if (group.length === 1) {
|
||||
survivors.push(group[0]!);
|
||||
continue;
|
||||
}
|
||||
// Annotate each member with its origIndex as the row's `index`
|
||||
// for the comparator, but only when the caller didn't already
|
||||
// set one. Caller-supplied indices outrank insertion order so
|
||||
// a deliberate "I want this row first" hint survives.
|
||||
const sources = new Set(group.map((entry) => entry.row.source));
|
||||
if (sources.size === 1) {
|
||||
survivors.push(...group);
|
||||
continue;
|
||||
}
|
||||
const annotated = group.map((entry) => ({
|
||||
...entry,
|
||||
row: {
|
||||
@@ -300,12 +297,10 @@ export function dedupeWorkoutBatch<T extends WorkoutRow>(
|
||||
} as T,
|
||||
}));
|
||||
annotated.sort((a, b) => compareCandidates(a.row, b.row, ladder));
|
||||
// Preserve the picker's input-order survival by remembering the
|
||||
// ORIGINAL row reference (not the annotated copy) so the caller
|
||||
// gets back exactly what it passed in.
|
||||
const winner = annotated[0]!;
|
||||
const original = group.find((g) => g.origIndex === winner.origIndex)!;
|
||||
survivors.push(original);
|
||||
const winningSource = annotated[0]!.row.source;
|
||||
survivors.push(
|
||||
...group.filter((entry) => entry.row.source === winningSource),
|
||||
);
|
||||
}
|
||||
|
||||
survivors.sort((a, b) => a.origIndex - b.origIndex);
|
||||
|
||||
@@ -14,17 +14,57 @@
|
||||
* for one filter within the 60 s TTL. Mirrors `src/lib/medications/list-read.ts`
|
||||
* and `src/lib/dashboard/snapshot-read.ts`.
|
||||
*/
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { prisma } from "@/lib/db";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { pickCanonicalWorkoutRows } from "@/lib/measurements/pick-canonical-workout-rows";
|
||||
import { cached, caches, type ServerCache } from "@/lib/cache/server-cache";
|
||||
import { cached, caches } from "@/lib/cache/server-cache";
|
||||
import { workoutSportTypeEnum } from "@/lib/validations/workout";
|
||||
|
||||
export interface WorkoutFilterParams {
|
||||
readonly since: string | null;
|
||||
readonly until: string | null;
|
||||
readonly sportType: string | null;
|
||||
function emptyQueryValueToUndefined(value: unknown): unknown {
|
||||
if (value == null) return undefined;
|
||||
if (typeof value !== "string") return value;
|
||||
const trimmed = value.trim();
|
||||
return trimmed === "" ? undefined : trimmed;
|
||||
}
|
||||
|
||||
const normalizedTimestampFilter = z
|
||||
.preprocess(
|
||||
emptyQueryValueToUndefined,
|
||||
z.iso
|
||||
.datetime({ offset: true })
|
||||
.transform((value) => new Date(value).toISOString())
|
||||
.optional(),
|
||||
)
|
||||
.transform((value) => value ?? null);
|
||||
|
||||
const normalizedSportTypeFilter = z
|
||||
.preprocess(emptyQueryValueToUndefined, workoutSportTypeEnum.optional())
|
||||
.transform((value) => value ?? null);
|
||||
|
||||
export const workoutListFilterSchema = z
|
||||
.object({
|
||||
since: normalizedTimestampFilter,
|
||||
until: normalizedTimestampFilter,
|
||||
sportType: normalizedSportTypeFilter,
|
||||
})
|
||||
.superRefine((filters, ctx) => {
|
||||
if (
|
||||
filters.since !== null &&
|
||||
filters.until !== null &&
|
||||
filters.since > filters.until
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
path: ["until"],
|
||||
message: "until must be at or after since",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export type WorkoutFilterParams = z.infer<typeof workoutListFilterSchema>;
|
||||
|
||||
interface CanonicalWorkoutRow {
|
||||
id: string;
|
||||
source: string;
|
||||
@@ -198,19 +238,20 @@ export async function readWorkoutsListCached(
|
||||
sportType: string | null;
|
||||
},
|
||||
): Promise<WorkoutsResult> {
|
||||
// Deliberately excludes `limit`/`offset` — every page of one filter
|
||||
// combination shares this cached projection.
|
||||
const projectionKey = `${userId}|${params.since ?? ""}|${params.until ?? ""}|${params.sportType ?? ""}`;
|
||||
const filters = workoutListFilterSchema.parse({
|
||||
since: params.since,
|
||||
until: params.until,
|
||||
sportType: params.sportType,
|
||||
});
|
||||
|
||||
// Deliberately excludes `limit`/`offset` — every page of one normalized
|
||||
// filter combination shares this cached projection.
|
||||
const projectionKey = `${userId}|${filters.since ?? ""}|${filters.until ?? ""}|${filters.sportType ?? ""}`;
|
||||
|
||||
const projection = await cached(
|
||||
caches.workouts as ServerCache<WorkoutsProjection>,
|
||||
caches.workouts,
|
||||
projectionKey,
|
||||
() =>
|
||||
buildWorkoutsProjection(userId, {
|
||||
since: params.since,
|
||||
until: params.until,
|
||||
sportType: params.sportType,
|
||||
}),
|
||||
() => buildWorkoutsProjection(userId, filters),
|
||||
annotate,
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user