Start fixing forecast service API 8

This commit is contained in:
Urtzi Alfaro
2025-07-29 18:12:06 +02:00
parent fe88b696c2
commit 322f1743eb
4 changed files with 19 additions and 18 deletions

View File

@@ -89,8 +89,8 @@ app.add_middleware(
) )
# Include API routers # Include API routers
app.include_router(forecasts.router, prefix="/api/v1/forecasts", tags=["forecasts"]) app.include_router(forecasts.router, prefix="/api/v1", tags=["forecasts"])
app.include_router(predictions.router, prefix="/api/v1/predictions", tags=["predictions"]) app.include_router(predictions.router, prefix="/api/v1", tags=["predictions"])
@app.get("/health") @app.get("/health")
async def health_check(): async def health_check():

View File

@@ -129,7 +129,7 @@ class ForecastingService:
except Exception as e: except Exception as e:
logger.error("Error generating forecast", logger.error("Error generating forecast",
error=str(e), error=str(e),
tenant_id=request.tenant_id, tenant_id=tenant_id,
product=request.product_name) product=request.product_name)
raise raise
@@ -247,11 +247,13 @@ class ForecastingService:
async def _get_latest_model(self, tenant_id: str, product_name: str) -> Optional[Dict[str, Any]]: async def _get_latest_model(self, tenant_id: str, product_name: str) -> Optional[Dict[str, Any]]:
"""Get the latest trained model for a tenant/product combination""" """Get the latest trained model for a tenant/product combination"""
try: try:
model_data = await self.model_client.get_best_model_for_forecasting(tenant_id, product_name) # Pass the product_name to the model client
model_data = await self.model_client.get_best_model_for_forecasting(
tenant_id=tenant_id,
product_name=product_name # Make sure to pass product_name
)
return model_data return model_data
except Exception as e: except Exception as e:
logger.error("Error getting latest model", error=str(e)) logger.error("Error getting latest model", error=str(e))
raise raise

View File

@@ -64,9 +64,10 @@ class ModelClient:
""" """
try: try:
# Get latest model # Get latest model
latest_model = await self.clients.training.get_latest_model( latest_model = await self.clients.training.get_active_model_for_product(
tenant_id=tenant_id, tenant_id=tenant_id,
model_type="forecasting" model_type="forecasting",
product_name=product_id
) )
if not latest_model: if not latest_model:

View File

@@ -86,19 +86,17 @@ class TrainingServiceClient(BaseServiceClient):
result = await self.get("models", tenant_id=tenant_id, params=params) result = await self.get("models", tenant_id=tenant_id, params=params)
return result.get("models", []) if result else None return result.get("models", []) if result else None
async def get_latest_model( async def get_active_model_for_product(
self, self,
tenant_id: str, tenant_id: str,
model_type: Optional[str] = None product_name: str
) -> Optional[Dict[str, Any]]: ) -> Optional[Dict[str, Any]]:
"""Get the latest trained model for a tenant""" """
params = {"latest": "true"} Get the active model for a specific product
if model_type: This is the preferred method since models are stored per product.
params["model_type"] = model_type """
result = await self.get(f"models/{product_name}/active", tenant_id=tenant_id)
result = await self.get("models", tenant_id=tenant_id, params=params) return result
models = result.get("models", []) if result else []
return models[0] if models else None
async def deploy_model(self, tenant_id: str, model_id: str) -> Optional[Dict[str, Any]]: async def deploy_model(self, tenant_id: str, model_id: str) -> Optional[Dict[str, Any]]:
"""Deploy a trained model""" """Deploy a trained model"""