Add form to create user overview graphs

This commit is contained in:
Peter Stockings
2023-10-21 19:20:49 +11:00
parent 3e110a7d4f
commit 07ad592895
3 changed files with 105 additions and 13 deletions

23
app.py
View File

@@ -216,6 +216,11 @@ def create_workout(user_id):
@app.route('/user/<int:user_id>/workouts/graph', methods=['GET'])
def graph_user_workouts(user_id):
if htmx:
return f"""
<img src="{request.full_path}"
loading="lazy" alt="No image" class="mx-auto">
"""
user = User.query.get(user_id)
workouts = user.workouts
@@ -464,7 +469,11 @@ def generate_user_data(user, workouts=[]):
'workouts_count': len(workouts),
'workouts': workouts,
'daily_duration_sparkline': generate_daily_duration_sparkline(workouts),
'calendar_month': generate_calendar_monthly_view(workouts, datetime.now().date())
'calendar_month': generate_calendar_monthly_view(workouts, datetime.now().date()),
'attributes': [('workout_count', 'Workout count'), ('duration', 'Duration'), ('average_rpm', 'Average RPM'), ('average_bpm', 'Average BPM'), ('distance', 'Distance'), ('calories', 'Calories')],
'periods': ['day', 'week', 'month'],
'first_workout_date': workouts[-1]['start_time_date'] if workouts else None,
'last_workout_date': workouts[0]['start_time_date'] if workouts else None,
}
@@ -513,7 +522,7 @@ def format_workout_data(workout, user):
'user_id': user.id,
'user_name': user.name,
'start_time': format_date_with_ordinal(workout.started_at, '%#H:%M %B %dth %Y'),
'start_time_date': workout.started_at,
'start_time_date': workout.started_at.date(),
'start_time_ago': humanize.naturaltime(workout.started_at),
'duration': humanize.naturaldelta(duration),
'duration_minutes': duration.total_seconds() / 60,
@@ -613,8 +622,8 @@ def generate_calendar_monthly_view(workouts, selected_date):
start_date, end_date = get_month_bounds(selected_date)
# Build a lookup dictionary for faster access
workout_lookup = {w['start_time_date'].date(
): w for w in workouts if start_date <= w['start_time_date'].date() <= end_date}
workout_lookup = {w['start_time_date']
: w for w in workouts if start_date <= w['start_time_date'] <= end_date}
current_date = datetime.now().date()
days_of_month = [
@@ -655,11 +664,11 @@ def generate_daily_duration_sparkline(workouts):
return ''
# Determine date range based on workouts data
start_date = workouts[-1]['start_time_date'].date()
end_date = workouts[0]['start_time_date'].date()
start_date = workouts[-1]['start_time_date']
end_date = workouts[0]['start_time_date']
# Build a mapping of dates to their respective durations for easier lookup
workouts_by_date = {w['start_time_date'].date(): int(
workouts_by_date = {w['start_time_date']: int(
w['duration_minutes']) for w in workouts}
daily_durations = [workouts_by_date.get(