fix: optimize enrollment counts to use read replica and show all configured modes#38007
Conversation
|
Thanks for the pull request, @brianjbuck-wgu! 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. |
… existing enrollment fields.
94d67b7 to
c2f2a1d
Compare
There was a problem hiding this comment.
Pull request overview
This PR optimizes enrollment data queries in the Instructor API v2 Course Info endpoint to reduce database load and show all configured course modes. The changes address three issues: showing modes with zero enrollments, reducing redundant queries, and using read replicas. The PR also adds new fields to distinguish between learner and staff enrollment counts.
Changes:
- Refactored enrollment count methods to use the existing
CourseEnrollment.objects.enrollment_counts()manager method - Added
learner_countandstaff_countfields to separate non-staff enrollments from staff/admin enrollments - Updated enrollment_counts to include all configured course modes, even those with 0 enrollments
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lms/djangoapps/instructor/views/serializers_v2.py | Refactored enrollment count methods and added learner/staff count fields; removed Count import |
| lms/djangoapps/instructor/tests/test_api_v2.py | Added tests for new learner/staff counts and for configured vs unconfigured modes; updated existing enrollment count tests |
| lms/djangoapps/instructor/docs/references/instructor-v2-course-info-spec.yaml | Updated API schema to document new fields and dynamic enrollment_counts structure |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
tested on local with this frontend PR openedx/frontend-app-instruct#54 looks good 👍 |

Description
Optimizes the enrollment data in the Instructor API v2 Course Info endpoint (
CourseInformationSerializerV2) to fix several issues: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.CourseEnrollment.objects.enrollment_counts()manager method.use_read_replica_if_available(), reducing load on the primary database.learner_count(enrollments excluding staff and admins) andstaff_count(total minus learner count) to support the enrollment summary UI.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