fix(analytics): withhold a trend slope when the window's readings cover too few days

A least-squares slope per day over readings minutes apart explodes: three
blood-pressure readings in one sitting, the only ones of the week, fitted
thousands of mmHg per day, and the dashboard tile printed seven days of it
as the 7-day trend. The 30-day fallback had the same flaw.

A window now earns a slope only when its readings fall on at least three
UTC days whose first and last lie at least half the window apart (4 days
for the 7-day window, 15 for the 30-day one). The rule lives once in
window-confidence.ts and gates every path that publishes a windowed trend:
trendSlope (summarize and its callers), the rollup composition and the
live REGR_SLOPE fallback of the summaries slice and the comprehensive
aggregator, and the per-bucket slope of folded week, month and year
buckets. Slope, direction and confidence are present or absent together.
The fit itself stays over individual readings, as the window means do.

The parity snapshots change only where the fixture falls short of the
rule: a weekly body-fat series has two days in its 7-day window, and a
ten-day oxygen-saturation series spans nine days of its 30-day window.
This commit is contained in:
Marc-André Bombeck committed 2026-10-24 11:13:25 +02:00
1 parent 5a8f59e0c0
commit c15ff8401b
32 files changed
+850 -247

No files matched your search

@@ -5,6 +5,8 @@ import { Activity, Smile } from "lucide-react";
import { TrendCard } from "../trend-card";
import { I18nProvider } from "@/lib/i18n/context";
import { primeLocales } from "@/__tests__/helpers/prime-locales";
import { summarize } from "@/lib/analytics/trends";
import { summaryToTrend7Delta } from "@/lib/analytics/trend-delta";
primeLocales("de");
@@ -166,3 +168,42 @@ describe("<TrendCard> 7-day trend label", () => {
expect(html).not.toContain("text-warning");
});
});
/**
* The blood-pressure tile read "7-Tage-Trend: 125 (−11.412)" when the
* week's only readings were one sitting minutes apart: the regression's
* slope per day exploded and the tile printed seven days of it. The tile is
* fed the way `strip-tiles.tsx` feeds it, from `summarize()` through
* `summaryToTrend7Delta()`, so the delta it renders is the one the
* dashboard would.
*/
describe("<TrendCard> blood-pressure tile after one sitting", () => {
it("renders the 7-day mean, not a thousands-of-mmHg trend", () => {
const MINUTE = 60 * 1000;
const summary = summarize([
{ date: new Date(Date.now() - 10 * MINUTE), value: 130 },
{ date: new Date(Date.now() - 8 * MINUTE), value: 124 },
{ date: new Date(Date.now() - 6 * MINUTE), value: 118 },
]);
const html = render(
<TrendCard
label="BD (SYS)"
latest={summary.latest}
unit="mmHg"
fractionDigits={0}
avg7={summary.avg7}
avg30={summary.avg30}
slope30={summary.slope30}
trend7Delta={summaryToTrend7Delta(summary)}
icon={Activity}
directionSentiment="up-bad"
/>,
"de",
);
expect(html).not.toContain('data-slot="trend7-delta"');
expect(html).not.toContain("7-Tage-Trend");
expect(html).toContain("7T");
// No grouped thousands anywhere on the tile.
expect(html).not.toMatch(/\d\.\d{3}/);
});
});