REFACTOR production scheduler
This commit is contained in:
@@ -11,6 +11,7 @@ import uuid
|
||||
|
||||
from app.services.forecasting_service import EnhancedForecastingService
|
||||
from app.services.prediction_service import PredictionService
|
||||
from app.services.forecast_cache import get_forecast_cache_service
|
||||
from app.schemas.forecasts import (
|
||||
ForecastRequest, ForecastResponse, BatchForecastRequest,
|
||||
BatchForecastResponse, MultiDayForecastResponse
|
||||
@@ -53,7 +54,7 @@ async def generate_single_forecast(
|
||||
current_user: dict = Depends(get_current_user_dep),
|
||||
enhanced_forecasting_service: EnhancedForecastingService = Depends(get_enhanced_forecasting_service)
|
||||
):
|
||||
"""Generate a single product forecast"""
|
||||
"""Generate a single product forecast with caching support"""
|
||||
metrics = get_metrics_collector(request_obj)
|
||||
|
||||
try:
|
||||
@@ -65,11 +66,41 @@ async def generate_single_forecast(
|
||||
if metrics:
|
||||
metrics.increment_counter("single_forecasts_total")
|
||||
|
||||
# Initialize cache service
|
||||
cache_service = get_forecast_cache_service(settings.REDIS_URL)
|
||||
|
||||
# Check cache first
|
||||
cached_forecast = await cache_service.get_cached_forecast(
|
||||
tenant_id=uuid.UUID(tenant_id),
|
||||
product_id=uuid.UUID(request.inventory_product_id),
|
||||
forecast_date=request.forecast_date
|
||||
)
|
||||
|
||||
if cached_forecast:
|
||||
if metrics:
|
||||
metrics.increment_counter("forecast_cache_hits_total")
|
||||
logger.info("Returning cached forecast",
|
||||
tenant_id=tenant_id,
|
||||
forecast_id=cached_forecast.get('id'))
|
||||
return ForecastResponse(**cached_forecast)
|
||||
|
||||
# Cache miss - generate forecast
|
||||
if metrics:
|
||||
metrics.increment_counter("forecast_cache_misses_total")
|
||||
|
||||
forecast = await enhanced_forecasting_service.generate_forecast(
|
||||
tenant_id=tenant_id,
|
||||
request=request
|
||||
)
|
||||
|
||||
# Cache the result
|
||||
await cache_service.cache_forecast(
|
||||
tenant_id=uuid.UUID(tenant_id),
|
||||
product_id=uuid.UUID(request.inventory_product_id),
|
||||
forecast_date=request.forecast_date,
|
||||
forecast_data=forecast.dict()
|
||||
)
|
||||
|
||||
if metrics:
|
||||
metrics.increment_counter("single_forecasts_success_total")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user