Improve te panel de control logic

This commit is contained in:
Urtzi Alfaro
2025-11-21 16:15:09 +01:00
parent 2ee94fb4b1
commit 3242c8d837
21 changed files with 2805 additions and 696 deletions

View File

@@ -456,6 +456,9 @@ class ProductionServiceClient(BaseServiceClient):
"""
Get today's production batches for dashboard timeline
For demo compatibility: Queries all recent batches and filters for actionable ones
scheduled for today, since demo session dates are adjusted relative to session creation time.
Args:
tenant_id: Tenant ID
@@ -463,13 +466,67 @@ class ProductionServiceClient(BaseServiceClient):
Dict with ProductionBatchListResponse: {"batches": [...], "total_count": n, "page": 1, "page_size": n}
"""
try:
from datetime import date
today = date.today()
return await self.get(
from datetime import datetime, timezone, timedelta
# Get today's date range (start of day to end of day in UTC)
now = datetime.now(timezone.utc)
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
today_end = today_start + timedelta(days=1)
# Query all batches without date/status filter for demo compatibility
# The dashboard will filter for PENDING, IN_PROGRESS, or SCHEDULED
result = await self.get(
"/production/batches",
tenant_id=tenant_id,
params={"start_date": today.isoformat(), "end_date": today.isoformat(), "page_size": 100}
params={"page_size": 100}
)
if result and "batches" in result:
# Filter for actionable batches scheduled for TODAY
actionable_statuses = {"PENDING", "IN_PROGRESS", "SCHEDULED"}
filtered_batches = []
for batch in result["batches"]:
# Check if batch is actionable
if batch.get("status") not in actionable_statuses:
continue
# Check if batch is scheduled for today
# Include batches that START today OR END today (for overnight batches)
planned_start = batch.get("planned_start_time")
planned_end = batch.get("planned_end_time")
include_batch = False
if planned_start:
# Parse the start date string
if isinstance(planned_start, str):
planned_start = datetime.fromisoformat(planned_start.replace('Z', '+00:00'))
# Include if batch starts today
if today_start <= planned_start < today_end:
include_batch = True
# Also check if batch ends today (for overnight batches)
if not include_batch and planned_end:
if isinstance(planned_end, str):
planned_end = datetime.fromisoformat(planned_end.replace('Z', '+00:00'))
# Include if batch ends today (even if it started yesterday)
if today_start <= planned_end < today_end:
include_batch = True
if include_batch:
filtered_batches.append(batch)
# Return filtered result
return {
**result,
"batches": filtered_batches,
"total_count": len(filtered_batches)
}
return result
except Exception as e:
logger.error("Error fetching today's batches", error=str(e), tenant_id=tenant_id)
return None