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

@@ -325,6 +325,9 @@ async def get_orchestration_summary(
try:
po_data = await procurement_client.get_pending_purchase_orders(tenant_id, limit=10)
if po_data and isinstance(po_data, list):
# Override stale orchestration count with actual real-time PO count
summary["purchaseOrdersCreated"] = len(po_data)
summary["userActionsRequired"] = len(po_data) # Update actions required to match actual pending POs
summary["purchaseOrdersSummary"] = [
PurchaseOrderSummary(
supplierName=po.get("supplier_name", "Unknown"),
@@ -341,6 +344,8 @@ async def get_orchestration_summary(
batch_data = await production_client.get_todays_batches(tenant_id)
if batch_data:
batches = batch_data.get("batches", [])
# Override stale orchestration count with actual real-time batch count
summary["productionBatchesCreated"] = len(batches)
summary["productionBatchesSummary"] = [
ProductionBatchSummary(
productName=batch.get("product_name", "Unknown"),

View File

@@ -0,0 +1,264 @@
"""
Internal Demo API Endpoints for Orchestrator Service
Used by demo_session service to clone data for virtual demo tenants
"""
from fastapi import APIRouter, Depends, HTTPException, Header
from typing import Dict, Any
from uuid import UUID
import structlog
import os
from app.core.database import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, delete, func
from app.models.orchestration_run import OrchestrationRun
import uuid
from datetime import datetime, timezone, timedelta
from typing import Optional
router = APIRouter()
logger = structlog.get_logger()
# Internal API key for service-to-service communication
INTERNAL_API_KEY = os.getenv("INTERNAL_API_KEY", "dev-internal-key-change-in-production")
def verify_internal_api_key(x_internal_api_key: str = Header(...)):
"""Verify internal API key for service-to-service communication"""
if x_internal_api_key != INTERNAL_API_KEY:
raise HTTPException(status_code=403, detail="Invalid internal API key")
return True
@router.post("/internal/demo/clone")
async def clone_demo_data(
base_tenant_id: str,
virtual_tenant_id: str,
demo_account_type: str,
session_id: Optional[str] = None,
session_created_at: Optional[str] = None,
db: AsyncSession = Depends(get_db),
_: bool = Depends(verify_internal_api_key)
):
"""
Clone orchestration run demo data from base tenant to virtual tenant
This endpoint is called by the demo_session service during session initialization.
It clones orchestration runs with date adjustments to make them appear recent.
"""
start_time = datetime.now(timezone.utc)
# Parse session_created_at or use current time
if session_created_at:
try:
reference_time = datetime.fromisoformat(session_created_at.replace('Z', '+00:00'))
except:
reference_time = datetime.now(timezone.utc)
else:
reference_time = datetime.now(timezone.utc)
logger.info(
"Starting orchestration runs cloning with date adjustment",
base_tenant_id=base_tenant_id,
virtual_tenant_id=virtual_tenant_id,
demo_account_type=demo_account_type,
session_id=session_id,
reference_time=reference_time.isoformat()
)
try:
base_uuid = uuid.UUID(base_tenant_id)
virtual_uuid = uuid.UUID(virtual_tenant_id)
# Fetch base tenant orchestration runs
# Get all completed and partial_success runs from the base tenant
result = await db.execute(
select(OrchestrationRun)
.where(OrchestrationRun.tenant_id == base_uuid)
.order_by(OrchestrationRun.started_at.desc())
.limit(10) # Clone last 10 runs for demo
)
base_runs = list(result.scalars().all())
runs_cloned = 0
# Clone each orchestration run with date adjustment
for base_run in base_runs:
# Calculate time offset: how old was this run relative to when it was created
# We'll adjust all timestamps to be relative to the session creation time
if base_run.started_at:
# Calculate how many days ago this run was from a reference point
# Use a fixed reference date for consistency
reference_date = datetime(2025, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
time_offset = base_run.started_at - reference_date
# Apply this offset to the current reference time
new_started_at = reference_time + time_offset
else:
new_started_at = reference_time - timedelta(hours=2)
# Adjust completed_at if it exists
if base_run.completed_at and base_run.started_at:
duration = base_run.completed_at - base_run.started_at
new_completed_at = new_started_at + duration
else:
new_completed_at = None
# Adjust all step timestamps proportionally
def adjust_timestamp(original_timestamp):
if not original_timestamp or not base_run.started_at:
return None
step_offset = original_timestamp - base_run.started_at
return new_started_at + step_offset
# Create new orchestration run for virtual tenant
new_run = OrchestrationRun(
id=uuid.uuid4(),
tenant_id=virtual_uuid,
run_number=f"{base_run.run_number}-DEMO",
status=base_run.status,
run_type=base_run.run_type,
priority=base_run.priority,
started_at=new_started_at,
completed_at=new_completed_at,
duration_seconds=base_run.duration_seconds,
# Forecasting step
forecasting_started_at=adjust_timestamp(base_run.forecasting_started_at),
forecasting_completed_at=adjust_timestamp(base_run.forecasting_completed_at),
forecasting_status=base_run.forecasting_status,
forecasting_error=base_run.forecasting_error,
# Production step
production_started_at=adjust_timestamp(base_run.production_started_at),
production_completed_at=adjust_timestamp(base_run.production_completed_at),
production_status=base_run.production_status,
production_error=base_run.production_error,
# Procurement step
procurement_started_at=adjust_timestamp(base_run.procurement_started_at),
procurement_completed_at=adjust_timestamp(base_run.procurement_completed_at),
procurement_status=base_run.procurement_status,
procurement_error=base_run.procurement_error,
# Notification step
notification_started_at=adjust_timestamp(base_run.notification_started_at),
notification_completed_at=adjust_timestamp(base_run.notification_completed_at),
notification_status=base_run.notification_status,
notification_error=base_run.notification_error,
# AI Insights (if exists)
ai_insights_started_at=adjust_timestamp(base_run.ai_insights_started_at) if hasattr(base_run, 'ai_insights_started_at') else None,
ai_insights_completed_at=adjust_timestamp(base_run.ai_insights_completed_at) if hasattr(base_run, 'ai_insights_completed_at') else None,
ai_insights_status=base_run.ai_insights_status if hasattr(base_run, 'ai_insights_status') else None,
ai_insights_generated=base_run.ai_insights_generated if hasattr(base_run, 'ai_insights_generated') else None,
ai_insights_posted=base_run.ai_insights_posted if hasattr(base_run, 'ai_insights_posted') else None,
# Results summary
forecasts_generated=base_run.forecasts_generated,
production_batches_created=base_run.production_batches_created,
procurement_plans_created=base_run.procurement_plans_created,
purchase_orders_created=base_run.purchase_orders_created,
notifications_sent=base_run.notifications_sent,
# Performance metrics
fulfillment_rate=base_run.fulfillment_rate,
on_time_delivery_rate=base_run.on_time_delivery_rate,
cost_accuracy=base_run.cost_accuracy,
quality_score=base_run.quality_score,
# Data
forecast_data=base_run.forecast_data,
run_metadata=base_run.run_metadata,
# Metadata
triggered_by=base_run.triggered_by,
created_at=reference_time,
updated_at=reference_time
)
db.add(new_run)
await db.flush()
runs_cloned += 1
await db.commit()
duration_ms = int((datetime.now(timezone.utc) - start_time).total_seconds() * 1000)
logger.info(
"Orchestration runs cloned successfully",
virtual_tenant_id=str(virtual_tenant_id),
runs_cloned=runs_cloned,
duration_ms=duration_ms
)
return {
"service": "orchestrator",
"status": "completed",
"success": True,
"records_cloned": runs_cloned,
"runs_cloned": runs_cloned,
"duration_ms": duration_ms
}
except Exception as e:
logger.error("Failed to clone orchestration runs", error=str(e), exc_info=True)
await db.rollback()
raise HTTPException(status_code=500, detail=f"Failed to clone orchestration runs: {str(e)}")
@router.delete("/internal/demo/tenant/{virtual_tenant_id}")
async def delete_demo_data(
virtual_tenant_id: str,
db: AsyncSession = Depends(get_db),
_: bool = Depends(verify_internal_api_key)
):
"""Delete all orchestration runs for a virtual demo tenant"""
logger.info("Deleting orchestration runs for virtual tenant", virtual_tenant_id=virtual_tenant_id)
start_time = datetime.now(timezone.utc)
try:
virtual_uuid = uuid.UUID(virtual_tenant_id)
# Count records
run_count = await db.scalar(
select(func.count(OrchestrationRun.id))
.where(OrchestrationRun.tenant_id == virtual_uuid)
)
# Delete orchestration runs
await db.execute(
delete(OrchestrationRun)
.where(OrchestrationRun.tenant_id == virtual_uuid)
)
await db.commit()
duration_ms = int((datetime.now(timezone.utc) - start_time).total_seconds() * 1000)
logger.info(
"Orchestration runs deleted successfully",
virtual_tenant_id=virtual_tenant_id,
duration_ms=duration_ms
)
return {
"service": "orchestrator",
"status": "deleted",
"virtual_tenant_id": virtual_tenant_id,
"records_deleted": {
"orchestration_runs": run_count,
"total": run_count
},
"duration_ms": duration_ms
}
except Exception as e:
logger.error("Failed to delete orchestration runs", error=str(e), exc_info=True)
await db.rollback()
raise HTTPException(status_code=500, detail=str(e))
@router.get("/internal/demo/clone/health")
async def health_check(_: bool = Depends(verify_internal_api_key)):
"""Health check for demo cloning endpoint"""
return {"status": "healthy", "service": "orchestrator"}

View File

@@ -99,8 +99,8 @@ service.add_router(orchestration_router)
service.add_router(dashboard_router)
# INTERNAL: Service-to-service endpoints
# from app.api import internal_demo
# service.add_router(internal_demo.router)
from app.api import internal_demo
service.add_router(internal_demo.router)
@app.middleware("http")

View File

@@ -417,14 +417,38 @@ class DashboardService:
# Get reasoning type and convert to i18n key
reasoning_type = reasoning_data.get('type', 'inventory_replenishment')
reasoning_type_i18n_key = self._get_reasoning_type_i18n_key(reasoning_type, context="purchaseOrder")
# Check if enhanced mode (has product_details with supply chain intelligence)
is_enhanced_mode = reasoning_data.get('metadata', {}).get('enhanced_mode', False)
# Use enhanced i18n key if available
if is_enhanced_mode and reasoning_type == 'low_stock_detection':
reasoning_type_i18n_key = "reasoning.purchaseOrder.low_stock_detection_detailed"
else:
reasoning_type_i18n_key = self._get_reasoning_type_i18n_key(reasoning_type, context="purchaseOrder")
# Preprocess parameters for i18n - MUST create a copy to avoid modifying immutable database objects
params = dict(reasoning_data.get('parameters', {}))
# Convert product_names array to product_names_joined string
if 'product_names' in params and isinstance(params['product_names'], list):
params['product_names_joined'] = ', '.join(params['product_names'])
# Convert critical_products array to indexed params and joined string for i18n
if 'critical_products' in params and isinstance(params['critical_products'], list):
critical_prods = params['critical_products']
# Add indexed params for select/plural statements
for i, prod in enumerate(critical_prods[:3]): # Limit to first 3
params[f'critical_products_{i}'] = prod
params['critical_products_joined'] = ', '.join(critical_prods)
# Convert affected_batches array to indexed params for i18n
if 'affected_batches' in params and isinstance(params['affected_batches'], list):
batches = params['affected_batches']
for i, batch in enumerate(batches[:3]): # Limit to first 3
params[f'affected_batches_{i}'] = batch
params['affected_batches_joined'] = ', '.join(batches)
actions.append({
"id": po["id"],
"type": ActionType.APPROVE_PO,
@@ -594,7 +618,8 @@ class DashboardService:
if actual_start and planned_end:
total_duration = (planned_end - actual_start).total_seconds()
elapsed = (now - actual_start).total_seconds()
progress = min(int((elapsed / total_duration) * 100), 99)
# Ensure progress is never negative (defensive programming)
progress = max(0, min(int((elapsed / total_duration) * 100), 99))
else:
progress = 50
status_icon = "🔄"
@@ -604,10 +629,12 @@ class DashboardService:
"params": {}
}
else:
# PENDING, SCHEDULED, or any other status
progress = 0
status_icon = ""
status_text = "PENDING"
status_text = status # Use actual status
status_i18n = {
"key": "production.status.pending",
"key": f"production.status.{status.lower()}",
"params": {}
}