Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

fix: batch-fetch lab progress to eliminate N+1 queries in simulation dashboard#5675

Open
yashhzd wants to merge 1 commit intoOWASP-BLT:mainfrom
yashhzd:fix/simulation-n-plus-1-lab-progress
Open

fix: batch-fetch lab progress to eliminate N+1 queries in simulation dashboard#5675
yashhzd wants to merge 1 commit intoOWASP-BLT:mainfrom
yashhzd:fix/simulation-n-plus-1-lab-progress

Conversation

@yashhzd
Copy link
Contributor

@yashhzd yashhzd commented Feb 12, 2026

Summary\n\nEliminate N+1 database queries in the simulation dashboard by batch-fetching all user lab progress records in a single query.\n\n## Changes\n\n**website/views/Simulation.py:\n\nBefore** (N+1 queries):\npython\nfor lab in labs:\n try:\n user_lab_progress = UserLabProgress.objects.get(user=request.user, lab=lab) # 1 query per lab\n progress = user_lab_progress.calculate_progress_percentage()\n except UserLabProgress.DoesNotExist:\n progress = 0\n\n\nAfter (2 queries total):\npython\n# Single query for all user progress\nuser_progress_map = {\n ulp.lab_id: ulp\n for ulp in UserLabProgress.objects.filter(user=request.user, lab__in=labs)\n}\n\nfor lab in labs:\n user_lab_progress = user_progress_map.get(lab.id) # O(1) dict lookup\n progress = user_lab_progress.calculate_progress_percentage() if user_lab_progress else 0\n\n\nThis reduces database queries from N+1 to 2 (one for labs, one for all progress records).\n\n## Test plan\n\n- [ ] Verify simulation dashboard loads correctly and shows progress for each lab\n- [ ] Verify labs without user progress show 0% progress\n- [ ] Verify labs with user progress show correct percentage

…dashboard

Replace per-lab UserLabProgress.objects.get() inside loop with a single
batch query using filter(lab__in=labs). Build a dictionary keyed by
lab_id for O(1) lookup. This reduces N+1 database queries to 2 queries
(one for labs, one for all user progress records).
@github-actions
Copy link
Contributor

👋 Hi @yashhzd!

This pull request needs a peer review before it can be merged. Please request a review from a team member who is not:

  • The PR author
  • DonnieBLT
  • coderabbitai
  • copilot

Once a valid peer review is submitted, this check will pass automatically. Thank you!

@github-actions github-actions bot added the needs-peer-review PR needs peer review label Feb 12, 2026
@github-actions
Copy link
Contributor

📊 Monthly Leaderboard

Hi @yashhzd! Here's how you rank for February 2026:

Rank User Open PRs PRs (merged) PRs (closed) Reviews Comments CR chats Total
#6 @Nachiket-Roy 7 3 1 2 12 31 69
#7 @yashhzd 29 2 0 0 7 0 63
#8 @Sumit6307 2 0 2 0 17 2 32

Scoring this month: Open PRs (+1 each), Merged PRs (+10), Closed (not merged) (−2), Reviews (+5; first two per PR in-month), Comments (+2, excludes CR). Coderabbit chats column is visible. Points per chat: 0; daily cap per user (UTC): 7.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 12, 2026

Warning

Rate limit exceeded

@yashhzd has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 4 minutes and 55 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added pre-commit: passed Pre-commit checks passed tests: P:324 F:0 S:0 Django tests passed last-active: 0d PR last updated 0 days ago labels Feb 12, 2026
Copy link
Contributor

@sidd190 sidd190 left a comment

Choose a reason for hiding this comment

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

Looks like an issue in scaling. Good catch, but again can be consolidated in one or a few PRs.

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

Labels

files-changed: 1 last-active: 0d PR last updated 0 days ago needs-peer-review PR needs peer review pre-commit: passed Pre-commit checks passed tests: P:324 F:0 S:0 Django tests passed

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants