Skip to content

feat: video metadata loading optimization#31

Merged
iloveagent57 merged 1 commit intomasterfrom
aed/moar-batching
Jan 27, 2026
Merged

feat: video metadata loading optimization#31
iloveagent57 merged 1 commit intomasterfrom
aed/moar-batching

Conversation

@iloveagent57
Copy link
Member

also adds more function_trace()s

Post-review

  • Squash commits into discrete sets of changes
  • Ensure that once the changes have been deployed to stage, prod is manually deployed

@iloveagent57 iloveagent57 requested review from a team as code owners January 27, 2026 17:33
Copilot AI review requested due to automatic review settings January 27, 2026 17:33
Comment on lines -1150 to +1163
videos = Video.objects.filter(
parent_content_metadata__content_key__in=course_run_content_keys
).select_related('parent_content_metadata')
with function_trace(AlgoliaTraceNames.VIDEO_RECORD_LOADING):
video_queryset = Video.objects.filter(
parent_content_metadata__content_key__in=course_run_content_keys
).select_related('parent_content_metadata')

all_videos = list(video_queryset)
video_ids_by_parent_content_key = defaultdict(list)
for video in all_videos:
parent_content_key = video.parent_content_metadata.content_key
video_ids_by_parent_content_key[parent_content_key].append(video.edx_video_id)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main change. instead of iterating (below) over videos every time we need them, we'll do it once, stash in a list, and then create a pre-optimized cache of parent content keys mapped to lists of the related edx_video_ids.

if metadata.content_type == COURSE_RUN:
related_video_ids = video_ids_by_parent_content_key.get(metadata.content_key, [])
video_ids_by_key[content_key].update(related_video_ids)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to a set update() to include all items from a list.

num_content_metadata_indexed += 1

for video in videos:
for video in all_videos:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the pre-populated list

also adds more function_trace()s
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes video metadata loading during Algolia indexing by pre-processing videos into a lookup dictionary, eliminating an expensive nested loop iteration. Additionally, test fixture dates are updated to 2056 to avoid issues with dates being too close to the current date.

Changes:

  • Optimized video loading to avoid O(N*M) nested loop by pre-loading videos and creating a lookup dictionary
  • Added function_trace decorators for enhanced monitoring of video loading and other operations
  • Updated test fixture dates from 2026 to 2056 to ensure tests remain valid long-term

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
enterprise_catalog/apps/catalog/tests/factories.py Updated test fixture dates from 2026 to 2056 for enroll_by_date, upgrade_deadline, and end dates
enterprise_catalog/apps/catalog/constants.py Added VIDEO_RECORD_LOADING and REPLACE_ALL_OBJECTS trace name constants to AlgoliaTraceNames enum
enterprise_catalog/apps/api/tests/test_tasks.py Updated test expectations for new dates with correctly calculated epoch timestamps
enterprise_catalog/apps/api/tasks.py Implemented video loading optimization by pre-loading videos into a dictionary; added function_trace decorators for monitoring

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@iloveagent57 iloveagent57 merged commit 2b09b82 into master Jan 27, 2026
6 checks passed
@iloveagent57 iloveagent57 deleted the aed/moar-batching branch January 27, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants