From 70cbb9af2bfc66c95e380f47026776413c0da4a2 Mon Sep 17 00:00:00 2001
From: Peter Stockings
Date: Mon, 8 May 2023 20:15:40 +1000
Subject: [PATCH] If a workout was tracked with a hear rate monitor heart rate
graph will be selected by default, and average bpm will be rendered as well
---
app.py | 18 +++++++++++++++++-
templates/workouts_list.html | 5 +++--
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/app.py b/app.py
index fa86f4b..fca160b 100644
--- a/app.py
+++ b/app.py
@@ -299,6 +299,7 @@ def get_workouts_for_user(user_id):
for workout in workouts:
cadence_readings = CadenceReading.query.filter_by(
workout_id=workout.id).all()
+
if cadence_readings:
start_time = min(
reading.created_at for reading in cadence_readings)
@@ -309,6 +310,18 @@ def get_workouts_for_user(user_id):
reading.rpm for reading in cadence_readings) / len(cadence_readings)
calories = cadence_readings[-1].calories
distance = cadence_readings[-1].distance
+
+ selected_graph_types = ['speed']
+ is_heart_rate_available = False
+ heart_rate_readings = HeartRateReading.query.filter_by(
+ workout_id=workout.id).all()
+ average_bpm = 0
+ if heart_rate_readings:
+ selected_graph_types.append('heart_rate')
+ is_heart_rate_available = True
+ average_bpm = sum(heartrate.bpm for heartrate in heart_rate_readings) / \
+ len(heart_rate_readings)
+
workouts_data.append({
'id': workout.id,
'user_id': user_id,
@@ -319,7 +332,10 @@ def get_workouts_for_user(user_id):
'average_rpm': int(average_rpm),
'calories': int(calories),
'distance': int(distance),
- 'bike_display_name': workout.bike.display_name
+ 'bike_display_name': workout.bike.display_name,
+ 'selected_graph_types': selected_graph_types,
+ 'is_heart_rate_available': is_heart_rate_available,
+ 'average_bpm': int(average_bpm)
})
return workouts_data
diff --git a/templates/workouts_list.html b/templates/workouts_list.html
index 2098741..16ccc8a 100644
--- a/templates/workouts_list.html
+++ b/templates/workouts_list.html
@@ -80,7 +80,8 @@
Duration: {{ w.duration }} | Average RPM: {{ w.average_rpm
}} |
- Calories: {{ w.calories }} | Distance: {{ w.distance }}
+ Calories: {{ w.calories }} | Distance: {{ w.distance }} {% if w.is_heart_rate_available
+ %} | Average: BPM: {{w.average_bpm }} {% endif %}
@@ -93,7 +94,7 @@
- {% with workout=w, graph_types=['speed'] %}
+ {% with workout=w, graph_types=w['selected_graph_types'] %}
{% include 'workout_view.html' %}
{% endwith %}