From 33b08a8d8250f579eb237bcc23661f6d9b9bf5a3 Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 20 Apr 2026 08:21:50 +0200 Subject: [PATCH] fix: update sleep chart payloads to use duration_minutes and quality_score - Changed SQL queries in `build_sleep_duration_quality_chart_payload` and `build_sleep_debt_chart_payload` to select `duration_minutes` instead of `total_sleep_min`. - Updated calculations for sleep duration and quality scores to reflect the new field names, ensuring accurate data representation in the recovery charts. --- backend/data_layer/recovery_chart_payloads.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/data_layer/recovery_chart_payloads.py b/backend/data_layer/recovery_chart_payloads.py index 20c87f1..0052024 100644 --- a/backend/data_layer/recovery_chart_payloads.py +++ b/backend/data_layer/recovery_chart_payloads.py @@ -211,7 +211,7 @@ def build_sleep_duration_quality_chart_payload(profile_id: str, days: int) -> Di with get_db() as conn: cur = get_cursor(conn) cur.execute( - """SELECT date, total_sleep_min + """SELECT date, duration_minutes FROM sleep_log WHERE profile_id=%s AND date >= %s ORDER BY date""", @@ -231,7 +231,9 @@ def build_sleep_duration_quality_chart_payload(profile_id: str, days: int) -> Di } labels = [row["date"].isoformat() for row in rows] - duration_hours = [safe_float(row["total_sleep_min"]) / 60 if row["total_sleep_min"] else None for row in rows] + duration_hours = [ + safe_float(row["duration_minutes"]) / 60 if row["duration_minutes"] else None for row in rows + ] quality_scores = [(d / 8 * 100) if d else None for d in duration_hours] @@ -266,7 +268,7 @@ def build_sleep_duration_quality_chart_payload(profile_id: str, days: int) -> Di "confidence": duration_data["confidence"], "data_points": len(rows), "avg_duration_hours": round(duration_data["avg_duration_hours"], 1), - "sleep_quality_score": quality_data.get("sleep_quality_score", 0), + "sleep_quality_score": quality_data.get("quality_score", 0), } ), } @@ -295,7 +297,7 @@ def build_sleep_debt_chart_payload(profile_id: str, days: int) -> Dict[str, Any] with get_db() as conn: cur = get_cursor(conn) cur.execute( - """SELECT date, total_sleep_min + """SELECT date, duration_minutes FROM sleep_log WHERE profile_id=%s AND date >= %s ORDER BY date""", @@ -321,7 +323,7 @@ def build_sleep_debt_chart_payload(profile_id: str, days: int) -> Dict[str, Any] debt_values = [] for row in rows: - actual_hours = safe_float(row["total_sleep_min"]) / 60 if row["total_sleep_min"] else 0 + actual_hours = safe_float(row["duration_minutes"]) / 60 if row["duration_minutes"] else 0 daily_deficit = target_hours - actual_hours cumulative_debt += daily_deficit debt_values.append(cumulative_debt)