Start fixing forecast service API 5

This commit is contained in:
Urtzi Alfaro
2025-07-29 17:24:56 +02:00
parent c8e0e1299e
commit fd5d701221
6 changed files with 17 additions and 16 deletions

View File

@@ -47,7 +47,7 @@ async def create_single_forecast(
) )
# Generate forecast # Generate forecast
forecast = await forecasting_service.generate_forecast(tenant_id, request, db) forecast = await forecasting_service.generate_forecast(request, db)
# Convert to response model # Convert to response model
return ForecastResponse( return ForecastResponse(

View File

@@ -108,7 +108,7 @@ async def health_check():
@app.get("/metrics") @app.get("/metrics")
async def get_metrics(): async def get_metrics():
"""Metrics endpoint for Prometheus""" """Metrics endpoint for Prometheus"""
return metrics_collector.generate_latest() return metrics_collector.get_metrics()
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn

View File

@@ -24,7 +24,12 @@ class ForecastRequest(BaseModel):
"""Request schema for generating forecasts""" """Request schema for generating forecasts"""
tenant_id: str = Field(..., description="Tenant ID") tenant_id: str = Field(..., description="Tenant ID")
product_name: str = Field(..., description="Product name") product_name: str = Field(..., description="Product name")
forecast_date: date = Field(..., description="Date for which to generate forecast") forecast_date: date = Field(..., description="Starting date for forecast")
forecast_days: int = Field(1, ge=1, le=30, description="Number of days to forecast")
location: str = Field(..., description="Location identifier")
# Optional parameters - internally handled
confidence_level: float = Field(0.8, ge=0.5, le=0.95, description="Confidence level")
@validator('forecast_date') @validator('forecast_date')
def validate_forecast_date(cls, v): def validate_forecast_date(cls, v):
@@ -37,14 +42,7 @@ class BatchForecastRequest(BaseModel):
tenant_id: str = Field(..., description="Tenant ID") tenant_id: str = Field(..., description="Tenant ID")
batch_name: str = Field(..., description="Batch name for tracking") batch_name: str = Field(..., description="Batch name for tracking")
products: List[str] = Field(..., description="List of product names") products: List[str] = Field(..., description="List of product names")
location: str = Field(..., description="Location identifier")
forecast_days: int = Field(7, ge=1, le=30, description="Number of days to forecast") forecast_days: int = Field(7, ge=1, le=30, description="Number of days to forecast")
business_type: BusinessType = Field(BusinessType.INDIVIDUAL, description="Business model type")
# Options
include_weather: bool = Field(True, description="Include weather data")
include_traffic: bool = Field(True, description="Include traffic data")
confidence_level: float = Field(0.8, ge=0.5, le=0.95, description="Confidence level")
class ForecastResponse(BaseModel): class ForecastResponse(BaseModel):
"""Response schema for forecast results""" """Response schema for forecast results"""

View File

@@ -249,7 +249,7 @@ class ForecastingService:
"""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.data_client.get_best_model_for_forecasting(tenant_id, product_name) model_data = await self.model_client.get_best_model_for_forecasting(tenant_id, product_name)
return model_data return model_data
except Exception as e: except Exception as e:

View File

@@ -163,7 +163,7 @@ async def health_check():
async def metrics(): async def metrics():
"""Prometheus metrics endpoint""" """Prometheus metrics endpoint"""
if metrics_collector: if metrics_collector:
return metrics_collector.generate_latest() return metrics_collector.get_metrics()
return {"metrics": "not_available"} return {"metrics": "not_available"}
# Exception handlers # Exception handlers

View File

@@ -572,13 +572,16 @@ log_step "5.1. Testing basic dashboard functionality"
# Use a real product name from our CSV for forecasting # Use a real product name from our CSV for forecasting
FIRST_PRODUCT=$(echo "$REAL_PRODUCTS" | sed 's/"//g' | cut -d',' -f1) FIRST_PRODUCT=$(echo "$REAL_PRODUCTS" | sed 's/"//g' | cut -d',' -f1)
FORECAST_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/tenants/$TENANT_ID/forecast/single" \ FORECAST_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/tenants/$TENANT_ID/forecasts/single" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \ -H "Authorization: Bearer $ACCESS_TOKEN" \
-d "{ -d "{
\"products\": [\"$FIRST_PRODUCT\"], \"product_name\": [\"$FIRST_PRODUCT\"],
\"forecast_days\": 7, \"forecast_date\": "2025-11-30",
\"date\": \"2025-09-15\" \"forecast_days\": 1,
\"date\": \"2025-09-15\",
\"location\": \"madrid_centro\",
\"confidence_level\": 0.85
}") }")
if echo "$FORECAST_RESPONSE" | grep -q '"predictions"\|"forecast"'; then if echo "$FORECAST_RESPONSE" | grep -q '"predictions"\|"forecast"'; then