Files
WerkoutAPI/generator/management/commands/recalculate_workout_times.py
Trey t 1c61b80731 workout generator audit: rules engine, structure rules, split patterns, injury UX, metadata cleanup
- Add rules_engine.py with quantitative rules for all 8 workout types
- Add quality gate retry loop in generate_single_workout()
- Expand calibrate_structure_rules to all 120 combinations (8 types × 5 goals × 3 sections)
- Wire WeeklySplitPattern DB records into _pick_weekly_split()
- Enforce movement patterns from WorkoutStructureRule in exercise selection
- Add straight-set strength support (single main lift, 4-6 rounds)
- Add modality consistency check for duration-dominant workout types
- Add InjuryStep component to onboarding and preferences
- Add sibling exercise exclusion in regenerate and preview_day endpoints
- Display generator warnings on dashboard
- Expand fix_rep_durations, fix_exercise_flags, fix_movement_pattern_typo
- Add audit_exercise_data and check_rules_drift management commands
- Add Next.js frontend with dashboard, onboarding, preferences, history pages
- Add generator app with ML-powered workout generation pipeline
- 96 new tests across 7 test modules

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 20:07:40 -06:00

106 lines
3.8 KiB
Python

"""
Recalculates estimated_time on all Workout and Superset records using
the corrected estimated_rep_duration values + rest between rounds.
Formula per superset:
active_time = sum(reps * exercise.estimated_rep_duration) + sum(durations)
rest_time = rest_between_rounds * (rounds - 1)
superset.estimated_time = active_time (stores single-round active time)
Formula per workout:
workout.estimated_time = sum(superset_active_time * rounds + rest_time)
Usage:
python manage.py recalculate_workout_times
python manage.py recalculate_workout_times --dry-run
python manage.py recalculate_workout_times --rest=45
"""
from django.core.management.base import BaseCommand
from workout.models import Workout
from superset.models import Superset, SupersetExercise
DEFAULT_REST_BETWEEN_ROUNDS = 45 # seconds
DEFAULT_REP_DURATION = 3.0 # fallback if null
class Command(BaseCommand):
help = 'Recalculate estimated_time on all Workouts and Supersets'
def add_arguments(self, parser):
parser.add_argument(
'--dry-run',
action='store_true',
help='Show changes without writing to DB',
)
parser.add_argument(
'--rest',
type=int,
default=DEFAULT_REST_BETWEEN_ROUNDS,
help=f'Rest between rounds in seconds (default: {DEFAULT_REST_BETWEEN_ROUNDS})',
)
def handle(self, *args, **options):
dry_run = options['dry_run']
rest_between_rounds = options['rest']
workouts = Workout.objects.all()
total = workouts.count()
updated = 0
for workout in workouts:
supersets = Superset.objects.filter(workout=workout).order_by('order')
workout_total_time = 0
for ss in supersets:
exercises = SupersetExercise.objects.filter(superset=ss)
active_time = 0.0
for se in exercises:
if se.reps and se.reps > 0:
rep_dur = se.exercise.estimated_rep_duration or DEFAULT_REP_DURATION
active_time += se.reps * rep_dur
elif se.duration and se.duration > 0:
active_time += se.duration
# Rest between rounds (not after the last round)
rest_time = rest_between_rounds * max(0, ss.rounds - 1)
# Superset stores single-round active time
old_ss_time = ss.estimated_time
ss.estimated_time = active_time
if not dry_run:
ss.save(update_fields=['estimated_time'])
# Workout accumulates: active per round * rounds + rest
workout_total_time += (active_time * ss.rounds) + rest_time
old_time = workout.estimated_time
new_time = workout_total_time
if not dry_run:
workout.estimated_time = new_time
workout.save(update_fields=['estimated_time'])
updated += 1
self.stdout.write(self.style.SUCCESS(
f'{"[DRY RUN] " if dry_run else ""}'
f'Recalculated {updated}/{total} workouts '
f'(rest between rounds: {rest_between_rounds}s)'
))
# Show some examples
if not dry_run:
self.stdout.write('\nSample workouts:')
for w in Workout.objects.order_by('-id')[:5]:
mins = w.estimated_time / 60 if w.estimated_time else 0
ss_count = Superset.objects.filter(workout=w).count()
ex_count = SupersetExercise.objects.filter(superset__workout=w).count()
self.stdout.write(
f' #{w.id} "{w.name}": {mins:.0f}m '
f'({ss_count} supersets, {ex_count} exercises)'
)