-
Notifications
You must be signed in to change notification settings - Fork 35
⚡ Bolt: Replace func.sum(case) with GROUP BY for single column aggregation #760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RohanExploit
wants to merge
1
commit into
main
Choose a base branch
from
bolt/group-by-optimization-2756740780356283571
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| from sqlalchemy.orm import Session | ||
| from sqlalchemy import func | ||
| from datetime import datetime, timedelta, timezone | ||
| from backend.models import Grievance, GrievanceFollower, ClosureConfirmation, GrievanceStatus | ||
| from backend.models import ( | ||
| Grievance, | ||
| GrievanceFollower, | ||
| ClosureConfirmation, | ||
| GrievanceStatus, | ||
| ) | ||
| import logging | ||
| import hashlib | ||
| import hmac | ||
|
|
@@ -10,102 +15,125 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ClosureService: | ||
| """Service for handling grievance closure confirmation logic""" | ||
|
|
||
| # Configuration | ||
| CONFIRMATION_THRESHOLD = 0.60 # 60% of followers must confirm | ||
| TIMEOUT_DAYS = 7 # 7 days to confirm | ||
| MINIMUM_FOLLOWERS = 3 # Minimum followers needed for confirmation process | ||
|
|
||
| @staticmethod | ||
| def request_closure(grievance_id: int, db: Session) -> dict: | ||
| """Request closure for a grievance - triggers confirmation process""" | ||
| grievance = db.query(Grievance).filter(Grievance.id == grievance_id).first() | ||
| if not grievance: | ||
| raise ValueError("Grievance not found") | ||
|
|
||
| if grievance.status == GrievanceStatus.RESOLVED: | ||
| raise ValueError("Grievance is already resolved") | ||
|
|
||
| # Count followers | ||
| follower_count = db.query(func.count(GrievanceFollower.id)).filter( | ||
| GrievanceFollower.grievance_id == grievance_id | ||
| ).scalar() | ||
|
|
||
| follower_count = ( | ||
| db.query(func.count(GrievanceFollower.id)) | ||
| .filter(GrievanceFollower.grievance_id == grievance_id) | ||
| .scalar() | ||
| ) | ||
|
|
||
| # If less than minimum followers, skip confirmation process | ||
| if follower_count < ClosureService.MINIMUM_FOLLOWERS: | ||
| grievance.status = GrievanceStatus.RESOLVED | ||
| grievance.resolved_at = datetime.now(timezone.utc) | ||
| grievance.closure_approved = True | ||
| db.commit() | ||
|
|
||
| return { | ||
| "message": "Grievance resolved (no confirmation needed - insufficient followers)", | ||
| "skip_confirmation": True, | ||
| "follower_count": follower_count | ||
| "follower_count": follower_count, | ||
| } | ||
|
|
||
| # Set closure pending | ||
| grievance.pending_closure = True | ||
| grievance.closure_requested_at = datetime.now(timezone.utc) | ||
| grievance.closure_confirmation_deadline = datetime.now(timezone.utc) + timedelta(days=ClosureService.TIMEOUT_DAYS) | ||
| grievance.closure_confirmation_deadline = datetime.now( | ||
| timezone.utc | ||
| ) + timedelta(days=ClosureService.TIMEOUT_DAYS) | ||
| db.commit() | ||
|
|
||
| required_confirmations = max(1, int(follower_count * ClosureService.CONFIRMATION_THRESHOLD)) | ||
|
|
||
|
|
||
| required_confirmations = max( | ||
| 1, int(follower_count * ClosureService.CONFIRMATION_THRESHOLD) | ||
| ) | ||
|
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Suggested patch+import math
...
- required_confirmations = max(
- 1, int(follower_count * ClosureService.CONFIRMATION_THRESHOLD)
- )
+ required_confirmations = max(
+ 1, math.ceil(follower_count * ClosureService.CONFIRMATION_THRESHOLD)
+ )
...
- required_confirmations = max(
- 1, int(total_followers * ClosureService.CONFIRMATION_THRESHOLD)
- )
+ required_confirmations = max(
+ 1, math.ceil(total_followers * ClosureService.CONFIRMATION_THRESHOLD)
+ )Also applies to: 187-189 🤖 Prompt for AI Agents |
||
|
|
||
| return { | ||
| "message": "Closure confirmation requested - waiting for community approval", | ||
| "skip_confirmation": False, | ||
| "follower_count": follower_count, | ||
| "required_confirmations": required_confirmations, | ||
| "deadline": grievance.closure_confirmation_deadline | ||
| "deadline": grievance.closure_confirmation_deadline, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def submit_confirmation(grievance_id: int, user_email: str, confirmation_type: str, reason: str, db: Session) -> dict: | ||
| def submit_confirmation( | ||
| grievance_id: int, | ||
| user_email: str, | ||
| confirmation_type: str, | ||
| reason: str, | ||
| db: Session, | ||
| ) -> dict: | ||
| """Submit a closure confirmation or dispute""" | ||
| grievance = db.query(Grievance).filter(Grievance.id == grievance_id).first() | ||
| if not grievance: | ||
| raise ValueError("Grievance not found") | ||
|
|
||
| if not grievance.pending_closure: | ||
| raise ValueError("Grievance is not pending closure confirmation") | ||
|
|
||
| # Check if user is a follower | ||
| is_follower = db.query(GrievanceFollower).filter( | ||
| GrievanceFollower.grievance_id == grievance_id, | ||
| GrievanceFollower.user_email == user_email | ||
| ).first() | ||
|
|
||
| is_follower = ( | ||
| db.query(GrievanceFollower) | ||
| .filter( | ||
| GrievanceFollower.grievance_id == grievance_id, | ||
| GrievanceFollower.user_email == user_email, | ||
| ) | ||
| .first() | ||
| ) | ||
|
|
||
| if not is_follower: | ||
| raise ValueError("Only followers can confirm or dispute closure") | ||
|
|
||
| # Check if user already submitted confirmation | ||
| existing = db.query(ClosureConfirmation).filter( | ||
| ClosureConfirmation.grievance_id == grievance_id, | ||
| ClosureConfirmation.user_email == user_email | ||
| ).first() | ||
|
|
||
| existing = ( | ||
| db.query(ClosureConfirmation) | ||
| .filter( | ||
| ClosureConfirmation.grievance_id == grievance_id, | ||
| ClosureConfirmation.user_email == user_email, | ||
| ) | ||
| .first() | ||
| ) | ||
|
|
||
| if existing: | ||
| raise ValueError("You have already submitted a response for this closure") | ||
|
|
||
| # Blockchain feature: calculate integrity hash for the closure confirmation | ||
| # Performance Boost: Use thread-safe cache to eliminate DB query for last hash | ||
| prev_hash = closure_last_hash_cache.get("last_hash") | ||
| if prev_hash is None: | ||
| # Cache miss: Fetch only the last hash from DB | ||
| last_record = db.query(ClosureConfirmation.integrity_hash).order_by(ClosureConfirmation.id.desc()).first() | ||
| last_record = ( | ||
| db.query(ClosureConfirmation.integrity_hash) | ||
| .order_by(ClosureConfirmation.id.desc()) | ||
| .first() | ||
| ) | ||
| prev_hash = last_record[0] if last_record and last_record[0] else "" | ||
| closure_last_hash_cache.set(data=prev_hash, key="last_hash") | ||
|
|
||
| # Chaining logic: hash(grievance_id|user_email|confirmation_type|prev_hash) | ||
| hash_content = f"{grievance_id}|{user_email}|{confirmation_type}|{prev_hash}" | ||
| secret_key = get_auth_config().secret_key | ||
| integrity_hash = hmac.new( | ||
| secret_key.encode('utf-8'), | ||
| hash_content.encode('utf-8'), | ||
| hashlib.sha256 | ||
| secret_key.encode("utf-8"), hash_content.encode("utf-8"), hashlib.sha256 | ||
| ).hexdigest() | ||
|
|
||
| # Create confirmation record | ||
|
|
@@ -115,7 +143,7 @@ def submit_confirmation(grievance_id: int, user_email: str, confirmation_type: s | |
| confirmation_type=confirmation_type, | ||
| reason=reason, | ||
| integrity_hash=integrity_hash, | ||
| previous_integrity_hash=prev_hash | ||
| previous_integrity_hash=prev_hash, | ||
| ) | ||
| db.add(confirmation) | ||
| db.commit() | ||
|
|
@@ -125,76 +153,92 @@ def submit_confirmation(grievance_id: int, user_email: str, confirmation_type: s | |
|
|
||
| # Check if threshold is met | ||
| return ClosureService.check_and_finalize_closure(grievance_id, db) | ||
|
|
||
| @staticmethod | ||
| def check_and_finalize_closure(grievance_id: int, db: Session) -> dict: | ||
| """Check if closure threshold is met and finalize if needed""" | ||
| grievance = db.query(Grievance).filter(Grievance.id == grievance_id).first() | ||
| if not grievance or not grievance.pending_closure: | ||
| return {"closure_finalized": False} | ||
|
|
||
| # Count followers and confirmations | ||
| total_followers = db.query(func.count(GrievanceFollower.id)).filter( | ||
| GrievanceFollower.grievance_id == grievance_id | ||
| ).scalar() | ||
|
|
||
| total_followers = ( | ||
| db.query(func.count(GrievanceFollower.id)) | ||
| .filter(GrievanceFollower.grievance_id == grievance_id) | ||
| .scalar() | ||
| ) | ||
|
|
||
| # Get all confirmation counts in a single query instead of multiple round-trips | ||
| from sqlalchemy import case | ||
| stats = db.query( | ||
| func.sum(case((ClosureConfirmation.confirmation_type == 'confirmed', 1), else_=0)).label('confirmed'), | ||
| func.sum(case((ClosureConfirmation.confirmation_type == 'disputed', 1), else_=0)).label('disputed') | ||
| ).filter(ClosureConfirmation.grievance_id == grievance_id).first() | ||
|
|
||
| confirmations_count = stats.confirmed or 0 | ||
| disputes_count = stats.disputed or 0 | ||
|
|
||
| required_confirmations = max(1, int(total_followers * ClosureService.CONFIRMATION_THRESHOLD)) | ||
|
|
||
| # Optimized: Replace expensive func.sum(case(...)) with a standard GROUP BY | ||
| counts = ( | ||
| db.query( | ||
| ClosureConfirmation.confirmation_type, | ||
| func.count(ClosureConfirmation.id), | ||
| ) | ||
| .filter(ClosureConfirmation.grievance_id == grievance_id) | ||
| .group_by(ClosureConfirmation.confirmation_type) | ||
| .all() | ||
| ) | ||
| counts_dict = dict(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) | ||
| ) | ||
|
|
||
| # Check if threshold is met | ||
| if confirmations_count >= required_confirmations: | ||
| grievance.status = GrievanceStatus.RESOLVED | ||
| grievance.resolved_at = datetime.now(timezone.utc) | ||
| grievance.closure_approved = True | ||
| grievance.pending_closure = False | ||
| db.commit() | ||
|
|
||
| return { | ||
| "closure_finalized": True, | ||
| "approved": True, | ||
| "confirmations": confirmations_count, | ||
| "required": required_confirmations, | ||
| "message": "Grievance closure approved by community" | ||
| "message": "Grievance closure approved by community", | ||
| } | ||
|
|
||
| return { | ||
| "closure_finalized": False, | ||
| "confirmations": confirmations_count, | ||
| "disputes": disputes_count, | ||
| "required": required_confirmations, | ||
| "total_followers": total_followers | ||
| "total_followers": total_followers, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def check_timeout_and_finalize(db: Session): | ||
| """Background task to check for timed-out closure requests""" | ||
| now = datetime.now(timezone.utc) | ||
|
|
||
| # Find grievances with expired deadlines | ||
| expired_grievances = db.query(Grievance).filter( | ||
| Grievance.pending_closure == True, | ||
| Grievance.closure_confirmation_deadline < now | ||
| ).all() | ||
|
|
||
| expired_grievances = ( | ||
| db.query(Grievance) | ||
| .filter( | ||
| Grievance.pending_closure == True, | ||
| Grievance.closure_confirmation_deadline < now, | ||
| ) | ||
| .all() | ||
| ) | ||
|
|
||
| for grievance in expired_grievances: | ||
| # Check current status | ||
| result = ClosureService.check_and_finalize_closure(grievance.id, db) | ||
|
|
||
| if not result.get("closure_finalized"): | ||
| # Timeout - log dispute and keep open | ||
| logger.warning(f"Grievance {grievance.id} closure timeout - threshold not met") | ||
| logger.warning( | ||
| f"Grievance {grievance.id} closure timeout - threshold not met" | ||
| ) | ||
| grievance.pending_closure = False | ||
| grievance.closure_approved = False | ||
| # Keep status as is (not resolved) | ||
| db.commit() | ||
| return len(expired_grievances) | ||
|
|
||
| return len(expired_grievances) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the learning entry date to match this PR’s actual timeline.
This note is dated 2026-05-19, but this PR was opened on 2026-05-14. Future-dating makes the learning log harder to trust and search.
🤖 Prompt for AI Agents