fix: prefetch video_image when getting a video#596
Conversation
|
Thanks for the pull request, @viadanna! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
kaustavb12
left a comment
There was a problem hiding this comment.
LGTM 👍
- I tested this: Tested as described
- I read through the code
ebc55f9 to
af5ddba
Compare
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR reduces query count when fetching videos by preloading related EncodedVideo.profile and CourseVideo.video_image data.
Changes:
- Prefetch
coursesusing aCourseVideoqueryset thatselect_related("video_image")in_get_video. - In
_get_videos_for_filter, add prefetching forcourses(withvideo_image) andencoded_videos(withprofile) before filtering.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| video_image = CourseVideo.objects.select_related("video_image") | ||
| encoded_videos = EncodedVideo.objects.select_related("profile") | ||
| videos = Video.objects \ | ||
| .prefetch_related(Prefetch("courses", queryset=video_image)) \ |
| encoded_videos = EncodedVideo.objects.select_related("profile") | ||
| course_videos = CourseVideo.objects.select_related("video_image") | ||
| return Video.objects \ | ||
| .prefetch_related(Prefetch("encoded_videos", queryset=encoded_videos)) \ | ||
| .prefetch_related("courses") \ | ||
| .prefetch_related(Prefetch("courses", queryset=course_videos)) \ | ||
| .get(edx_video_id=edx_video_id) |
| videos = Video.objects \ | ||
| .prefetch_related(Prefetch("courses", queryset=video_image)) \ | ||
| .prefetch_related(Prefetch("encoded_videos", queryset=encoded_videos)) \ | ||
| .filter(**video_filter) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #596 +/- ##
==========================================
+ Coverage 94.48% 94.79% +0.31%
==========================================
Files 31 33 +2
Lines 3099 3361 +262
Branches 173 129 -44
==========================================
+ Hits 2928 3186 +258
- Misses 152 157 +5
+ Partials 19 18 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Description
This pull request updates the way related course and image data are fetched for videos in the
edxval/api.pymodule, optimizing the database query for retrieving a Video by including video image data.This prevents the number of database queries during video serialization from increasing with the number of CourseVideo objects pointing to the Video object.
Testing instructions