@@ -262,71 +262,31 @@ def get_workouts(
262
262
bookings = self .otf .bookings .get_bookings_new (
263
263
start_dtme , end_dtme , exclude_cancelled = True , remove_duplicates = True
264
264
)
265
- bookings_dict = self ._filter_bookings_for_workouts (bookings )
265
+ filtered_bookings = [b for b in bookings if not (b .starts_at and b .starts_at > pendulum .now ().naive ())]
266
+ bookings_list = [(b , b .workout .id if b .workout else None ) for b in filtered_bookings ]
266
267
267
- perf_summaries_dict = self .client .get_perf_summaries_threaded (list (bookings_dict .keys ()))
268
+ workout_ids = [b .workout .id for b in filtered_bookings if b .workout ]
269
+ perf_summaries_dict = self .client .get_perf_summaries_threaded (workout_ids )
268
270
telemetry_dict = self .client .get_telemetry_threaded (list (perf_summaries_dict .keys ()), max_data_points )
269
271
perf_summary_to_class_uuid_map = self .client .get_perf_summary_to_class_uuid_mapping ()
270
272
271
273
workouts : list [models .Workout ] = []
272
- for perf_id , perf_summary in perf_summaries_dict . items () :
274
+ for booking , perf_summary_id in bookings_list :
273
275
try :
276
+ perf_summary = perf_summaries_dict .get (perf_summary_id , {}) if perf_summary_id else {}
277
+ telemetry = telemetry_dict .get (perf_summary_id , None ) if perf_summary_id else None
278
+ class_uuid = perf_summary_to_class_uuid_map .get (perf_summary_id , None ) if perf_summary_id else None
274
279
workout = models .Workout .create (
275
- ** perf_summary ,
276
- v2_booking = bookings_dict [perf_id ],
277
- telemetry = telemetry_dict .get (perf_id ),
278
- class_uuid = perf_summary_to_class_uuid_map .get (perf_id ),
279
- api = self .otf ,
280
+ ** perf_summary , v2_booking = booking , telemetry = telemetry , class_uuid = class_uuid , api = self .otf
280
281
)
281
282
workouts .append (workout )
282
283
except ValueError :
283
- LOGGER .exception ("Failed to create Workout for performance summary %s" , perf_id )
284
+ LOGGER .exception ("Failed to create Workout for performance summary %s" , perf_summary_id )
284
285
285
286
LOGGER .debug ("Returning %d workouts" , len (workouts ))
286
287
287
288
return workouts
288
289
289
- def _filter_bookings_for_workouts (self , bookings : list [models .BookingV2 ]) -> dict [str , models .BookingV2 ]:
290
- """Filter bookings to only those that have a workout and are not in the future.
291
-
292
- This is being pulled out of `get_workouts` to add more robust logging and error handling.
293
-
294
- Args:
295
- bookings (list[BookingV2]): The list of bookings to filter.
296
-
297
- Returns:
298
- dict[str, BookingV2]: A dictionary mapping workout IDs to bookings that have workouts.
299
- """
300
- future_bookings = [b for b in bookings if b .starts_at and b .starts_at > pendulum .now ().naive ()]
301
- missing_workouts = [b for b in bookings if not b .workout and b not in future_bookings ]
302
- LOGGER .debug ("Found %d future bookings and %d missing workouts" , len (future_bookings ), len (missing_workouts ))
303
-
304
- if future_bookings :
305
- for booking in future_bookings :
306
- LOGGER .warning (
307
- "Booking %s for class '%s' (class_uuid=%s) is in the future, filtering out." ,
308
- booking .booking_id ,
309
- booking .otf_class ,
310
- booking .class_uuid or "Unknown" ,
311
- )
312
-
313
- if missing_workouts :
314
- for booking in missing_workouts :
315
- LOGGER .warning (
316
- "Booking %s for class '%s' (class_uuid=%s) is missing a workout, filtering out." ,
317
- booking .booking_id ,
318
- booking .otf_class ,
319
- booking .class_uuid or "Unknown" ,
320
- )
321
-
322
- bookings_dict = {
323
- b .workout .id : b for b in bookings if b .workout and b not in future_bookings and b not in missing_workouts
324
- }
325
-
326
- LOGGER .debug ("Filtered bookings to %d valid bookings for workouts mapping" , len (bookings_dict ))
327
-
328
- return bookings_dict
329
-
330
290
def get_lifetime_workouts (self ) -> list [models .Workout ]:
331
291
"""Get the member's lifetime workouts.
332
292
0 commit comments