feat: add history_overview_viz widget and enhance configuration handling
- Introduced the `history_overview_viz` widget to the dashboard, allowing users to visualize consolidated history data across various metrics. - Updated widget configuration to include `history_overview_viz` in the allowed widgets and added validation for its configuration. - Enhanced the widget catalog with details for the new `history_overview_viz` entry. - Implemented default values and validation logic for the widget's configuration, ensuring proper handling of user inputs. - Added tests to ensure proper validation of the `history_overview_viz` widget configuration. - Bumped application version to reflect the addition of the new widget.
This commit is contained in:
1 parent
e20b321b64
commit
97dbb0f80b
15 files changed
+1105
-727
No files matched your search
+11
-258
@@ -69,10 +69,11 @@ from data_layer.recovery_metrics import (
|
||||
calculate_rhr_vs_baseline_pct,
|
||||
calculate_sleep_debt_hours
|
||||
)
|
||||
from data_layer.correlations import (
|
||||
calculate_lag_correlation,
|
||||
calculate_correlation_sleep_recovery,
|
||||
calculate_top_drivers
|
||||
from data_layer.correlation_chart_payloads import (
|
||||
build_lbm_protein_correlation_chart_payload,
|
||||
build_load_vitals_correlation_chart_payload,
|
||||
build_recovery_performance_chart_payload,
|
||||
build_weight_energy_correlation_chart_payload,
|
||||
)
|
||||
from data_layer.utils import serialize_dates, safe_float, calculate_confidence
|
||||
from data_layer.nutrition_chart_payloads import (
|
||||
@@ -362,7 +363,8 @@ def get_history_overview_viz(
|
||||
session: dict = Depends(require_auth),
|
||||
) -> Dict:
|
||||
"""
|
||||
Layer 2b: Gesamtansicht «Verlauf» — KPI-Kurzformen aus den vier History-Bundles + Lag-Korrelationen C1–C4 (Metadaten).
|
||||
Layer 2b: Gesamtansicht «Verlauf» — KPI-Kurzformen aus den vier History-Bundles,
|
||||
Lag-Korrelationen C1–C4 (Metadaten) und Chart.js-Payloads C1–C4 (chart_payloads, wie /charts/*).
|
||||
"""
|
||||
profile_id = session["profile_id"]
|
||||
bundle = get_history_overview_viz_bundle(profile_id, days)
|
||||
@@ -1111,58 +1113,7 @@ def get_weight_energy_correlation_chart(
|
||||
Chart.js scatter chart with correlation data
|
||||
"""
|
||||
profile_id = session['profile_id']
|
||||
|
||||
corr_data = calculate_lag_correlation(profile_id, "energy_balance", "weight", max_lag)
|
||||
|
||||
if not corr_data or corr_data.get('correlation') is None:
|
||||
msg = "Nicht genug Daten für Korrelationsanalyse"
|
||||
if isinstance(corr_data, dict):
|
||||
msg = str(corr_data.get("interpretation") or corr_data.get("reason") or msg)
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {
|
||||
"labels": [],
|
||||
"datasets": []
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": "insufficient",
|
||||
"data_points": corr_data.get("data_points", 0) if isinstance(corr_data, dict) else 0,
|
||||
"message": msg,
|
||||
"lag_details": corr_data.get("lag_details") if isinstance(corr_data, dict) else None,
|
||||
"tdee_kcal_used": corr_data.get("tdee_kcal_used") if isinstance(corr_data, dict) else None,
|
||||
}
|
||||
}
|
||||
|
||||
# Ein Punkt: bestes Lag (max. |r|) — Berechnung in data_layer.correlations (Issue 53)
|
||||
best_lag = corr_data.get('best_lag_days', corr_data.get('best_lag', 0))
|
||||
correlation = corr_data.get('correlation', 0)
|
||||
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {
|
||||
"labels": [f"Lag {best_lag} Tage"],
|
||||
"datasets": [
|
||||
{
|
||||
"label": "Korrelation",
|
||||
"data": [{"x": best_lag, "y": correlation}],
|
||||
"backgroundColor": "#1D9E75",
|
||||
"borderColor": "#085041",
|
||||
"borderWidth": 2,
|
||||
"pointRadius": 8
|
||||
}
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": corr_data.get('confidence', 'low'),
|
||||
"correlation": round(float(correlation), 3),
|
||||
"best_lag_days": best_lag,
|
||||
"interpretation": corr_data.get('interpretation', ''),
|
||||
"data_points": corr_data.get('data_points', 0),
|
||||
"lag_details": corr_data.get("lag_details"),
|
||||
"tdee_kcal_used": corr_data.get("tdee_kcal_used"),
|
||||
"layer_1": "correlations._correlate_energy_weight",
|
||||
}
|
||||
}
|
||||
return build_weight_energy_correlation_chart_payload(profile_id, max_lag)
|
||||
|
||||
|
||||
@router.get("/lbm-protein-correlation")
|
||||
@@ -1183,55 +1134,7 @@ def get_lbm_protein_correlation_chart(
|
||||
Chart.js scatter chart with correlation data
|
||||
"""
|
||||
profile_id = session['profile_id']
|
||||
|
||||
corr_data = calculate_lag_correlation(profile_id, "protein", "lbm", max_lag)
|
||||
|
||||
if not corr_data or corr_data.get('correlation') is None:
|
||||
msg = "Nicht genug Daten für LBM-Protein Korrelation"
|
||||
if isinstance(corr_data, dict):
|
||||
msg = str(corr_data.get("interpretation") or corr_data.get("reason") or msg)
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {
|
||||
"labels": [],
|
||||
"datasets": []
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": "insufficient",
|
||||
"data_points": corr_data.get("data_points", 0) if isinstance(corr_data, dict) else 0,
|
||||
"message": msg,
|
||||
"lag_details": corr_data.get("lag_details") if isinstance(corr_data, dict) else None,
|
||||
}
|
||||
}
|
||||
|
||||
best_lag = corr_data.get('best_lag_days', corr_data.get('best_lag', 0))
|
||||
correlation = corr_data.get('correlation', 0)
|
||||
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {
|
||||
"labels": [f"Lag {best_lag} Tage"],
|
||||
"datasets": [
|
||||
{
|
||||
"label": "Korrelation",
|
||||
"data": [{"x": best_lag, "y": correlation}],
|
||||
"backgroundColor": "#3B82F6",
|
||||
"borderColor": "#1E40AF",
|
||||
"borderWidth": 2,
|
||||
"pointRadius": 8
|
||||
}
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": corr_data.get('confidence', 'low'),
|
||||
"correlation": round(float(correlation), 3),
|
||||
"best_lag_days": best_lag,
|
||||
"interpretation": corr_data.get('interpretation', ''),
|
||||
"data_points": corr_data.get('data_points', 0),
|
||||
"lag_details": corr_data.get("lag_details"),
|
||||
"layer_1": "correlations._correlate_protein_lbm",
|
||||
}
|
||||
}
|
||||
return build_lbm_protein_correlation_chart_payload(profile_id, max_lag)
|
||||
|
||||
|
||||
@router.get("/load-vitals-correlation")
|
||||
@@ -1252,83 +1155,7 @@ def get_load_vitals_correlation_chart(
|
||||
Chart.js scatter chart with correlation data
|
||||
"""
|
||||
profile_id = session['profile_id']
|
||||
|
||||
corr_hrv = calculate_lag_correlation(profile_id, "load", "hrv", max_lag)
|
||||
corr_rhr = calculate_lag_correlation(profile_id, "load", "rhr", max_lag)
|
||||
|
||||
def _abs_corr(c):
|
||||
if not c or c.get("correlation") is None:
|
||||
return -1.0
|
||||
try:
|
||||
return abs(float(c["correlation"]))
|
||||
except (TypeError, ValueError):
|
||||
return -1.0
|
||||
|
||||
if _abs_corr(corr_hrv) < 0 and _abs_corr(corr_rhr) < 0:
|
||||
msg = "Nicht genug Daten für Load-Vitals Korrelation"
|
||||
h_msg = corr_hrv.get("interpretation") if isinstance(corr_hrv, dict) else None
|
||||
r_msg = corr_rhr.get("interpretation") if isinstance(corr_rhr, dict) else None
|
||||
if h_msg or r_msg:
|
||||
msg = f"HRV: {h_msg or '—'} · RHR: {r_msg or '—'}"
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {"labels": [], "datasets": []},
|
||||
"metadata": {
|
||||
"confidence": "insufficient",
|
||||
"data_points": 0,
|
||||
"message": msg,
|
||||
"lag_details_hrv": corr_hrv.get("lag_details") if isinstance(corr_hrv, dict) else None,
|
||||
"lag_details_rhr": corr_rhr.get("lag_details") if isinstance(corr_rhr, dict) else None,
|
||||
},
|
||||
}
|
||||
|
||||
if _abs_corr(corr_hrv) >= _abs_corr(corr_rhr):
|
||||
corr_data = corr_hrv
|
||||
metric_name = "HRV"
|
||||
else:
|
||||
corr_data = corr_rhr
|
||||
metric_name = "RHR"
|
||||
|
||||
if not corr_data or corr_data.get("correlation") is None:
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {"labels": [], "datasets": []},
|
||||
"metadata": {
|
||||
"confidence": "insufficient",
|
||||
"data_points": 0,
|
||||
"message": str(corr_data.get("interpretation") or "Nicht genug Daten für Load-Vitals Korrelation"),
|
||||
},
|
||||
}
|
||||
|
||||
best_lag = corr_data.get('best_lag_days', corr_data.get('best_lag', 0))
|
||||
correlation = corr_data.get('correlation', 0)
|
||||
|
||||
return {
|
||||
"chart_type": "scatter",
|
||||
"data": {
|
||||
"labels": [f"Load → {metric_name} (Lag {best_lag}d)"],
|
||||
"datasets": [
|
||||
{
|
||||
"label": "Korrelation",
|
||||
"data": [{"x": best_lag, "y": correlation}],
|
||||
"backgroundColor": "#F59E0B",
|
||||
"borderColor": "#D97706",
|
||||
"borderWidth": 2,
|
||||
"pointRadius": 8
|
||||
}
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": corr_data.get('confidence', 'low'),
|
||||
"correlation": round(float(correlation), 3),
|
||||
"best_lag_days": best_lag,
|
||||
"metric": metric_name,
|
||||
"interpretation": corr_data.get('interpretation', ''),
|
||||
"data_points": corr_data.get('data_points', 0),
|
||||
"lag_details": corr_data.get("lag_details"),
|
||||
"layer_1": "correlations._correlate_load_vitals",
|
||||
}
|
||||
}
|
||||
return build_load_vitals_correlation_chart_payload(profile_id, max_lag)
|
||||
|
||||
|
||||
@router.get("/recovery-performance")
|
||||
@@ -1347,81 +1174,7 @@ def get_recovery_performance_chart(
|
||||
Chart.js bar chart with top drivers
|
||||
"""
|
||||
profile_id = session['profile_id']
|
||||
|
||||
# Get top drivers (hindering/helpful factors)
|
||||
drivers = calculate_top_drivers(profile_id)
|
||||
|
||||
if not drivers or len(drivers) == 0:
|
||||
return {
|
||||
"chart_type": "bar",
|
||||
"data": {
|
||||
"labels": [],
|
||||
"datasets": []
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": "insufficient",
|
||||
"data_points": 0,
|
||||
"message": "Nicht genug Daten für Driver-Analyse"
|
||||
}
|
||||
}
|
||||
|
||||
# Separate hindering and helpful
|
||||
hindering = [d for d in drivers if d.get('impact', '') == 'hindering']
|
||||
helpful = [d for d in drivers if d.get('impact', '') == 'helpful']
|
||||
|
||||
# Take top 3 of each
|
||||
top_hindering = hindering[:3]
|
||||
top_helpful = helpful[:3]
|
||||
|
||||
labels = []
|
||||
values = []
|
||||
colors = []
|
||||
|
||||
for d in top_hindering:
|
||||
labels.append(f"❌ {d.get('factor', '')}")
|
||||
values.append(-abs(d.get('score', 0))) # Negative for hindering
|
||||
colors.append("#EF4444")
|
||||
|
||||
for d in top_helpful:
|
||||
labels.append(f"✅ {d.get('factor', '')}")
|
||||
values.append(abs(d.get('score', 0))) # Positive for helpful
|
||||
colors.append("#1D9E75")
|
||||
|
||||
if not labels:
|
||||
return {
|
||||
"chart_type": "bar",
|
||||
"data": {
|
||||
"labels": [],
|
||||
"datasets": []
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": "low",
|
||||
"data_points": 0,
|
||||
"message": "Keine signifikanten Treiber gefunden"
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
"chart_type": "bar",
|
||||
"data": {
|
||||
"labels": labels,
|
||||
"datasets": [
|
||||
{
|
||||
"label": "Impact Score",
|
||||
"data": values,
|
||||
"backgroundColor": colors,
|
||||
"borderColor": "#085041",
|
||||
"borderWidth": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"confidence": "medium",
|
||||
"hindering_count": len(top_hindering),
|
||||
"helpful_count": len(top_helpful),
|
||||
"total_factors": len(drivers)
|
||||
}
|
||||
}
|
||||
return build_recovery_performance_chart_payload(profile_id)
|
||||
|
||||
|
||||
# ── Health Endpoint ──────────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user