feat: complete sleep module overhaul - app standard compliance
Backend improvements: - Plausibility check: phases must sum to duration (±5 min tolerance) - Auto-calculate wake_count from awake segments in import - Applied to both create_sleep and update_sleep endpoints Frontend complete rewrite: - ✅ Drag & Drop CSV import (like NutritionPage) - ✅ Inline editing (no scroll to top, edit directly in list) - ✅ Toast notifications (no more alerts, auto-dismiss 4s) - ✅ Source badges (Manual/Apple Health/Garmin with colors) - ✅ Expandable segment timeline view (JSONB sleep_segments) - ✅ Live plausibility check (shows error if phases ≠ duration) - ✅ Color-coded sleep phases (deep/rem/light/awake) - ✅ Show wake_count in list view Design improvements: - Stats card on top (7-day avg) - Import drag zone with visual feedback - Clean inline edit mode with validation - Timeline view with phase colors - Responsive button layout Confirmed: Kernschlaf (Apple Health) = Leichtschlaf (light_minutes) ✓ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
da376a8b18
commit
b52c877367
3 files changed
+551
-398
No files matched your search
@@ -171,6 +171,16 @@ def create_sleep(
|
||||
bedtime = data.bedtime if data.bedtime else None
|
||||
wake_time = data.wake_time if data.wake_time else None
|
||||
|
||||
# Plausibility check: phases should sum to duration (tolerance: 5 min)
|
||||
if any([data.deep_minutes, data.rem_minutes, data.light_minutes, data.awake_minutes]):
|
||||
phase_sum = (data.deep_minutes or 0) + (data.rem_minutes or 0) + (data.light_minutes or 0) + (data.awake_minutes or 0)
|
||||
diff = abs(data.duration_minutes - phase_sum)
|
||||
if diff > 5:
|
||||
raise HTTPException(
|
||||
400,
|
||||
f"Plausibilitätsprüfung fehlgeschlagen: Phasen-Summe ({phase_sum} min) weicht um {diff} min von Gesamtdauer ({data.duration_minutes} min) ab. Max. Toleranz: 5 min."
|
||||
)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
@@ -221,6 +231,16 @@ def update_sleep(
|
||||
bedtime = data.bedtime if data.bedtime else None
|
||||
wake_time = data.wake_time if data.wake_time else None
|
||||
|
||||
# Plausibility check: phases should sum to duration (tolerance: 5 min)
|
||||
if any([data.deep_minutes, data.rem_minutes, data.light_minutes, data.awake_minutes]):
|
||||
phase_sum = (data.deep_minutes or 0) + (data.rem_minutes or 0) + (data.light_minutes or 0) + (data.awake_minutes or 0)
|
||||
diff = abs(data.duration_minutes - phase_sum)
|
||||
if diff > 5:
|
||||
raise HTTPException(
|
||||
400,
|
||||
f"Plausibilitätsprüfung fehlgeschlagen: Phasen-Summe ({phase_sum} min) weicht um {diff} min von Gesamtdauer ({data.duration_minutes} min) ab. Max. Toleranz: 5 min."
|
||||
)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
@@ -545,6 +565,9 @@ async def import_apple_health_sleep(
|
||||
night['awake_minutes']
|
||||
)
|
||||
|
||||
# Calculate wake_count (number of awake segments)
|
||||
wake_count = sum(1 for seg in night['segments'] if seg['phase'] == 'awake')
|
||||
|
||||
# Prepare JSONB segments with full datetime
|
||||
sleep_segments = [
|
||||
{
|
||||
@@ -571,15 +594,16 @@ async def import_apple_health_sleep(
|
||||
cur.execute("""
|
||||
INSERT INTO sleep_log (
|
||||
profile_id, date, bedtime, wake_time, duration_minutes,
|
||||
deep_minutes, rem_minutes, light_minutes, awake_minutes,
|
||||
wake_count, deep_minutes, rem_minutes, light_minutes, awake_minutes,
|
||||
sleep_segments, source, updated_at
|
||||
) VALUES (
|
||||
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'apple_health', CURRENT_TIMESTAMP
|
||||
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'apple_health', CURRENT_TIMESTAMP
|
||||
)
|
||||
ON CONFLICT (profile_id, date) DO UPDATE SET
|
||||
bedtime = EXCLUDED.bedtime,
|
||||
wake_time = EXCLUDED.wake_time,
|
||||
duration_minutes = EXCLUDED.duration_minutes,
|
||||
wake_count = EXCLUDED.wake_count,
|
||||
deep_minutes = EXCLUDED.deep_minutes,
|
||||
rem_minutes = EXCLUDED.rem_minutes,
|
||||
light_minutes = EXCLUDED.light_minutes,
|
||||
@@ -594,6 +618,7 @@ async def import_apple_health_sleep(
|
||||
night['bedtime'].time(),
|
||||
night['wake_time'].time(),
|
||||
duration_minutes,
|
||||
wake_count,
|
||||
night['deep_minutes'],
|
||||
night['rem_minutes'],
|
||||
night['light_minutes'],
|
||||
|
||||
Reference in new issue
Block a user