Render svg graphs in initial response rather then requesting each graph individually. Initial load file size of dashboard will be larger, unsure if I will rollback this change

This commit is contained in:
Peter Stockings
2023-12-09 23:10:13 +11:00
parent c5e825f4df
commit d0afd92126
4 changed files with 90 additions and 71 deletions

67
db.py
View File

@@ -7,7 +7,7 @@ from urllib.parse import urlparse
from flask import g
from utils import get_all_exercises_from_topsets, get_stats_from_topsets, get_workouts
from utils import get_all_exercises_from_topsets, get_exercise_graph_model, get_stats_from_topsets, get_workouts
class DataBase():
@@ -476,69 +476,8 @@ class DataBase():
repetitions = [t['repetitions'] for t in topsets]
weight = [t['weight'] for t in topsets]
start_dates = [t['start_date'] for t in topsets]
min_date, max_date = min(start_dates), max(start_dates)
min_e1rm, max_e1rm = min(estimated_1rm), max(estimated_1rm)
min_reps, max_reps = min(repetitions), max(repetitions)
min_weight, max_weight = min(weight), max(weight)
# Calculate viewBox dimensions
date_range = max_date - min_date
total_span = date_range.days or 1
e1rm_range = (max_e1rm - min_e1rm) or 1
reps_range = (max_reps - min_reps) or 1
weight_range = (max_weight - min_weight) or 1
vb_width, vb_height = total_span, e1rm_range
vb_width *= 200 / vb_width # Scale to 200px width
vb_height *= 75 / vb_height # Scale to 75px height
# Scale estimated_1rm values for SVG plotting
estimated_1rm_scaled = [((value - min_e1rm) / e1rm_range) * vb_height for value in estimated_1rm]
repetitions_scaled = [((value - min_reps) / reps_range) * vb_height for value in repetitions]
weight_scaled = [((value - min_weight) / weight_range) * vb_height for value in weight]
relative_positions = [(date - min_date).days / total_span for date in start_dates]
# Convert relative positions and scaled estimated 1RM values to numpy arrays
x = np.array(relative_positions)
y = np.array(estimated_1rm_scaled)
# Calculate the slope (m) and y-intercept (b) of the line of best fit
m, b = np.polyfit(x, y, 1)
# Generate points along the line of best fit
y_best_fit = [m * xi + b for xi in x]
best_fit_points = zip(y_best_fit, relative_positions)
# Create messages and zip data for SVG plotting
messages = [f'{t["repetitions"]} x {t["weight"]}kg ({t["estimated_1rm"]}kg E1RM) on {t["start_date"].strftime("%d %b %y")}' for t in topsets]
estimated_1rm_points = zip(estimated_1rm_scaled, relative_positions, messages)
repetitions_points = zip(repetitions_scaled, relative_positions, messages)
weight_points = zip(weight_scaled, relative_positions, messages)
repetitions = {
'label': 'Reps',
'color': '#388fed',
'points': list(repetitions_points)
}
weight = {
'label': 'Weight',
'color': '#bd3178',
'points': list(weight_points)
}
estimated_1rm = {
'label': 'E1RM',
'color': '#2ca02c',
'points': list(estimated_1rm_points)
}
exercise_progress = get_exercise_graph_model(topsets[0]['exercise_name'], estimated_1rm, repetitions, weight, start_dates, messages)
plot_labels = zip(relative_positions, messages)
# Return exercise data with SVG dimensions and data points
return {
'title': topsets[0]['exercise_name'],
'vb_width': vb_width,
'vb_height': vb_height,
'plots': [repetitions, weight, estimated_1rm],
'best_fit_points': list(best_fit_points),
'plot_labels': plot_labels
}
return exercise_progress