Skip to content

⚡ Bolt: [performance improvement] Optimize Stats Queries#545

Open
RohanExploit wants to merge 1 commit intomainfrom
fix-stats-queries-69517637003798733
Open

⚡ Bolt: [performance improvement] Optimize Stats Queries#545
RohanExploit wants to merge 1 commit intomainfrom
fix-stats-queries-69517637003798733

Conversation

@RohanExploit
Copy link
Owner

@RohanExploit RohanExploit commented Mar 14, 2026

perf: optimize stats queries

Optimize database aggregate queries by combining multiple separate count/sum operations into a single query in field_officer.py and utility.py. This reduces the number of database round-trips and scan overhead.


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


Summary by cubic

Optimized visit and issue stats by consolidating multiple DB queries into single aggregate queries, reducing round-trips and scan cost. No API changes; responses remain the same.

  • Performance
    • Combined counts/sums into one SELECT using SQLAlchemy aggregates and CASE in field_officer.get_visit_statistics and utility.get_stats.
    • Defaults to 0 for null aggregates and keeps average distance rounding.
    • Fewer database calls and faster responses under load.

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

Summary by CodeRabbit

  • Chores
    • Optimized database query performance by consolidating multiple queries into single, more efficient queries for visit statistics and issue metrics.

Optimize database aggregate queries by combining multiple separate count/sum operations into a single query in field_officer.py and utility.py. This reduces the number of database round-trips and scan overhead.
@google-labs-jules
Copy link
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 March 14, 2026 11:16
@netlify
Copy link

netlify bot commented Mar 14, 2026

Deploy Preview for fixmybharat canceled.

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

@github-actions
Copy link

🙏 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

coderabbitai bot commented Mar 14, 2026

📝 Walkthrough

Walkthrough

Two backend router endpoints consolidate multiple database queries into single optimized queries using labeled aggregates and case expressions, reducing database round-trips and improving query efficiency for visit statistics and issue status metrics.

Changes

Cohort / File(s) Summary
Query Optimization
backend/routers/field_officer.py, backend/routers/utility.py
Consolidated separate aggregate queries into single SQL queries with labeled aggregates and case expressions. Field officer endpoint now computes all visit metrics (total, verified, geofence counts, officers, distance) in one query. Utility endpoint combines total and resolved issue counts with status-based case logic. Both retain fallback values to 0 for null handling.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

size/m

Poem

🐰 Single queries hopping through,
Where once were many, now are few,
Aggregates dance with labeled grace,
Database calls find their place,
Efficiency bounds without a trace! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

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.
Description check ❓ Inconclusive The PR description covers the main purpose (query optimization), includes auto-generated summaries with technical details, but lacks required template sections like Type of Change checkbox selection, Testing Done confirmation, and other standard checklist items. Complete the PR description template by selecting appropriate Type of Change checkboxes (should be ⚡ Performance improvement), confirming Testing Done, and filling out the Checklist sections to ensure proper review and merge readiness.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: optimizing stats queries for performance improvement. It directly matches the core objective of combining multiple database queries into single aggregated queries.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-stats-queries-69517637003798733
📝 Coding Plan
  • Generate coding plan for human review comments

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
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

This PR optimizes backend stats endpoints by collapsing multiple aggregate queries into single SQL aggregate queries to reduce DB round-trips.

Changes:

  • Combine total/resolved issue counts into a single aggregate query in /stats.
  • Combine multiple field officer visit aggregates (counts, distinct count, avg) into a single query in /visit-stats.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
backend/routers/utility.py Consolidates issue stats aggregation into one query (but currently missing an import needed for the new query).
backend/routers/field_officer.py Consolidates visit statistics aggregates into a single SQL query.

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

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +56 to +59
stats = db.query(
func.count(Issue.id).label("total"),
func.sum(case((Issue.status.in_(['resolved', 'verified']), 1), else_=0)).label("resolved")
).first()
Copy link
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.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/routers/utility.py">

<violation number="1" location="backend/routers/utility.py:58">
P1: Import `case` from SQLAlchemy before using it here; otherwise `/stats` will raise `NameError` at runtime.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

