diff --git a/app.py b/app.py index 658c26b..1dccc0a 100644 --- a/app.py +++ b/app.py @@ -448,12 +448,13 @@ def get_exercise_progress_for_user(person_id, exercise_id): return render_template('partials/sparkline.html', **exercise_progress) -@app.route("/stats/person/", methods=['GET']) -def get_stats_for_person(person_id): +@app.route("/stats", methods=['GET']) +def get_stats(): + selected_people_ids = request.args.getlist('person_id', type=int) min_date = request.args.get('min_date', type=convert_str_to_date) max_date = request.args.get('max_date', type=convert_str_to_date) selected_exercise_ids = request.args.getlist('exercise_id', type=int) - stats = db.stats.fetch_stats_for_person(person_id, min_date, max_date, selected_exercise_ids) + stats = db.stats.fetch_stats(selected_people_ids, min_date, max_date, selected_exercise_ids) return render_template('partials/stats.html', stats=stats, refresh_url=request.full_path) @ app.route("/person//workout/", methods=['GET']) diff --git a/features/stats.py b/features/stats.py index b1871ae..5e2cc6a 100644 --- a/features/stats.py +++ b/features/stats.py @@ -84,7 +84,7 @@ class Stats: return stats - def fetch_stats_for_person(self, person_id, min_date=None, max_date=None, selected_exercise_ids=None): + def fetch_stats(self, selected_people_ids=None, min_date=None, max_date=None, selected_exercise_ids=None): # Base query query = """ SELECT @@ -97,45 +97,35 @@ class Stats: JOIN workout w ON t.workout_id = w.workout_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 """ # Parameters for the query - params = [person_id] + params = [] # Add optional filters + conditions = [] # Collect conditions dynamically + if selected_people_ids: + placeholders = ", ".join(["%s"] * len(selected_people_ids)) + conditions.append(f"p.person_id IN ({placeholders})") + params.extend(selected_people_ids) if min_date: - query += " AND w.start_date >= %s" + conditions.append("w.start_date >= %s") params.append(min_date) if max_date: - query += " AND w.start_date <= %s" + conditions.append("w.start_date <= %s") params.append(max_date) if selected_exercise_ids: placeholders = ", ".join(["%s"] * len(selected_exercise_ids)) - query += f" AND e.exercise_id IN ({placeholders})" + conditions.append(f"e.exercise_id IN ({placeholders})") params.extend(selected_exercise_ids) + # Add conditions to the query + if conditions: + query += " WHERE " + " AND ".join(conditions) + # Execute the query workouts_data = self.execute(query, params) # Generate stats from the retrieved data - person_stats = self.get_stats_from_topsets(workouts_data) - return person_stats - - def fetch_all_stats(self): - query = """ - SELECT - t.workout_id AS "WorkoutId", - w.person_id AS "PersonId", - w.start_date AS "StartDate", - e.exercise_id AS "ExerciseId" - FROM - topset t - JOIN workout w ON t.workout_id = w.workout_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, []) - - person_stats = self.get_stats_from_topsets(workouts_data) - return person_stats + stats = self.get_stats_from_topsets(workouts_data) + return stats diff --git a/templates/calendar.html b/templates/calendar.html index 89f605a..b5e0c14 100644 --- a/templates/calendar.html +++ b/templates/calendar.html @@ -158,7 +158,7 @@ {% endif %} - diff --git a/templates/dashboard.html b/templates/dashboard.html index 3338850..858f569 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -194,6 +194,10 @@ -{{ render_partial('partials/stats.html', stats=model['Stats']) }} + + {% endblock %} \ No newline at end of file diff --git a/templates/notes.html b/templates/notes.html index a932c3a..6664bdd 100644 --- a/templates/notes.html +++ b/templates/notes.html @@ -81,7 +81,7 @@ - diff --git a/templates/person_overview.html b/templates/person_overview.html index d2a1146..6cdb23b 100644 --- a/templates/person_overview.html +++ b/templates/person_overview.html @@ -159,9 +159,9 @@ - -