perf(insights): fold the all-time extremes from the DAY rollup tier

readAllTimeExtremes read every raw reading of weight, blood pressure and
pulse twice on each call, a sequential pass over the measurements table
that made it the largest read on the database. The DAY tier already
holds the same per-source day fold except for the extremes of the
readings themselves: min_value and max_value widen to a row's own
intra-hour range.

Migration 0417 adds reading_min_value and reading_max_value to
measurement_rollups. The writer fills them from the same aggregate, and
the boot re-fold refills existing rows through the arm that already
watches sum_xy. The reader folds the tier for whole UTC days inside the
fold window and the raw rows for the rest: older days, the partial day
a lookback floor falls in, and any type whose tier rows lack the new
columns. The raw arm reads narrow columns once instead of twice.

An integration test compares the result with the previous query on a
seeded history with two sources per day, hourly pulse statistics with
their own range, readings older than the window, midnight edges and
deletions, and shows the tier is the path that answers.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 20:22:19 +02:00
1 parent 567a564913
commit 1cfae83578
6 files changed
+542 -74

No files matched your search

@@ -0,0 +1,24 @@
-- The lowest and highest reading value per rollup bucket.
--
-- The full-history extremes the insight features carry (`allTimeMin`,
-- `allTimeMax`, `allTimeAvg` for weight, blood pressure and pulse) were read
-- from the raw `measurements` rows on every call: two passes over every
-- reading of those types the account ever recorded, the largest read on the
-- database. The DAY rollups already hold everything that fold needs except
-- the extremes of the readings themselves: `min_value` / `max_value` widen to
-- a row's own `value_min` / `value_max`, the intra-hour range an hourly
-- heart-rate statistic carries, so they are not the same figure.
--
-- Two nullable columns. Existing rows read NULL until the boot re-fold
-- (`rollup-full-backfill`, discovered by the NULL arm beside `sum_xy`)
-- refills them; new writes fill them from the same aggregate as every other
-- column. A reader treats a type with a NULL in its window as uncovered and
-- reads the raw rows, as before.
--
-- Reversibility:
-- ALTER TABLE "measurement_rollups" DROP COLUMN IF EXISTS "reading_min_value";
-- ALTER TABLE "measurement_rollups" DROP COLUMN IF EXISTS "reading_max_value";
ALTER TABLE "measurement_rollups" ADD COLUMN IF NOT EXISTS "reading_min_value" DOUBLE PRECISION;
ALTER TABLE "measurement_rollups" ADD COLUMN IF NOT EXISTS "reading_max_value" DOUBLE PRECISION;
+28 -18
View File
@@ -2253,31 +2253,31 @@ enum RollupGranularity {
}
model MeasurementRollup {
userId String @map("user_id")
type MeasurementType
granularity RollupGranularity
bucketStart DateTime @map("bucket_start") @db.Timestamptz(3)
userId String @map("user_id")
type MeasurementType
granularity RollupGranularity
bucketStart DateTime @map("bucket_start") @db.Timestamptz(3)
/// v1.11.1 — source-aware grain. Rollup rows are minted per source so the
/// read path can collapse overlapping sources (e.g. WHOOP + Apple Watch
/// resting heart rate) to the ladder-canonical reading, while cumulative
/// metrics (steps, energy) sum per source. Always non-null; migration 0115
/// purged the legacy source-blind aggregates and the boot backfill re-mints
/// per-source rows.
source MeasurementSource
count Int
mean Float
minValue Float @map("min_value")
maxValue Float @map("max_value")
sd Float?
slope Float?
r2 Float?
source MeasurementSource
count Int
mean Float
minValue Float @map("min_value")
maxValue Float @map("max_value")
sd Float?
slope Float?
r2 Float?
/// v1.4.39 W-SUM — cumulative-metric sum for the bucket. Populated
/// alongside `mean / count` for every type; cumulative read paths
/// (steps, flights, distance, daylight, active-energy) consume this
/// directly instead of re-deriving from raw measurements. Nullable
/// to keep the migration additive — the boot backfill fills NULLs
/// for existing rows; new rows always carry the sum.
sumValue Float? @map("sum_value")
sumValue Float? @map("sum_value")
/// v1.20.0 F6 — per-bucket OLS regression accumulators over the same
/// canonical-source raw rows the live REGR_* query reads. With
/// `n = count` and `Σy = mean·count` these close the additive set so a
@@ -2291,11 +2291,21 @@ model MeasurementRollup {
/// parity-matches the cross-bucket compose. Nullable to keep migration 0190
/// additive — the boot backfill fills NULLs for pre-migration rows; new
/// rows always carry them (they ride the same aggregate as mean / sum).
sumX Float? @map("sum_x")
sumXy Float? @map("sum_xy")
sumXx Float? @map("sum_xx")
sumYy Float? @map("sum_yy")
computedAt DateTime @default(now()) @map("computed_at") @db.Timestamptz(3)
sumX Float? @map("sum_x")
sumXy Float? @map("sum_xy")
sumXx Float? @map("sum_xx")
sumYy Float? @map("sum_yy")
/// The lowest and highest reading `value` of the bucket. `minValue` /
/// `maxValue` widen to a row's own range (`Measurement.valueMin` /
/// `valueMax`, the intra-hour range an hourly heart-rate statistic
/// carries); these do not, so a reader whose figure is over the readings
/// themselves (the full-history extremes in `feature-blocks.ts`) can fold
/// the DAY tier instead of the raw rows. Nullable to keep migration 0417
/// additive: the boot re-fold fills existing rows, new rows always carry
/// them, and a reader treats a type with a NULL in its range as uncovered.
readingMinValue Float? @map("reading_min_value")
readingMaxValue Float? @map("reading_max_value")
computedAt DateTime @default(now()) @map("computed_at") @db.Timestamptz(3)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)