Skip to content

fix: filter learner search by requested usernames instead of scanning…#282

Open
Alam-2U wants to merge 1 commit into
openedx:masterfrom
Alam-2U:ealam/LP-956
Open

fix: filter learner search by requested usernames instead of scanning…#282
Alam-2U wants to merge 1 commit into
openedx:masterfrom
Alam-2U:ealam/LP-956

Conversation

@Alam-2U

@Alam-2U Alam-2U commented Jul 23, 2026

Copy link
Copy Markdown

Description

The _get_stats_for_usernames function in forum/api/users.py calls backend.get_users() with no arguments, triggering a full collection scan on the users collection for every activity_stats request.

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

  • Severe performance degradation — every Learners tab page load or search queries millions of documents instead of the 1–25 actually needed

Fix

Pass the usernames filter to backend.get_users() so the filtering happens at the database query level instead of in Python.

Ticket (Private 2U Link)

LP-956

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_usernames to call backend.get_users(username__in=usernames) instead of retrieving all users and filtering in Python.
  • Extend the MySQL backend get_users() to support a username__in filter by translating it to the Django ORM field path (user__username__in), and ensure sort_key is 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants