Refactor stats endpoint so supports filtering people and make dashboard page utilise it
This commit is contained in:
7
app.py
7
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/<int:person_id>", 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/<int:person_id>/workout/<int:workout_id>", methods=['GET'])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -158,7 +158,7 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats_for_person', person_id=person_id) }}" hx-trigger="load"
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats') }}" hx-vals='{"person_id": "{{ person_id }}"}' hx-trigger="load"
|
||||
hx-target="#stats" hx-swap="innerHTML">
|
||||
</div>
|
||||
|
||||
|
||||
@@ -194,6 +194,10 @@
|
||||
|
||||
</div>
|
||||
|
||||
{{ render_partial('partials/stats.html', stats=model['Stats']) }}
|
||||
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats') }}"
|
||||
hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='person_id']" hx-trigger="load"
|
||||
hx-target="#stats" hx-swap="innerHTML">
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@@ -81,7 +81,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats_for_person', person_id=person_id) }}" hx-trigger="load"
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats') }}" hx-vals='{"person_id": "{{ person_id }}"}' hx-trigger="load"
|
||||
hx-target="#stats" hx-swap="innerHTML">
|
||||
</div>
|
||||
|
||||
|
||||
@@ -159,9 +159,9 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats_for_person', person_id=person_id) }}"
|
||||
hx-include="[name='exercise_id'],[name='min_date'],[name='max_date']" hx-trigger="load" hx-target="#stats"
|
||||
hx-swap="innerHTML">
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats') }}"
|
||||
hx-include="[name='exercise_id'],[name='min_date'],[name='max_date']" hx-vals='{"person_id": "{{ person_id }}"}'
|
||||
hx-trigger="load" hx-target="#stats" hx-swap="innerHTML">
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -142,7 +142,7 @@
|
||||
|
||||
</div>
|
||||
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats_for_person', person_id=person_id) }}" hx-trigger="load"
|
||||
<div class="hidden" hx-get="{{ url_for('get_stats') }}" hx-vals='{"person_id": "{{ person_id }}"}' hx-trigger="load"
|
||||
hx-target="#stats" hx-swap="innerHTML">
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user