feat: add vitals history analytics to recovery dashboard
Deploy Development / deploy (push) Successful in 1m0s
Build Test / pytest-backend (push) Successful in 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 17s

- Integrated the `build_vitals_history_and_analytics` function into the recovery dashboard to provide historical insights on vital signs.
- Updated the `get_recovery_dashboard_viz_bundle` function to include a new chart for vitals history, enhancing the data visualization capabilities.
- Enhanced the `RecoveryDashboardOverview` component to render vitals history, including improved messaging for insufficient data and visual representation of trends.
This commit is contained in:
Lars committed 2026-04-20 09:36:10 +02:00
1 parent 819914b7cc
commit e7bcdc3228
3 files changed
+422 -97

No files matched your search

@@ -1,17 +1,6 @@
import { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import {
LineChart,
Line,
BarChart,
Bar,
Cell,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
CartesianGrid,
} from 'recharts'
import { LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts'
import { api } from '../utils/api'
import KpiTilesOverview from './KpiTilesOverview'
import { getStatusColor, getStatusBg } from '../utils/interpret'
@@ -19,19 +8,11 @@ import dayjs from 'dayjs'
const fmtDate = (d) => dayjs(d).format('DD.MM.')
function vitalToneToUi(tone) {
if (tone === 'good') return 'good'
if (tone === 'bad') return 'bad'
if (tone === 'neutral') return 'neutral'
return 'warn'
}
function barFillForTone(tone) {
const ui = vitalToneToUi(tone)
if (ui === 'good') return '#1D9E75'
if (ui === 'bad') return '#D85A30'
if (ui === 'neutral') return '#6B7280'
return '#EF9F27'
function insightBulletStripe(tone) {
if (tone === 'good') return getStatusColor('good')
if (tone === 'bad') return getStatusColor('bad')
if (tone === 'neutral') return '#6B7280'
return getStatusColor('warn')
}
function ChartCard({ title, loading, error, children }) {
@@ -127,6 +108,7 @@ export default function RecoveryDashboardOverview({
const sleepData = viz.charts?.sleep_duration_quality
const debtData = viz.charts?.sleep_debt
const vitalsData = viz.charts?.vital_signs_matrix
const vitalsHistory = viz.charts?.vitals_history
const kpiTiles = (viz.kpi_tiles || []).map((t) => ({
...t,
@@ -315,51 +297,146 @@ export default function RecoveryDashboardOverview({
)
}
const renderVitalsHistory = () => {
const vh = vitalsHistory
if (!vh) {
return <div style={{ padding: 16, fontSize: 12, color: 'var(--text3)' }}>Keine Verlaufs-Daten (Bundle).</div>
}
if (vh.metadata?.confidence === 'insufficient') {
return (
<div style={{ padding: 16, fontSize: 12, color: 'var(--text3)', lineHeight: 1.5 }}>
{vh.metadata?.message || 'Zu wenige Vitaldaten im gewählten Fenster für Verläufe.'}
</div>
)
}
const series = vh.series || {}
const keys = Object.keys(series)
const bullets = vh.analytics?.bullets || []
const corrNote = vh.metadata?.load_rhr_correlation
const pairsN = vh.metadata?.load_rhr_pairs_n
return (
<div style={{ width: '100%', minWidth: 0 }}>
{bullets.length > 0 ? (
<div style={{ marginBottom: 14 }}>
<div style={{ fontSize: 12, fontWeight: 600, color: 'var(--text3)', marginBottom: 8 }}>
Einordnung (Vital & Belastung)
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
{bullets.map((b) => (
<div
key={b.key}
style={{
borderRadius: 8,
padding: '10px 12px',
border: '1px solid var(--border)',
borderLeft: `4px solid ${insightBulletStripe(b.tone)}`,
background: 'var(--surface2)',
}}
>
<div style={{ fontSize: 12, fontWeight: 600, marginBottom: 4 }}>{b.title}</div>
<div style={{ fontSize: 12, color: 'var(--text2)', lineHeight: 1.45 }}>{b.body}</div>
</div>
))}
</div>
{corrNote != null && pairsN != null ? (
<div style={{ marginTop: 8, fontSize: 10, color: 'var(--text3)' }}>
Korrelation Trainingsminuten (Tag) ↔ Ruhepuls (Folgetag): r ≈ {corrNote} (n = {pairsN} Paare)
</div>
) : null}
</div>
) : null}
<div style={{ fontSize: 11, color: 'var(--text3)', marginBottom: 8 }}>
Je Kennzahl eigene Skala (physische Einheit). Verlauf sinnvoll ab ca. 2–3 Messpunkten.
</div>
{keys.map((k) => {
const m = series[k]
const pts = m.points || []
if (pts.length === 0) return null
const chartData = pts.map((p) => ({
...p,
d: fmtDate(p.date),
}))
const vals = pts.map((p) => p.value)
const mn = Math.min(...vals)
const mx = Math.max(...vals)
const span = mx - mn
const pad = span < 1e-9 ? Math.max(Math.abs(mn) * 0.05, 0.5) : span * 0.12
return (
<div key={k} style={{ marginBottom: 16, width: '100%' }}>
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text2)', marginBottom: 4 }}>
{m.label_de} ({m.unit})
{m.n != null ? (
<span style={{ fontWeight: 400, color: 'var(--text3)' }}> · n = {m.n}</span>
) : null}
{m.mean != null ? (
<span style={{ fontWeight: 400, color: 'var(--text3)' }}> · Ø {m.mean}</span>
) : null}
</div>
{pts.length === 1 ? (
<div style={{ fontSize: 12, color: 'var(--text3)', padding: '8px 0' }}>
Ein Messpunkt ({m.last}) — weiter erfassen, um einen Verlauf zu sehen.
</div>
) : (
<div style={{ width: '100%', height: 200, minHeight: 200 }}>
<ResponsiveContainer width="100%" height="100%">
<LineChart data={chartData} margin={{ top: 4, right: 12, bottom: 0, left: 0 }}>
<CartesianGrid stroke="var(--border)" strokeDasharray="3 3" />
<XAxis dataKey="d" tick={{ fontSize: 9, fill: 'var(--text3)' }} interval="preserveStartEnd" />
<YAxis
domain={[mn - pad, mx + pad]}
tick={{ fontSize: 9, fill: 'var(--text3)' }}
width={44}
/>
<Tooltip
contentStyle={{
background: 'var(--surface)',
border: '1px solid var(--border)',
borderRadius: 8,
fontSize: 11,
}}
/>
<Line
type="monotone"
dataKey="value"
stroke={m.color || '#1D9E75'}
strokeWidth={2}
dot={{ r: 3 }}
name={m.label_de}
/>
</LineChart>
</ResponsiveContainer>
</div>
)}
</div>
)
})}
{keys.length === 0 ? (
<div style={{ padding: 12, fontSize: 12, color: 'var(--text3)' }}>Keine Vital-Zeitreihen im Fenster.</div>
) : null}
</div>
)
}
const renderVitalSigns = () => {
if (!vitalsData) {
return (
<div style={{ padding: 20, textAlign: 'center', color: 'var(--text3)', fontSize: 12 }}>
Keine Vital-Matrix-Daten
Keine Snapshot-Daten zur Vital-Matrix.
</div>
)
}
const meta = vitalsData.metadata || {}
const items = meta.vital_items || []
const ds0 = vitalsData.data?.datasets?.[0]
const hasRawChart =
Array.isArray(vitalsData.data?.labels) &&
vitalsData.data.labels.length > 0 &&
Array.isArray(ds0?.data) &&
ds0.data.length > 0
const ins = meta.confidence === 'insufficient'
if (ins && items.length === 0 && !hasRawChart) {
if (ins && items.length === 0) {
return (
<div style={{ padding: 20, textAlign: 'center', color: 'var(--text3)', fontSize: 12 }}>
{meta.message || 'Keine aktuellen Vitalwerte'}
</div>
)
}
let chartRows = items.map((it) => ({
name: it.label_de,
value: Number(it.bar_value ?? 0),
fill: barFillForTone(it.tone),
tone: it.tone,
}))
if (chartRows.length === 0 && hasRawChart) {
const bg = ds0.backgroundColor
chartRows = vitalsData.data.labels.map((name, i) => ({
name,
value: Number(ds0.data[i] ?? 0),
fill: Array.isArray(bg) ? bg[i] || '#1D9E75' : bg || '#1D9E75',
tone: 'neutral',
}))
}
if (items.length === 0 && chartRows.length === 0) {
return (
<div style={{ padding: 20, textAlign: 'center', color: 'var(--text3)', fontSize: 12 }}>
Keine Vitalwerte zur Anzeige (Server lieferte weder Kennzeilen noch Diagrammdaten).
{meta.message || 'Keine zusammengefassten Vitalwerte für die Einordnung.'}
</div>
)
}
@@ -424,54 +501,19 @@ export default function RecoveryDashboardOverview({
</div>
) : null}
{items.length === 0 && chartRows.length > 0 ? (
<div style={{ fontSize: 11, color: 'var(--text3)', marginBottom: 8 }}>
Diagramm aus Server-Daten (ohne Zonen-Detail — bitte App aktualisieren oder Cache leeren).
</div>
) : null}
{chartRows.length > 0 ? (
<>
<div style={{ fontSize: 11, color: 'var(--text3)', marginBottom: 6 }}>
Relative Einordnung (0–100, nur Übersicht — keine körperliche Messgröße)
</div>
<ResponsiveContainer width="100%" height={Math.max(200, chartRows.length * 36)}>
<BarChart data={chartRows} margin={{ top: 4, right: 8, bottom: 0, left: 8 }} layout="horizontal">
<CartesianGrid stroke="var(--border)" strokeDasharray="3 3" />
<XAxis type="number" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={[0, 100]} />
<YAxis type="category" dataKey="name" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} width={100} />
<Tooltip
contentStyle={{
background: 'var(--surface)',
border: '1px solid var(--border)',
borderRadius: 8,
fontSize: 11,
}}
formatter={(v) => [`${Number(v).toFixed(0)} (relativ)`, 'Einordnung']}
/>
<Bar dataKey="value" name="Einordnung" radius={[0, 3, 3, 0]}>
{chartRows.map((row, i) => (
<Cell key={`c-${i}`} fill={row.fill} />
))}
</Bar>
</BarChart>
</ResponsiveContainer>
</>
) : null}
<div style={{ marginTop: 10, fontSize: 10, color: 'var(--text3)', lineHeight: 1.45 }}>
{vitDate ? (
<>
Baseline-Vitals Stand: <strong>{fmtDate(vitDate)}</strong>
Baseline-Vitals (Snapshot): <strong>{fmtDate(vitDate)}</strong>
</>
) : null}
{vitDate && bpDate ? ' · ' : null}
{bpDate ? (
<>
Blutdruck Stand: <strong>{fmtDate(bpDate)}</strong>
Blutdruck: <strong>{fmtDate(bpDate)}</strong>
</>
) : null}
{!vitDate && !bpDate ? <>Anzeige-Zeitraum Vital-Matrix: {vDays} Tage</> : null}
{!vitDate && !bpDate ? <>Bezug: Vital-Matrix {vDays} Tage</> : null}
</div>
{disclaimer ? (
<div style={{ marginTop: 8, fontSize: 10, color: 'var(--text3)', fontStyle: 'italic' }}>{disclaimer}</div>
@@ -538,9 +580,10 @@ export default function RecoveryDashboardOverview({
<ChartCard title="📊 Recovery Score">{renderRecoveryScore()}</ChartCard>
<ChartCard title="📊 HRV & Ruhepuls">{renderHrvRhr()}</ChartCard>
<ChartCard title="📈 Vitalwerte — Verlauf (je Kennzahl)">{renderVitalsHistory()}</ChartCard>
<ChartCard title="📊 Schlaf: Dauer & Qualität">{renderSleepQuality()}</ChartCard>
<ChartCard title="📊 Schlafschuld">{renderSleepDebt()}</ChartCard>
<ChartCard title="📊 Vitalwerte Überblick">{renderVitalSigns()}</ChartCard>
<ChartCard title="📋 Vitalwerte — aktuelle Einordnung">{renderVitalSigns()}</ChartCard>
</div>
)
}