feat: Refactor sleep metrics calculations and improve error handling
Deploy Development / deploy (push) Successful in 54s
Build Test / pytest-backend (push) Successful in 3s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 16s

- Updated `get_sleep_avg_duration` and `get_sleep_avg_quality` functions in `placeholder_resolver.py` to provide clearer error messages when data is unavailable.
- Enhanced sleep quality calculations in `recovery_metrics.py` to handle cases with insufficient data more robustly.
- Improved data handling in various metrics files (`activity_metrics.py`, `body_metrics.py`, `nutrition_metrics.py`, `recovery_metrics.py`, and `scores.py`) to ensure consistent float conversions for calculations.
- Added utility functions in `recovery_metrics.py` for parsing and normalizing sleep segment data, enhancing the accuracy of sleep quality assessments.

These changes improve the reliability and clarity of sleep-related metrics and enhance overall data handling across the application.
This commit is contained in:
Lars committed 2026-04-11 21:27:49 +02:00
1 parent 04e23d8115
commit 41bf593d4c
7 files changed
+174 -74

No files matched your search

+6 -5
View File
@@ -469,10 +469,10 @@ def get_sleep_avg_duration(profile_id: str, days: int = 7) -> str:
if data['confidence'] == 'insufficient':
return pv_unavailable(
"Schlafdauer (formatiert) nicht aus sleep_segments ableitbar",
"Schlafdauer nicht ermittelbar",
f"confidence={data.get('confidence')}, "
f"nächte_mit_segmenten={data.get('nights_with_data', 0)}/{data.get('days_analyzed', days)}; "
f"Hinweis: {{sleep_avg_duration_7d}} nutzt duration_minutes und kann trotzdem Werte liefern",
f"nächte_mit_wert={data.get('nights_with_data', 0)}/{data.get('days_analyzed', days)} "
f"(Quellen: sleep_segments und/oder sleep_log.duration_minutes)",
)
return f"{data['avg_duration_hours']:.1f}h"
@@ -489,9 +489,10 @@ def get_sleep_avg_quality(profile_id: str, days: int = 7) -> str:
if data['confidence'] == 'insufficient':
return pv_unavailable(
"Schlafqualität (Deep+REM) nicht aus sleep_segments ableitbar",
"Schlafqualität (Deep+REM-Anteil) nicht ermittelbar",
f"confidence={data.get('confidence')}, "
f"nächte_analysiert={data.get('nights_analyzed', 0)}/{data.get('days_analyzed', days)}",
f"nächte_analysiert={data.get('nights_analyzed', 0)}/{data.get('days_analyzed', days)} "
f"(Quellen: sleep_segments oder Spalten deep/rem/light/awake_minutes)",
)
return f"{data['quality_score']:.0f}% (Deep+REM)"