diff --git a/services/production/app/services/production_alert_service.py b/services/production/app/services/production_alert_service.py index f28f9f76..f54e99e9 100644 --- a/services/production/app/services/production_alert_service.py +++ b/services/production/app/services/production_alert_service.py @@ -5,6 +5,7 @@ Monitors production capacity, delays, quality issues, and optimization opportuni """ import json +import asyncio from typing import List, Dict, Any, Optional from uuid import UUID from datetime import datetime, timedelta @@ -105,7 +106,7 @@ class ProductionAlertService(BaseAlertService, AlertServiceMixin): FROM production_batches pb WHERE pb.planned_start_time >= CURRENT_DATE AND pb.planned_start_time <= CURRENT_DATE + INTERVAL '3 days' - AND pb.status IN ('PLANNED', 'PENDING', 'IN_PROGRESS') + AND pb.status IN ('planned', 'in_progress') GROUP BY pb.tenant_id, DATE(pb.planned_start_time) HAVING COUNT(*) > 10 -- Alert if more than 10 batches per day ORDER BY total_planned DESC @@ -202,6 +203,9 @@ class ProductionAlertService(BaseAlertService, AlertServiceMixin): try: self._checks_performed += 1 + # Import text function at the beginning + from sqlalchemy import text + # Simplified query with timeout and proper error handling query = text(""" SELECT @@ -212,11 +216,8 @@ class ProductionAlertService(BaseAlertService, AlertServiceMixin): COALESCE(pb.priority::text, 'medium') as priority_level, 1 as affected_orders -- Default to 1 since we can't count orders FROM production_batches pb - WHERE pb.status IN ('IN_PROGRESS', 'ON_HOLD', 'QUALITY_CHECK') - AND ( - (pb.planned_end_time < NOW() AND pb.status = 'IN_PROGRESS') - OR pb.status IN ('ON_HOLD', 'QUALITY_CHECK') - ) + WHERE pb.status = 'in_progress' + AND pb.planned_end_time < NOW() AND pb.planned_end_time > NOW() - INTERVAL '24 hours' ORDER BY CASE COALESCE(pb.priority::text, 'MEDIUM') @@ -227,7 +228,6 @@ class ProductionAlertService(BaseAlertService, AlertServiceMixin): """) try: - from sqlalchemy import text async with self.db_manager.get_session() as session: # Set statement timeout await session.execute(text("SET statement_timeout = '30s'")) @@ -827,7 +827,7 @@ class ProductionAlertService(BaseAlertService, AlertServiceMixin): FROM production_batches pb JOIN recipe_ingredients ri ON ri.recipe_id = pb.recipe_id WHERE ri.ingredient_id = $1 - AND pb.status IN ('PENDING', 'IN_PROGRESS') + AND pb.status = 'in_progress' AND pb.planned_completion_time > NOW() """