Live plot cadence and heart rate when both devices are connected and display heart rate graph in workout view
This commit is contained in:
12
app.py
12
app.py
@@ -125,7 +125,7 @@ def workouts(user_id):
|
||||
app.logger.info(f'Creating workout for user {user.name} ({user.id})')
|
||||
|
||||
data = request.json
|
||||
workout = data['workout'] or []
|
||||
workout_data = data['workout'] or []
|
||||
heart_rate = data['heart_rate'] or []
|
||||
|
||||
# create a new workout
|
||||
@@ -134,10 +134,10 @@ def workouts(user_id):
|
||||
db.session.commit()
|
||||
|
||||
app.logger.info(
|
||||
f'Workout({workout.id}) created for user {user.name} ({user.id}) with {len(workout)} cadence readings and {len(heart_rate)} heart rate readings')
|
||||
f'Workout({workout.id}) created for user {user.name} ({user.id}) with {len(workout_data)} cadence readings and {len(heart_rate)} heart rate readings')
|
||||
|
||||
# add cadence readings to the workout
|
||||
for w in workout:
|
||||
for w in workout_data:
|
||||
cadence_reading = CadenceReading(
|
||||
workout_id=workout.id, created_at=w['timestamp'], rpm=w['rpm'], distance=w['distance'], speed=w['speed'], calories=w['calories'], power=w['power'])
|
||||
db.session.add(cadence_reading)
|
||||
@@ -176,6 +176,12 @@ def workout(user_id, workout_id, graph_type):
|
||||
elif graph_type == 'power':
|
||||
y_values = [reading.power for reading in cadence_readings]
|
||||
return create_graph(x_values=x_values, y_values=y_values, y_label='Power (WATTS)', filename='power'), 200
|
||||
heart_rate_readings = HeartRateReading.query.filter_by(
|
||||
workout_id=workout_id).all()
|
||||
if heart_rate_readings:
|
||||
x_values = [reading.created_at for reading in heart_rate_readings]
|
||||
y_values = [reading.bpm for reading in heart_rate_readings]
|
||||
return create_graph(x_values=x_values, y_values=y_values, y_label='Heart Rate (BPM)', filename='heart_rate'), 200
|
||||
else:
|
||||
return jsonify({'message': 'No cadence readings for workout {}.'.format(workout_id)}), 404
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user