diff --git a/wger/manager/migrations/0028_backfill_session_day.py b/wger/manager/migrations/0028_backfill_session_day.py new file mode 100644 index 000000000..84e727260 --- /dev/null +++ b/wger/manager/migrations/0028_backfill_session_day.py @@ -0,0 +1,97 @@ +from django.core.cache import cache +from django.db import ( + migrations, + models, + transaction, +) + +from wger.utils.cache import CacheKeyMapper + + +BATCH_SIZE = 2000 + + +def clear_routine_caches(routines): + """ + Drops the cached date sequences of the given (routine, user) pairs. + + The sequence is cached for a month, so without this the routines would keep + serving the stalled sequence the backfill just repaired. The structure is + untouched and keeps its cache. + """ + keys = [] + for routine_id, user_id in routines: + keys.append(CacheKeyMapper.routine_date_sequence_key(routine_id)) + keys.append(CacheKeyMapper.routine_api_date_sequence_display_key(routine_id, user_id)) + keys.append(CacheKeyMapper.routine_api_date_sequence_gym_key(routine_id, user_id)) + keys.append(CacheKeyMapper.routine_api_logs(routine_id, user_id)) + keys.append(CacheKeyMapper.routine_api_stats(routine_id, user_id)) + + for i in range(0, len(keys), BATCH_SIZE): + cache.delete_many(keys[i : i + BATCH_SIZE]) + + +def backfill_session_day(apps, schema_editor): + """ + Set the day on sessions that were created automatically for a log. + + Until recently these were created with only a user, date and routine, which + made them invisible to days with need_logs_to_advance: that gate looks + sessions up by their day. The affected routines stall on the gated day + since it can't advance. + + The day is recovered from the session's logs, which point at a slot entry + and through it at the day. Sessions whose logs span several days, or that + have no logs on a routine day at all, are left alone: a wrong day would + open the gate on a day the user never trained. + """ + WorkoutSession = apps.get_model('manager', 'WorkoutSession') + WorkoutLog = apps.get_model('manager', 'WorkoutLog') + + resolved = ( + WorkoutLog.objects.filter( + session__day__isnull=True, + session__routine__isnull=False, + slot_entry__isnull=False, + # Only trust logs whose day actually belongs to the session's routine + slot_entry__slot__day__routine=models.F('session__routine'), + ) + .values('session_id') + .annotate( + day_count=models.Count('slot_entry__slot__day', distinct=True), + day_id=models.Min('slot_entry__slot__day'), + ) + .filter(day_count=1) + .values_list('session_id', 'day_id', 'session__routine_id', 'session__user_id') + .order_by('session_id') + .iterator() + ) + + batch = [] + touched_routines = set() + + for session_id, day_id, routine_id, user_id in resolved: + batch.append(WorkoutSession(pk=session_id, day_id=day_id)) + touched_routines.add((routine_id, user_id)) + + if len(batch) >= BATCH_SIZE: + WorkoutSession.objects.bulk_update(batch, ['day_id']) + batch = [] + + if batch: + WorkoutSession.objects.bulk_update(batch, ['day_id']) + + # Only after the commit: while the transaction is open, a concurrent read + # still sees the old rows and would cache the stalled sequence again + if touched_routines: + transaction.on_commit(lambda: clear_routine_caches(touched_routines)) + + +class Migration(migrations.Migration): + dependencies = [ + ('manager', '0027_cleanup_fields'), + ] + + operations = [ + migrations.RunPython(backfill_session_day, migrations.RunPython.noop), + ] diff --git a/wger/manager/models/log.py b/wger/manager/models/log.py index 62fd228e6..0ab157720 100644 --- a/wger/manager/models/log.py +++ b/wger/manager/models/log.py @@ -271,11 +271,18 @@ class WorkoutLog(models.Model): # Auto-create a session only if the client didn't provide one. if not self.session_id: + # The day is only a default: an existing session for the date keeps + # its day, and the (user, date, routine) dedup semantics stay unchanged. + day = None + if self.slot_entry and self.slot_entry.slot.day.routine_id == self.routine_id: + day = self.slot_entry.slot.day + try: self.session = WorkoutSession.objects.get_or_create( user=self.user, date=self.date, routine=self.routine, + defaults={'day': day}, )[0] except WorkoutSession.MultipleObjectsReturned: # TODO: duplicate sessions can exist for the same (user, date, routine) @@ -300,3 +307,23 @@ class WorkoutLog(models.Model): # Save to db super().save(*args, **kwargs) + + # The app creates its sessions itself and can leave the day empty, which + # hides them from days with need_logs_to_advance. Only filled in when all + # logs of the session agree on one day, like migration 0028 does: a wrong + # day would open the gate on a day the user never trained. + # + # Can be removed once the app versions that set the day are widely adopted. + if self.session_id and self.slot_entry and self.session.day_id is None: + days = list( + WorkoutLog.objects.filter( + session_id=self.session_id, + slot_entry__isnull=False, + ) + .values_list('slot_entry__slot__day_id', 'slot_entry__slot__day__routine_id') + .distinct() + ) + + if len(days) == 1 and days[0][1] == self.session.routine_id: + self.session.day_id = days[0][0] + self.session.save(update_fields=['day']) diff --git a/wger/manager/tests/test_log_model.py b/wger/manager/tests/test_log_model.py index b934a6fc4..6bf394b64 100644 --- a/wger/manager/tests/test_log_model.py +++ b/wger/manager/tests/test_log_model.py @@ -18,6 +18,8 @@ import logging # wger from wger.core.tests.base_testcase import WgerTestCase from wger.manager.models import ( + Slot, + SlotEntry, WorkoutLog, WorkoutSession, ) @@ -50,6 +52,196 @@ class LogModelTestCase(WgerTestCase): self.assertEqual(WorkoutSession.objects.count(), 1) + def test_create_session_sets_day(self): + """ + The auto-created session inherits the day of the log's slot entry, so + that need_logs_to_advance days can find it + """ + + WorkoutLog.objects.all().delete() + WorkoutSession.objects.all().delete() + + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=1, + weight=10, + repetitions=10, + ).save() + + session = WorkoutSession.objects.get() + self.assertEqual(session.day_id, 1) + + def test_create_session_without_slot_entry_has_no_day(self): + """ + A free log without a slot entry can't know its day + """ + + WorkoutLog.objects.all().delete() + WorkoutSession.objects.all().delete() + + WorkoutLog( + user_id=1, + exercise_id=1, + weight=10, + repetitions=10, + ).save() + + session = WorkoutSession.objects.get() + self.assertIsNone(session.day_id) + + def test_fills_missing_day_on_a_client_created_session(self): + """ + A session the client created without a day gets it from the log, so that + need_logs_to_advance days can find it + """ + + session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001') + self.assertIsNone(session.day_id) + + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=1, + session=session, + weight=10, + repetitions=10, + ).save() + + session.refresh_from_db() + self.assertEqual(session.day_id, 1) + + def test_does_not_overwrite_the_day_of_a_session(self): + """ + A session that already has a day keeps it, even when the log points + somewhere else + """ + + session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001') + session.day_id = 3 + session.save(update_fields=['day']) + + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=1, + session=session, + weight=10, + repetitions=10, + ).save() + + session.refresh_from_db() + self.assertEqual(session.day_id, 3) + + def test_does_not_fill_the_day_when_the_logs_span_several_days(self): + """ + A session whose logs come from more than one day stays without one, since + no single day is the right answer + """ + + session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001') + self.assertIsNone(session.day_id) + + # A second entry, on another day of the same routine + other_slot = Slot.objects.create(day_id=3, order=1) + other_entry = SlotEntry.objects.create(slot=other_slot, exercise_id=1, order=1) + + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=1, + session=session, + weight=10, + repetitions=10, + ).save() + + session.refresh_from_db() + self.assertEqual(session.day_id, 1) + + # The day of the first log is no longer the whole truth, but an already + # set day is never taken away again + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry=other_entry, + session=session, + weight=10, + repetitions=10, + ).save() + + session.refresh_from_db() + self.assertEqual(session.day_id, 1) + + def test_does_not_fill_the_day_when_earlier_logs_disagree(self): + """ + Logs that were already there decide as well, not just the one being saved + """ + + session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001') + other_slot = Slot.objects.create(day_id=3, order=1) + other_entry = SlotEntry.objects.create(slot=other_slot, exercise_id=1, order=1) + + # Two logs from different days, written without going through save() + for slot_entry_id in (1, other_entry.pk): + WorkoutLog.objects.bulk_create( + [ + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=slot_entry_id, + session=session, + weight=10, + repetitions=10, + ) + ] + ) + + session.refresh_from_db() + self.assertIsNone(session.day_id) + + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=1, + session=session, + weight=20, + repetitions=8, + ).save() + + session.refresh_from_db() + self.assertIsNone(session.day_id) + + def test_does_not_fill_the_day_from_another_routine(self): + """ + The day of a slot entry from a different routine must not leak into the + session + """ + + session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000003') + self.assertIsNone(session.day_id) + self.assertEqual(session.routine_id, 2) + + # slot entry 1 lives in routine 1, the session in routine 2 + WorkoutLog( + user_id=1, + exercise_id=1, + routine_id=1, + slot_entry_id=1, + session=session, + weight=10, + repetitions=10, + ).save() + + session.refresh_from_db() + self.assertIsNone(session.day_id) + def test_save_reuses_session_when_duplicates_exist(self): """ Duplicate routine-less sessions can exist for one (user, date) because the