resolved = db.query(func.count(Issue.id)).filter(Issue.status.in_(['resolved', 'verified'])).scalar()
stats = db.query(
func.count(Issue.id).label("total"),
func.sum(case((Issue.status.in_(['resolved', 'verified']), 1), else_=0)).label("resolved")
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 14, 2026

Choose a reason for hiding this comment

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

P1: Import case from SQLAlchemy before using it here; otherwise /stats will raise NameError at runtime.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/routers/utility.py, line 58:

<comment>Import `case` from SQLAlchemy before using it here; otherwise `/stats` will raise `NameError` at runtime.</comment>

<file context>
@@ -53,8 +53,13 @@ def get_stats(db: Session = Depends(get_db)):
-    resolved = db.query(func.count(Issue.id)).filter(Issue.status.in_(['resolved', 'verified'])).scalar()
+    stats = db.query(
+        func.count(Issue.id).label("total"),
+        func.sum(case((Issue.status.in_(['resolved', 'verified']), 1), else_=0)).label("resolved")
+    ).first()
+
</file context>
Fix with Cubic

Copy link

@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

🧹 Nitpick comments (1)
backend/routers/field_officer.py (1)

412-426: Good optimization consolidating multiple queries into one.

The aggregation query correctly reduces database round-trips. However, for the boolean comparisons, consider using SQLAlchemy's is_() method instead of == True/== False to satisfy linter rules while maintaining explicit SQL generation.

♻️ Suggested refinement for boolean comparisons
         stats = db.query(
             func.count(FieldOfficerVisit.id).label("total_visits"),
             func.sum(case((FieldOfficerVisit.verified_at.isnot(None), 1), else_=0)).label("verified_visits"),
-            func.sum(case((FieldOfficerVisit.within_geofence == True, 1), else_=0)).label("within_geofence_count"),
-            func.sum(case((FieldOfficerVisit.within_geofence == False, 1), else_=0)).label("outside_geofence_count"),
+            func.sum(case((FieldOfficerVisit.within_geofence.is_(True), 1), else_=0)).label("within_geofence_count"),
+            func.sum(case((FieldOfficerVisit.within_geofence.is_(False), 1), else_=0)).label("outside_geofence_count"),
             func.count(func.distinct(FieldOfficerVisit.officer_email)).label("unique_officers"),
             func.avg(FieldOfficerVisit.distance_from_site).label("average_distance")
         ).first()
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/routers/field_officer.py` around lines 412 - 426, In the aggregation
query building stats, replace the boolean comparisons that use
"FieldOfficerVisit.within_geofence == True" and "== False" with SQLAlchemy's
is_() to satisfy the linter and generate explicit SQL; e.g., change the case
expressions in the db.query call to use
FieldOfficerVisit.within_geofence.is_(True) and
FieldOfficerVisit.within_geofence.is_(False) (the surrounding case(...) labels
and other aggregates like total_visits, verified_visits, unique_officers,
average_distance remain the same).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/routers/utility.py`:
- Around line 56-59: The stats query in utility.py uses SQLAlchemy's case
function (seen in the db.query call building stats with func.sum(case(...))) but
case is not imported, causing a NameError; fix by adding case to the SQLAlchemy
imports at the top of the file so the case symbol is available to the stats
query that references Issue, func.count and func.sum.

---

Nitpick comments:
In `@backend/routers/field_officer.py`:
- Around line 412-426: In the aggregation query building stats, replace the
boolean comparisons that use "FieldOfficerVisit.within_geofence == True" and "==
False" with SQLAlchemy's is_() to satisfy the linter and generate explicit SQL;
e.g., change the case expressions in the db.query call to use
FieldOfficerVisit.within_geofence.is_(True) and
FieldOfficerVisit.within_geofence.is_(False) (the surrounding case(...) labels
and other aggregates like total_visits, verified_visits, unique_officers,
average_distance remain the same).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1b3059cb-c76b-4613-bb3c-69a597dfa448

📥 Commits

Reviewing files that changed from the base of the PR and between 6e85a8b and 0511394.

📒 Files selected for processing (2)
  • backend/routers/field_officer.py
  • backend/routers/utility.py

Comment on lines +56 to +59
stats = db.query(
func.count(Issue.id).label("total"),
func.sum(case((Issue.status.in_(['resolved', 'verified']), 1), else_=0)).label("resolved")
).first()
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Critical: case is not imported, causing a NameError at runtime.

The case function from SQLAlchemy is used on line 58 but is not imported. This will cause the /stats endpoint to fail.

🐛 Fix: Add `case` to the import statement
-from sqlalchemy import func
+from sqlalchemy import func, case
🧰 Tools
🪛 Ruff (0.15.5)

[error] 58-58: Undefined name case

(F821)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/routers/utility.py` around lines 56 - 59, The stats query in
utility.py uses SQLAlchemy's case function (seen in the db.query call building
stats with func.sum(case(...))) but case is not imported, causing a NameError;
fix by adding case to the SQLAlchemy imports at the top of the file so the case
symbol is available to the stats query that references Issue, func.count and
func.sum.

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