From 36438b125cdca442acdd26bb40f2efa2f3774a7e Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 8 Mar 2023 22:37:32 +1100 Subject: [PATCH] Return number of workouts for users and average/max rpm for workouts --- app.py | 14 ++++++++++++-- templates/attemptv2.html | 1 - 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 7fc2383..042711b 100644 --- a/app.py +++ b/app.py @@ -26,6 +26,7 @@ class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) + workouts = db.relationship('Workout', backref='user', lazy=True) class Workout(db.Model): @@ -57,7 +58,8 @@ def users(): if request.method == 'GET': # get a list of all users in the database users = User.query.all() - users_list = [{'id': user.id, 'name': user.name} for user in users] + users_list = [{'id': user.id, 'name': user.name, + 'workouts': len(user.workouts)} for user in users] return jsonify(users_list), 200 elif request.method == 'POST': @@ -105,11 +107,19 @@ def create_workout(user_id): duration_str = str(duration) else: duration_str = str(duration).split('.')[0] + + # calculate average and maximum rpm + rpms = [reading.rpm for reading in cadence_readings] + avg_rpm = sum(rpms) / len(rpms) + max_rpm = max(rpms) + workouts_data.append({ 'id': workout.id, 'started_at': start_time.strftime('%a %b %d %Y %H:%M:%S'), 'finished_at': end_time.strftime('%a %b %d %Y %H:%M:%S'), - 'duration': duration_str + 'duration': duration_str, + 'avg_rpm': int(avg_rpm), + 'max_rpm': max_rpm }) return jsonify({'workouts': workouts_data}), 200 diff --git a/templates/attemptv2.html b/templates/attemptv2.html index 931d5da..55fb0b7 100644 --- a/templates/attemptv2.html +++ b/templates/attemptv2.html @@ -48,7 +48,6 @@ - No image