fix: filter learner search by requested usernames instead of scanning…#282
Open
Alam-2U wants to merge 1 commit into
Open
fix: filter learner search by requested usernames instead of scanning…#282Alam-2U wants to merge 1 commit into
Alam-2U wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes the learner stats lookup path by pushing username filtering into the backend query, avoiding fetching all users and then filtering in Python. This aligns with the “Learners tab / activity_stats” performance concern described in the PR.
Changes:
- Update
_get_stats_for_usernamesto callbackend.get_users(username__in=usernames)instead of retrieving all users and filtering in Python. - Extend the MySQL backend
get_users()to support ausername__infilter by translating it to the Django ORM field path (user__username__in), and ensuresort_keyis not accidentally passed to.filter().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| forum/backends/mysql/api.py | Adds support for username__in (mapped to user__username__in) and safely separates sort_key from ORM filters. |
| forum/api/users.py | Stops full user scans for username-filtered stats by querying only the requested usernames via the backend. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The
_get_stats_for_usernamesfunction inforum/api/users.pycallsbackend.get_users()with no arguments, triggering a full collection scan on theuserscollection for everyactivity_statsrequest.This means viewing the Learners tab for a single course — or searching for a single learner — causes the database to iterate over millions of user documents across all courses, only to discard them via Python-side filtering.
Impact
Fix
Pass the
usernamesfilter tobackend.get_users()so the filtering happens at the database query level instead of in Python.Ticket (Private 2U Link)
LP-956