fix: optimize enrollment counts to use read replica and show all configured modes #38007
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Optimizes the enrollment data in the Instructor API v2 Course Info endpoint (
CourseInformationSerializerV2) to fix three issues:Show all configured modes: Enrollment counts now include all modes configured for the course via
CourseMode.modes_for_course(), even those with 0 enrollments. Modes not configured for the course are omitted. Previously, only modes with at least 1 enrollment appeared in the response.Reduce redundant DB queries: Replaced 3 separate database queries with a single call to the existing
CourseEnrollment.objects.enrollment_counts()manager method.Use read replica: The manager method routes queries through
use_read_replica_if_available(), reducing load on the primary database.Impacted roles: Course Author, Operator (Instructor Dashboard MFE consumers)
Files changed:
lms/djangoapps/instructor/views/serializers_v2.py— Refactoredget_enrollment_counts()andget_total_enrollment()lms/djangoapps/instructor/tests/test_api_v2.py— Updated and added enrollment count testslms/djangoapps/instructor/docs/references/instructor-v2-course-info-spec.yaml— Updated schema to document dynamic mode keysSupporting information
Other information