Fix schedulers of teh production service

This commit is contained in:
Urtzi Alfaro
2025-09-25 14:49:42 +02:00
parent 89b75bd7af
commit c75aed9dfe

View File

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