Minor refactor in attempt to speed up site

This commit is contained in:
Peter Stockings
2025-01-25 00:16:32 +11:00
parent 78f2ce2317
commit 76b610c949
2 changed files with 64 additions and 74 deletions

61
db.py
View File

@@ -534,7 +534,7 @@ class DataBase():
w.workout_id,
to_char(w.start_date, 'Mon DD YYYY') AS formatted_start_date,
w.note,
t.filter as tag_filter,
t.filter AS tag_filter,
t.name AS tag_name
FROM person p
LEFT JOIN workout w ON p.person_id = w.person_id AND w.note IS NOT NULL AND w.note <> ''
@@ -545,20 +545,20 @@ class DataBase():
"""
# Execute the SQL query
raw_workout_notes = self.execute(sql_query, [person_id])
raw_data = self.execute(sql_query, [person_id])
if not raw_data:
return None, []
# Extract person name from the first row (all rows have the same person name)
person_name = raw_data[0]['person_name']
# Initialize variables to hold the person's name and the workouts
person_name = None
# Process the workout notes
workout_notes = {}
for row in raw_workout_notes:
# Update person_name (it will be the same for all rows)
if person_name is None:
person_name = row['person_name']
# Process workout notes and tags if there's a note associated with the workout
if row['workout_id'] and row['note']: # Check if workout_id exists and note is not None or empty
workout_id = row['workout_id']
for row in raw_data:
workout_id = row['workout_id']
if workout_id and row['note']:
# Initialize the workout entry if it doesn't exist
if workout_id not in workout_notes:
workout_notes[workout_id] = {
'workout_id': workout_id,
@@ -566,37 +566,38 @@ class DataBase():
'note': row['note'],
'tags': []
}
if row['tag_name']: # Only add the tag if it is not None
workout_notes[workout_id]['tags'].append({'tag_filter': row['tag_filter'], 'tag_name': row['tag_name'], 'person_id': person_id})
# Add tags if present
if row['tag_name']:
workout_notes[workout_id]['tags'].append({
'tag_filter': row['tag_filter'],
'tag_name': row['tag_name'],
'person_id': person_id
})
# Convert the workout_notes dictionary back into a list as the final result
# Convert to a list for the final output
workout_notes_list = list(workout_notes.values())
return person_name, workout_notes_list
# Return a tuple containing the person's name and their workout notes
return (person_name, workout_notes_list)
def get_exercise_earliest_and_latest_dates(self, person_id, exercise_id):
sql_query = """
SELECT
w.start_date
MIN(w.start_date) AS earliest_date,
MAX(w.start_date) AS latest_date
FROM workout w
INNER JOIN topset t on w.workout_id = t.workout_id
INNER JOIN exercise e on t.exercise_id = e.exercise_id
WHERE w.person_id = %s AND e.exercise_id = %s
ORDER BY w.start_date DESC;
INNER JOIN topset t ON w.workout_id = t.workout_id
INNER JOIN exercise e ON t.exercise_id = e.exercise_id
WHERE w.person_id = %s AND e.exercise_id = %s;
"""
# Execute the SQL query
workout_exercise_dates = self.execute(sql_query, [person_id, exercise_id])
result = self.execute(sql_query, [person_id, exercise_id])
if not workout_exercise_dates:
if not result or not result[0]:
return None, None
latest_date = workout_exercise_dates[0]['start_date']
earliest_date = workout_exercise_dates[-1]['start_date']
return earliest_date, latest_date
return result[0]['earliest_date'], result[0]['latest_date']