Start fixing forecast service 16
This commit is contained in:
@@ -89,16 +89,39 @@ class ForecastingService:
|
||||
prediction=adjusted_prediction['prediction'])
|
||||
|
||||
return ForecastResponse(
|
||||
id=forecast.id,
|
||||
forecast_date=forecast.forecast_date,
|
||||
id=str(forecast.id),
|
||||
tenant_id=str(forecast.tenant_id),
|
||||
product_name=forecast.product_name,
|
||||
predicted_quantity=forecast.predicted_quantity,
|
||||
location=forecast.location,
|
||||
forecast_date=forecast.forecast_date,
|
||||
|
||||
# Predictions
|
||||
predicted_demand=forecast.predicted_demand,
|
||||
confidence_lower=forecast.confidence_lower,
|
||||
confidence_upper=forecast.confidence_upper,
|
||||
confidence_level=forecast.confidence_level,
|
||||
lower_bound=forecast.lower_bound,
|
||||
upper_bound=forecast.upper_bound,
|
||||
model_id=forecast.model_id,
|
||||
|
||||
# Model info
|
||||
model_id=str(forecast.model_id),
|
||||
model_version=forecast.model_version,
|
||||
algorithm=forecast.algorithm,
|
||||
|
||||
# Context
|
||||
business_type=forecast.business_type,
|
||||
is_holiday=forecast.is_holiday,
|
||||
is_weekend=forecast.is_weekend,
|
||||
day_of_week=forecast.day_of_week,
|
||||
|
||||
# External factors
|
||||
weather_temperature=forecast.weather_temperature,
|
||||
weather_precipitation=forecast.weather_precipitation,
|
||||
weather_description=forecast.weather_description,
|
||||
traffic_volume=forecast.traffic_volume,
|
||||
|
||||
# Metadata
|
||||
created_at=forecast.created_at,
|
||||
external_factors=forecast.external_factors
|
||||
processing_time_ms=forecast.processing_time_ms,
|
||||
features_used=forecast.features_used
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
@@ -171,7 +194,7 @@ class ForecastingService:
|
||||
await self._add_weather_features_with_fallbacks(features, tenant_id)
|
||||
|
||||
# Add traffic data with fallbacks
|
||||
# await self._add_traffic_features_with_fallbacks(features, tenant_id)
|
||||
await self._add_traffic_features_with_fallbacks(features, tenant_id)
|
||||
|
||||
return features
|
||||
|
||||
@@ -248,23 +271,23 @@ class ForecastingService:
|
||||
) -> None:
|
||||
"""Add traffic features with fallbacks"""
|
||||
|
||||
try:
|
||||
traffic_data = await self.data_client.get_traffic_data(
|
||||
tenant_id=tenant_id,
|
||||
latitude=40.4168,
|
||||
longitude=-3.7038
|
||||
)
|
||||
|
||||
if traffic_data:
|
||||
features.update({
|
||||
"traffic_volume": traffic_data.get("traffic_volume", 100),
|
||||
"pedestrian_count": traffic_data.get("pedestrian_count", 50),
|
||||
})
|
||||
logger.info("Traffic data acquired successfully", tenant_id=tenant_id)
|
||||
return
|
||||
# try:
|
||||
# traffic_data = await self.data_client.get_traffic_data(
|
||||
# tenant_id=tenant_id,
|
||||
# latitude=40.4168,
|
||||
# longitude=-3.7038
|
||||
# )
|
||||
#
|
||||
# if traffic_data:
|
||||
# features.update({
|
||||
# "traffic_volume": traffic_data.get("traffic_volume", 100),
|
||||
# "pedestrian_count": traffic_data.get("pedestrian_count", 50),
|
||||
# })
|
||||
# logger.info("Traffic data acquired successfully", tenant_id=tenant_id)
|
||||
# return
|
||||
|
||||
except Exception as e:
|
||||
logger.warning("Traffic data acquisition failed", error=str(e))
|
||||
# except Exception as e:
|
||||
# logger.warning("Traffic data acquisition failed", error=str(e))
|
||||
|
||||
# Fallback: Use typical values based on day of week
|
||||
day_of_week = features["day_of_week"]
|
||||
@@ -273,6 +296,7 @@ class ForecastingService:
|
||||
features.update({
|
||||
"traffic_volume": int(100 * weekend_factor),
|
||||
"pedestrian_count": int(50 * weekend_factor),
|
||||
"congestion_level": 1
|
||||
})
|
||||
|
||||
logger.warning("Using default traffic values", tenant_id=tenant_id)
|
||||
@@ -398,15 +422,36 @@ class ForecastingService:
|
||||
|
||||
forecast = Forecast(
|
||||
tenant_id=tenant_id,
|
||||
forecast_date=request.forecast_date,
|
||||
product_name=request.product_name,
|
||||
predicted_quantity=prediction["prediction"],
|
||||
location=request.location,
|
||||
forecast_date=request.forecast_date,
|
||||
|
||||
# Predictions
|
||||
predicted_demand=prediction['prediction'],
|
||||
confidence_lower=prediction['lower_bound'],
|
||||
confidence_upper=prediction['upper_bound'],
|
||||
confidence_level=request.confidence_level,
|
||||
lower_bound=prediction["lower_bound"],
|
||||
upper_bound=prediction["upper_bound"],
|
||||
model_id=model_data["model_id"],
|
||||
external_factors=features,
|
||||
created_at=datetime.utcnow()
|
||||
|
||||
# Model info
|
||||
model_id=model_data['model_id'],
|
||||
model_version=model_data.get('version', '1.0'),
|
||||
algorithm=model_data.get('algorithm', 'prophet'),
|
||||
|
||||
# Context
|
||||
business_type=features.get('business_type', 'individual'),
|
||||
is_holiday=features.get('is_holiday', False),
|
||||
is_weekend=features.get('is_weekend', False),
|
||||
day_of_week=features.get('day_of_week', 0),
|
||||
|
||||
# External factors
|
||||
weather_temperature=features.get('temperature'),
|
||||
weather_precipitation=features.get('precipitation'),
|
||||
weather_description=features.get('weather_description'),
|
||||
traffic_volume=features.get('traffic_volume'),
|
||||
|
||||
# Metadata
|
||||
processing_time_ms=int((datetime.now() - start_time).total_seconds() * 1000),
|
||||
features_used=features
|
||||
)
|
||||
|
||||
db.add(forecast)
|
||||
|
||||
Reference in New Issue
Block a user