⚡ Bolt: optimize badge evaluation with EvaluationCache - #123
⚡ Bolt: optimize badge evaluation with EvaluationCache#123projectamazonph wants to merge 3 commits into
Conversation
Optimize the badge evaluation logic by instantiating a transient, request-scoped EvaluationCache that holds and shares database query promises (for lesson completed counts, tool session counts, and user profile details). This collapses duplicate database queries from O(R) queries to O(1) during a single badge evaluation cycle. Added a targeted unit test to verify query deduplication. Co-authored-by: projectamazonph <286085559+projectamazonph@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. |
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 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 |
…anager pinning - Introduce short-lived request-scoped `EvaluationCache` to cache and share database query promises across badge criteria evaluations during a single `evaluateBadges` call. This collapses the duplicate queries from O(R) queries to O(1) per evaluation. - Upgrade and correct `packageManager` in `package.json` to `pnpm@11.14.0` because `pnpm@11.13.0` is a broken release without a binary, causing CI setup to fail. - Added a targeted unit test to verify query deduplication during badge evaluation. All 213 unit tests pass perfectly. Co-authored-by: projectamazonph <286085559+projectamazonph@users.noreply.github.com>
… / packageManager pinning - Introduce short-lived request-scoped `EvaluationCache` to cache and share database query promises across badge criteria evaluations during a single `evaluateBadges` call. This collapses duplicate queries from O(R) queries to O(1) per evaluation. - Upgrade `packageManager` in `package.json` to stable `pnpm@11.14.0` because `pnpm@11.13.0` is a broken release without a binary, causing CI setup to fail. - Added comprehensive unit tests for `src/lib/rate-limit.ts` in `src/lib/__tests__/rate-limit.test.ts` to push overall branch coverage above the global 70% quality gate threshold (successfully achieved 71.05%). - Added unit tests for `EvaluationCache` in `src/lib/__tests__/badges.test.ts` to verify query promise sharing and deduplication. All 218 tests are fully passing. Co-authored-by: projectamazonph <286085559+projectamazonph@users.noreply.github.com>
What
We have introduced a request-scoped
EvaluationCachetosrc/lib/badges.tsto hold and share database query promises (completed lessons, tool sessions, user details) across all badge criteria checks during a singleevaluateBadgescall.Why
During badge evaluation, we iterate over all published badges and check their criteria. Evaluating these criteria independently triggered redundant database queries for the same metrics and user profiles (e.g., querying streakDays or completed lessons for multiple badges). This led to O(R) database roundtrips, where R is the number of badges.
Impact
Collapses database queries from O(R) roundtrips down to O(1) per evaluation lifecycle, leading to significantly faster response times and lower database load.
Measurement
Added a targeted unit test
collapses redundant database queries during a single evaluateBadges run using EvaluationCacheinsrc/lib/__tests__/badges.test.tswhich verifies thatdb.user.findUniqueis called exactly once despite multiple criteria checking user-specific properties during the evaluation. All 213 unit tests are fully passing.PR created automatically by Jules for task 3719912686428013341 started by @projectamazonph