mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 04:50:59 +00:00
fix(analytics): count each reading once in the all-time summaries
On the rollup path the all-time count, min, max and mean are composed from DAY buckets plus one raw aggregate of the readings older than the fold window. The bucket half read every DAY bucket the account had, the raw half every reading before now minus five years, so a reading on any day that had a bucket before that instant was counted in both. Such buckets are common: the per-write hook folds whatever day it touches, the Apple Health import folded whole spans until this release clamped it, every fold snaps its start down to a whole day, and nothing removes a bucket once the moving window has passed it. The count came out too high and the mean leaned towards the doubled days, while the live fallback reported the true figures for the same account. Both halves now meet at the start of the window's first UTC day: buckets from there on, raw readings before it. A reading lands in exactly one half whichever buckets exist, so buckets already written before the window need no repair.
This commit is contained in:
1 parent
aba147152a
commit
1aa405cb44
2 files changed
+167
-2
No files matched your search
@@ -430,7 +430,16 @@ async function computeFromRollups(userId: string): Promise<SummariesSlice> {
|
||||
// for accounts with older history, while the cold live path reported
|
||||
// the true unbounded numbers. The pre-fold remainder query below
|
||||
// splices the older rows back in.
|
||||
const foldWindowStart = new Date(Date.now() - ROLLUP_FOLD_WINDOW_MS);
|
||||
//
|
||||
// The two halves meet at ONE instant, and it is a DAY bucket boundary:
|
||||
// buckets from `foldSplit` on, raw readings before it. Buckets older than
|
||||
// the window do exist — the per-write hook folds any day it touches, the
|
||||
// import folded whole spans up to v1.39.0, every fold snaps its start down
|
||||
// to the whole day, and nothing prunes a bucket the moving window has
|
||||
// passed — so bounding only the raw half counted each reading on such a
|
||||
// day twice. Split on a boundary, a reading is in exactly one half
|
||||
// whichever buckets exist.
|
||||
const foldSplit = startOfUtcDay(new Date(Date.now() - ROLLUP_FOLD_WINDOW_MS));
|
||||
|
||||
const [narrows, latests, dayBuckets, accumulatorRows, preFoldRemainder] =
|
||||
await Promise.all([
|
||||
@@ -504,6 +513,7 @@ async function computeFromRollups(userId: string): Promise<SummariesSlice> {
|
||||
FROM "measurement_rollups"
|
||||
WHERE "user_id" = $1
|
||||
AND "granularity" = 'DAY'
|
||||
AND "bucket_start" >= $2
|
||||
ORDER BY "type", "bucket_start", (${rankUnqualified}), "source"
|
||||
)
|
||||
SELECT
|
||||
@@ -519,6 +529,7 @@ async function computeFromRollups(userId: string): Promise<SummariesSlice> {
|
||||
GROUP BY "type"
|
||||
`,
|
||||
userId,
|
||||
foldSplit,
|
||||
),
|
||||
// v1.20.0 F6 — trailing 90-day DAY rollup rows carrying the per-bucket
|
||||
// regression accumulators. The windowed slope / r² / sd compose from
|
||||
@@ -576,7 +587,7 @@ async function computeFromRollups(userId: string): Promise<SummariesSlice> {
|
||||
FROM measurements m
|
||||
WHERE m."user_id" = ${userId}
|
||||
AND m."deleted_at" IS NULL
|
||||
AND m."measured_at" < ${foldWindowStart}
|
||||
AND m."measured_at" < ${foldSplit}
|
||||
GROUP BY m."type"
|
||||
`,
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user