Self hosted FLOSS fitness/workout, nutrition and weight tracker
Model sleep as a group of stage categories
Roland Geider8585a28369b0
diff --git a/wger/measurements/management/commands/dummy-generator-health-measurements.py b/wger/measurements/management/commands/dummy-generator-health-measurements.pyindex e30eac278..d73ca0773 100644--- a/wger/measurements/management/commands/dummy-generator-health-measurements.py+++ b/wger/measurements/management/commands/dummy-generator-health-measurements.py@@ -79,6 +79,15 @@ CATEGORIES = {'sleep': (MetricType.SLEEP, 'Sleep', 'min'),}+# Platform record type each sleep stage category collects, the total excluded:+# it is an aggregate over several of them rather than a stage of its own+SLEEP_STAGE_RECORDS = {+ MetricType.SLEEP_LIGHT: 'SLEEP_LIGHT',+ MetricType.SLEEP_DEEP: 'SLEEP_DEEP',+ MetricType.SLEEP_REM: 'SLEEP_REM',+ MetricType.SLEEP_AWAKE: 'SLEEP_AWAKE',+}+class Command(BaseCommand):"""@@ -234,7 +243,7 @@ class Command(BaseCommand):rng, category, record_type='ACTIVE_ENERGY_BURNED', daily_min=250, daily_max=900)case 'sleep':- return self.generate_sleep(rng, category)+ return self.generate_sleep(rng, self.get_components(category))case _:raise CommandError(f'No generator for metric {metric}')@@ -634,14 +643,20 @@ class Command(BaseCommand):)return entries- def generate_sleep(self, rng: random.Random, category: Category) -> list[Measurement]:+ def generate_sleep(self, rng: random.Random, components: list[Category]) -> list[Measurement]:"""Generates sleep in minutes, the unit both platforms report.- Sparse mode writes the night as the importer does: one entry per wake- day, the summed time asleep as the value and the window the segments- really cover in extra_data. Realistic mode writes the stage segments.+ A night is written per stage into the stage's own category, plus the+ total into its own. The total is always a daily aggregate, since that+ is the only shape it has; the stages follow the mode, sparse writing+ one entry per wake day and realistic the raw segments."""+ total_category, *stage_categories = components+ by_stage = {+ SLEEP_STAGE_RECORDS[category.metric_type]: category for category in stage_categories+ }+entries = []for offset in self.day_range():# A night is attributed to the day the user wakes up on, so it@@ -649,36 +664,54 @@ class Command(BaseCommand):wake_day = self.day_start(offset)bedtime = wake_day - datetime.timedelta(minutes=rng.randint(60, 180))segments = self.sleep_segments(rng, bedtime, rng.uniform(330, 540))+ asleep = [segment for segment in segments if segment[3] != 'SLEEP_AWAKE']++ entries.append(+ self.aggregate_entry(+ total_category,+ wake_day,+ sum(minutes for _, _, minutes, _ in asleep),+ record_type='SLEEP_ASLEEP',+ sample_count=len(asleep),+ extra={+ 'date_from': segments[0][0].isoformat(),+ 'date_to': segments[-1][1].isoformat(),+ },+ )+ )++ for stage, category in by_stage.items():+ of_stage = [segment for segment in segments if segment[3] == stage]+ if not of_stage:+ continue++ if self.realistic:+ entries += [+ self.entry(+ category,+ start,+ minutes,+ record_type=stage,+ key=f'sleep:{start.isoformat()}',+ extra={'date_to': end.isoformat()},+ )+ for start, end, minutes, _ in of_stage+ ]+ continue- if not self.realistic:- # The night as the importer condenses it: the summed time- # asleep, and the window the segments really coverentries.append(self.aggregate_entry(category,wake_day,- sum(minutes for _, _, minutes, _ in segments),- record_type='SLEEP_ASLEEP',- sample_count=len(segments),+ sum(minutes for _, _, minutes, _ in of_stage),+ record_type=stage,+ sample_count=len(of_stage),extra={- 'date_from': segments[0][0].isoformat(),- 'date_to': segments[-1][1].isoformat(),+ 'date_from': of_stage[0][0].isoformat(),+ 'date_to': of_stage[-1][1].isoformat(),},))- continue-- entries += [- self.entry(- category,- start,- minutes,- record_type=stage,- key=f'sleep:{start.isoformat()}',- extra={'date_to': end.isoformat()},- )- for start, end, minutes, stage in segments- ]return entriesdef sleep_segments(diff --git a/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py b/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.pyindex 452beaa8c..6e29b46b4 100644--- a/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py+++ b/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py@@ -49,6 +49,11 @@ class Migration(migrations.Migration):('distance', 'Distance'),('energy', 'Energy'),('sleep', 'Sleep'),+ ('sleep_total', 'Total sleep'),+ ('sleep_light', 'Light sleep'),+ ('sleep_deep', 'Deep sleep'),+ ('sleep_rem', 'REM sleep'),+ ('sleep_awake', 'Awake'),],default='custom',max_length=30,diff --git a/wger/measurements/models/category.py b/wger/measurements/models/category.pyindex 612596d81..fc20e7394 100644--- a/wger/measurements/models/category.py+++ b/wger/measurements/models/category.py@@ -42,6 +42,12 @@ class MetricType(models.TextChoices):DISTANCE = 'distance'ENERGY = 'energy'SLEEP = 'sleep'+ # Spelled out because the derived labels would read 'Sleep Rem'+ SLEEP_TOTAL = 'sleep_total', 'Total sleep'+ SLEEP_LIGHT = 'sleep_light', 'Light sleep'+ SLEEP_DEEP = 'sleep_deep', 'Deep sleep'+ SLEEP_REM = 'sleep_rem', 'REM sleep'+ SLEEP_AWAKE = 'sleep_awake', 'Awake'## Roles. Each type is exactly one of leaf (top-level, carries the@@ -85,6 +91,17 @@ GROUP_COMPONENTS: dict[str, list[tuple[str, str]]] = {(MetricType.BLOOD_PRESSURE_SYSTOLIC, 'Systolic'),(MetricType.BLOOD_PRESSURE_DIASTOLIC, 'Diastolic'),],+ # The total is a component of its own because a group carries no+ # measurements. It is not the sum of the three stages below it: platforms+ # also report sleep without a stage breakdown, which counts towards the+ # total and has no stage category to live in+ MetricType.SLEEP: [+ (MetricType.SLEEP_TOTAL, 'Total sleep'),+ (MetricType.SLEEP_LIGHT, 'Light sleep'),+ (MetricType.SLEEP_DEEP, 'Deep sleep'),+ (MetricType.SLEEP_REM, 'REM sleep'),+ (MetricType.SLEEP_AWAKE, 'Awake'),+ ],}"""The components of the multi-value metric types, in group order.