Refactor render user and workouts function
This commit is contained in:
103
app.py
103
app.py
@@ -313,66 +313,11 @@ def render_users_and_workouts():
|
|||||||
'bike_id': user.bike_id,
|
'bike_id': user.bike_id,
|
||||||
'workouts_count': 0,
|
'workouts_count': 0,
|
||||||
'workouts': [],
|
'workouts': [],
|
||||||
'workout_counts_by_week': 0,
|
'daily_duration_sparkline': '',
|
||||||
'duration_by_week': 0
|
'calendar_month': generate_calendar_monthly_view([], datetime.now().date())
|
||||||
}
|
}
|
||||||
users_data.append(user_data)
|
users_data.append(user_data)
|
||||||
continue
|
continue
|
||||||
workout_counts_by_week = []
|
|
||||||
duration_by_week = []
|
|
||||||
# get start date from last workout
|
|
||||||
start_date = workouts[-1]['start_time_date'].date()
|
|
||||||
# get end date from first workout
|
|
||||||
end_date = workouts[0]['start_time_date'].date()
|
|
||||||
# get difference in days between start and end date
|
|
||||||
num_days = (end_date - start_date).days + 1
|
|
||||||
# calculate number of weeks between start and end date
|
|
||||||
num_weeks = (end_date - start_date).days // 7 + 1
|
|
||||||
|
|
||||||
for i in range(num_weeks):
|
|
||||||
# Calculate the start and end of the current week
|
|
||||||
start = start_date + timedelta(days=7*i)
|
|
||||||
end = start + timedelta(days=6)
|
|
||||||
|
|
||||||
# Get the workouts for the current week
|
|
||||||
weekly_workouts = [w for w in workouts if start <=
|
|
||||||
w['start_time_date'].date() <= end]
|
|
||||||
|
|
||||||
# Calculate the workout counts and duration for the current week
|
|
||||||
workout_counts_by_week.append(len(weekly_workouts))
|
|
||||||
if weekly_workouts:
|
|
||||||
duration_by_week.append(
|
|
||||||
int(sum([int(w['duration_minutes']) for w in weekly_workouts]))/len(weekly_workouts))
|
|
||||||
|
|
||||||
workout_counts_sparkline = sparklines.sparklines(
|
|
||||||
workout_counts_by_week)
|
|
||||||
duration_sparkline = sparklines.sparklines(
|
|
||||||
[int(w['duration_minutes']) for w in workouts])[0]
|
|
||||||
|
|
||||||
# REFACTOR
|
|
||||||
|
|
||||||
def date_range(start_date, end_date):
|
|
||||||
delta = end_date - start_date
|
|
||||||
return [(start_date + timedelta(days=i)) for i in range(delta.days + 1)]
|
|
||||||
|
|
||||||
dates = date_range(start_date, end_date)
|
|
||||||
daily_duration = []
|
|
||||||
for date in dates:
|
|
||||||
# check if workout exists for date
|
|
||||||
workout = [w for w in workouts if w['start_time_date'].date()
|
|
||||||
== date]
|
|
||||||
if workout:
|
|
||||||
daily_duration.append(int(workout[0]['duration_minutes']))
|
|
||||||
else:
|
|
||||||
daily_duration.append(0)
|
|
||||||
|
|
||||||
# Reverse the daily duration list so that the most recent day is on the right
|
|
||||||
daily_duration.reverse()
|
|
||||||
|
|
||||||
daily_duration_sparkline = sparklines.sparklines(
|
|
||||||
daily_duration)[0]
|
|
||||||
|
|
||||||
# REFACTOR BOUNDARY
|
|
||||||
|
|
||||||
user_data = {
|
user_data = {
|
||||||
'id': user.id,
|
'id': user.id,
|
||||||
@@ -380,12 +325,7 @@ def render_users_and_workouts():
|
|||||||
'bike_id': user.bike_id,
|
'bike_id': user.bike_id,
|
||||||
'workouts_count': len(workouts),
|
'workouts_count': len(workouts),
|
||||||
'workouts': workouts,
|
'workouts': workouts,
|
||||||
'workout_counts_by_week': workout_counts_by_week,
|
'daily_duration_sparkline': generate_daily_duration_sparkline(workouts),
|
||||||
'duration_by_week': duration_by_week,
|
|
||||||
'num_days': num_days,
|
|
||||||
'workout_counts_sparkline': workout_counts_sparkline,
|
|
||||||
'duration_sparkline': duration_sparkline,
|
|
||||||
'daily_duration_sparkline': daily_duration_sparkline,
|
|
||||||
'calendar_month': generate_calendar_monthly_view(workouts, datetime.now().date())
|
'calendar_month': generate_calendar_monthly_view(workouts, datetime.now().date())
|
||||||
}
|
}
|
||||||
users_data.append(user_data)
|
users_data.append(user_data)
|
||||||
@@ -560,6 +500,43 @@ def generate_calendar_monthly_view(workouts, selected_date):
|
|||||||
return calendar_month
|
return calendar_month
|
||||||
|
|
||||||
|
|
||||||
|
def date_range(start_date, end_date):
|
||||||
|
"""Return a list of dates between the given start and end dates (inclusive)."""
|
||||||
|
delta = end_date - start_date
|
||||||
|
return [(start_date + timedelta(days=i)) for i in range(delta.days + 1)]
|
||||||
|
|
||||||
|
|
||||||
|
def generate_daily_duration_sparkline(workouts):
|
||||||
|
"""
|
||||||
|
Generate a sparkline string representation of daily workout durations.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- workouts (list of dict): Each dict should contain 'start_time_date' and 'duration_minutes' keys.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- str: Sparkline representation of daily durations.
|
||||||
|
"""
|
||||||
|
if not 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()
|
||||||
|
dates_in_range = date_range(start_date, end_date)
|
||||||
|
|
||||||
|
# Build a mapping of dates to their respective durations for easier lookup
|
||||||
|
workouts_by_date = {w['start_time_date'].date(): int(
|
||||||
|
w['duration_minutes']) for w in workouts}
|
||||||
|
|
||||||
|
daily_durations = [workouts_by_date.get(
|
||||||
|
date, 0) for date in dates_in_range]
|
||||||
|
|
||||||
|
# Reverse the list to make the most recent day appear on the right
|
||||||
|
daily_durations.reverse()
|
||||||
|
|
||||||
|
return sparklines.sparklines(daily_durations)[0]
|
||||||
|
|
||||||
|
|
||||||
def toDate(dateString):
|
def toDate(dateString):
|
||||||
return datetime.strptime(dateString, "%Y-%m-%d").date()
|
return datetime.strptime(dateString, "%Y-%m-%d").date()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user