Fix new services implementation 3

This commit is contained in:
Urtzi Alfaro
2025-08-14 16:47:34 +02:00
parent 0951547e92
commit 03737430ee
51 changed files with 657 additions and 982 deletions

View File

@@ -28,22 +28,22 @@ router = APIRouter()
training_service = TrainingService()
@router.get("/tenants/{tenant_id}/models/{product_name}/active")
@router.get("/tenants/{tenant_id}/models/{inventory_product_id}/active")
async def get_active_model(
tenant_id: str = Path(..., description="Tenant ID"),
product_name: str = Path(..., description="Product name"),
inventory_product_id: str = Path(..., description="Inventory product UUID"),
db: AsyncSession = Depends(get_db)
):
"""
Get the active model for a product - used by forecasting service
"""
try:
logger.debug("Getting active model", tenant_id=tenant_id, product_name=product_name)
logger.debug("Getting active model", tenant_id=tenant_id, inventory_product_id=inventory_product_id)
# ✅ FIX: Wrap SQL with text() for SQLAlchemy 2.0 and add case-insensitive product name matching
query = text("""
SELECT * FROM trained_models
WHERE tenant_id = :tenant_id
AND LOWER(product_name) = LOWER(:product_name)
AND inventory_product_id = :inventory_product_id
AND is_active = true
AND is_production = true
ORDER BY created_at DESC
@@ -52,16 +52,16 @@ async def get_active_model(
result = await db.execute(query, {
"tenant_id": tenant_id,
"product_name": product_name
"inventory_product_id": inventory_product_id
})
model_record = result.fetchone()
if not model_record:
logger.info("No active model found", tenant_id=tenant_id, product_name=product_name)
logger.info("No active model found", tenant_id=tenant_id, inventory_product_id=inventory_product_id)
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No active model found for product {product_name}"
detail=f"No active model found for product {inventory_product_id}"
)
# ✅ FIX: Wrap update query with text() too
@@ -99,11 +99,11 @@ async def get_active_model(
raise
except Exception as e:
error_msg = str(e) if str(e) else f"{type(e).__name__}: {repr(e)}"
logger.error(f"Failed to get active model: {error_msg}", tenant_id=tenant_id, product_name=product_name)
logger.error(f"Failed to get active model: {error_msg}", tenant_id=tenant_id, inventory_product_id=inventory_product_id)
# Handle client disconnection gracefully
if "EndOfStream" in str(type(e)) or "WouldBlock" in str(type(e)):
logger.info("Client disconnected during model retrieval", tenant_id=tenant_id, product_name=product_name)
logger.info("Client disconnected during model retrieval", tenant_id=tenant_id, inventory_product_id=inventory_product_id)
raise HTTPException(
status_code=status.HTTP_408_REQUEST_TIMEOUT,
detail="Request connection closed"
@@ -205,7 +205,7 @@ async def list_models(
models.append({
"model_id": str(record.id),
"tenant_id": str(record.tenant_id),
"product_name": record.product_name,
"inventory_product_id": str(record.inventory_product_id),
"model_type": record.model_type,
"model_path": record.model_path,
"version": 1, # Default version