From ca44b96764bb69c90b97fabda3749f4aeda998d6 Mon Sep 17 00:00:00 2001 From: voynow Date: Sat, 10 Aug 2024 12:14:13 -0400 Subject: [PATCH] training week with coaching table in supabase --- src/lambda_function.py | 9 +++-- src/supabase_client.py | 26 +++++++++++++- test.ipynb | 78 ++++++++++++++---------------------------- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/lambda_function.py b/src/lambda_function.py index e14936d..3659d06 100644 --- a/src/lambda_function.py +++ b/src/lambda_function.py @@ -7,6 +7,7 @@ ) from src.auth_manager import get_strava_client from src.email_manager import send_email, training_week_to_html +from src.supabase_client import upsert_training_week_with_coaching from src.training_week import generate_training_week @@ -15,14 +16,18 @@ def lambda_handler(event, context): athlete_id = os.environ["JAMIES_ATHLETE_ID"] strava_client = get_strava_client(athlete_id) + # process activities data activities_df = get_activities_df(strava_client) day_of_week_summaries = get_day_of_week_summaries(activities_df) weekly_summaries = get_weekly_summaries(activities_df) + # Generate the training week training_week = generate_training_week(weekly_summaries, day_of_week_summaries) + upsert_training_week_with_coaching( + athlete_id=athlete_id, training_week_with_coaching=training_week + ) - subject = "Training Schedule Just Dropped 🏃" send_email( - subject=subject, + subject="Training Schedule Just Dropped 🏃", html_content=training_week_to_html(training_week), ) diff --git a/src/supabase_client.py b/src/supabase_client.py index 24d02b0..8169539 100644 --- a/src/supabase_client.py +++ b/src/supabase_client.py @@ -1,11 +1,11 @@ import datetime import os -from typing import Dict from dotenv import load_dotenv from postgrest.base_request_builder import APIResponse from supabase import Client, create_client +from src.types.training_week_with_coaching import TrainingWeekWithCoaching from src.types.user_auth_row import UserAuthRow load_dotenv() @@ -54,3 +54,27 @@ def get_user_auth(athlete_id: int) -> UserAuthRow: raise ValueError(f"Cound not find user_auth row with {athlete_id=}") return UserAuthRow(**response.data[0]) + + +def upsert_training_week_with_coaching( + athlete_id: int, + training_week_with_coaching: TrainingWeekWithCoaching, +) -> APIResponse: + """ + Upsert a row into the training_week_with_coaching table. + + :param athlete_id: The ID of the athlete. + :param training_week_with_coaching: An instance of TrainingWeekWithCoaching. + :return: APIResponse from Supabase. + """ + row_data = { + "athlete_id": athlete_id, + "training_week": training_week_with_coaching.training_week.json(), + "typical_week_training_review": training_week_with_coaching.typical_week_training_review, + "weekly_mileage_target": training_week_with_coaching.weekly_mileage_target, + } + + table = client.table("training_week_with_coaching") + response = table.upsert(row_data).execute() + + return response diff --git a/test.ipynb b/test.ipynb index eced17c..d5cc09b 100644 --- a/test.ipynb +++ b/test.ipynb @@ -12,14 +12,14 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "athlete_id='98390356' token still valid until 2024-08-10 01:33:24+00:00\n" + "athlete_id='98390356' token still valid until 2024-08-10 22:00:52+00:00\n" ] } ], @@ -30,6 +30,7 @@ "\n", "from src.activities import get_activities_df, get_day_of_week_summaries, get_weekly_summaries\n", "from src.auth_manager import get_strava_client\n", + "from src.supabase_client import upsert_training_week_with_coaching\n", "from src.training_week import generate_training_week\n", "from src.email_manager import send_email, training_week_to_html\n", "\n", @@ -46,9 +47,21 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'message_id': '<202408101610.79765560078@smtp-relay.mailin.fr>',\n", + " 'message_ids': None}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "@freeze_time(\"2024-08-04 20:00:00\")\n", "def mock_get_activities_df(strava_client):\n", @@ -60,60 +73,19 @@ "weekly_summaries = get_weekly_summaries(activities_df)\n", "\n", "client_preferences = \"A) Training for a marathon B) This will be my second marathon C) Prefer workouts on Wednesdays and long runs on Saturdays\"\n", - "training_week_with_coaching = generate_training_week(\n", + "training_week = generate_training_week(\n", " client_preferences=client_preferences,\n", " weekly_summaries=weekly_summaries,\n", " day_of_week_summaries=day_of_week_summaries\n", ")\n", + "upsert_training_week_with_coaching(\n", + " athlete_id=athlete_id, training_week_with_coaching=training_week\n", + ")\n", "\n", - "# send_email(\n", - "# subject=\"Training Schedule Just Dropped 🏃\",\n", - "# html_content=training_week_to_html(training_week_with_coaching),\n", - "# )" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'training_week': {'mon': {'session_type': 'easy run',\n", - " 'distance': 7.5,\n", - " 'notes': 'Maintain a conversational pace (9m 30s/mile). Focus on form and breathing.'},\n", - " 'tues': {'session_type': 'moderate run',\n", - " 'distance': 4.0,\n", - " 'notes': 'Run at a steady pace (9m 45s/mile). You can push slightly harder but stay comfortable.'},\n", - " 'wed': {'session_type': 'speed workout',\n", - " 'distance': 6.0,\n", - " 'notes': 'Include intervals: 8x400m at 7m 30s/mile pace with 400m jog recoveries.'},\n", - " 'thurs': {'session_type': 'rest day',\n", - " 'distance': 0.0,\n", - " 'notes': 'Take a rest day or perform light cross-training like swimming or yoga.'},\n", - " 'fri': {'session_type': 'easy run',\n", - " 'distance': 5.0,\n", - " 'notes': 'Relaxed pace (9m 50s/mile). Focus on recovery and keeping the legs loose.'},\n", - " 'sat': {'session_type': 'long run',\n", - " 'distance': 15.0,\n", - " 'notes': 'Long slow distance (10m/mile). Practice fueling and hydration strategies.'},\n", - " 'sun': {'session_type': 'moderate run',\n", - " 'distance': 4.5,\n", - " 'notes': 'Run at a comfortable pace (9m 30s/mile). Use this as a flexible recovery day.'}},\n", - " 'typical_week_training_review': 'Based on your training history, it appears that you have a balanced approach to running throughout the week, with your best days for consistent training being Monday (6 runs, 7.59 miles, 9m 34s/mile), Tuesday (7 runs, 3.32 miles, 10m 15s/mile), and Friday (6 runs, 4.67 miles, 9m 54s/mile). You seem to rest primarily on Thursdays with only one short run logged, which could serve as a rest or recovery day. Your preferred long run day is Saturday (6 runs, 10.17 miles, 9m 59s/mile), which aligns with your preference. Wednesdays show a focus on shorter runs (11 runs, 4.76 miles, 9m 20s/mile), indicating this might be an ideal day for speed work or tempo runs to further enhance your performance. Sundays have fewer longer runs (2 runs, 8.04 miles, 9m 27s/mile), which might either serve as a backup long run day or additional flexible mileage.',\n", - " 'weekly_mileage_target': \"Based on your recent training summaries, you have shown a consistent and progressive increase in your weekly mileage as well as in the distance of your longest run, which is excellent preparation for your second marathon. Your weekly mileage has increased from 28.7 miles in Week 25 to 40.19 miles in Week 31, with your longest run extending from 8.45 miles to 14.03 miles. This gradual build-up is crucial for endurance and injury prevention. Given this steady progression, I recommend a slight increase in mileage for next week. Target a weekly mileage of around 42 miles, with a long run of approximately 15 miles on Saturday. This will help you continue to build your endurance while allowing some recovery time after such consistent increases. Keep your Wednesday workouts consistent to maintain your mid-week strength and stamina. Great job, and let's keep this momentum going!\"}" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import json\n", - "\n", - "json.loads(training_week_with_coaching.json())" + "send_email(\n", + " subject=\"Training Schedule Just Dropped 🏃\",\n", + " html_content=training_week_to_html(training_week),\n", + ")\n" ] }, {