Display workout stats
This commit is contained in:
5
app.py
5
app.py
@@ -2,7 +2,7 @@ import os
|
||||
from flask import Flask, render_template, redirect, request, url_for
|
||||
from decorators import validate_person, validate_topset, validate_workout
|
||||
from db import DataBase
|
||||
from utils import get_people_and_exercise_rep_maxes
|
||||
from utils import get_people_and_exercise_rep_maxes, get_dashboard_stats
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_pyfile('config.py')
|
||||
@@ -15,7 +15,8 @@ def dashboard():
|
||||
all_topsets = db.get_all_topsets()
|
||||
people_and_exercise_rep_maxes = get_people_and_exercise_rep_maxes(
|
||||
all_topsets)
|
||||
return render_template('index.html', model=people_and_exercise_rep_maxes)
|
||||
stats = get_dashboard_stats(all_topsets)
|
||||
return render_template('index.html', model=people_and_exercise_rep_maxes, stats=stats)
|
||||
|
||||
|
||||
@ app.route("/person/<int:person_id>")
|
||||
|
||||
3
db.py
3
db.py
@@ -4,7 +4,7 @@ from psycopg2.extras import RealDictCursor
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from utils import get_all_exercises_from_topsets, get_people_and_exercise_rep_maxes, get_workouts
|
||||
from utils import get_all_exercises_from_topsets, get_people_and_exercise_rep_maxes, get_workouts, get_person_stats
|
||||
|
||||
|
||||
class DataBase():
|
||||
@@ -145,6 +145,7 @@ class DataBase():
|
||||
return {
|
||||
'PersonId': next((t['PersonId'] for t in topsets), -1),
|
||||
'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'),
|
||||
'Stats': get_person_stats(topsets),
|
||||
'Exercises': get_all_exercises_from_topsets(topsets),
|
||||
'Workouts': get_workouts(topsets)
|
||||
}
|
||||
|
||||
@@ -9,10 +9,15 @@
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://cdn.plot.ly/plotly-2.14.0.min.js"></script>
|
||||
<script>
|
||||
let doit;
|
||||
function debounce(func, timeout = 300) {
|
||||
let timer;
|
||||
return (...args) => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => { func.apply(this, args); }, timeout);
|
||||
};
|
||||
}
|
||||
window.addEventListener('resize', () => {
|
||||
clearTimeout(doit);
|
||||
doit = setTimeout(window.dispatchEvent(new Event('resize')), 100);
|
||||
debounce(() => window.dispatchEvent(new Event('resize')))
|
||||
})
|
||||
</script>
|
||||
</head>
|
||||
|
||||
@@ -28,6 +28,12 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if p['NumberOfWorkouts'] == 0 %}
|
||||
<div class="bg-purple-100 rounded-lg py-5 px-6 mb-4 text-base text-purple-700 mb-3" role="alert">
|
||||
No workouts completed.
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% for e in p['Exercises'] %}
|
||||
<div class="flex flex-col mt-8">
|
||||
<div class="overflow-x-auto rounded-lg">
|
||||
@@ -84,55 +90,56 @@
|
||||
|
||||
</div>
|
||||
|
||||
<div class="mt-4 w-full grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
|
||||
<div class="mt-4 w-full grid grid-cols-1 md:grid-cols-3 2xl:grid-cols-4 xl:grid-cols-5 gap-4">
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">3</span>
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{ stats['TotalWorkouts']
|
||||
}}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Total workouts tracked</h3>
|
||||
</div>
|
||||
<div class="ml-5 w-0 flex items-center justify-end flex-1 text-green-500 text-base font-bold">
|
||||
14.6%
|
||||
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
|
||||
<path fill-rule="evenodd"
|
||||
d="M5.293 7.707a1 1 0 010-1.414l4-4a1 1 0 011.414 0l4 4a1 1 0 01-1.414 1.414L11 5.414V17a1 1 0 11-2 0V5.414L6.707 7.707a1 1 0 01-1.414 0z"
|
||||
clip-rule="evenodd"></path>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
|
||||
stats['AverageWorkoutsPerWeek']
|
||||
}}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Avg. weekly workouts</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">1</span>
|
||||
<h3 class="text-base font-normal text-gray-500">New rep max this week</h3>
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{ stats['NumberOfPeople']
|
||||
}}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">People tracked</h3>
|
||||
</div>
|
||||
<div class="ml-5 w-0 flex items-center justify-end flex-1 text-green-500 text-base font-bold">
|
||||
32.9%
|
||||
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
|
||||
<path fill-rule="evenodd"
|
||||
d="M5.293 7.707a1 1 0 010-1.414l4-4a1 1 0 011.414 0l4 4a1 1 0 01-1.414 1.414L11 5.414V17a1 1 0 11-2 0V5.414L6.707 7.707a1 1 0 01-1.414 0z"
|
||||
clip-rule="evenodd"></path>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
{% if stats['TotalWorkouts'] > 0 %}
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
|
||||
stats['DaysSinceFirstWorkout']
|
||||
}}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Days since first workout</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">2</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Tracking two people</h3>
|
||||
</div>
|
||||
<div class="ml-5 w-0 flex items-center justify-end flex-1 text-red-500 text-base font-bold">
|
||||
-2.7%
|
||||
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
|
||||
<path fill-rule="evenodd"
|
||||
d="M14.707 12.293a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 111.414-1.414L9 14.586V3a1 1 0 012 0v11.586l2.293-2.293a1 1 0 011.414 0z"
|
||||
clip-rule="evenodd"></path>
|
||||
</svg>
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{ stats['DaysSinceLastWorkout']
|
||||
}}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Days since last workout</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@@ -95,4 +95,50 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4 w-full grid grid-cols-1 md:grid-cols-3 2xl:grid-cols-4 xl:grid-cols-5 gap-4">
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
|
||||
person['Stats']['NumberOfWorkouts'] }}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Total workouts tracked</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
|
||||
person['Stats']['TrainingDurationInDays'] }}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Duration of workout tracking</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
|
||||
person['Stats']['AverageWorkoutsPerWeek'] }}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Average weekly workouts</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if person['Stats']['NumberOfWorkouts'] > 0 %}
|
||||
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
|
||||
<div class="flex items-center">
|
||||
<div class="flex-shrink-0">
|
||||
<span class="text-2xl sm:text-3xl leading-none font-bold text-gray-900">{{
|
||||
person['Stats']['DaysSinceLastWorkout'] }}</span>
|
||||
<h3 class="text-base font-normal text-gray-500">Days since last workout</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
59
utils.py
59
utils.py
@@ -82,6 +82,65 @@ def get_people_and_exercise_rep_maxes(topsets):
|
||||
people.append({
|
||||
'PersonId': person_id,
|
||||
'PersonName': workouts_for_person[0]['PersonName'],
|
||||
'NumberOfWorkouts': len(list(set([t['WorkoutId'] for t in workouts_for_person if t['WorkoutId'] is not None]))),
|
||||
'Exercises': get_rep_maxes_for_person(workouts_for_person)
|
||||
})
|
||||
return people
|
||||
|
||||
|
||||
def get_person_stats(topsets):
|
||||
workout_start_dates = [datetime.strptime(
|
||||
workout['StartDate'], '%Y-%m-%d') for workout in topsets if workout['StartDate'] is not None]
|
||||
if not workout_start_dates:
|
||||
return {
|
||||
'FirstWorkout': None,
|
||||
'LastWorkout': None,
|
||||
'NumberOfWorkouts': 0,
|
||||
'TrainingDurationInDays': 0,
|
||||
'AverageWorkoutsPerWeek': 0,
|
||||
'DaysSinceLastWorkout': None,
|
||||
}
|
||||
first_workout_date = min(workout_start_dates)
|
||||
last_workout_date = max(workout_start_dates)
|
||||
training_duration = last_workout_date - first_workout_date
|
||||
no_of_workouts = len(list(set([t['WorkoutId'] for t in topsets])))
|
||||
return {
|
||||
'FirstWorkout': first_workout_date.strftime("%b %d %Y"),
|
||||
'LastWorkout': last_workout_date.strftime("%b %d %Y"),
|
||||
'NumberOfWorkouts': no_of_workouts,
|
||||
'TrainingDurationInDays': training_duration.days,
|
||||
'AverageWorkoutsPerWeek': round(no_of_workouts / (training_duration.days / 7), 2),
|
||||
'DaysSinceLastWorkout': (datetime.now() - last_workout_date).days,
|
||||
}
|
||||
|
||||
|
||||
def get_dashboard_stats(topsets):
|
||||
workout_count = len(set([t['WorkoutId']
|
||||
for t in topsets if t['WorkoutId'] is not None]))
|
||||
people_count = len(set([t['PersonId']
|
||||
for t in topsets if t['PersonId'] is not None]))
|
||||
workout_start_dates = [datetime.strptime(
|
||||
t['StartDate'], '%Y-%m-%d') for t in topsets if t['StartDate'] is not None]
|
||||
|
||||
if workout_count == 0:
|
||||
return {
|
||||
'TotalWorkouts': workout_count,
|
||||
'NumberOfPeople': people_count,
|
||||
'DaysSinceLastWorkout': None,
|
||||
'AverageWorkoutsPerWeek': 0,
|
||||
}
|
||||
|
||||
first_workout_date = min(workout_start_dates)
|
||||
last_workout_date = max(workout_start_dates)
|
||||
training_duration = last_workout_date - first_workout_date
|
||||
average_workouts_per_week = round(
|
||||
workout_count / (training_duration.days / 7), 2)
|
||||
|
||||
stats = {
|
||||
'TotalWorkouts': workout_count,
|
||||
'NumberOfPeople': people_count,
|
||||
'DaysSinceFirstWorkout': (datetime.now() - first_workout_date).days,
|
||||
'DaysSinceLastWorkout': training_duration.days,
|
||||
'AverageWorkoutsPerWeek': average_workouts_per_week,
|
||||
}
|
||||
return stats
|
||||
|
||||
Reference in New Issue
Block a user