Add badges to sets showing stats ie weight/rep increase or how many weeks stalled
This commit is contained in:
89
db.py
89
db.py
@@ -466,6 +466,95 @@ class DataBase():
|
||||
|
||||
return result[0]['earliest_date'], result[0]['latest_date']
|
||||
|
||||
def get_topset_achievements(self, topset_id):
|
||||
# 1. Fetch current topset details
|
||||
current = self.execute("""
|
||||
SELECT
|
||||
t.weight, t.repetitions, t.exercise_id, w.person_id, w.start_date, w.workout_id,
|
||||
ROUND((100 * t.weight::NUMERIC::INTEGER) / (101.3 - 2.67123 * t.repetitions), 0)::NUMERIC::INTEGER AS estimated_1rm
|
||||
FROM topset t
|
||||
JOIN workout w ON t.workout_id = w.workout_id
|
||||
WHERE t.topset_id = %s
|
||||
""", [topset_id], one=True)
|
||||
|
||||
if not current:
|
||||
return {}
|
||||
|
||||
person_id = current['person_id']
|
||||
exercise_id = current['exercise_id']
|
||||
current_date = current['start_date']
|
||||
current_weight = current['weight']
|
||||
current_reps = current['repetitions']
|
||||
current_e1rm = current['estimated_1rm']
|
||||
|
||||
# 2. Fetch "Last Time" (previous workout's best set for this exercise)
|
||||
last_set = self.execute("""
|
||||
SELECT t.weight, t.repetitions
|
||||
FROM topset t
|
||||
JOIN workout w ON t.workout_id = w.workout_id
|
||||
WHERE w.person_id = %s AND t.exercise_id = %s AND w.start_date < %s
|
||||
ORDER BY w.start_date DESC, (100 * t.weight::NUMERIC::INTEGER) / (101.3 - 2.67123 * t.repetitions) DESC
|
||||
LIMIT 1
|
||||
""", [person_id, exercise_id, current_date], one=True)
|
||||
|
||||
# 3. Fetch All-Time Bests (strictly before current workout)
|
||||
best_stats = self.execute("""
|
||||
SELECT
|
||||
MAX(t.weight) as max_weight,
|
||||
MAX(ROUND((100 * t.weight::NUMERIC::INTEGER) / (101.3 - 2.67123 * t.repetitions), 0)) as max_e1rm,
|
||||
MAX(t.repetitions) FILTER (WHERE t.weight >= %s) as max_reps_at_weight
|
||||
FROM topset t
|
||||
JOIN workout w ON t.workout_id = w.workout_id
|
||||
WHERE w.person_id = %s AND t.exercise_id = %s AND w.start_date < %s
|
||||
""", [current_weight, person_id, exercise_id, current_date], one=True)
|
||||
|
||||
achievements = {
|
||||
'is_pr_weight': False,
|
||||
'is_pr_e1rm': False,
|
||||
'is_pr_reps': False,
|
||||
'weight_increase': 0,
|
||||
'rep_increase': 0,
|
||||
'stalled_sessions': 0
|
||||
}
|
||||
|
||||
# Calculate PRs
|
||||
if best_stats:
|
||||
if best_stats['max_weight'] and current_weight > best_stats['max_weight']:
|
||||
achievements['is_pr_weight'] = True
|
||||
if best_stats['max_e1rm'] and current_e1rm > best_stats['max_e1rm']:
|
||||
achievements['is_pr_e1rm'] = True
|
||||
if best_stats['max_reps_at_weight'] and current_reps > best_stats['max_reps_at_weight']:
|
||||
achievements['is_pr_reps'] = True
|
||||
|
||||
# Calculate Stalled Sessions
|
||||
# Count consecutive previous workouts for this exercise where weight and reps were identical to current
|
||||
previous_sets = self.execute("""
|
||||
SELECT t.weight, t.repetitions
|
||||
FROM topset t
|
||||
JOIN workout w ON t.workout_id = w.workout_id
|
||||
WHERE w.person_id = %s AND t.exercise_id = %s AND w.start_date < %s
|
||||
ORDER BY w.start_date DESC
|
||||
""", [person_id, exercise_id, current_date])
|
||||
|
||||
stalled_count = 0
|
||||
for s in previous_sets:
|
||||
if s['weight'] == current_weight and s['repetitions'] == current_reps:
|
||||
stalled_count += 1
|
||||
else:
|
||||
break
|
||||
|
||||
if stalled_count >= 1: # If it's the same as at least the previous session
|
||||
achievements['stalled_sessions'] = stalled_count
|
||||
|
||||
# Calculate Increases vs Last Time
|
||||
if last_set:
|
||||
if current_weight > last_set['weight']:
|
||||
achievements['weight_increase'] = current_weight - last_set['weight']
|
||||
elif current_weight == last_set['weight'] and current_reps > last_set['repetitions']:
|
||||
achievements['rep_increase'] = current_reps - last_set['repetitions']
|
||||
|
||||
return achievements
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user