fix: batch-fetch lab progress to eliminate N+1 queries in simulation dashboard#5675
fix: batch-fetch lab progress to eliminate N+1 queries in simulation dashboard#5675yashhzd wants to merge 1 commit intoOWASP-BLT:mainfrom
Conversation
…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).
|
👋 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:
Once a valid peer review is submitted, this check will pass automatically. Thank you! |
📊 Monthly LeaderboardHi @yashhzd! Here's how you rank for February 2026:
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. |
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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)
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. Comment |
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