From 13135792d8bcb29e1620ec0a50cef9fec6879012 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sun, 20 Nov 2022 15:42:58 +1100 Subject: [PATCH] Update database scheming naming convention --- db.py | 170 ++++++++-------- migrations/00_Initial_setup_and_seed.sql | 242 ++++++++++------------- utils.py | 16 +- 3 files changed, 192 insertions(+), 236 deletions(-) diff --git a/db.py b/db.py index 28d11d0..56f15e2 100644 --- a/db.py +++ b/db.py @@ -33,84 +33,84 @@ class DataBase(): def get_exercises(self): exercises = self.execute( - 'SELECT ExerciseId AS "ExerciseId", Name AS "Name" FROM Exercise') + 'SELECT exercise_id AS "ExerciseId", name AS "Name" FROM exercise') return [{"ExerciseId": e['ExerciseId'], "Name": e['Name']} for e in exercises] def get_exercise(self, exercise_id): exercise = self.execute( - 'SELECT ExerciseId AS "ExerciseId", Name AS "Name" FROM Exercise WHERE ExerciseId=%s LIMIT 1', [exercise_id], one=True) + 'SELECT exercise_id AS "ExerciseId", name AS "Name" FROM exercise WHERE exercise_id=%s LIMIT 1', [exercise_id], one=True) return exercise def create_exercise(self, name): - new_exercise = self.execute('INSERT INTO Exercise (Name) VALUES (%s) RETURNING ExerciseId AS "ExerciseId"', + new_exercise = self.execute('INSERT INTO exercise (name) VALUES (%s) RETURNING exercise_id AS "ExerciseId"', [name], commit=True, one=True) return new_exercise['ExerciseId'] def delete_exercise(self, exercise_id): - self.execute('DELETE FROM Exercise WHERE ExerciseId=%s', [ + self.execute('DELETE FROM exercise WHERE exercise_id=%s', [ exercise_id], commit=True) def update_exercise(self, exercise_id, name): - self.execute('UPDATE Exercise SET Name=%s WHERE ExerciseId=%s', [ + self.execute('UPDATE Exercise SET Name=%s WHERE exercise_id=%s', [ name, exercise_id], commit=True) def get_people(self): people = self.execute( - 'SELECT PersonId AS "PersonId", Name AS "Name" FROM Person') + 'SELECT person_id AS "PersonId", name AS "Name" FROM person') return people def is_valid_person(self, person_id): person = self.execute( - 'SELECT PersonId AS "PersonId" FROM Person WHERE PersonId=%s LIMIT 1', [person_id], one=True) + 'SELECT person_id AS "PersonId" FROM person WHERE person_id=%s LIMIT 1', [person_id], one=True) return person def create_person(self, name): - new_person = self.execute('INSERT INTO Person (Name) VALUES (%s) RETURNING PersonId AS "PersonId"', [ + new_person = self.execute('INSERT INTO person (name) VALUES (%s) RETURNING person_id AS "PersonId"', [ name], commit=True, one=True) return new_person['PersonId'] def delete_person(self, person_id): - self.execute('DELETE FROM TopSet WHERE WorkoutId IN (SELECT WorkoutId FROM Workout WHERE PersonId=%s)', [ + self.execute('DELETE FROM topset WHERE workout_id IN (SELECT workout_id FROM workout WHERE person_id=%s)', [ person_id], commit=True) - self.execute('DELETE FROM Workout WHERE PersonId=%s', + self.execute('DELETE FROM workout WHERE person_id=%s', [person_id], commit=True) - self.execute('DELETE FROM Person WHERE PersonId=%s', + self.execute('DELETE FROM person WHERE person_id=%s', [person_id], commit=True) def update_person_name(self, person_id, name): - self.execute('UPDATE Person SET Name=%s WHERE PersonId=%s', [ + self.execute('UPDATE person SET name=%s WHERE person_id=%s', [ name, person_id], commit=True) def is_valid_workout(self, person_id, workout_id): - workout = self.execute('SELECT W.WorkoutId AS "WorkoutId" FROM Person P, Workout W WHERE P.PersonId=W.PersonId AND P.PersonId=%s AND W.WorkoutId=%s LIMIT 1', [ + workout = self.execute('SELECT W.workout_id AS "WorkoutId" FROM Person P, Workout W WHERE P.person_id=W.person_id AND P.person_id=%s AND W.workout_id=%s LIMIT 1', [ person_id, workout_id], one=True) return workout def is_valid_topset(self, person_id, workout_id, topset_id): topset = self.execute(""" - SELECT T.TopSetId AS "TopSetId" + SELECT T.topset_id AS "TopSetId" FROM Person P, Workout W, TopSet T - WHERE W.PersonId=W.PersonId AND W.WorkoutId=T.WorkoutId AND P.PersonId=%s AND W.WorkoutId = %s AND T.TopSetId = %s + WHERE W.person_id=W.person_id AND W.workout_id=T.workout_id AND P.person_id=%s AND W.workout_id = %s AND T.topset_id = %s LIMIT 1""", [person_id, workout_id, topset_id], one=True) return topset def delete_workout(self, workout_id): - self.execute('DELETE FROM TopSet WHERE WorkoutId=%s', + self.execute('DELETE FROM topset WHERE workout_id=%s', [workout_id], commit=True) - self.execute('DELETE FROM Workout WHERE WorkoutId=%s', + self.execute('DELETE FROM workout WHERE workout_id=%s', [workout_id], commit=True) def update_topset(self, exercise_id, repetitions, weight, topset_id): - self.execute('UPDATE TopSet SET ExerciseId=%s, Repetitions=%s, Weight=%s WHERE TopSetId=%s', [ + self.execute('UPDATE topset SET exercise_id=%s, repetitions=%s, weight=%s WHERE topSet_id=%s', [ exercise_id, repetitions, weight, topset_id], commit=True) def create_topset(self, workout_id, exercise_id, repetitions, weight): - new_top_set = self.execute('INSERT INTO TopSet (WorkoutId, ExerciseId, Repetitions, Weight) VALUES (%s, %s, %s, %s) RETURNING TopSetId AS "TopSetId"', [ + new_top_set = self.execute('INSERT INTO topset (workout_id, exercise_id, repetitions, weight) VALUES (%s, %s, %s, %s) RETURNING topset_id AS "TopSetId"', [ workout_id, exercise_id, repetitions, weight], commit=True, one=True) return new_top_set['TopSetId'] def delete_topset(self, topset_id): - self.execute('DELETE FROM TopSet WHERE TopSetId=%s', [ + self.execute('DELETE FROM topset WHERE topset_id=%s', [ topset_id], commit=True) def create_workout(self, person_id): @@ -118,49 +118,49 @@ class DataBase(): date_string = now.strftime('%Y-%m-%d') print( f'Creating workout for PersonId {person_id} starting at {date_string}') - new_workout = self.execute('INSERT INTO Workout (PersonId, StartDate) VALUES (%s, %s) RETURNING WorkoutId AS "WorkoutId"', [ + new_workout = self.execute('INSERT INTO workout (person_id, start_date) VALUES (%s, %s) RETURNING workout_id AS "WorkoutId"', [ person_id, date_string], commit=True, one=True) return new_workout['WorkoutId'] def get_people_and_workout_count(self, person_id): return self.execute(""" SELECT - P.PersonId AS "PersonId", - P.Name AS "Name", - COUNT(W.WorkoutId) AS "NumberOfWorkouts", - CASE P.PersonId + P.person_id AS "PersonId", + P.name AS "Name", + COUNT(W.workout_id) AS "NumberOfWorkouts", + CASE P.person_id WHEN %s THEN 1 ELSE 0 END "IsActive" FROM - Person P LEFT JOIN Workout W ON P.PersonId = W.PersonId + Person P LEFT JOIN Workout W ON P.person_id = W.person_id GROUP BY - P.PersonId + P.person_id ORDER BY - P.PersonId""", [person_id]) + P.person_id""", [person_id]) def update_workout_start_date(self, workout_id, start_date): - self.execute('UPDATE Workout SET StartDate=%s WHERE WorkoutId=%s', [ + self.execute('UPDATE workout SET start_date=%s WHERE workout_id=%s', [ start_date, workout_id], commit=True) def get_person(self, person_id): topsets = self.execute(""" SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", - E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", - T.Weight AS "Weight" + P.person_id AS "PersonId", + P.name AS "PersonName", + W.workout_id AS "WorkoutId", + W.start_date AS "StartDate", + T.topset_id AS "TopSetId", + E.exercise_id AS "ExerciseId", + E.name AS "ExerciseName", + T.repetitions AS "Repetitions", + T.weight AS "Weight" FROM Person P - LEFT JOIN Workout W ON P.PersonId=W.PersonId - LEFT JOIN TopSet T ON W.WorkoutId=T.WorkoutId - LEFT JOIN Exercise E ON T.ExerciseId=E.ExerciseId - WHERE P.PersonId=%s""", [person_id]) + LEFT JOIN Workout W ON P.person_id=W.person_id + LEFT JOIN TopSet T ON W.workout_id=T.workout_id + LEFT JOIN Exercise E ON T.exercise_id=E.exercise_id + WHERE P.person_id=%s""", [person_id]) return { 'PersonId': next((t['PersonId'] for t in topsets), -1), @@ -173,27 +173,27 @@ class DataBase(): def get_workout(self, person_id, workout_id): topsets = self.execute(""" SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", - E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", - T.Weight AS "Weight" + P.person_id AS "PersonId", + P.name AS "PersonName", + W.workout_id AS "WorkoutId", + W.start_date AS "StartDate", + T.topset_id AS "TopSetId", + E.exercise_id AS "ExerciseId", + E.name AS "ExerciseName", + T.repetitions AS "Repetitions", + T.weight AS "Weight" FROM Person P - LEFT JOIN Workout W ON P.PersonId=W.PersonId - LEFT JOIN TopSet T ON W.WorkoutId=T.WorkoutId - LEFT JOIN Exercise E ON T.ExerciseId=E.ExerciseId - WHERE P.PersonId=%s - AND W.WorkoutId = %s""", [person_id, workout_id]) + LEFT JOIN Workout W ON P.person_id=W.person_id + LEFT JOIN TopSet T ON W.workout_id=T.workout_id + LEFT JOIN Exercise E ON T.exercise_id=E.exercise_id + WHERE P.person_id=%s + AND W.workout_id = %s""", [person_id, workout_id]) return { 'PersonId': next((t['PersonId'] for t in topsets), -1), 'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'), 'WorkoutId': workout_id, - 'StartDate': datetime.strptime(topsets[0]['StartDate'], "%Y-%m-%d").strftime("%Y-%m-%d"), + 'StartDate': topsets[0]['StartDate'].strftime("%Y-%m-%d"), 'Exercises': self.get_exercises(), 'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets if t['TopSetId'] is not None] } @@ -201,22 +201,22 @@ class DataBase(): def get_topset(self, person_id, workout_id, topset_id): topset = self.execute(""" SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", - E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", - T.Weight AS "Weight" + P.person_id AS "PersonId", + P.name AS "PersonName", + W.workout_id AS "WorkoutId", + W.start_date AS "StartDate", + T.topset_id AS "TopSetId", + E.exercise_id AS "ExerciseId", + E.name AS "ExerciseName", + T.repetitions AS "Repetitions", + T.weight AS "Weight" FROM Person P - INNER JOIN Workout W ON P.PersonId=W.PersonId - INNER JOIN TopSet T ON W.WorkoutId=T.WorkoutId - INNER JOIN Exercise E ON T.ExerciseId=E.ExerciseId - WHERE P.PersonId=%s - AND W.WorkoutId = %s - AND T.TopSetId = %s""", [person_id, workout_id, topset_id], one=True) + INNER JOIN Workout W ON P.person_id=W.person_id + INNER JOIN TopSet T ON W.workout_id=T.workout_id + INNER JOIN Exercise E ON T.exercise_id=E.exercise_id + WHERE P.person_id=%s + AND W.workout_id = %s + AND T.topset_id = %s""", [person_id, workout_id, topset_id], one=True) return { 'PersonId': topset['PersonId'], @@ -234,19 +234,19 @@ class DataBase(): def get_all_topsets(self): all_topsets = self.execute(""" SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", - E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", - T.Weight AS "Weight", - round((100 * T.Weight)/(101.3-2.67123 * T.Repetitions),0)::numeric::integer AS "Estimated1RM" + P.person_id AS "PersonId", + P.name AS "PersonName", + W.workout_id AS "WorkoutId", + W.start_date AS "StartDate", + T.topset_id AS "TopSetId", + E.exercise_id AS "ExerciseId", + E.name AS "ExerciseName", + T.repetitions AS "Repetitions", + T.weight AS "Weight", + round((100 * T.Weight::numeric::integer)/(101.3-2.67123 * T.Repetitions),0)::numeric::integer AS "Estimated1RM" FROM Person P - LEFT JOIN Workout W ON P.PersonId=W.PersonId - LEFT JOIN TopSet T ON W.WorkoutId=T.WorkoutId - LEFT JOIN Exercise E ON T.ExerciseId=E.ExerciseId""") + LEFT JOIN Workout W ON P.person_id=W.person_id + LEFT JOIN TopSet T ON W.workout_id=T.workout_id + LEFT JOIN Exercise E ON T.exercise_id=E.exercise_id""") return all_topsets diff --git a/migrations/00_Initial_setup_and_seed.sql b/migrations/00_Initial_setup_and_seed.sql index 57969ca..45d4d6b 100644 --- a/migrations/00_Initial_setup_and_seed.sql +++ b/migrations/00_Initial_setup_and_seed.sql @@ -1,91 +1,115 @@ -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */ -; +BEGIN; -/*!40101 SET NAMES */ -; - -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */ -; - -/*!40103 SET TIME_ZONE='+00:00' */ -; - -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */ -; - -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */ -; - -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */ -; - --- Dumping structure for table public.exercise CREATE TABLE IF NOT EXISTS "exercise" ( - "exerciseid" BIGINT NOT NULL DEFAULT 'nextval(''exercise_exerciseid_seq''::regclass)', - "name" TEXT NULL DEFAULT NULL, - PRIMARY KEY ("exerciseid") + "exercise_id" SERIAL PRIMARY KEY, + "name" TEXT NOT NULL, + CONSTRAINT min_name_chk CHECK (length (name) >= 2), + CONSTRAINT max_name_chk CHECK (length (name) <= 100) ); --- Dumping data for table public.exercise: 5 rows -/*!40000 ALTER TABLE "exercise" DISABLE KEYS */ -; - INSERT INTO - "exercise" ("exerciseid", "name") + "exercise" ("exercise_id", "name") VALUES (1, 'Squat'), (2, 'Bench'), (3, 'Deadlift'), (4, 'Hotep'), - (5, 'Lat Pulldown'); + (5, 'Lat Pulldown'), + (8, 'DB Seal Row'), + (9, 'Seated BB Hotep'); -/*!40000 ALTER TABLE "exercise" ENABLE KEYS */ -; - --- Dumping structure for table public.person CREATE TABLE IF NOT EXISTS "person" ( - "personid" BIGINT NOT NULL DEFAULT 'nextval(''person_personid_seq''::regclass)', - "name" TEXT NULL DEFAULT NULL, - PRIMARY KEY ("personid") + "person_id" SERIAL PRIMARY KEY, + "name" TEXT NOT NULL, + CONSTRAINT min_name_chk CHECK (length (name) >= 2), + CONSTRAINT max_name_chk CHECK (length (name) <= 100) ); --- Dumping data for table public.person: 2 rows -/*!40000 ALTER TABLE "person" DISABLE KEYS */ -; - INSERT INTO - "person" ("personid", "name") + "person" ("person_id", "name") VALUES (1, 'Gabe'), (2, 'Michael'); -/*!40000 ALTER TABLE "person" ENABLE KEYS */ -; - --- Dumping structure for table public.topset CREATE TABLE - IF NOT EXISTS "topset" ( - "topsetid" BIGINT NOT NULL DEFAULT 'nextval(''topset_topsetid_seq''::regclass)', - "workoutid" BIGINT NULL DEFAULT NULL, - "exerciseid" BIGINT NULL DEFAULT NULL, - "repetitions" BIGINT NULL DEFAULT NULL, - "weight" BIGINT NULL DEFAULT NULL, - PRIMARY KEY ("topsetid"), - CONSTRAINT "topset_exerciseid_fkey" FOREIGN KEY ("exerciseid") REFERENCES "exercise" ("exerciseid") ON UPDATE NO ACTION ON DELETE CASCADE, - CONSTRAINT "topset_workoutid_fkey" FOREIGN KEY ("workoutid") REFERENCES "workout" ("workoutid") ON UPDATE NO ACTION ON DELETE CASCADE + IF NOT EXISTS "workout" ( + "workout_id" SERIAL PRIMARY KEY, + "person_id" BIGINT NOT NULL, + "start_date" DATE NOT NULL DEFAULT CURRENT_DATE, + CONSTRAINT "workout_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "person" ("person_id") ON UPDATE NO ACTION ON DELETE CASCADE ); --- Dumping data for table public.topset: 90 rows -/*!40000 ALTER TABLE "topset" DISABLE KEYS */ -; +INSERT INTO + "workout" ("workout_id", "person_id", "start_date") +VALUES + (10, 2, '2022-01-13'), + (11, 2, '2022-01-18'), + (12, 2, '2022-01-20'), + (13, 2, '2022-01-25'), + (15, 2, '2022-01-27'), + (16, 2, '2022-02-01'), + (17, 2, '2022-02-03'), + (18, 2, '2022-02-08'), + (19, 2, '2022-02-15'), + (20, 2, '2022-02-17'), + (21, 2, '2022-03-01'), + (22, 2, '2022-03-10'), + (23, 2, '2022-03-15'), + (24, 2, '2022-03-22'), + (25, 2, '2022-04-12'), + (26, 2, '2022-04-21'), + (27, 2, '2022-04-26'), + (28, 2, '2022-04-28'), + (29, 2, '2022-05-03'), + (30, 2, '2022-05-05'), + (31, 2, '2022-05-17'), + (32, 2, '2022-05-19'), + (33, 2, '2022-06-07'), + (34, 2, '2022-06-14'), + (35, 1, '2022-01-13'), + (36, 1, '2022-01-18'), + (37, 1, '2022-02-08'), + (38, 1, '2022-02-15'), + (39, 1, '2022-02-17'), + (40, 1, '2022-03-01'), + (41, 1, '2022-03-10'), + (42, 1, '2022-03-15'), + (43, 1, '2022-03-22'), + (44, 1, '2022-04-07'), + (45, 1, '2022-04-21'), + (46, 1, '2022-04-26'), + (47, 1, '2022-04-28'), + (48, 1, '2022-05-03'), + (49, 1, '2022-05-05'), + (50, 1, '2022-05-17'), + (51, 1, '2022-05-19'), + (52, 1, '2022-06-07'), + (53, 1, '2022-06-14'), + (54, 1, '2022-06-29'), + (55, 1, '2022-07-07'), + (56, 1, '2022-07-12'), + (57, 1, '2022-07-15'), + (65, 1, '2022-11-10'), + (66, 1, '2022-11-17'); + +CREATE TABLE + IF NOT EXISTS "topset" ( + "topset_id" SERIAL PRIMARY KEY, + "workout_id" BIGINT NOT NULL, + "exercise_id" BIGINT NOT NULL, + "repetitions" BIGINT NOT NULL, + "weight" REAL NOT NULL, + CONSTRAINT "topset_exercise_id_fkey" FOREIGN KEY ("exercise_id") REFERENCES "exercise" ("exercise_id") ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT "topset_workout_id_fkey" FOREIGN KEY ("workout_id") REFERENCES "workout" ("workout_id") ON UPDATE NO ACTION ON DELETE CASCADE + ); INSERT INTO "topset" ( - "topsetid", - "workoutid", - "exerciseid", + "topset_id", + "workout_id", + "exercise_id", "repetitions", "weight" ) @@ -179,90 +203,22 @@ VALUES (99, 56, 2, 4, 60), (100, 56, 3, 9, 100), (101, 57, 1, 6, 75), - (102, 57, 4, 3, 35); + (102, 57, 4, 3, 35), + (106, 65, 8, 10, 17.5), + (107, 65, 2, 6, 45), + (108, 66, 5, 12, 45), + (109, 66, 9, 8, 25); -/*!40000 ALTER TABLE "topset" ENABLE KEYS */ -; +SELECT + setval ('exercise_exercise_id_seq', 1000, FALSE); --- Dumping structure for table public.workout -CREATE TABLE - IF NOT EXISTS "workout" ( - "workoutid" BIGINT NOT NULL DEFAULT 'nextval(''workout_workoutid_seq''::regclass)', - "personid" BIGINT NULL DEFAULT NULL, - "startdate" TEXT NULL DEFAULT NULL, - PRIMARY KEY ("workoutid"), - CONSTRAINT "workout_personid_fkey" FOREIGN KEY ("personid") REFERENCES "person" ("personid") ON UPDATE NO ACTION ON DELETE CASCADE - ); +SELECT + setval ('person_person_id_seq', 1000, FALSE); --- Dumping data for table public.workout: 47 rows -/*!40000 ALTER TABLE "workout" DISABLE KEYS */ -; +SELECT + setval ('workout_workout_id_seq', 1000, FALSE); -INSERT INTO - "workout" ("workoutid", "personid", "startdate") -VALUES - (10, 2, '2022-01-13'), - (11, 2, '2022-01-18'), - (12, 2, '2022-01-20'), - (13, 2, '2022-01-25'), - (15, 2, '2022-01-27'), - (16, 2, '2022-02-01'), - (17, 2, '2022-02-03'), - (18, 2, '2022-02-08'), - (19, 2, '2022-02-15'), - (20, 2, '2022-02-17'), - (21, 2, '2022-03-01'), - (22, 2, '2022-03-10'), - (23, 2, '2022-03-15'), - (24, 2, '2022-03-22'), - (25, 2, '2022-04-12'), - (26, 2, '2022-04-21'), - (27, 2, '2022-04-26'), - (28, 2, '2022-04-28'), - (29, 2, '2022-05-03'), - (30, 2, '2022-05-05'), - (31, 2, '2022-05-17'), - (32, 2, '2022-05-19'), - (33, 2, '2022-06-07'), - (34, 2, '2022-06-14'), - (35, 1, '2022-01-13'), - (36, 1, '2022-01-18'), - (37, 1, '2022-02-08'), - (38, 1, '2022-02-15'), - (39, 1, '2022-02-17'), - (40, 1, '2022-03-01'), - (41, 1, '2022-03-10'), - (42, 1, '2022-03-15'), - (43, 1, '2022-03-22'), - (44, 1, '2022-04-07'), - (45, 1, '2022-04-21'), - (46, 1, '2022-04-26'), - (47, 1, '2022-04-28'), - (48, 1, '2022-05-03'), - (49, 1, '2022-05-05'), - (50, 1, '2022-05-17'), - (51, 1, '2022-05-19'), - (52, 1, '2022-06-07'), - (53, 1, '2022-06-14'), - (54, 1, '2022-06-29'), - (55, 1, '2022-07-07'), - (56, 1, '2022-07-12'), - (57, 1, '2022-07-15'); +SELECT + setval ('topset_topset_id_seq', 1000, FALSE); -/*!40000 ALTER TABLE "workout" ENABLE KEYS */ -; - -/*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */ -; - -/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */ -; - -/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */ -; - -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */ -; - -/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */ -; \ No newline at end of file +COMMIT; \ No newline at end of file diff --git a/utils.py b/utils.py index 57b4f25..95654bc 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import date import json @@ -14,7 +14,7 @@ def get_workouts(topsets): t for t in topsets if t['WorkoutId'] == workout_id] workouts.append({ 'WorkoutId': workout_id, - 'StartDate': datetime.strptime(topsets_in_workout[0]['StartDate'], "%Y-%m-%d").strftime("%b %d %Y"), + 'StartDate': topsets_in_workout[0]['StartDate'].strftime("%b %d %Y"), 'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets_in_workout] }) return workouts @@ -47,7 +47,7 @@ def get_rep_maxes_for_person(person_topsets): max_weight = max([t['Weight'] for t in reps]) max_topset_for_rep = [t for t in reps if t['Weight'] == max_weight] topsets_for_exercise.append({ - 'StartDate': datetime.strptime(max_topset_for_rep[0]['StartDate'], "%Y-%m-%d").strftime("%b %d %Y"), + 'StartDate': max_topset_for_rep[0]['StartDate'].strftime("%b %d %Y"), 'Repetitions': rep, 'Weight': max_weight, 'Estimated1RM': max_topset_for_rep[0]['Estimated1RM'], @@ -62,7 +62,7 @@ def get_rep_maxes_for_person(person_topsets): 'ExerciseName': e['ExerciseName'], 'RepMaxes': topsets_for_exercise, 'EstimatedOneRepMaxProgressions': { - 'StartDates': json.dumps([t['StartDate'] for t in exercise_topsets]), + 'StartDates': json.dumps([t['StartDate'].strftime("%Y-%m-%d") for t in exercise_topsets]), 'TopSets': json.dumps([f"{t['Repetitions']} x {t['Weight']}kg" for t in exercise_topsets]), 'Estimated1RMs': json.dumps([t['Estimated1RM'] for t in exercise_topsets]), } @@ -93,8 +93,8 @@ def get_stats_from_topsets(topsets): 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] + workout_start_dates = [t['StartDate'] + for t in topsets if t['StartDate'] is not None] stats = [{"Text": "Total Workouts", "Value": workout_count}] if people_count > 1: @@ -104,11 +104,11 @@ def get_stats_from_topsets(topsets): last_workout_date = max(workout_start_dates) stats.append({"Text": "Days Since First Workout", "Value": ( - datetime.now() - first_workout_date).days}) + date.today() - first_workout_date).days}) if workout_count >= 2: stats.append({"Text": "Days Since Last Workout", "Value": ( - datetime.now() - last_workout_date).days}) + date.today() - last_workout_date).days}) training_duration = last_workout_date - first_workout_date average_workouts_per_week = round(