Add stats to new person overview page, just copied the logic so will need to refactor when the dashboard page is refactored as well

This commit is contained in:
Peter Stockings
2025-01-26 20:39:20 +11:00
parent dc543c7b75
commit 4783ce6026

View File

@@ -1,4 +1,5 @@
from datetime import date, timedelta
from utils import calculate_estimated_1rm, get_exercise_graph_model
@@ -137,6 +138,10 @@ class PersonOverview:
# Initialize the exercise sets dictionary
exercise_sets = {exercise["id"]: {"exercise_id": exercise["id"], "name": exercise["name"], "sets": []} for exercise in exercises}
workout_start_dates = []
set_count = 0
exercise_count = len(unique_exercise_ids)
for row in result:
workout_id = row["workout_id"]
@@ -149,6 +154,8 @@ class PersonOverview:
"exercises": {exercise["id"]: [] for exercise in exercises} # Keyed by exercise_id
}
workout_start_dates.append(row["start_date"])
# Add topset to the corresponding exercise
if row["exercise_id"] and row["topset_id"]:
# Add to workout exercises
@@ -165,6 +172,7 @@ class PersonOverview:
"workout_start_date": row["start_date"],
"exercise_name": row["exercise_name"]
})
set_count += 1
# Transform into a list of rows
for workout_id, workout in workout_map.items():
@@ -172,11 +180,39 @@ class PersonOverview:
exercise_progress_graphs = self.generate_exercise_progress_graphs(person_info["person_id"], exercise_sets)
workout_count = len(workout_start_dates)
stats = [{"Text": "Total Workouts", "Value": workout_count},
{"Text": "Total Sets", "Value": set_count},
{"Text": "Total Exercises", "Value": exercise_count}]
if workout_count > 0:
first_workout_date = min(workout_start_dates)
last_workout_date = max(workout_start_dates)
stats.append({"Text": "Days Since First Workout", "Value": (
date.today() - first_workout_date).days})
if workout_count >= 2:
stats.append({"Text": "Days Since Last Workout",
"Value": (
date.today() - last_workout_date).days})
average_number_sets_per_workout = round(
set_count / workout_count, 1)
stats.append({"Text": "Average sets per workout",
"Value": average_number_sets_per_workout})
training_duration = last_workout_date - first_workout_date
if training_duration > timedelta(days=0):
average_workouts_per_week = round(
workout_count / (training_duration.days / 7), 1)
stats.append({"Text": "Average Workouts Per Week",
"Value": average_workouts_per_week})
return {
**person_info,
"workouts": workouts,
"selected_exercises": exercises,
"exercise_progress_graphs": exercise_progress_graphs
"exercise_progress_graphs": exercise_progress_graphs,
"stats": stats
}
def generate_exercise_progress_graphs(self, person_id, exercise_sets):