feat: Phase 0c - recovery_metrics.py module complete
Data Layer: - get_sleep_duration_data() - avg duration with hours/minutes breakdown - get_sleep_quality_data() - Deep+REM percentage with phase breakdown - get_rest_days_data() - total count + breakdown by rest type Placeholder Layer: - get_sleep_avg_duration() - refactored to use data layer - get_sleep_avg_quality() - refactored to use data layer - get_rest_days_count() - refactored to use data layer All 3 recovery data functions + 3 placeholder refactors complete. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b2ad9fa1c
commit
432f7ba49f
3 files changed
+324
-68
No files matched your search
@@ -28,6 +28,11 @@ from data_layer.activity_metrics import (
|
||||
get_activity_detail_data,
|
||||
get_training_type_distribution_data
|
||||
)
|
||||
from data_layer.recovery_metrics import (
|
||||
get_sleep_duration_data,
|
||||
get_sleep_quality_data,
|
||||
get_rest_days_data
|
||||
)
|
||||
|
||||
|
||||
# ── Helper Functions ──────────────────────────────────────────────────────────
|
||||
@@ -305,85 +310,44 @@ def get_trainingstyp_verteilung(profile_id: str, days: int = 14) -> str:
|
||||
|
||||
|
||||
def get_sleep_avg_duration(profile_id: str, days: int = 7) -> str:
|
||||
"""Calculate average sleep duration in hours."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||
cur.execute(
|
||||
"""SELECT sleep_segments FROM sleep_log
|
||||
WHERE profile_id=%s AND date >= %s
|
||||
ORDER BY date DESC""",
|
||||
(profile_id, cutoff)
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
"""
|
||||
Calculate average sleep duration in hours.
|
||||
|
||||
if not rows:
|
||||
return "nicht verfügbar"
|
||||
Phase 0c: Refactored to use data_layer.recovery_metrics.get_sleep_duration_data()
|
||||
This function now only FORMATS the data for AI consumption.
|
||||
"""
|
||||
data = get_sleep_duration_data(profile_id, days)
|
||||
|
||||
total_minutes = 0
|
||||
for row in rows:
|
||||
segments = row['sleep_segments']
|
||||
if segments:
|
||||
# Sum duration_min from all segments
|
||||
for seg in segments:
|
||||
total_minutes += seg.get('duration_min', 0)
|
||||
if data['confidence'] == 'insufficient':
|
||||
return "nicht verfügbar"
|
||||
|
||||
if total_minutes == 0:
|
||||
return "nicht verfügbar"
|
||||
|
||||
avg_hours = total_minutes / len(rows) / 60
|
||||
return f"{avg_hours:.1f}h"
|
||||
return f"{data['avg_duration_hours']:.1f}h"
|
||||
|
||||
|
||||
def get_sleep_avg_quality(profile_id: str, days: int = 7) -> str:
|
||||
"""Calculate average sleep quality (Deep+REM %)."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||
cur.execute(
|
||||
"""SELECT sleep_segments FROM sleep_log
|
||||
WHERE profile_id=%s AND date >= %s
|
||||
ORDER BY date DESC""",
|
||||
(profile_id, cutoff)
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
"""
|
||||
Calculate average sleep quality (Deep+REM %).
|
||||
|
||||
if not rows:
|
||||
return "nicht verfügbar"
|
||||
Phase 0c: Refactored to use data_layer.recovery_metrics.get_sleep_quality_data()
|
||||
This function now only FORMATS the data for AI consumption.
|
||||
"""
|
||||
data = get_sleep_quality_data(profile_id, days)
|
||||
|
||||
total_quality = 0
|
||||
count = 0
|
||||
for row in rows:
|
||||
segments = row['sleep_segments']
|
||||
if segments:
|
||||
# Note: segments use 'phase' key (not 'stage'), stored lowercase (deep, rem, light, awake)
|
||||
deep_rem_min = sum(s.get('duration_min', 0) for s in segments if s.get('phase') in ['deep', 'rem'])
|
||||
total_min = sum(s.get('duration_min', 0) for s in segments)
|
||||
if total_min > 0:
|
||||
quality_pct = (deep_rem_min / total_min) * 100
|
||||
total_quality += quality_pct
|
||||
count += 1
|
||||
if data['confidence'] == 'insufficient':
|
||||
return "nicht verfügbar"
|
||||
|
||||
if count == 0:
|
||||
return "nicht verfügbar"
|
||||
|
||||
avg_quality = total_quality / count
|
||||
return f"{avg_quality:.0f}% (Deep+REM)"
|
||||
return f"{data['quality_score']:.0f}% (Deep+REM)"
|
||||
|
||||
|
||||
def get_rest_days_count(profile_id: str, days: int = 30) -> str:
|
||||
"""Count rest days in the given period."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||
cur.execute(
|
||||
"""SELECT COUNT(DISTINCT date) as count FROM rest_days
|
||||
WHERE profile_id=%s AND date >= %s""",
|
||||
(profile_id, cutoff)
|
||||
)
|
||||
row = cur.fetchone()
|
||||
count = row['count'] if row else 0
|
||||
return f"{count} Ruhetage"
|
||||
"""
|
||||
Count rest days in the given period.
|
||||
|
||||
Phase 0c: Refactored to use data_layer.recovery_metrics.get_rest_days_data()
|
||||
This function now only FORMATS the data for AI consumption.
|
||||
"""
|
||||
data = get_rest_days_data(profile_id, days)
|
||||
return f"{data['total_rest_days']} Ruhetage"
|
||||
|
||||
|
||||
def get_vitals_avg_hr(profile_id: str, days: int = 7) -> str:
|
||||
|
||||
Reference in new issue
Block a user