Skip to content

Commit

Permalink
date util for est
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Aug 20, 2024
1 parent c80bac6 commit 3e8c828
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 69 deletions.
7 changes: 4 additions & 3 deletions src/activities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import timedelta
from typing import Dict, List

import polars as pl
Expand All @@ -9,6 +9,7 @@
from src.types.activity_summary import ActivitySummary
from src.types.day_of_week_summary import DayOfWeekSummary
from src.types.week_summary import WeekSummary
from src.utils import datetime_now_est

load_dotenv()

Expand Down Expand Up @@ -84,9 +85,9 @@ def get_activities_df(strava_client: Client, num_weeks: int = 8) -> pl.DataFrame
:param num_weeks: The number of weeks to fetch activities for
:return: A cleaned and processed DataFrame of the athlete's activities
"""
timedela_x_weeks = datetime.now() - timedelta(weeks=num_weeks)
timedela_x_weeks = datetime_now_est() - timedelta(weeks=num_weeks)
activities = strava_client.get_activities(
after=timedela_x_weeks, before=datetime.now()
after=timedela_x_weeks, before=datetime_now_est()
)
raw_df = activities_to_df(activities)
return preprocess_activities_df(raw_df)
Expand Down
4 changes: 2 additions & 2 deletions src/types/mid_week_analysis.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime
from typing import List

from pydantic import BaseModel

from src.types.activity_summary import ActivitySummary
from src.types.training_week import TrainingSession
from src.utils import datetime_now_est


class MidWeekAnalysis(BaseModel):
Expand All @@ -13,7 +13,7 @@ class MidWeekAnalysis(BaseModel):

@property
def training_week_future(self):
return self.training_week[datetime.now().weekday() + 1 :]
return self.training_week[datetime_now_est().weekday() + 1 :]

@property
def miles_ran(self):
Expand Down
13 changes: 13 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from datetime import datetime

from zoneinfo import ZoneInfo


def datetime_now_est() -> datetime:
"""
Returns the current time in the specified timezone
:param zone: The timezone name (default is 'America/New_York')
:return: The current datetime in the specified timezone
"""
return datetime.now(ZoneInfo("America/New_York"))
73 changes: 9 additions & 64 deletions test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -62,28 +53,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"athlete_id='98390356' token still valid until 2024-08-19 02:00:31+00:00\n"
]
},
{
"data": {
"text/plain": [
"{'message_id': '<202408182143.60797480276@smtp-relay.mailin.fr>',\n",
" 'message_ids': None}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"@freeze_time(\"2024-08-11 20:00:00\")\n",
"def get_activities_df_wrapper(strava_client):\n",
Expand Down Expand Up @@ -125,27 +97,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"athlete_id='98390356' token still valid until 2024-08-19 02:00:31+00:00\n"
]
},
{
"data": {
"text/plain": [
"APIResponse(data=[{'id': 15, 'athlete_id': 98390356, 'activities': '[{\"date_and_time\": \"Monday, August 12, 2024 06:06 AM\", \"distance_in_miles\": 8.01, \"elevation_gain_in_feet\": 183.73, \"pace_minutes_per_mile\": 9.43}, {\"date_and_time\": \"Tuesday, August 13, 2024 06:15 AM\", \"distance_in_miles\": 4.01, \"elevation_gain_in_feet\": 52.49, \"pace_minutes_per_mile\": 10.64}]', 'training_week': '[{\"day\": \"sat\", \"session_type\": \"long run\", \"distance\": 15.0, \"notes\": \"Long run at easy pace, aiming for 15 miles to build endurance for the marathon.\"}, {\"day\": \"wed\", \"session_type\": \"speed workout\", \"distance\": 6.0, \"notes\": \"Speed workout, including intervals: 2 miles warm-up, 2x1.5 miles at 10k pace (7m 30s), 1 mile cool-down.\"}, {\"day\": \"sun\", \"session_type\": \"easy run\", \"distance\": 5.0, \"notes\": \"Recovery run at easy pace to help recover from long run.\"}, {\"day\": \"mon\", \"session_type\": \"moderate run\", \"distance\": 5.0, \"notes\": \"Moderate pace run, slightly faster than easy pace.\"}, {\"day\": \"tues\", \"session_type\": \"easy run\", \"distance\": 4.0, \"notes\": \"Easy pace run, relaxed effort.\"}, {\"day\": \"fri\", \"session_type\": \"easy run\", \"distance\": 4.0, \"notes\": \"Easy pace run, relaxed effort to stay fresh for the long run.\"}, {\"day\": \"thurs\", \"session_type\": \"rest day\", \"distance\": 0.0, \"notes\": \"Rest day or cross-training, focusing on recovery.\"}]', 'planning': \"The client has already completed 12.02 miles running on Monday and Tuesday. The weekly target is 39.0 miles, so they have 26.98 miles left to run. We must distribute this mileage over the remaining days of the week, keeping their preference for speed workouts on Wednesdays and long runs on Saturdays. Here's a step-by-step plan: Wednesday: As a speed workout day, plan a moderate distance speed session of 5 miles. Thursday: Rest day to recover from the speed workout. Friday: An easy run of 5 miles to keep the legs moving. Saturday: Long run day, where we should plan a long run of 10 miles. Sunday: Another easy run to make up the remainder, 6.98 miles. Let's ensure to spread these runs keeping in mind the training needs and recovery, adjusting if needed.\", 'training_week_update': '[{\"day\": \"wed\", \"session_type\": \"speed workout\", \"distance\": 5.0, \"notes\": \"Tempo run at 7m 30s pace\"}, {\"day\": \"thurs\", \"session_type\": \"rest day\", \"distance\": 0.0, \"notes\": \"Full recovery\"}, {\"day\": \"fri\", \"session_type\": \"easy run\", \"distance\": 5.0, \"notes\": \"Easy pace\"}, {\"day\": \"sat\", \"session_type\": \"long run\", \"distance\": 10.0, \"notes\": \"Steady pace, focus on endurance\"}, {\"day\": \"sun\", \"session_type\": \"easy run\", \"distance\": 6.98, \"notes\": \"Easy pace, enjoy the run\"}]', 'created_at': '2024-08-18T22:24:05.552442+00:00'}], count=None)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"@freeze_time(\"2024-08-13 20:00:00\")\n",
"def mock_get_activity_summaries(strava_client, num_weeks=8):\n",
Expand Down Expand Up @@ -185,18 +139,9 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"athlete_id='98390356' token still valid until 2024-08-19 02:00:31+00:00\n",
"athlete_id='98390356' token still valid until 2024-08-19 02:00:31+00:00\n"
]
}
],
"outputs": [],
"source": [
"from src.lambda_function import lambda_handler\n",
"\n",
Expand Down

0 comments on commit 3e8c828

Please sign in to comment.