Refactor stats into generic function
This commit is contained in:
66
utils.py
66
utils.py
@@ -85,36 +85,10 @@ def get_people_and_exercise_rep_maxes(topsets):
|
||||
'NumberOfWorkouts': len(list(set([t['WorkoutId'] for t in workouts_for_person if t['WorkoutId'] is not None]))),
|
||||
'Exercises': get_rep_maxes_for_person(workouts_for_person)
|
||||
})
|
||||
return people
|
||||
return {"People": people, "Stats": get_stats_from_topsets(topsets)}
|
||||
|
||||
|
||||
def get_person_stats(topsets):
|
||||
workout_start_dates = [datetime.strptime(
|
||||
workout['StartDate'], '%Y-%m-%d') for workout in topsets if workout['StartDate'] is not None]
|
||||
if not workout_start_dates:
|
||||
return {
|
||||
'FirstWorkout': None,
|
||||
'LastWorkout': None,
|
||||
'NumberOfWorkouts': 0,
|
||||
'TrainingDurationInDays': 0,
|
||||
'AverageWorkoutsPerWeek': 0,
|
||||
'DaysSinceLastWorkout': None,
|
||||
}
|
||||
first_workout_date = min(workout_start_dates)
|
||||
last_workout_date = max(workout_start_dates)
|
||||
training_duration = last_workout_date - first_workout_date
|
||||
no_of_workouts = len(list(set([t['WorkoutId'] for t in topsets])))
|
||||
return {
|
||||
'FirstWorkout': first_workout_date.strftime("%b %d %Y"),
|
||||
'LastWorkout': last_workout_date.strftime("%b %d %Y"),
|
||||
'NumberOfWorkouts': no_of_workouts,
|
||||
'TrainingDurationInDays': training_duration.days,
|
||||
'AverageWorkoutsPerWeek': round(no_of_workouts / (training_duration.days / 7), 2),
|
||||
'DaysSinceLastWorkout': (datetime.now() - last_workout_date).days,
|
||||
}
|
||||
|
||||
|
||||
def get_dashboard_stats(topsets):
|
||||
def get_stats_from_topsets(topsets):
|
||||
workout_count = len(set([t['WorkoutId']
|
||||
for t in topsets if t['WorkoutId'] is not None]))
|
||||
people_count = len(set([t['PersonId']
|
||||
@@ -122,25 +96,21 @@ def get_dashboard_stats(topsets):
|
||||
workout_start_dates = [datetime.strptime(
|
||||
t['StartDate'], '%Y-%m-%d') for t in topsets if t['StartDate'] is not None]
|
||||
|
||||
if workout_count == 0:
|
||||
return {
|
||||
'TotalWorkouts': workout_count,
|
||||
'NumberOfPeople': people_count,
|
||||
'DaysSinceLastWorkout': None,
|
||||
'AverageWorkoutsPerWeek': 0,
|
||||
}
|
||||
stats = [{"Text": "Total Workouts", "Value": workout_count}]
|
||||
if people_count > 1:
|
||||
stats.append({"Text": "Number of People", "Value": people_count})
|
||||
if workout_count > 0:
|
||||
first_workout_date = min(workout_start_dates)
|
||||
last_workout_date = max(workout_start_dates)
|
||||
training_duration = last_workout_date - first_workout_date
|
||||
average_workouts_per_week = round(
|
||||
workout_count / (training_duration.days / 7), 2)
|
||||
stats.append({"Text": "Days Since First Workout", "Value": (
|
||||
datetime.now() - first_workout_date).days})
|
||||
stats.append({"Text": "Days Since Last Workout",
|
||||
"Value": (
|
||||
datetime.now() - last_workout_date).days})
|
||||
stats.append({"Text": "Average Workouts Per Week",
|
||||
"Value": average_workouts_per_week})
|
||||
|
||||
first_workout_date = min(workout_start_dates)
|
||||
last_workout_date = max(workout_start_dates)
|
||||
training_duration = last_workout_date - first_workout_date
|
||||
average_workouts_per_week = round(
|
||||
workout_count / (training_duration.days / 7), 2)
|
||||
|
||||
stats = {
|
||||
'TotalWorkouts': workout_count,
|
||||
'NumberOfPeople': people_count,
|
||||
'DaysSinceFirstWorkout': (datetime.now() - first_workout_date).days,
|
||||
'DaysSinceLastWorkout': training_duration.days,
|
||||
'AverageWorkoutsPerWeek': average_workouts_per_week,
|
||||
}
|
||||
return stats
|
||||
|
||||
Reference in New Issue
Block a user