Files
HealthLog/src/lib/analytics/health-score-inputs.ts
T
Marc-André Bombeck 3bb130d4a1 fix(validation): stop a future timestamp emitting a spurious past-bound error
Review follow-up to the shared entry-instant guard. The past-floor refine
re-ran the full plausibility predicate, so a future-dated instant tripped
both refines and the 422 carried a nonsensical "too far in the past" message
alongside the correct future one. The floor refine now checks the past bound
alone; a future value emits only the future message, matching the pre-refactor
single-message contract.

Also harden the Health-Score BP inputs and de-duplicate the tile-window
derivation surfaced in the same review:

- the prior-week graded score now keys off the prior-week window's own
  sufficiency rather than the current window's, so the week-over-week graded
  delta never pairs a confident current value against a thin-sample prior one;
- the BD-Zielbereich 90-day count + effective span derive through one shared
  `deriveBpWindow90` helper, so the dashboard snapshot and the analytics thick
  slice can never compute the gate or the label span differently.
2026-06-14 13:59:56 +02:00

109 lines
4.9 KiB
TypeScript

/**
* v1.17 W1b — single Health-Score BP-pillar input builder.
*
* The Personal Health Score is computed by `computeUserHealthScoreFastPath`
* from two server callers: the analytics route (the Insights health-score
* card) and the dashboard snapshot (the hero ring). Before this builder the
* two callers assembled the BP-pillar inputs by hand and DIVERGED:
*
* - the analytics route fed the BP pillar from the all-time in-target rate
* and passed the prior-week values for the week-over-week delta;
* - the dashboard snapshot fed the pillar from the trailing-30-day rate and
* omitted the prior-week values entirely (so the ring's delta zeroed BP
* out and the pillar's presence gate flipped on a user with BP history
* but no readings in the last month).
*
* Same user, two different scores and two different bands. v1.17 W1d already
* unified the WINDOW (both read the trailing-90-day rate); this builder
* unifies the ASSEMBLY so the window choice, the all-time fallback, the
* graded-score wiring and the prior-week delta inputs can never drift between
* the two surfaces again. Both callers pass the identical
* `BpInTargetEnvelope` pair through here and consume the result verbatim.
*
* Pure & deterministic — no I/O. The callers own the
* `computeBpInTargetFastPath` reads (current + prior-week) and hand the two
* envelopes in.
*/
import type { BpInTargetEnvelope } from "./bp-in-target-fast-path";
import { isWindowSufficient } from "./window-confidence";
/**
* The BP-pillar slice of `HealthScoreFastPathInput`. Both Health-Score
* callers spread this into their `computeUserHealthScoreFastPath` call so the
* pillar is graded off identical inputs regardless of surface.
*/
export interface HealthScoreBpInputs {
bpInTargetPct: number | null;
bpInTargetPctPriorWeek: number | null;
bpGradedScore: number | null;
bpGradedScorePriorWeek: number | null;
}
/**
* Build the BP-pillar inputs from the current-window and prior-week
* `computeBpInTargetFastPath` envelopes.
*
* - The pillar rate (presence gate + secondary stat) reads the canonical
* trailing-90-day window (W1d), falling back to the all-time rate only when
* the 90-day window is itself null so a sparse-but-historical account keeps
* its BP pillar.
* - v1.17 W1b — when the 90-day window holds fewer than the confidence floor
* (`isWindowSufficient`) the pillar collapses to `null` so a thin-data user
* does not get a confident BP pillar grading off a handful of readings. The
* dashboard tile shows "collecting data" for the same input; the pillar and
* the tile agree on the gate. The all-time fallback still rescues an account
* with a deep history but a quiet last quarter.
* - The prior-week rate uses the prior-week run's 90-day window so the
* week-over-week delta compares the same window on both ends.
* - The graded clinical-proximity score (the pillar VALUE) and its prior-week
* counterpart come straight off the envelopes.
*
* `null` for the current envelope (no BP targets / no readings) collapses
* every field to `null`, which the score helper reads as "pillar absent".
*/
export function buildHealthScoreBpInputs(
current: BpInTargetEnvelope | null,
priorWeek: BpInTargetEnvelope | null,
): HealthScoreBpInputs {
if (!current) {
return {
bpInTargetPct: null,
bpInTargetPctPriorWeek: null,
bpGradedScore: null,
bpGradedScorePriorWeek: null,
};
}
// v1.17 W1b — a thin 90-day window (below the confidence floor) is not a
// trustworthy pillar input. Fall through to the all-time rate when the
// 90-day window is itself sparse so a deep-history account survives a quiet
// quarter, but never grade off a sub-floor 90-day sample.
const last90Sufficient =
current.last90Days !== null &&
isWindowSufficient(current.last90Days.pairs);
const bpInTargetPct = last90Sufficient
? (current.last90Days?.pct ?? null)
: (current.allTime?.pct ?? null);
const priorWeek90Sufficient =
priorWeek?.last90Days != null &&
isWindowSufficient(priorWeek.last90Days.pairs);
const bpInTargetPctPriorWeek = priorWeek90Sufficient
? (priorWeek?.last90Days?.pct ?? null)
: (priorWeek?.allTime?.pct ?? null);
// The graded clinical score is the pillar VALUE; suppress it together with
// the rate when the 90-day window is thin AND no all-time rate rescues the
// pillar, so the pillar disappears cleanly instead of grading a thin sample.
// The prior-week graded score keys off the PRIOR-WEEK window's own presence
// (not the current one) so the week-over-week delta never pairs a confident
// current value against a thin-sample prior value.
const pillarPresent = bpInTargetPct !== null;
const priorPillarPresent = bpInTargetPctPriorWeek !== null;
return {
bpInTargetPct,
bpInTargetPctPriorWeek,
bpGradedScore: pillarPresent ? current.gradedScore : null,
bpGradedScorePriorWeek: priorPillarPresent
? (priorWeek?.gradedScore ?? null)
: null,
};
}