Add brotli complression, cache graph requests for 5mins and add pagination for person overview
This commit is contained in:
@@ -77,11 +77,33 @@ class PersonOverview:
|
||||
return exercises
|
||||
|
||||
|
||||
def get(self, person_id, start_date, end_date, selected_exercise_ids):
|
||||
def get(self, person_id, start_date, end_date, selected_exercise_ids, limit=20, offset=0):
|
||||
# Build placeholders for exercise IDs
|
||||
placeholders = ", ".join(["%s"] * len(selected_exercise_ids))
|
||||
exercise_placeholders = ", ".join(["%s"] * len(selected_exercise_ids))
|
||||
|
||||
# Dynamically inject placeholders into the query
|
||||
# 1. Fetch workout IDs first for pagination
|
||||
# We need to filter by person, date, and selected exercises
|
||||
workout_ids_query = f"""
|
||||
SELECT DISTINCT w.workout_id, w.start_date
|
||||
FROM workout w
|
||||
JOIN topset t ON w.workout_id = t.workout_id
|
||||
WHERE w.person_id = %s
|
||||
AND w.start_date BETWEEN %s AND %s
|
||||
AND t.exercise_id IN ({exercise_placeholders})
|
||||
ORDER BY w.start_date DESC
|
||||
LIMIT %s OFFSET %s
|
||||
"""
|
||||
params = [person_id, start_date, end_date] + selected_exercise_ids + [limit + 1, offset]
|
||||
workout_id_results = self.execute(workout_ids_query, params)
|
||||
|
||||
if not workout_id_results:
|
||||
return {"person_id": person_id, "person_name": None, "workouts": [], "selected_exercises": [], "exercise_progress_graphs": [], "has_more": False}
|
||||
|
||||
has_more = len(workout_id_results) > limit
|
||||
target_workout_ids = [r["workout_id"] for r in workout_id_results[:limit]]
|
||||
workout_id_placeholders = ", ".join(["%s"] * len(target_workout_ids))
|
||||
|
||||
# 2. Fetch all details for these specific workouts
|
||||
sql_query = f"""
|
||||
SELECT
|
||||
p.person_id,
|
||||
@@ -103,19 +125,18 @@ class PersonOverview:
|
||||
JOIN
|
||||
exercise e ON t.exercise_id = e.exercise_id
|
||||
WHERE
|
||||
p.person_id = %s
|
||||
AND w.start_date BETWEEN %s AND %s
|
||||
AND e.exercise_id IN ({placeholders})
|
||||
w.workout_id IN ({workout_id_placeholders})
|
||||
AND e.exercise_id IN ({exercise_placeholders})
|
||||
ORDER BY
|
||||
w.start_date DESC, e.exercise_id ASC, t.topset_id ASC;
|
||||
"""
|
||||
|
||||
# Add parameters for the query
|
||||
params = [person_id, start_date, end_date] + selected_exercise_ids
|
||||
# Parameters for the detailed query
|
||||
params = target_workout_ids + selected_exercise_ids
|
||||
result = self.execute(sql_query, params)
|
||||
|
||||
if not result:
|
||||
return {"person_id": person_id, "person_name": None, "workouts": [], "selected_exercises": [], "exercise_progress_graphs": []}
|
||||
return {"person_id": person_id, "person_name": None, "workouts": [], "selected_exercises": [], "exercise_progress_graphs": [], "has_more": False}
|
||||
|
||||
# Extract person info from the first row
|
||||
person_info = {"person_id": result[0]["person_id"], "person_name": result[0]["person_name"]}
|
||||
@@ -132,7 +153,6 @@ class PersonOverview:
|
||||
exercises = sorted(exercises, key=lambda ex: ex["name"])
|
||||
|
||||
# Initialize the table structure
|
||||
workouts = []
|
||||
workout_map = {} # Map to track workouts
|
||||
|
||||
# Initialize the exercise sets dictionary
|
||||
@@ -153,10 +173,11 @@ class PersonOverview:
|
||||
# Add topset to the corresponding exercise
|
||||
if row["exercise_id"] and row["topset_id"]:
|
||||
# Add to workout exercises
|
||||
workout_map[workout_id]["exercises"][row["exercise_id"]].append({
|
||||
"repetitions": row["repetitions"],
|
||||
"weight": row["weight"]
|
||||
})
|
||||
if row["exercise_id"] in workout_map[workout_id]["exercises"]:
|
||||
workout_map[workout_id]["exercises"][row["exercise_id"]].append({
|
||||
"repetitions": row["repetitions"],
|
||||
"weight": row["weight"]
|
||||
})
|
||||
|
||||
# Add to the exercise sets dictionary with workout start date
|
||||
exercise_sets[row["exercise_id"]]["sets"].append({
|
||||
@@ -167,9 +188,8 @@ class PersonOverview:
|
||||
"exercise_name": row["exercise_name"]
|
||||
})
|
||||
|
||||
# Transform into a list of rows
|
||||
for workout_id, workout in workout_map.items():
|
||||
workouts.append(workout)
|
||||
# Transform into a list of rows, maintaining DESC order
|
||||
workouts = [workout_map[wid] for wid in target_workout_ids if wid in workout_map]
|
||||
|
||||
exercise_progress_graphs = self.generate_exercise_progress_graphs(person_info["person_id"], exercise_sets)
|
||||
|
||||
@@ -177,7 +197,8 @@ class PersonOverview:
|
||||
**person_info,
|
||||
"workouts": workouts,
|
||||
"selected_exercises": exercises,
|
||||
"exercise_progress_graphs": exercise_progress_graphs
|
||||
"exercise_progress_graphs": exercise_progress_graphs,
|
||||
"has_more": has_more
|
||||
}
|
||||
|
||||
def generate_exercise_progress_graphs(self, person_id, exercise_sets):
|
||||
|
||||
Reference in New Issue
Block a user