Skip to content

⚡ Bolt: Optimize field officer visit statistics query#776

Open
RohanExploit wants to merge 2 commits into
mainfrom
bolt-optimize-visit-statistics-4327000524806729581
Open

⚡ Bolt: Optimize field officer visit statistics query#776
RohanExploit wants to merge 2 commits into
mainfrom
bolt-optimize-visit-statistics-4327000524806729581

Conversation

@RohanExploit
Copy link
Copy Markdown
Owner

@RohanExploit RohanExploit commented May 18, 2026

💡 What: Optimized get_visit_statistics endpoint to use a single SQL aggregate query with conditional logic.
🎯 Why: The previous implementation performed multiple sequential queries and a python loop over a GROUP BY result, which caused redundant database scans and round-trips.
📊 Impact: Consolidates execution into a single database round-trip (~60% expected reduction in query time as per bolt.md learnings) and bypasses Python loop overhead.
🔬 Measurement: Verified functionality by running backend tests pytest backend/tests/ and ensuring the API responds correctly with the aggregated values.


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


Summary by cubic

Optimized get_visit_statistics into a single SQL aggregate and hardened the visit image upload flow with strict extension validation and non-blocking file writes. This cuts DB round-trips and keeps uploads secure and responsive without changing API responses.

  • Refactors

    • Consolidated all metrics into one query using func.sum(case(...)) (totals, verified, geofence counts, unique officers, avg distance).
    • Kept response shape; average distance still rounded to 2 decimals with a 0.0 default; caching behavior unchanged and still invalidated on updates.
    • Added .venv/ to .gitignore.
  • Bug Fixes

    • Fixed path traversal risk by validating image extensions against an allowlist before saving.
    • Offloaded image file writes to a threadpool to avoid blocking the FastAPI event loop.
    • Removed unused werkzeug to prevent crashes.

Written for commit 98378d5. Summary will update on new commits. Review in cubic

Summary by CodeRabbit

Release Notes

  • New Features

    • Field officer check-in and check-out endpoints now include creation timestamps in visit responses.
    • Image upload functionality now enforces stricter validation, including file type restrictions and size limits for individual files and cumulative uploads.
  • Bug Fixes

    • Enhanced error handling and logging in visit history retrieval and verification endpoints.

Review Change Stack

Refactored `get_visit_statistics` in `backend/routers/field_officer.py` to use a single aggregate SQL query utilizing `func.sum(case(...))`. This eliminates multiple database queries, a redundant `GROUP BY` operation, and a subsequent Python-side iteration loop, significantly reducing database round-trips and scan overhead. Tests run successfully with no breaking changes.
Copilot AI review requested due to automatic review settings May 18, 2026 14:05
@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.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 18, 2026

Deploy Preview for fixmybharat canceled.

Name Link
🔨 Latest commit 98378d5
🔍 Latest deploy log https://app.netlify.com/projects/fixmybharat/deploys/6a0b2080c3d61e000892b4b7

@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 May 18, 2026

📝 Walkthrough

Walkthrough

This PR refactors field-officer API endpoints to implement blockchain-style visit hashing, add strict image validation with filesystem operations, optimize visit-statistics queries from multiple roundtrips to a single aggregate SQL call, and enhance visit responses with timestamps. It also reorganizes imports and adds Python virtual environment to .gitignore.

Changes

Field Officer API Enhancements

Layer / File(s) Summary
Environment and import setup
.gitignore, backend/routers/field_officer.py
Virtual environment directory added to .gitignore, and geofencing service import reorganized in multi-import block.
Check-in and check-out visit lifecycle
backend/routers/field_officer.py
Check-in endpoint generates immutable visit_hash with chained previous_visit_hash, persists blockchain fields on creation, and returns response including created_at. Check-out refactored to explicit try/except structure with status updates, field recording, and cache invalidation.
Image upload with validation and file handling
backend/routers/field_officer.py
Upload-images endpoint enforces strict validation: MIME type presence, extension allowlist via ALLOWED_IMAGE_EXTENSIONS, filename presence, per-file size limits, and cumulative per-visit image limits. Files written with timestamped safe filenames to visit-images directory, database updated, and relative paths returned.
Visit history response enrichment
backend/routers/field_officer.py
Visit-history endpoint includes created_at on each public visit response and refactors exception logging around retrieval and serialization.
Visit statistics SQL optimization
backend/routers/field_officer.py
Visit-stats computation replaced from multiple grouped queries plus Python aggregation loop to single SQL aggregate query using conditional sums to compute total visits, verified counts, geofence in/out distribution, unique officers, and average distance. Results cached as JSON and returned as raw Response.
Admin visit verification
backend/routers/field_officer.py
Admin verification refactored with clearer query/update block, explicit timestamp assignment, status transition to verified, cache invalidation, and improved exception logging.
Blockchain integrity verification
backend/routers/field_officer.py
Blockchain verification refactored to build response message dynamically from is_valid flag, improved error logging, and preserved HTTP error behavior on unexpected exceptions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • RohanExploit/VishwaGuru#741: Directly modifies visit-stats aggregation logic in the same endpoint, switching between SUM(CASE ...) and group_by query approaches.
  • RohanExploit/VishwaGuru#549: Also optimizes visit-statistics aggregation by replacing multi-query with single CASE-based SQL aggregate to reduce database roundtrips.

Suggested labels

backend, api, field-officer, data-optimization, validation, size/m

Poem

🐰 A rabbit hops through visit logs with care,
Hashing chains to prove the trips are fair,
Images validated, stats in one SQL prayer,
Blockchain whispers through the field-officer air!
thump thump 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Optimize field officer visit statistics query' is directly related to the main change—optimizing the visit statistics endpoint with a single SQL aggregate query.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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 comprehensively covers changes, rationale, impact, and verification steps, following the template structure with clear sections.

✏️ 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 bolt-optimize-visit-statistics-4327000524806729581

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.

Comment thread backend/routers/field_officer.py Fixed
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

This PR optimizes the field officer visit statistics endpoint by consolidating multiple database queries and Python-side aggregation into a single SQL aggregate query, improving backend API performance.

Changes:

  • Replaced grouped query + Python loop aggregation in get_visit_statistics with conditional SQL aggregates.
  • Preserved response shaping, JSON serialization cache behavior, and cache invalidation paths.
  • Added .venv/ to .gitignore.

Reviewed changes

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

File Description
backend/routers/field_officer.py Optimizes visit statistics aggregation and applies formatting cleanup.
.gitignore Ignores local Python virtual environment directories.

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

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

Re-trigger cubic

- Fixed CodeQL Path Traversal vulnerability (CWE-022) by strictly extracting and validating the file extension using an allowlist string mapping.
- Offloaded blocking synchronous file writing (`f.write`) to a separate threadpool (`run_in_threadpool`) in `upload_visit_images` to keep the FastAPI event loop responsive.
- Removed unnecessary `werkzeug` dependency that previously crashed the app.
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.

3 participants