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 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()
"""