mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(labs): show each marker's slope and where the line would leave the range
The biomarker detail read now carries a trend: the least-squares line through every numeric reading of the marker (all stored in its unit), with the slope's 95 % interval and p-value. It needs three readings over at least a year. The line is placed against the marker's band, and an exit date is given only when the interval excludes zero, the line is inside the band today and reaches a bound within three years. The detail page words it as a straight-line projection, not a forecast.
This commit is contained in:
1 parent
faa3ef867b
commit
2ba6214a65
20 files changed
+993
-11
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { primeLocales } from "@/__tests__/helpers/prime-locales";
|
||||
|
||||
import { LabTrendCard } from "../lab-trend-card";
|
||||
import type { BiomarkerTrendDto } from "../types";
|
||||
|
||||
primeLocales("de");
|
||||
|
||||
const OK: BiomarkerTrendDto = {
|
||||
status: "ok",
|
||||
n: 4,
|
||||
firstAt: "2023-01-10",
|
||||
lastAt: "2026-01-10",
|
||||
spanDays: 1096,
|
||||
slopePerYear: 12.5,
|
||||
slopeCi95: [4.25, 20.75],
|
||||
pValue: 0.02,
|
||||
fittedNow: 130,
|
||||
asOf: "2026-10-21",
|
||||
projection: "exits",
|
||||
exitDate: "2027-11-02",
|
||||
exitBound: "high",
|
||||
};
|
||||
|
||||
function render(
|
||||
trend: BiomarkerTrendDto | undefined,
|
||||
locale: "en" | "de" = "en",
|
||||
) {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale={locale}>
|
||||
<LabTrendCard trend={trend} unit="mg/dL" />
|
||||
</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("<LabTrendCard>", () => {
|
||||
it("words the server's slope and labels the exit as a projection", () => {
|
||||
const html = render(OK);
|
||||
expect(html).toContain("Rising by about 13 mg/dL a year across 4 readings");
|
||||
expect(html).toContain('data-projection="exits"');
|
||||
expect(html).toContain("upper end of your reference range");
|
||||
expect(html).toContain("projection, not a forecast");
|
||||
expect(html).toContain("4.25 to 20.75 mg/dL");
|
||||
});
|
||||
|
||||
it("speaks German with du", () => {
|
||||
const html = render(OK, "de");
|
||||
expect(html).toContain("deines Referenzbereichs");
|
||||
expect(html).toContain("Fortschreibung, keine Vorhersage");
|
||||
});
|
||||
|
||||
it("draws no projection sentence without a clear trend, and nothing when insufficient", () => {
|
||||
const html = render({
|
||||
...OK,
|
||||
projection: "no_clear_trend",
|
||||
exitDate: null,
|
||||
exitBound: null,
|
||||
});
|
||||
expect(html).toContain('data-projection="no_clear_trend"');
|
||||
expect(html).not.toContain("forecast");
|
||||
expect(
|
||||
render({
|
||||
status: "insufficient",
|
||||
reason: "too_few_values",
|
||||
n: 2,
|
||||
spanDays: 40,
|
||||
}),
|
||||
).toBe("");
|
||||
expect(render(undefined)).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -55,6 +55,7 @@ import { importWithRetry } from "@/lib/retry-import";
|
||||
import { BiomarkerAliases } from "./biomarker-aliases";
|
||||
import { BiomarkerForm } from "./biomarker-form";
|
||||
import { LabForm } from "./lab-form";
|
||||
import { LabTrendCard } from "./lab-trend-card";
|
||||
import type {
|
||||
BiomarkerDto,
|
||||
LabResultDto,
|
||||
@@ -505,6 +506,10 @@ export function LabBiomarkerDetail({ biomarkerId }: { biomarkerId: string }) {
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<LabTrendCard
|
||||
trend={marker?.trend}
|
||||
unit={marker?.unit ?? latest?.unit ?? ""}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
"use client";
|
||||
|
||||
import { TrendingUp } from "lucide-react";
|
||||
|
||||
import { Card, CardContent, CardHeader } from "@/components/ui/card";
|
||||
import { TileHeader } from "@/components/insights/tile-header";
|
||||
import { useFormatters, useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
import type { BiomarkerTrendDto } from "./types";
|
||||
|
||||
/**
|
||||
* v1.44 — the marker's slope over all its readings, and where that straight
|
||||
* line would leave the reference band.
|
||||
*
|
||||
* Server-authoritative: every number is the route's (`computeLabTrend`); this
|
||||
* only words it. Renders nothing below three readings over a year. The exit
|
||||
* sentence names itself a projection, never a forecast.
|
||||
*/
|
||||
export function LabTrendCard({
|
||||
trend,
|
||||
unit,
|
||||
}: {
|
||||
trend: BiomarkerTrendDto | undefined;
|
||||
unit: string;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const fmt = useFormatters();
|
||||
if (!trend || trend.status !== "ok") return null;
|
||||
|
||||
const digits = Math.abs(trend.slopePerYear) >= 10 ? 0 : 2;
|
||||
const per = (v: number) => fmt.number(Math.abs(v), digits);
|
||||
const direction =
|
||||
trend.slopePerYear > 0
|
||||
? "rising"
|
||||
: trend.slopePerYear < 0
|
||||
? "falling"
|
||||
: "flat";
|
||||
const slopeLine = t(`labs.trend.slope.${direction}`, {
|
||||
amount: per(trend.slopePerYear),
|
||||
unit,
|
||||
n: trend.n,
|
||||
since: fmt.dateShortSmartCalendar(trend.firstAt),
|
||||
});
|
||||
const projectionLine =
|
||||
trend.projection === "exits" && trend.exitDate
|
||||
? t(`labs.trend.projection.exits.${trend.exitBound ?? "high"}`, {
|
||||
date: fmt.dateShortSmartCalendar(trend.exitDate),
|
||||
})
|
||||
: trend.projection === "no_band"
|
||||
? null
|
||||
: t(`labs.trend.projection.${trend.projection}`);
|
||||
|
||||
return (
|
||||
<Card data-slot="lab-trend">
|
||||
<CardHeader>
|
||||
<TileHeader icon={TrendingUp} title={t("labs.trend.title")} />
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-1">
|
||||
<p className="text-sm" data-slot="lab-trend-slope">
|
||||
{slopeLine}
|
||||
</p>
|
||||
{projectionLine ? (
|
||||
<p
|
||||
className="text-sm"
|
||||
data-slot="lab-trend-projection"
|
||||
data-projection={trend.projection}
|
||||
>
|
||||
{projectionLine}
|
||||
</p>
|
||||
) : null}
|
||||
<p className="text-muted-foreground text-xs">
|
||||
{t("labs.trend.interval", {
|
||||
lo: fmt.number(trend.slopeCi95[0], 2),
|
||||
hi: fmt.number(trend.slopeCi95[1], 2),
|
||||
unit,
|
||||
})}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -108,8 +108,40 @@ export interface BiomarkerDto {
|
||||
hidden: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
/** v1.44 — the detail read's trend line; absent on the list and older servers. */
|
||||
trend?: BiomarkerTrendDto;
|
||||
}
|
||||
|
||||
/** v1.44 — `GET /api/biomarkers/{id}` `trend` (`computeLabTrend`). */
|
||||
export type BiomarkerTrendDto =
|
||||
| {
|
||||
status: "ok";
|
||||
n: number;
|
||||
firstAt: string;
|
||||
lastAt: string;
|
||||
spanDays: number;
|
||||
slopePerYear: number;
|
||||
slopeCi95: [number, number];
|
||||
pValue: number;
|
||||
fittedNow: number;
|
||||
asOf: string;
|
||||
projection:
|
||||
| "exits"
|
||||
| "beyond_horizon"
|
||||
| "no_exit"
|
||||
| "already_outside"
|
||||
| "no_clear_trend"
|
||||
| "no_band";
|
||||
exitDate: string | null;
|
||||
exitBound: "low" | "high" | null;
|
||||
}
|
||||
| {
|
||||
status: "insufficient";
|
||||
reason: "too_few_values" | "span_too_short";
|
||||
n: number;
|
||||
spanDays: number;
|
||||
};
|
||||
|
||||
export interface BiomarkerListResponse {
|
||||
biomarkers: BiomarkerDto[];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user