Fix forecasting service

This commit is contained in:
Urtzi Alfaro
2025-07-21 20:43:17 +02:00
parent 0e7ca10a29
commit 153ae3f154
11 changed files with 107 additions and 534 deletions

View File

@@ -12,7 +12,10 @@ from typing import List, Optional
from datetime import date
from app.core.database import get_db
from app.core.auth import get_current_user_from_headers
from shared.auth.decorators import (
get_current_user_dep,
get_current_tenant_id_dep
)
from app.services.forecasting_service import ForecastingService
from app.schemas.forecasts import (
ForecastRequest, ForecastResponse, BatchForecastRequest,
@@ -30,13 +33,14 @@ forecasting_service = ForecastingService()
async def create_single_forecast(
request: ForecastRequest,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep),
current_user: dict = Depends(get_current_user_dep)
):
"""Generate a single product forecast"""
try:
# Verify tenant access
if str(request.tenant_id) != str(current_user.get("tenant_id")):
if str(request.tenant_id) != tenant_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied to this tenant"
@@ -88,13 +92,14 @@ async def create_single_forecast(
async def create_batch_forecast(
request: BatchForecastRequest,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep),
current_user: dict = Depends(get_current_user_dep)
):
"""Generate batch forecasts for multiple products"""
try:
# Verify tenant access
if str(request.tenant_id) != str(current_user.get("tenant_id")):
if str(request.tenant_id) != tenant_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied to this tenant"
@@ -172,12 +177,11 @@ async def list_forecasts(
end_date: Optional[date] = Query(None),
product_name: Optional[str] = Query(None),
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep)
):
"""List forecasts with filtering"""
try:
tenant_id = str(current_user.get("tenant_id"))
# Get forecasts
forecasts = await forecasting_service.get_forecasts(
@@ -230,15 +234,14 @@ async def list_forecasts(
async def get_forecast_alerts(
active_only: bool = Query(True),
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep),
current_user: dict = Depends(get_current_user_dep)
):
"""Get forecast alerts for tenant"""
try:
from sqlalchemy import select, and_
tenant_id = current_user.get("tenant_id")
# Build query
query = select(ForecastAlert).where(
ForecastAlert.tenant_id == tenant_id
@@ -281,7 +284,8 @@ async def get_forecast_alerts(
async def acknowledge_alert(
alert_id: str,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep),
current_user: dict = Depends(get_current_user_dep)
):
"""Acknowledge a forecast alert"""
@@ -289,8 +293,6 @@ async def acknowledge_alert(
from sqlalchemy import select, update
from datetime import datetime
tenant_id = current_user.get("tenant_id")
# Get alert
result = await db.execute(
select(ForecastAlert).where(

View File

@@ -12,7 +12,10 @@ from typing import List, Dict, Any
from datetime import date, datetime, timedelta
from app.core.database import get_db
from app.core.auth import get_current_user_from_headers
from shared.auth.decorators import (
get_current_user_dep,
get_current_tenant_id_dep
)
from app.services.prediction_service import PredictionService
from app.schemas.forecasts import ForecastRequest
@@ -28,12 +31,11 @@ async def get_realtime_prediction(
location: str,
forecast_date: date,
features: Dict[str, Any],
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep)
):
"""Get real-time prediction without storing in database"""
try:
tenant_id = str(current_user.get("tenant_id"))
# Get latest model
from app.services.forecasting_service import ForecastingService
@@ -83,13 +85,12 @@ async def get_quick_prediction(
product_name: str,
location: str = Query(...),
days_ahead: int = Query(1, ge=1, le=7),
current_user: dict = Depends(get_current_user_from_headers)
tenant_id: str = Depends(get_current_tenant_id_dep)
):
"""Get quick prediction for next few days"""
try:
tenant_id = str(current_user.get("tenant_id"))
# Generate predictions for the next N days
predictions = []