Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rustybrooks committed Oct 12, 2024
1 parent fa25778 commit ff36966
Show file tree
Hide file tree
Showing 11 changed files with 607 additions and 138 deletions.
2 changes: 1 addition & 1 deletion src/api/bikes/management/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def handle(self, *args, **options):
logger.exception("Failed to retrieve activities")

took = time.time() - t1
diff = (60 * 5) - took
diff = (60 * 10) - took
print(f"sleeping {diff:0.1f} seconds (took {took:0.1f} seconds)")
time.sleep(diff)
3 changes: 0 additions & 3 deletions src/api/bikes/models/strava_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def sync_one(cls, user, activity, full=False, rebuild=False):
from bikes.models import (
StravaActivitySegmentEffort,
StravaActivityStream,
StravaSegmentHistory,
StravaSpeedCurve, # type: ignore
)

Expand All @@ -117,14 +116,12 @@ def sync_one(cls, user, activity, full=False, rebuild=False):
f"Syncing strava activity id={activity['id']} start={activity['start_date']}"
)

new = False
if len(actlist):
act = actlist[0]
else:
act = StravaActivity()
act.activity_id = activity_id
act.user = user
new = True

for key in [
"external_id",
Expand Down
16 changes: 0 additions & 16 deletions src/api/bikes/models/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from bikes import plans

# from bikes import plans # type: ignore

logger = logging.getLogger(__name__)

tpmap = {
Expand All @@ -29,20 +27,6 @@ def tp_from_season(s):


class TrainingWeek(models.Model):
# WEEK_TYPE_CHOICES = (
# ('Prep', 'Prep'),
# ('Base 1', 'Base 1'),
# ('Base 2', 'Base 2'),
# ('Base 3', 'Base 3'),
# ('Build 1', 'Build 1'),
# ('Build 2', 'Build 2'),
# ('Peak', 'Peak'),
# ('RaceSat', 'Race Saturday'),
# ('RaceSun', 'Race Sunday'),
# ('RaceSatSun', 'Race Saturday and Sunday'),
# ('Transition', 'Transition'),
# )

week_start_date = models.DateField()
season = models.ForeignKey(
"Season",
Expand Down
40 changes: 27 additions & 13 deletions src/api/bikes/views/seasons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import logging

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import serializers # type: ignore
from rest_framework.decorators import action
Expand Down Expand Up @@ -33,10 +34,12 @@ class Meta:
exclude: list[str] = []
depth = 2

workout_types = serializers.DictField()


class TrainingBiblePreviewOut(serializers.Serializer):
entries: list[TrainingEntryOut]
hour_selection: list[int]
entries = serializers.ListField(child=TrainingEntryOut())
hour_selection = serializers.ListField(child=serializers.IntegerField())


class SeasonViewSet(ModelViewSet):
Expand All @@ -46,37 +49,47 @@ class SeasonViewSet(ModelViewSet):

@swagger_auto_schema(
request_body=TrainingBibleV1In,
responses={200: TrainingBiblePreviewOut(many=False)},
responses={200: openapi.Response("", TrainingBiblePreviewOut(many=False))},
)
@action(detail=False, methods=["post"])
def preview_training_bible_v1(self, request: Request):
ctb = CTBv1({"annual_hours": request.data["annual_hours"]})
progression = ctb.progression()

weeks: list[TrainingWeek] = []
start_date = datetime.date.fromisoformat(request.data["season_start_date"])
start_date = (
datetime.date.fromisoformat(request.data["season_start_date"])
if request.data["season_start_date"]
else None
)
end_date = (
datetime.date.fromisoformat(request.data["season_end_date"])
if request.data["season_end_date"]
else None
)
logger.info("start=%r end=%r", start_date, end_date)

season = Season(
season_start_date=start_date,
training_plan="CTB",
params={"annual_hours": 200},
params={"annual_hours": request.data["annual_hours"]},
)

entries = []
this_day = start_date
this_day = start_date or datetime.date.today()
prog_index = 0
prog_ct = 1
week_prog = progression[prog_index]
while prog_index < len(progression) - 1:
while start_date:
if prog_ct > week_prog[1]:
prog_ct = 1
if prog_index < len(progression) - 1:
prog_index += 1
prog_index += 1

if prog_index >= len(progression):
break

if end_date and this_day > end_date:
break

week_prog = progression[prog_index]

Expand All @@ -94,13 +107,14 @@ def preview_training_bible_v1(self, request: Request):
this_day = next_day
prog_ct += 1

for to in entries:
to.workout_types = {
wt: ctb.workout_description(wt) for wt in to.workout_type_list()
}

training_serialized = TrainingEntryOut(data=entries, many=True)
training_serialized.is_valid()
training_entries_data = training_serialized.data
for t, to in zip(training_entries_data, entries):
t["workout_types"] = [
{wt: ctb.workout_description(wt)} for wt in to.workout_type_list()
]

data = {
"entries": training_entries_data,
Expand Down
Loading

0 comments on commit ff36966

Please sign in to comment.