Fix new services implementation 8

This commit is contained in:
Urtzi Alfaro
2025-08-15 23:11:53 +02:00
parent 399ba80067
commit 055ce10c66
5 changed files with 161 additions and 80 deletions

View File

@@ -83,10 +83,68 @@ class EnhancedForecastingService:
skip: int = 0, limit: int = 100) -> List[Dict]:
"""Get tenant forecasts with filtering"""
try:
# Implementation would use repository pattern to fetch forecasts
return []
# Get session and initialize repositories
async with self.database_manager.get_background_session() as session:
repos = await self._init_repositories(session)
# Build filters
filters = {"tenant_id": tenant_id}
if inventory_product_id:
filters["inventory_product_id"] = inventory_product_id
# If date range specified, use specialized method
if start_date and end_date:
forecasts = await repos['forecast'].get_forecasts_by_date_range(
tenant_id=tenant_id,
start_date=start_date,
end_date=end_date,
inventory_product_id=inventory_product_id
)
else:
# Use general get_multi with tenant filter
forecasts = await repos['forecast'].get_multi(
filters=filters,
skip=skip,
limit=limit,
order_by="forecast_date",
order_desc=True
)
# Convert to dict format
forecast_list = []
for forecast in forecasts:
forecast_dict = {
"id": str(forecast.id),
"tenant_id": str(forecast.tenant_id),
"inventory_product_id": forecast.inventory_product_id,
"location": forecast.location,
"forecast_date": forecast.forecast_date.isoformat(),
"predicted_demand": float(forecast.predicted_demand),
"confidence_lower": float(forecast.confidence_lower),
"confidence_upper": float(forecast.confidence_upper),
"confidence_level": float(forecast.confidence_level),
"model_id": forecast.model_id,
"model_version": forecast.model_version,
"algorithm": forecast.algorithm,
"business_type": forecast.business_type,
"is_holiday": forecast.is_holiday,
"is_weekend": forecast.is_weekend,
"processing_time_ms": forecast.processing_time_ms,
"created_at": forecast.created_at.isoformat() if forecast.created_at else None
}
forecast_list.append(forecast_dict)
logger.info("Retrieved tenant forecasts",
tenant_id=tenant_id,
count=len(forecast_list),
filters=filters)
return forecast_list
except Exception as e:
logger.error("Failed to get tenant forecasts", error=str(e))
logger.error("Failed to get tenant forecasts",
tenant_id=tenant_id,
error=str(e))
raise
async def get_forecast_by_id(self, forecast_id: str) -> Optional[Dict]:
@@ -199,7 +257,7 @@ class EnhancedForecastingService:
date=request.forecast_date.isoformat())
# Get session and initialize repositories
async with self.database_manager.get_session() as session:
async with self.database_manager.get_background_session() as session:
repos = await self._init_repositories(session)
# Step 1: Check cache first