Skip to content

Resolve merge conflicts and clean up benchmark files#562

Draft
Copilot wants to merge 11 commits intobolt-optimize-closure-count-queries-6106104410388610831from
copilot/sub-pr-558-again
Draft

Resolve merge conflicts and clean up benchmark files#562
Copilot wants to merge 11 commits intobolt-optimize-closure-count-queries-6106104410388610831from
copilot/sub-pr-558-again

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

Both this branch and main independently applied the same GROUP BY optimization to closure status queries, causing conflicts. Merged main and resolved conflicts, plus addressed reviewer feedback on the benchmark files.

Description

Conflict resolution (backend/closure_service.py, backend/routers/grievances.py):

  • Both branches made identical GROUP BY optimization with different formatting; accepted main's more compact version

backend/tests/test_closure_status_benchmark.py:

  • Removed unused sys, os imports
  • Extracted module-level engine/create_all into _make_session() to prevent DB side effects at pytest collection time
  • Added a real test_grouped_query_faster_than_separate() pytest function verifying correct counts from the GROUP BY query

backend/tests/benchmark_closure_status.py:

  • Removed unused imports: sys, os, get_db, ClosureService, patch, MagicMock, and sys.path.insert
  • Fixed seed_data() to populate all required non-null Grievance fields (severity, current_jurisdiction_id, assigned_authority, sla_deadline) — previously would raise IntegrityError on commit

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📝 Documentation update
  • 🎨 Code style update (formatting, renaming)
  • ♻️ Refactoring (no functional changes)
  • ⚡ Performance improvement
  • ✅ Test update

Related Issue

Closes #

Testing Done

  • Tested locally
  • Added/updated tests
  • All tests passing

Screenshots (if applicable)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Co-Authors


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.


Summary by cubic

Optimizes closure status counts with a single GROUP BY query and replaces detection caching with a faster, thread‑safe cache. Adds facial emotion detection and an in‑app camera capture to improve reporting. Aligns with Linear 6106104410388610831.

  • Refactors

    • Use one COUNT(*) GROUP BY query for closure confirmations in closure_service and grievances router; preserves API shape (missing statuses → 0).
    • Replace manual detection cache with ThreadSafeCache (TTL+LRU, O(K) expiry cleanup) to reduce latency.
  • New Features

    • New /api/detect-emotion endpoint using Hugging Face dima806/facial_emotions_image_detection and detectorsApi.emotion client method.
    • ReportForm integrates react-webcam to snap photos; converts captures to File for existing upload flow.
    • Remove duplicate frontend/public/_redirects to fix Netlify deploy.

Written for commit 23a5e8e. Summary will update on new commits.

RohanExploit and others added 10 commits March 16, 2026 14:17
💡 What:
- Optimized `ThreadSafeCache` by using `collections.OrderedDict` for timestamps.
- Improved `_cleanup_expired` complexity from O(N) to O(K) where K is the number of expired entries.
- Replaced manual dictionary-based caching in `backend/routers/detection.py` with `ThreadSafeCache`.

🎯 Why:
- O(N) cache cleanup on every `set` operation causes unnecessary latency as the cache grows.
- Manual caching in the detection router lacked thread safety and efficient eviction.

📊 Impact:
- `ThreadSafeCache.set` performance improved by ~32x in benchmarks (from ~10.5k ops/sec to ~342k ops/sec).
- Detection router now has robust, thread-safe caching with proper TTL and LRU eviction.

🔬 Measurement:
- Run `backend/tests/benchmark_cache.py` to verify ops/sec improvement.
- Run `backend/tests/test_cache_unit.py` to verify correctness.
What: Consolidated multiple separate `db.query(func.count(ClosureConfirmation.id)).filter(...)` database calls into a single `GROUP BY` query in `get_closure_status` and `check_and_finalize_closure`.

Why: In high-traffic environments, executing multiple sequential aggregate queries on the same table causes unnecessary database roundtrips and scan overhead.

Impact: Reduces database roundtrips from two separate counts to a single group by query when fetching or finalizing a grievance closure status.

Measurement: Local benchmarking shows a latency reduction from ~0.214ms to ~0.159ms per call (a ~25% speedup) using SQLite. Performance gains will be even more significant with network-bound PostgreSQL in production.
…ctionality in ReportForm

Replaced standard file input with a reliable react-webcam modal, allowing users to capture issue photos directly in the web app. The base64 output is automatically converted to a File object, maintaining compatibility with the existing API.

Confirmed that Netlify/Render deployment configuration files (`netlify.toml` and `render.yaml`) and Hugging Face configuration are already properly set up in the repository.
…ment

Removed `frontend/public/_redirects` which conflicts with the redirect rules already defined in the root `netlify.toml` file. This resolves the Netlify CI check failures for 'Redirect rules' and 'Header rules'.
…ed features

- Removed `frontend/public/_redirects` which conflicts with the redirect rules already defined in the root `netlify.toml` file, resolving the Netlify deployment CI check failures for 'Redirect rules' and 'Header rules'.
- Replaced standard file input with a reliable react-webcam modal, allowing users to capture issue photos directly in the web app, maintaining compatibility with the existing API.
- Integrated a new Hugging Face facial emotions detection model (`dima806/facial_emotions_image_detection`), exposing it via `/api/detect-emotion` in the backend and updating the frontend API client.
…uild

Fixed a syntax error in `frontend/src/api/detectors.js` which caused `vite build` to fail silently, leading to Netlify's deployment failure (`Pages changed` and `Redirect rules` checks). The error was introduced during the Hugging Face emotion detector integration patch. The local build now completes successfully.
…9847583124469405

⚡ Bolt: Optimize ThreadSafeCache and detection router caching
…us-4445710699147296264

⚡ Bolt: Optimize grievance closure status performance
…e0870e71

Integrate react-webcam for reliable in-app camera functionality in ReportForm
Copilot AI changed the title [WIP] [6106104410388610831] Optimize closure status queries using GROUP BY Resolve merge conflicts and clean up benchmark files Mar 19, 2026
Copilot AI requested a review from RohanExploit March 19, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants