Skip to content

Commit

Permalink
perf: reduce ORM queries needed for video quality info
Browse files Browse the repository at this point in the history
The CourseQualityView used to call edx-val's get_video_for_course(),
which would return a fully serialized data structure that included all
encodings and inefficiently serialized them with many n+1 queries. This
is tolerable in a paginated web view, but not when pulling all of a
large courses's videos at once.

Making this change collapsed the number of queries for a large sample
MIT course from over 3000 down to 1.
  • Loading branch information
ormsbee committed Jan 15, 2025
1 parent 4a7d991 commit b7cf910
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cms/djangoapps/contentstore/api/views/course_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time

import numpy as np
from edxval.api import get_videos_for_course
from edxval.api import get_course_videos_qset
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from scipy import stats
Expand Down Expand Up @@ -180,13 +180,11 @@ def _units_quality(self, course, request): # lint-amnesty, pylint: disable=miss

def _videos_quality(self, course): # lint-amnesty, pylint: disable=missing-function-docstring
video_blocks_in_course = modulestore().get_items(course.id, qualifiers={'category': 'video'})
videos, __ = get_videos_for_course(course.id)
videos_in_val = list(videos)
video_durations = [video['duration'] for video in videos_in_val]
video_durations = [cv.video.duration for cv in get_course_videos_qset(course.id)]

return dict(
total_number=len(video_blocks_in_course),
num_mobile_encoded=len(videos_in_val),
num_mobile_encoded=len(video_durations),
num_with_val_id=len([v for v in video_blocks_in_course if v.edx_video_id]),
durations=self._stats_dict(video_durations),
)
Expand Down

0 comments on commit b7cf910

Please sign in to comment.