Skip to content

Backend: Optimize leaderboard query with two-stage fetch to reduce payload#5087

Open
akanshajain231999 wants to merge 4 commits into
Cloud-CV:masterfrom
akanshajain231999:sentry_fix4
Open

Backend: Optimize leaderboard query with two-stage fetch to reduce payload#5087
akanshajain231999 wants to merge 4 commits into
Cloud-CV:masterfrom
akanshajain231999:sentry_fix4

Conversation

@akanshajain231999
Copy link
Copy Markdown
Contributor

Summary

  • Refactor calculate_distinct_sorted_leaderboard_data in apps/jobs/utils.py into a two-stage query to avoid transferring heavy JSON columns (result, error, submission_metadata) for rows discarded by per-team deduplication.
  • Stage 1 fetches a lightweight projection (id + dedup/sort fields) and runs the sort + per-team dedup in Python.
  • Stage 2 re-fetches full row data (including JSON columns and the optional submission__execution_time annotation) only for the retained IDs, preserving stage-1 order.
  • Update tests/unit/jobs/test_utils.py to include id on test rows and extend the mock query chain for the stage-2 fetch.

Test plan

  • pytest tests/unit/jobs/test_utils.py passes locally
  • Manually hit the leaderboard endpoint on a populated challenge and confirm row contents (scores, error, metadata, execution time when enabled) are unchanged
  • Confirm Sentry query duration drops on the previously slow challenge phase split

🤖 Generated with Claude Code

akanshajain231999 and others added 2 commits May 19, 2026 23:58
Refactor calculate_distinct_sorted_leaderboard_data to avoid transferring
heavy JSON columns (result, error, submission_metadata) for rows that get
discarded by per-team deduplication. Stage 1 fetches a lightweight
projection used for sort + dedup; stage 2 re-fetches full rows only for
the retained IDs.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- Reformat unique_team_ids set comprehension to satisfy black.
- Add "id" key to mocked test_data in five TestReorderSubmissionsComparator
  cases so stage-2 fetch (filter id__in=...) does not KeyError.
- Switch test_multiple_banned_participants_in_team to use
  _create_mock_leaderboard_chain helper for the stage-2 chain.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 88.46154% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.69%. Comparing base (7d19d31) to head (9e33e6c).

Files with missing lines Patch % Lines
apps/jobs/utils.py 88.46% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5087      +/-   ##
==========================================
- Coverage   91.72%   91.69%   -0.03%     
==========================================
  Files          96       96              
  Lines        8155     8168      +13     
==========================================
+ Hits         7480     7490      +10     
- Misses        675      678       +3     
Flag Coverage Δ
backend 95.16% <88.46%> (-0.04%) ⬇️
frontend 87.41% <ø> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Accounts & Authentication 97.31% <ø> (ø)
Challenges Management 95.52% <ø> (ø)
Job Processing 89.71% <88.46%> (-0.09%) ⬇️
Participants & Teams 100.00% <ø> (ø)
Challenge Hosts 100.00% <ø> (ø)
Analytics 100.00% <ø> (ø)
Web Interface 100.00% <ø> (ø)
Frontend (Gulp) 87.41% <ø> (-0.03%) ⬇️
All Models 97.80% <ø> (ø)
All Views 100.00% <ø> (ø)
All Serializers 98.60% <ø> (ø)
Utility Functions 96.57% <88.46%> (-0.10%) ⬇️
Core Configuration 82.35% <ø> (ø)
Files with missing lines Coverage Δ
apps/jobs/utils.py 93.20% <88.46%> (-0.48%) ⬇️

... and 1 file with indirect coverage changes

Files with missing lines Coverage Δ
apps/jobs/utils.py 93.20% <88.46%> (-0.48%) ⬇️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7d19d31...9e33e6c. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant