Add more stats
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from collections import Counter
|
from collections import defaultdict
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
|
|
||||||
class Stats:
|
class Stats:
|
||||||
def __init__(self, db_connection_method):
|
def __init__(self, db_connection_method):
|
||||||
self.execute = db_connection_method
|
self.execute = db_connection_method
|
||||||
@@ -9,21 +10,52 @@ class Stats:
|
|||||||
if not topsets:
|
if not topsets:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Extracting necessary fields
|
# Extract necessary fields
|
||||||
workout_ids = [t['WorkoutId'] for t in topsets if t['WorkoutId']]
|
workout_ids = [t["WorkoutId"] for t in topsets if t["WorkoutId"]]
|
||||||
person_ids = [t['PersonId'] for t in topsets if t['PersonId']]
|
person_ids = [t["PersonId"] for t in topsets if t["PersonId"]]
|
||||||
start_dates = [t['StartDate'] for t in topsets if t['StartDate']]
|
start_dates = [t["StartDate"] for t in topsets if t["StartDate"]]
|
||||||
|
exercise_ids = [t["ExerciseId"] for t in topsets if t["ExerciseId"]]
|
||||||
|
|
||||||
workout_count = len(set(workout_ids))
|
workout_count = len(set(workout_ids))
|
||||||
people_count = len(set(person_ids))
|
people_count = len(set(person_ids))
|
||||||
total_sets = len(topsets)
|
total_sets = len(topsets)
|
||||||
|
exercise_count = len(set(exercise_ids))
|
||||||
|
|
||||||
|
# Group sets by workout and exercise
|
||||||
|
sets_per_exercise_per_workout = defaultdict(lambda: defaultdict(int))
|
||||||
|
for t in topsets:
|
||||||
|
if t["WorkoutId"] and t["ExerciseId"]:
|
||||||
|
sets_per_exercise_per_workout[t["WorkoutId"]][t["ExerciseId"]] += 1
|
||||||
|
|
||||||
|
# Calculate the average sets per exercise across all workouts
|
||||||
|
total_sets_per_exercise = []
|
||||||
|
for workout_exercises in sets_per_exercise_per_workout.values():
|
||||||
|
total_sets_per_exercise.extend(workout_exercises.values())
|
||||||
|
|
||||||
|
average_sets_per_exercise = round(sum(total_sets_per_exercise) / len(total_sets_per_exercise), 1) if total_sets_per_exercise else 0
|
||||||
|
|
||||||
|
# Group exercises by workout
|
||||||
|
exercises_by_workout = defaultdict(set)
|
||||||
|
for t in topsets:
|
||||||
|
if t["WorkoutId"] and t["ExerciseId"]:
|
||||||
|
exercises_by_workout[t["WorkoutId"]].add(t["ExerciseId"])
|
||||||
|
|
||||||
|
# Calculate average exercises per workout
|
||||||
|
average_exercises_per_workout = round(
|
||||||
|
sum(len(exercises) for exercises in exercises_by_workout.values()) / workout_count, 1
|
||||||
|
) if workout_count > 0 else 0
|
||||||
|
|
||||||
|
# Stats
|
||||||
stats = [
|
stats = [
|
||||||
{"Text": "Total Workouts", "Value": workout_count},
|
{"Text": "Total Workouts", "Value": workout_count},
|
||||||
{"Text": "Total Sets", "Value": total_sets}
|
{"Text": "Total Sets", "Value": total_sets},
|
||||||
|
{"Text": "Total Exercises", "Value": exercise_count},
|
||||||
|
{"Text": "Average Sets Per Exercise", "Value": average_sets_per_exercise},
|
||||||
|
{"Text": "Average Exercises Per Workout", "Value": average_exercises_per_workout},
|
||||||
]
|
]
|
||||||
|
|
||||||
if people_count > 1:
|
if people_count > 1:
|
||||||
stats.append({"Text": "People tracked", "Value": people_count})
|
stats.append({"Text": "People Tracked", "Value": people_count})
|
||||||
|
|
||||||
if workout_count > 0:
|
if workout_count > 0:
|
||||||
first_workout_date = min(start_dates)
|
first_workout_date = min(start_dates)
|
||||||
@@ -38,7 +70,7 @@ class Stats:
|
|||||||
"Value": (current_date - last_workout_date).days})
|
"Value": (current_date - last_workout_date).days})
|
||||||
|
|
||||||
average_sets_per_workout = round(total_sets / workout_count, 1)
|
average_sets_per_workout = round(total_sets / workout_count, 1)
|
||||||
stats.append({"Text": "Average sets per workout",
|
stats.append({"Text": "Average Sets Per Workout",
|
||||||
"Value": average_sets_per_workout})
|
"Value": average_sets_per_workout})
|
||||||
|
|
||||||
training_duration = last_workout_date - first_workout_date
|
training_duration = last_workout_date - first_workout_date
|
||||||
@@ -50,17 +82,18 @@ class Stats:
|
|||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
def fetch_stats_for_person(self, person_id):
|
def fetch_stats_for_person(self, person_id):
|
||||||
query = """
|
query = """
|
||||||
SELECT
|
SELECT
|
||||||
t.workout_id AS "WorkoutId",
|
t.workout_id AS "WorkoutId",
|
||||||
w.person_id AS "PersonId",
|
w.person_id AS "PersonId",
|
||||||
w.start_date AS "StartDate"
|
w.start_date AS "StartDate",
|
||||||
|
e.exercise_id AS "ExerciseId"
|
||||||
FROM
|
FROM
|
||||||
topset t
|
topset t
|
||||||
JOIN workout w ON t.workout_id = w.workout_id
|
JOIN workout w ON t.workout_id = w.workout_id
|
||||||
JOIN person p ON w.person_id = p.person_id
|
JOIN person p ON w.person_id = p.person_id
|
||||||
|
JOIN exercise e ON t.exercise_id = e.exercise_id
|
||||||
WHERE p.person_id = %s
|
WHERE p.person_id = %s
|
||||||
"""
|
"""
|
||||||
workouts_data = self.execute(query, [person_id])
|
workouts_data = self.execute(query, [person_id])
|
||||||
@@ -73,11 +106,13 @@ class Stats:
|
|||||||
SELECT
|
SELECT
|
||||||
t.workout_id AS "WorkoutId",
|
t.workout_id AS "WorkoutId",
|
||||||
w.person_id AS "PersonId",
|
w.person_id AS "PersonId",
|
||||||
w.start_date AS "StartDate"
|
w.start_date AS "StartDate",
|
||||||
|
e.exercise_id AS "ExerciseId"
|
||||||
FROM
|
FROM
|
||||||
topset t
|
topset t
|
||||||
JOIN workout w ON t.workout_id = w.workout_id
|
JOIN workout w ON t.workout_id = w.workout_id
|
||||||
JOIN person p ON w.person_id = p.person_id;
|
JOIN person p ON w.person_id = p.person_id
|
||||||
|
JOIN exercise e ON t.exercise_id = e.exercise_id
|
||||||
"""
|
"""
|
||||||
workouts_data = self.execute(query, [])
|
workouts_data = self.execute(query, [])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user