diff --git a/backend/civic_intelligence.py b/backend/civic_intelligence.py index 4a90640f..2106b4ac 100644 --- a/backend/civic_intelligence.py +++ b/backend/civic_intelligence.py @@ -51,7 +51,12 @@ def run_daily_cycle(self): # 1. Fetch Data # Get issues created in the last 24 hours - issues_24h = db.query(Issue).filter(Issue.created_at >= last_24h).all() + # Performance Optimization: Use column projection to avoid loading full ORM models, + # since trend analyzer only needs specific attributes (id, description, category, lat, lon, upvotes, created_at) + issues_24h = db.query( + Issue.id, Issue.description, Issue.category, + Issue.latitude, Issue.longitude, Issue.upvotes, Issue.created_at + ).filter(Issue.created_at >= last_24h).all() # 2. Trend Analysis trends = trend_analyzer.analyze(issues_24h) diff --git a/backend/tests/test_civic_intelligence.py b/backend/tests/test_civic_intelligence.py index dec96015..c242453f 100644 --- a/backend/tests/test_civic_intelligence.py +++ b/backend/tests/test_civic_intelligence.py @@ -152,15 +152,18 @@ def open_side_effect(file, mode='r', *args, **kwargs): # Define query side effects def query_side_effect(*args): - if len(args) == 1: + if len(args) > 0: model = args[0] - if getattr(model, '__name__', '') == 'Issue': + # Handle column projection (InstrumentedAttribute) by checking class_ + class_name = getattr(model, 'class_', model).__name__ if hasattr(model, 'class_') else getattr(model, '__name__', '') + + if class_name == 'Issue': return mock_query_issues elif hasattr(model, 'name') and model.name == 'count': return mock_query_issues - elif getattr(model, '__name__', '') == 'EscalationAudit': + elif class_name == 'EscalationAudit': return mock_query_upgrades - elif getattr(model, '__name__', '') == 'Grievance': + elif class_name == 'Grievance': return mock_query_grievance return MagicMock()