Skip to content

⚡ Bolt: [performance improvement] Optimize grievance query to use load_only#694

Open
RohanExploit wants to merge 2 commits intomainfrom
jules-4290250909438725555-dcad3437
Open

⚡ Bolt: [performance improvement] Optimize grievance query to use load_only#694
RohanExploit wants to merge 2 commits intomainfrom
jules-4290250909438725555-dcad3437

Conversation

@RohanExploit
Copy link
Copy Markdown
Owner

@RohanExploit RohanExploit commented Apr 21, 2026

💡 What: Replaced full ORM loading of the Grievance model with .options(load_only(Grievance.id, Grievance.category)) in the daily civic intelligence job.
🎯 Why: Selecting full SQLAlchemy model instances is slower and uses more memory, especially as the number of upgrades scales up, and only the id and category fields were needed.
📊 Impact: Reduces memory usage and database I/O for fetching associated grievances by avoiding loading unnecessary fields like action_plan and description while maintaining the ORM object structure.
🔬 Measurement: Verified that all 107 tests pass in the backend suite and the logic correctly interacts with the object properties.


PR created automatically by Jules for task 4290250909438725555 started by @RohanExploit

Summary by CodeRabbit

Release Notes

  • Refactor

    • Optimized the grievance data loading process to retrieve only essential information instead of fetching all available fields, reducing memory overhead and improving overall performance during the daily processing cycle.
  • Tests

    • Updated test cases to accurately reflect the changes to the grievance query logic and ensure proper coverage.

Summary by cubic

Optimized the grievance lookup in the daily civic intelligence job by using SQLAlchemy load_only to fetch only id and category. This reduces DB I/O and memory; tests now mock the .options(...).filter().all() chain to cover the new query path.

Written for commit fbe885f. Summary will update on new commits.

@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings April 21, 2026 16:28
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 21, 2026

Deploy Preview for fixmybharat canceled.

Name Link
🔨 Latest commit fbe885f
🔍 Latest deploy log https://app.netlify.com/projects/fixmybharat/deploys/69e7d54bde5fe400088d8d2e

@github-actions
Copy link
Copy Markdown

🙏 Thank you for your contribution, @RohanExploit!

PR Details:

Quality Checklist:
Please ensure your PR meets the following criteria:

  • Code follows the project's style guidelines
  • Self-review of code completed
  • Code is commented where necessary
  • Documentation updated (if applicable)
  • No new warnings generated
  • Tests added/updated (if applicable)
  • All tests passing locally
  • No breaking changes to existing functionality

Review Process:

  1. Automated checks will run on your code
  2. A maintainer will review your changes
  3. Address any requested changes promptly
  4. Once approved, your PR will be merged! 🎉

Note: The maintainers will monitor code quality and ensure the overall project flow isn't broken.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

📝 Walkthrough

Walkthrough

SQLAlchemy ORM queries in the daily grievance-loading cycle now use .options(load_only(...)) to fetch only specific columns, reducing data transfer. Corresponding test mocks were updated to handle the extended query chain, and documentation was added to .jules/bolt.md noting this pattern.

Changes

Cohort / File(s) Summary
Documentation
.jules/bolt.md
Added entry documenting the requirement to update test mocks when .options() is introduced into SQLAlchemy query chains.
Implementation
backend/civic_intelligence.py
Modified Grievance query to use .options(load_only(...)) for fetching only id and category columns in the daily cycle's grievance-loading step.
Tests
backend/tests/test_civic_intelligence.py
Extended mock configuration to handle the .options(...).filter(...).all() query chain with appropriate return values.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Suggested labels

size/s

Poem

🐰 A query hops lighter today,
With columns curated the SQLAlchemy way,
Mock chains now extended with care,
Less data to fetch through the air!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: optimizing a grievance query to use load_only, which is the core performance improvement made across the codebase.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description is comprehensive and well-structured, covering what was changed, why it was changed, the impact, and verification steps.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jules-4290250909438725555-dcad3437

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Optimizes the daily civic intelligence job’s grievance fetch by loading only the ORM fields that are actually used, reducing DB I/O and memory overhead.

Changes:

  • Update the grievance query to use options(load_only(Grievance.id, Grievance.category)).
  • Adjust the unit test’s mocked SQLAlchemy query chain to include .options(...).
  • Add a Bolt learning entry documenting how to mock load_only-augmented queries.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
backend/civic_intelligence.py Uses load_only when loading grievances for the daily cycle to avoid fetching unused columns.
backend/tests/test_civic_intelligence.py Updates mock query chaining to match the new .options(...).filter(...).all() call pattern.
.jules/bolt.md Documents the testing/mocking change required when adding .options(load_only(...)).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.jules/bolt.md:
- Around line 81-83: The entry heading "## 2026-05-18 - Mocking SQLAlchemy
load_only options" is dated in the future; update the heading date to the PR
creation/learning date (e.g., change "2026-05-18" to "2026-04-21") so the
timeline is accurate, leaving the rest of the heading and content unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9a7f2c7c-2080-40bc-8fe3-1a258c911d39

📥 Commits

Reviewing files that changed from the base of the PR and between c839aa2 and 7171270.

📒 Files selected for processing (3)
  • .jules/bolt.md
  • backend/civic_intelligence.py
  • backend/tests/test_civic_intelligence.py

Comment thread .jules/bolt.md Outdated
Comment on lines +81 to +83
## 2026-05-18 - Mocking SQLAlchemy load_only options
**Learning:** When changing a database query from `db.query(Model)` to `db.query(Model).options(load_only(...))` to optimize data fetching, the return type remains the ORM model (unlike column projection which returns tuples). However, if the tests mock the SQLAlchemy query chain, the mock assertions will fail because `.options()` is added to the chain.
**Action:** When adding `.options(load_only(...))` to a query, always ensure the corresponding mock queries in tests are updated to include `.options.return_value` (e.g., `mock_query.options.return_value.filter.return_value.all.return_value = ...`) to correctly mock the new chain.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix the future-dated learning entry.

This PR was created on April 21, 2026, but the new entry is dated May 18, 2026. Please update the heading date to the actual learning date to keep the timeline accurate.

📝 Proposed documentation fix
-## 2026-05-18 - Mocking SQLAlchemy load_only options
+## 2026-04-21 - Mocking SQLAlchemy load_only options
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## 2026-05-18 - Mocking SQLAlchemy load_only options
**Learning:** When changing a database query from `db.query(Model)` to `db.query(Model).options(load_only(...))` to optimize data fetching, the return type remains the ORM model (unlike column projection which returns tuples). However, if the tests mock the SQLAlchemy query chain, the mock assertions will fail because `.options()` is added to the chain.
**Action:** When adding `.options(load_only(...))` to a query, always ensure the corresponding mock queries in tests are updated to include `.options.return_value` (e.g., `mock_query.options.return_value.filter.return_value.all.return_value = ...`) to correctly mock the new chain.
## 2026-04-21 - Mocking SQLAlchemy load_only options
**Learning:** When changing a database query from `db.query(Model)` to `db.query(Model).options(load_only(...))` to optimize data fetching, the return type remains the ORM model (unlike column projection which returns tuples). However, if the tests mock the SQLAlchemy query chain, the mock assertions will fail because `.options()` is added to the chain.
**Action:** When adding `.options(load_only(...))` to a query, always ensure the corresponding mock queries in tests are updated to include `.options.return_value` (e.g., `mock_query.options.return_value.filter.return_value.all.return_value = ...`) to correctly mock the new chain.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.jules/bolt.md around lines 81 - 83, The entry heading "## 2026-05-18 -
Mocking SQLAlchemy load_only options" is dated in the future; update the heading
date to the PR creation/learning date (e.g., change "2026-05-18" to
"2026-04-21") so the timeline is accurate, leaving the rest of the heading and
content unchanged.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 3 files

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants