From dfb619a7b552142ddcfa748f67eb749e65beb096 Mon Sep 17 00:00:00 2001 From: Urtzi Alfaro Date: Tue, 29 Jul 2025 13:02:42 +0200 Subject: [PATCH] Start fixing forecast service API --- services/forecasting/app/api/forecasts.py | 22 +++++++++---------- .../app/services/forecasting_service.py | 8 ++----- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/services/forecasting/app/api/forecasts.py b/services/forecasting/app/api/forecasts.py index 9d1f6a8c..1a9b6ca9 100644 --- a/services/forecasting/app/api/forecasts.py +++ b/services/forecasting/app/api/forecasts.py @@ -6,7 +6,7 @@ Forecast API endpoints """ import structlog -from fastapi import APIRouter, Depends, HTTPException, status, Query +from fastapi import APIRouter, Depends, HTTPException, status, Query, Path from sqlalchemy.ext.asyncio import AsyncSession from typing import List, Optional from datetime import date @@ -29,11 +29,11 @@ router = APIRouter() # Initialize service forecasting_service = ForecastingService() -@router.post("/single", response_model=ForecastResponse) +@router.post("/tenants/{tenant_id}/forecasts/single", response_model=ForecastResponse) async def create_single_forecast( request: ForecastRequest, db: AsyncSession = Depends(get_db), - tenant_id: str = Depends(get_current_tenant_id_dep), + tenant_id: str = Path(..., description="Tenant ID"), current_user: dict = Depends(get_current_user_dep) ): """Generate a single product forecast""" @@ -88,11 +88,11 @@ async def create_single_forecast( detail="Internal server error" ) -@router.post("/batch", response_model=BatchForecastResponse) +@router.post("/tenants/{tenant_id}/forecasts/batch", response_model=BatchForecastResponse) async def create_batch_forecast( request: BatchForecastRequest, db: AsyncSession = Depends(get_db), - tenant_id: str = Depends(get_current_tenant_id_dep), + tenant_id: str = Path(..., description="Tenant ID"), current_user: dict = Depends(get_current_user_dep) ): """Generate batch forecasts for multiple products""" @@ -170,14 +170,14 @@ async def create_batch_forecast( detail="Internal server error" ) -@router.get("/list", response_model=List[ForecastResponse]) +@router.get("/tenants/{tenant_id}/forecasts/list", response_model=List[ForecastResponse]) async def list_forecasts( location: str, start_date: Optional[date] = Query(None), end_date: Optional[date] = Query(None), product_name: Optional[str] = Query(None), db: AsyncSession = Depends(get_db), - tenant_id: str = Depends(get_current_tenant_id_dep) + tenant_id: str = Path(..., description="Tenant ID") ): """List forecasts with filtering""" @@ -230,11 +230,11 @@ async def list_forecasts( detail="Internal server error" ) -@router.get("/alerts", response_model=List[AlertResponse]) +@router.get("/tenants/{tenant_id}/forecasts/alerts", response_model=List[AlertResponse]) async def get_forecast_alerts( active_only: bool = Query(True), db: AsyncSession = Depends(get_db), - tenant_id: str = Depends(get_current_tenant_id_dep), + tenant_id: str = Path(..., description="Tenant ID"), current_user: dict = Depends(get_current_user_dep) ): """Get forecast alerts for tenant""" @@ -280,11 +280,11 @@ async def get_forecast_alerts( detail="Internal server error" ) -@router.put("/alerts/{alert_id}/acknowledge") +@router.put("/tenants/{tenant_id}/forecasts/alerts/{alert_id}/acknowledge") async def acknowledge_alert( alert_id: str, db: AsyncSession = Depends(get_db), - tenant_id: str = Depends(get_current_tenant_id_dep), + tenant_id: str = Path(..., description="Tenant ID"), current_user: dict = Depends(get_current_user_dep) ): """Acknowledge a forecast alert""" diff --git a/services/forecasting/app/services/forecasting_service.py b/services/forecasting/app/services/forecasting_service.py index 8eccac77..1ce60dd2 100644 --- a/services/forecasting/app/services/forecasting_service.py +++ b/services/forecasting/app/services/forecasting_service.py @@ -250,12 +250,8 @@ class ForecastingService: # Call training service to get model information async with httpx.AsyncClient() as client: response = await client.get( - f"{settings.TRAINING_SERVICE_URL}/api/v1/models/latest", - params={ - "tenant_id": tenant_id, - "product_name": product_name, - "location": location - }, + f"{settings.TRAINING_SERVICE_URL}/tenants/{tenant_id}/models/{product_name}/active", + params={}, headers={"X-Service-Auth": settings.SERVICE_AUTH_TOKEN} )