⚡ Bolt: Optimize Lending Service N+1 queries with aggregation#40
⚡ Bolt: Optimize Lending Service N+1 queries with aggregation#40
Conversation
This commit optimizes several methods in \`LendingService\` (\`get_active_lendings\`, \`get_recent_consumable_usage\`, \`get_worker_consumable_history\`, and \`get_tool_lending_history\`) by replacing loop-based \`find_one\` calls with MongoDB aggregation pipelines using \`\$lookup\`. Additionally, the \`manual_lending\` route in \`app/routes/admin/system.py\` has been refactored to use these optimized service methods, eliminating manual N+1 query loops in the GET handler. Performance Impact: - Reduces database roundtrips for \`get_active_lendings\` from 101 to 1 for 50 records (~99% reduction). - Significantly improves page load time for the Manual Lending admin view. - Centralizes data enrichment logic in the service layer. Co-authored-by: Woschj <81321922+Woschj@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
⚡ Bolt: Optimized Lending Service N+1 queries
This PR addresses a significant performance bottleneck in the database access layer, specifically targeting N+1 query patterns in the `LendingService` and the Manual Lending admin route.
💡 What:
🎯 Why:$1 + 2N$ queries. For a typical list of 50 lendings, this resulted in over 100 database roundtrips, causing noticeable latency in the admin dashboard.
The previous implementation performed individual database lookups for every tool and worker in a list of lendings, leading to
📊 Impact:
🔬 Measurement:
Verified using a benchmark script that counts database calls before and after the optimization.
Tests were run to ensure functional parity and no regressions were introduced.
PR created automatically by Jules for task 10619731348114346618 started by @Woschj