From 11c29b1e43b44106eba5e94125671c8de7ce45c3 Mon Sep 17 00:00:00 2001 From: RohanExploit <178623867+RohanExploit@users.noreply.github.com> Date: Tue, 17 Mar 2026 14:05:34 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvem?= =?UTF-8?q?ent]=20Consolidate=20closure=20status=20queries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consolidates the separate `count` queries in the `get_closure_status` endpoint into a single `group_by` query over `ClosureConfirmation.confirmation_type`. This reduces the number of database queries executed during closure status retrieval. --- backend/routers/grievances.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/backend/routers/grievances.py b/backend/routers/grievances.py index ad602753..d8f462a7 100644 --- a/backend/routers/grievances.py +++ b/backend/routers/grievances.py @@ -402,15 +402,19 @@ def get_closure_status( GrievanceFollower.grievance_id == grievance_id ).scalar() - confirmations_count = db.query(func.count(ClosureConfirmation.id)).filter( - ClosureConfirmation.grievance_id == grievance_id, - ClosureConfirmation.confirmation_type == "confirmed" - ).scalar() + # Optimized: Single query with group_by replaces multiple count queries + closure_counts = db.query( + ClosureConfirmation.confirmation_type, + func.count(ClosureConfirmation.id) + ).filter( + ClosureConfirmation.grievance_id == grievance_id + ).group_by( + ClosureConfirmation.confirmation_type + ).all() - disputes_count = db.query(func.count(ClosureConfirmation.id)).filter( - ClosureConfirmation.grievance_id == grievance_id, - ClosureConfirmation.confirmation_type == "disputed" - ).scalar() + counts_dict = {ctype: count for ctype, count in closure_counts} + confirmations_count = counts_dict.get("confirmed", 0) + disputes_count = counts_dict.get("disputed", 0) required_confirmations = max(1, int(total_followers * ClosureService.CONFIRMATION_THRESHOLD)) From 3102912dab3f0aa8a33d5a8372e184ea800f3f2e Mon Sep 17 00:00:00 2001 From: RohanExploit <178623867+RohanExploit@users.noreply.github.com> Date: Tue, 17 Mar 2026 14:26:04 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvem?= =?UTF-8?q?ent]=20Consolidate=20closure=20status=20queries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consolidates the separate `count` queries in the `get_closure_status` endpoint into a single `group_by` query over `ClosureConfirmation.confirmation_type`. This reduces the number of database queries executed during closure status retrieval.