From 6dafdf71dd92030967c126acc0ae6f4930ab918f Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 3 Apr 2024 20:31:13 +1100 Subject: [PATCH] Add options to filter epoch for exercise progress graphs (1M, 3M, 6M, All), however if there isnt data in a selected epoch the endpoint returns 404. Havent bothered to look into to it, probs should clean up code as well --- app.py | 4 +++- db.py | 12 ++++++++++-- templates/partials/sparkline.html | 15 ++++++++++++++- utils.py | 13 ++++++++++--- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 95ebc1c..bdc14f8 100644 --- a/app.py +++ b/app.py @@ -413,7 +413,9 @@ def get_exercise_progress_for_user(person_id, exercise_id): 'min_date'), '%Y-%m-%d') max_date = convert_str_to_date(request.args.get( 'max_date'), '%Y-%m-%d') - exercise_progress = db.get_exercise_progress_for_user(person_id, exercise_id, min_date, max_date) + epoch = request.args.get('epoch', default='All') + + exercise_progress = db.get_exercise_progress_for_user(person_id, exercise_id, min_date, max_date, epoch) if not exercise_progress: abort(404) diff --git a/db.py b/db.py index b7c2f07..cde3ad3 100644 --- a/db.py +++ b/db.py @@ -3,6 +3,7 @@ import psycopg2 import numpy as np from psycopg2.extras import RealDictCursor from datetime import datetime +from dateutil.relativedelta import relativedelta from urllib.parse import urlparse from flask import g @@ -450,7 +451,14 @@ class DataBase(): 'SELECT exercise_id, name FROM exercise') return exercises - def get_exercise_progress_for_user(self, person_id, exercise_id, min_date=None, max_date=None): + def get_exercise_progress_for_user(self, person_id, exercise_id, min_date=None, max_date=None, epoch='all'): + today = datetime.now() + if epoch == '1M': + min_date = today - relativedelta(months=1) + elif epoch == '3M': + min_date = today - relativedelta(months=3) + elif epoch == '6M': + min_date = today - relativedelta(months=6) # Execute SQL query to fetch topset data for a specific person and exercise topsets = self.execute(""" SELECT @@ -484,7 +492,7 @@ class DataBase(): start_dates = [t['start_date'] for t in topsets] messages = [f'{t["repetitions"]} x {t["weight"]}kg ({t["estimated_1rm"]}kg E1RM) on {t["start_date"].strftime("%d %b %y")}' for t in topsets] - exercise_progress = get_exercise_graph_model(topsets[0]['exercise_name'], estimated_1rm, repetitions, weight, start_dates, messages) + exercise_progress = get_exercise_graph_model(topsets[0]['exercise_name'], estimated_1rm, repetitions, weight, start_dates, messages, epoch, person_id, exercise_id) return exercise_progress diff --git a/templates/partials/sparkline.html b/templates/partials/sparkline.html index f9ac889..5353e89 100644 --- a/templates/partials/sparkline.html +++ b/templates/partials/sparkline.html @@ -47,11 +47,24 @@

{{ title }}

-

+

{% if best_fit_formula %} y = {{ best_fit_formula.slope }}x {% if best_fit_formula.intercept != 0 %}+ {{ best_fit_formula.intercept }}{% endif %}, {{ best_fit_formula.kg_per_week }} kg/week, {{ best_fit_formula.kg_per_month }} kg/month {% endif %}

+
+ {% for epoch in epochs %} +
+ {{ epoch}} +
+ {% endfor %} +
{% for plot in plots %} diff --git a/utils.py b/utils.py index 6450d96..bbe2eeb 100644 --- a/utils.py +++ b/utils.py @@ -52,8 +52,11 @@ def get_topsets_for_person(person_topsets): weight = [t['Weight'] for t in exercise_topsets] start_dates = [t['StartDate'] for t in exercise_topsets] messages = [f'{t["Repetitions"]} x {t["Weight"]}kg ({t["Estimated1RM"]}kg E1RM) on {t["StartDate"].strftime("%d %b %y")}' for t in exercise_topsets] + epoch = 'All' + person_id = exercise_topsets[0]['PersonId'] + exercise_id = exercise_topsets[0]['ExerciseId'] - exercise_progress = get_exercise_graph_model(exercise_topsets[0]['ExerciseName'], estimated_1rm, repetitions, weight, start_dates, messages) + exercise_progress = get_exercise_graph_model(exercise_topsets[0]['ExerciseName'], estimated_1rm, repetitions, weight, start_dates, messages, epoch, person_id, exercise_id) exercises_topsets.append({ 'ExerciseId': e['ExerciseId'], @@ -236,7 +239,7 @@ def get_date_info(input_date, selected_view): 'end_date': last_day_of_year, } -def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_dates, messages): +def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_dates, messages, epoch, person_id, exercise_id): min_date, max_date = min(start_dates), max(start_dates) min_e1rm, max_e1rm = min(estimated_1rm), max(estimated_1rm) min_reps, max_reps = min(repetitions), max(repetitions) @@ -318,7 +321,11 @@ def get_exercise_graph_model(title, estimated_1rm, repetitions, weight, start_da 'plots': [repetitions, weight, estimated_1rm], 'best_fit_points': best_fit_points, 'best_fit_formula': best_fit_formula, - 'plot_labels': plot_labels + 'plot_labels': plot_labels, + 'epochs': ['1M', '3M', '6M', 'All'], + 'selected_epoch': epoch, + 'person_id': person_id, + 'exercise_id': exercise_id } def get_workout_counts(workouts, period='week'):