feat: improve history overview visualization and data handling
- Added `safe_float` utility to enhance float handling in correlation calculations, preventing potential errors. - Refactored lag correlation logic in `get_history_overview_viz_bundle` to utilize absolute values safely, improving accuracy in metric comparisons. - Enhanced nutrition body merge logic to ensure proper date handling and data integrity, optimizing the retrieval of nutrition and weight logs. - Introduced new functions in the frontend for processing lag details, improving the visualization of correlation data in the History page.
This commit is contained in:
1 parent
3106ebedae
commit
0365d9eb52
3 files changed
+128
-13
No files matched your search
@@ -1205,6 +1205,34 @@ function chartJsScatterPoints(payload) {
|
||||
return raw.map((p) => ({ x: Number(p.x), y: Number(p.y) }))
|
||||
}
|
||||
|
||||
/** Backend metadata.lag_details: [{ lag, n_pairs, r }] — für Lag-Kurve L → r (C3: ggf. lag_details_hrv / lag_details_rhr) */
|
||||
function lagDetailsToCurve(meta) {
|
||||
let ld = meta?.lag_details
|
||||
if (!Array.isArray(ld) || ld.length === 0) {
|
||||
const m = String(meta?.metric || '').toUpperCase()
|
||||
if (m === 'HRV' && Array.isArray(meta?.lag_details_hrv)) ld = meta.lag_details_hrv
|
||||
else if (m === 'RHR' && Array.isArray(meta?.lag_details_rhr)) ld = meta.lag_details_rhr
|
||||
else {
|
||||
const h = meta?.lag_details_hrv
|
||||
const r = meta?.lag_details_rhr
|
||||
const hl = Array.isArray(h) ? h.length : 0
|
||||
const rl = Array.isArray(r) ? r.length : 0
|
||||
if (hl >= rl && hl > 0) ld = h
|
||||
else if (rl > 0) ld = r
|
||||
else ld = []
|
||||
}
|
||||
}
|
||||
if (!Array.isArray(ld) || ld.length === 0) return []
|
||||
return ld
|
||||
.map((d) => ({
|
||||
lag: Number(d?.lag),
|
||||
r: d?.r == null || d?.r === '' ? null : Number(d.r),
|
||||
n_pairs: d?.n_pairs != null ? Number(d.n_pairs) : null,
|
||||
}))
|
||||
.filter((d) => Number.isFinite(d.lag) && d.r != null && Number.isFinite(d.r))
|
||||
.sort((a, b) => a.lag - b.lag)
|
||||
}
|
||||
|
||||
function driverBarFromStatus(st) {
|
||||
const s = String(st || '').toLowerCase()
|
||||
if (s.includes('hinder')) return { v: -1, fill: 'var(--danger)' }
|
||||
@@ -1240,10 +1268,13 @@ function chartJsBarRows(payload, fallbackDrivers) {
|
||||
function CorrelationScatterTile({ title, accent, payload }) {
|
||||
const meta = payload?.metadata || {}
|
||||
const pts = chartJsScatterPoints(payload)
|
||||
const curve = lagDetailsToCurve(meta)
|
||||
const hasChart = pts.length > 0 && meta.correlation != null
|
||||
const r = Number(meta.correlation)
|
||||
const strength =
|
||||
!Number.isFinite(r) ? 'bad' : Math.abs(r) >= 0.35 ? 'good' : Math.abs(r) >= 0.15 ? 'warn' : 'bad'
|
||||
const bestLag = meta.best_lag_days != null ? Number(meta.best_lag_days) : null
|
||||
const maxLagAxis = curve.length ? Math.max(14, ...curve.map((d) => d.lag), bestLag || 0) : 28
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -1257,12 +1288,76 @@ function CorrelationScatterTile({ title, accent, payload }) {
|
||||
<div style={{ fontSize: 11, fontWeight: 700, color: 'var(--text1)', marginBottom: 4 }}>{title}</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', lineHeight: 1.35, marginBottom: 6 }}>
|
||||
r = {meta.correlation != null ? Number(meta.correlation).toFixed(3) : '—'}
|
||||
{meta.best_lag_days != null ? ` · Lag ${meta.best_lag_days} T` : ''}
|
||||
{meta.best_lag_days != null ? ` · bestes Lag ${meta.best_lag_days} T` : ''}
|
||||
{meta.metric ? ` · ${meta.metric}` : ''}
|
||||
{meta.confidence ? ` · ${meta.confidence}` : ''}
|
||||
</div>
|
||||
{!hasChart ? (
|
||||
<div style={{ fontSize: 11, color: 'var(--text3)' }}>{meta.message || 'Keine Daten für diese Korrelation.'}</div>
|
||||
<>
|
||||
<div style={{ fontSize: 11, color: 'var(--text3)', marginBottom: curve.length ? 8 : 0 }}>
|
||||
{meta.message || 'Keine Daten für diese Korrelation.'}
|
||||
</div>
|
||||
{curve.length > 0 && (
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginBottom: 6 }}>
|
||||
Lag-Sweep (kein Lag mit ≥15 Paaren): r über Lags — nur zur Einordnung.
|
||||
</div>
|
||||
)}
|
||||
{curve.length > 0 && (
|
||||
<ResponsiveContainer width="100%" height={120}>
|
||||
<ComposedChart data={curve} margin={{ top: 4, right: 6, bottom: 4, left: -14 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--border)" />
|
||||
<XAxis dataKey="lag" type="number" domain={[0, maxLagAxis]} tick={{ fontSize: 9, fill: 'var(--text3)' }} label={{ value: 'Lag (T)', fontSize: 9, fill: 'var(--text3)', offset: -2 }} />
|
||||
<YAxis dataKey="r" domain={[-1, 1]} tick={{ fontSize: 9, fill: 'var(--text3)' }} width={36} label={{ value: 'r', fontSize: 9, fill: 'var(--text3)', angle: -90 }} />
|
||||
<ReferenceLine y={0} stroke="var(--text3)" strokeDasharray="4 4" />
|
||||
<Tooltip
|
||||
contentStyle={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, fontSize: 10 }}
|
||||
formatter={(v, _n, item) => [`r = ${Number(v).toFixed(3)}`, `Lag ${item?.payload?.lag} T · n = ${item?.payload?.n_pairs ?? '—'}`]}
|
||||
/>
|
||||
<Line type="monotone" dataKey="r" stroke={accent} strokeWidth={2} dot={{ r: 3, fill: accent }} isAnimationActive={false} />
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
)}
|
||||
</>
|
||||
) : curve.length >= 1 ? (
|
||||
<>
|
||||
<div style={{ fontSize: 9, color: 'var(--text3)', marginBottom: 4 }}>
|
||||
Kurve: Pearson-r je Lag (Tage); starker Punkt = gewähltes bestes Lag.
|
||||
</div>
|
||||
<ResponsiveContainer width="100%" height={132}>
|
||||
<ComposedChart data={curve} margin={{ top: 4, right: 6, bottom: 4, left: -14 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--border)" />
|
||||
<XAxis dataKey="lag" type="number" domain={[0, maxLagAxis]} tick={{ fontSize: 9, fill: 'var(--text3)' }} />
|
||||
<YAxis dataKey="r" domain={[-1, 1]} tick={{ fontSize: 9, fill: 'var(--text3)' }} width={36} />
|
||||
<ReferenceLine y={0} stroke="var(--text3)" strokeDasharray="4 4" />
|
||||
<Tooltip
|
||||
contentStyle={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, fontSize: 10 }}
|
||||
formatter={(v, _n, item) => [`r = ${Number(v).toFixed(3)}`, `Lag ${item?.payload?.lag} T · n = ${item?.payload?.n_pairs ?? '—'}`]}
|
||||
/>
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey="r"
|
||||
stroke={accent}
|
||||
strokeWidth={2}
|
||||
isAnimationActive={false}
|
||||
dot={(props) => {
|
||||
const { cx, cy, payload: pl } = props
|
||||
if (cx == null || cy == null || !pl) return null
|
||||
const isBest = bestLag != null && Number(pl.lag) === bestLag
|
||||
return (
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={isBest ? 6 : 3.5}
|
||||
fill={isBest ? 'var(--surface)' : accent}
|
||||
stroke={isBest ? accent : 'none'}
|
||||
strokeWidth={isBest ? 2.5 : 0}
|
||||
/>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
</>
|
||||
) : (
|
||||
<ResponsiveContainer width="100%" height={118}>
|
||||
<ScatterChart margin={{ top: 2, right: 4, bottom: 2, left: -18 }}>
|
||||
|
||||
Reference in new issue
Block a user