Merge branch 'fix/v1439' into release/v1.43.9

This commit is contained in:
Marc-André Bombeck committed 2026-10-20 22:49:33 +02:00
commit b4618f2ec3
55 files changed
+1547 -270

No files matched your search

@@ -188,6 +188,8 @@ describe("recomputeBucketsForMeasurement", () => {
sd: 2.0,
slope: -0.1,
r2: 0.9,
reading_min_value: 80.5,
reading_max_value: 84.5,
},
]);
getGlobalBossMock.mockReturnValue({ send: bossSend });
@@ -210,14 +212,14 @@ describe("recomputeBucketsForMeasurement", () => {
// instead of raising (or being dropped). Param layout per row:
// [userId, type, granularity, bucketStart, source, count, mean,
// min, max, sum, sd, slope, r2, sumX, sumXy, sumXx, sumYy,
// computedAt].
// readingMin, readingMax, computedAt].
expect(txExecuteRawUnsafe).toHaveBeenCalledTimes(1);
const [sql, ...params] = txExecuteRawUnsafe.mock.calls[0];
expect(sql).toContain(
"ON CONFLICT (user_id, type, granularity, bucket_start, source)",
);
expect(sql).toContain("DO UPDATE SET");
expect(params).toHaveLength(18);
expect(params).toHaveLength(20);
expect(params[0]).toBe("user-1");
expect(params[1]).toBe("WEIGHT");
expect(params[2]).toBe("DAY");
@@ -227,6 +229,10 @@ describe("recomputeBucketsForMeasurement", () => {
// v1.4.39 — sumValue flows through. Cumulative read paths
// (steps, flights, distance, daylight, active-energy) consume it.
expect(params[9]).toBe(247.5);
// The reading extremes, beside the range-widened min / max.
expect(params[7]).toBe(80.0);
expect(params[17]).toBe(80.5);
expect(params[18]).toBe(84.5);
// WEEK / MONTH / YEAR — three enqueues against the worker queue.
expect(bossSend).toHaveBeenCalledTimes(3);
@@ -453,13 +459,13 @@ describe("persistRollupRows — large path (via recomputeUserRollups)", () => {
expect(deleteMany.mock.calls[0][0].where.userId).toBe("user-1");
expect(deleteMany.mock.calls[0][0].where.granularity).toBe("DAY");
// 3 chunks of 18 params per row, every one an ON CONFLICT DO UPDATE
// 3 chunks of 20 params per row, every one an ON CONFLICT DO UPDATE
// upsert — tolerant of a mid-transaction hook insert AND of a
// concurrent fold on the same partition (v1.28.33, issue #486).
expect(txExecuteRawUnsafe).toHaveBeenCalledTimes(3);
expect(txExecuteRawUnsafe.mock.calls[0]).toHaveLength(1 + 500 * 18);
expect(txExecuteRawUnsafe.mock.calls[1]).toHaveLength(1 + 500 * 18);
expect(txExecuteRawUnsafe.mock.calls[2]).toHaveLength(1 + 200 * 18);
expect(txExecuteRawUnsafe.mock.calls[0]).toHaveLength(1 + 500 * 20);
expect(txExecuteRawUnsafe.mock.calls[1]).toHaveLength(1 + 500 * 20);
expect(txExecuteRawUnsafe.mock.calls[2]).toHaveLength(1 + 200 * 20);
for (const call of txExecuteRawUnsafe.mock.calls) {
expect(call[0]).toContain("ON CONFLICT");
expect(call[0]).toContain("DO UPDATE SET");
@@ -575,7 +581,7 @@ describe("persistRollupRows — large path (via recomputeUserRollups)", () => {
expect(transaction).toHaveBeenCalledTimes(1);
expect(typeof transaction.mock.calls[0][0]).toBe("function");
expect(txExecuteRawUnsafe).toHaveBeenCalledTimes(1);
expect(txExecuteRawUnsafe.mock.calls[0]).toHaveLength(1 + 2 * 18);
expect(txExecuteRawUnsafe.mock.calls[0]).toHaveLength(1 + 2 * 20);
expect(result.rowsUpserted).toBe(2);
});
});
+22 -6
View File
@@ -184,6 +184,10 @@ interface RollupRow {
sum_xy: number | null;
sum_xx: number | null;
sum_yy: number | null;
// The extremes of the readings' own `value`, without the per-row range
// `min_value` / `max_value` widen to. See `MeasurementRollup.readingMinValue`.
reading_min_value: number | null;
reading_max_value: number | null;
}
/**
@@ -703,7 +707,9 @@ async function runRollupAggregate(
SUM(${REBASED_X})::double precision AS sum_x,
SUM(${REBASED_X} * m."value")::double precision AS sum_xy,
SUM(${REBASED_X} * ${REBASED_X})::double precision AS sum_xx,
SUM(m."value" * m."value")::double precision AS sum_yy
SUM(m."value" * m."value")::double precision AS sum_yy,
MIN(m."value")::double precision AS reading_min_value,
MAX(m."value")::double precision AS reading_max_value
FROM measurements m
WHERE m."user_id" = $1
AND m."type" IN (${typeList})
@@ -753,7 +759,9 @@ async function runRollupAggregate(
SUM(${REBASED_X})::double precision AS sum_x,
SUM(${REBASED_X} * m."value")::double precision AS sum_xy,
SUM(${REBASED_X} * ${REBASED_X})::double precision AS sum_xx,
SUM(m."value" * m."value")::double precision AS sum_yy
SUM(m."value" * m."value")::double precision AS sum_yy,
MIN(m."value")::double precision AS reading_min_value,
MAX(m."value")::double precision AS reading_max_value
FROM measurements m
WHERE m."user_id" = $1
AND m."measured_at" >= $2
@@ -1089,20 +1097,24 @@ function buildRollupUpsert(
row.sum_xy ?? null,
row.sum_xx ?? null,
row.sum_yy ?? null,
row.reading_min_value ?? null,
row.reading_max_value ?? null,
computedAt,
);
tuples.push(
`($${base + 1}, $${base + 2}::"measurement_type", $${base + 3}::"measurement_rollup_granularity", ` +
`$${base + 4}::timestamptz, $${base + 5}::"measurement_source", $${base + 6}::int, ` +
`$${base + 7}, $${base + 8}, $${base + 9}, $${base + 10}, $${base + 11}, $${base + 12}, ` +
`$${base + 13}, $${base + 14}, $${base + 15}, $${base + 16}, $${base + 17}, $${base + 18}::timestamptz)`,
`$${base + 13}, $${base + 14}, $${base + 15}, $${base + 16}, $${base + 17}, $${base + 18}, ` +
`$${base + 19}, $${base + 20}::timestamptz)`,
);
}
const sql = `
INSERT INTO measurement_rollups
(user_id, type, granularity, bucket_start, source, count, mean,
min_value, max_value, sum_value, sd, slope, r2,
sum_x, sum_xy, sum_xx, sum_yy, computed_at)
sum_x, sum_xy, sum_xx, sum_yy, reading_min_value, reading_max_value,
computed_at)
VALUES ${tuples.join(", ")}
ON CONFLICT (user_id, type, granularity, bucket_start, source)
DO UPDATE SET
@@ -1118,6 +1130,8 @@ function buildRollupUpsert(
sum_xy = EXCLUDED.sum_xy,
sum_xx = EXCLUDED.sum_xx,
sum_yy = EXCLUDED.sum_yy,
reading_min_value = EXCLUDED.reading_min_value,
reading_max_value = EXCLUDED.reading_max_value,
computed_at = EXCLUDED.computed_at
`;
return { sql, params };
@@ -1369,7 +1383,9 @@ export async function enqueueBootTimeRollupBackfill(
// catches up — mirrors the v1.4.39 sum_value rollout, which the
// v1.4.41 note confirms converged via this same boot queue. Bounded
// to the fold window so days outside it (un-foldable by design)
// never re-flag a user on every boot.
// never re-flag a user on every boot. The same arm refills the
// reading extremes of migration 0417 (`reading_min_value IS NULL`);
// the extremes reader treats a NULL as uncovered until it lands.
const users = await prisma.$queryRaw<Array<{ id: string }>>`
SELECT DISTINCT id FROM (
SELECT DISTINCT mt."user_id" AS id
@@ -1395,7 +1411,7 @@ export async function enqueueBootTimeRollupBackfill(
FROM measurement_rollups r
WHERE r."granularity" = 'DAY'
AND r."bucket_start" >= ${foldWindowStart}
AND r."sum_xy" IS NULL
AND (r."sum_xy" IS NULL OR r."reading_min_value" IS NULL)
) discovered
`;