demo seed change 3

This commit is contained in:
Urtzi Alfaro
2025-12-14 16:04:16 +01:00
parent a030bd14c8
commit 4ae5356ad1
25 changed files with 2969 additions and 1645 deletions

View File

@@ -218,32 +218,29 @@ async def clone_demo_data(
detail=f"Invalid UUID format in forecast data: {str(e)}"
)
# Transform dates
# Transform dates using the proper parse_date_field function
for date_field in ['forecast_date', 'created_at']:
if date_field in forecast_data:
try:
date_value = forecast_data[date_field]
if isinstance(date_value, str):
original_date = datetime.fromisoformat(date_value)
elif hasattr(date_value, 'isoformat'):
original_date = date_value
else:
logger.warning("Skipping invalid date format",
date_field=date_field,
date_value=date_value)
continue
adjusted_forecast_date = adjust_date_for_demo(
original_date,
session_time
parsed_date = parse_date_field(
forecast_data[date_field],
session_time,
date_field
)
forecast_data[date_field] = adjusted_forecast_date
except (ValueError, AttributeError) as e:
logger.warning("Failed to parse date, skipping",
if parsed_date:
forecast_data[date_field] = parsed_date
else:
# If parsing fails, use session_time as fallback
forecast_data[date_field] = session_time
logger.warning("Using fallback date for failed parsing",
date_field=date_field,
original_value=forecast_data[date_field])
except Exception as e:
logger.warning("Failed to parse date, using fallback",
date_field=date_field,
date_value=forecast_data[date_field],
error=str(e))
forecast_data.pop(date_field, None)
forecast_data[date_field] = session_time
# Create forecast
# Map product_id to inventory_product_id if needed
@@ -252,17 +249,20 @@ async def clone_demo_data(
# Map predicted_quantity to predicted_demand if needed
predicted_demand = forecast_data.get('predicted_demand') or forecast_data.get('predicted_quantity')
# Set default location if not provided in seed data
location = forecast_data.get('location') or "Main Bakery"
new_forecast = Forecast(
id=transformed_id,
tenant_id=virtual_uuid,
inventory_product_id=inventory_product_id,
product_name=forecast_data.get('product_name'),
location=forecast_data.get('location'),
location=location,
forecast_date=forecast_data.get('forecast_date'),
created_at=forecast_data.get('created_at', session_time),
predicted_demand=predicted_demand,
confidence_lower=forecast_data.get('confidence_lower'),
confidence_upper=forecast_data.get('confidence_upper'),
confidence_lower=forecast_data.get('confidence_lower', max(0.0, float(predicted_demand or 0.0) * 0.8)),
confidence_upper=forecast_data.get('confidence_upper', max(0.0, float(predicted_demand or 0.0) * 1.2)),
confidence_level=forecast_data.get('confidence_level', 0.8),
model_id=forecast_data.get('model_id'),
model_version=forecast_data.get('model_version'),
@@ -299,32 +299,29 @@ async def clone_demo_data(
detail=f"Invalid UUID format in batch data: {str(e)}"
)
# Transform dates
# Transform dates using proper parse_date_field function
for date_field in ['requested_at', 'completed_at']:
if date_field in batch_data:
try:
date_value = batch_data[date_field]
if isinstance(date_value, str):
original_date = datetime.fromisoformat(date_value)
elif hasattr(date_value, 'isoformat'):
original_date = date_value
else:
logger.warning("Skipping invalid date format",
date_field=date_field,
date_value=date_value)
continue
adjusted_batch_date = adjust_date_for_demo(
original_date,
session_time
parsed_date = parse_date_field(
batch_data[date_field],
session_time,
date_field
)
batch_data[date_field] = adjusted_batch_date
except (ValueError, AttributeError) as e:
logger.warning("Failed to parse date, skipping",
if parsed_date:
batch_data[date_field] = parsed_date
else:
# If parsing fails, use session_time as fallback
batch_data[date_field] = session_time
logger.warning("Using fallback date for failed parsing",
date_field=date_field,
original_value=batch_data[date_field])
except Exception as e:
logger.warning("Failed to parse date, using fallback",
date_field=date_field,
date_value=batch_data[date_field],
error=str(e))
batch_data.pop(date_field, None)
batch_data[date_field] = session_time
# Create prediction batch
new_batch = PredictionBatch(

View File

@@ -382,8 +382,8 @@ class EnhancedForecastingService:
"location": request.location,
"forecast_date": forecast_datetime,
"predicted_demand": adjusted_prediction['prediction'],
"confidence_lower": adjusted_prediction.get('lower_bound', adjusted_prediction['prediction'] * 0.8),
"confidence_upper": adjusted_prediction.get('upper_bound', adjusted_prediction['prediction'] * 1.2),
"confidence_lower": adjusted_prediction.get('lower_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 0.8)),
"confidence_upper": adjusted_prediction.get('upper_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 1.2)),
"confidence_level": request.confidence_level,
"model_id": model_data['model_id'],
"model_version": str(model_data.get('version', '1.0')),
@@ -410,8 +410,8 @@ class EnhancedForecastingService:
location=request.location,
forecast_date=forecast_datetime,
predicted_demand=adjusted_prediction['prediction'],
confidence_lower=adjusted_prediction.get('lower_bound', adjusted_prediction['prediction'] * 0.8),
confidence_upper=adjusted_prediction.get('upper_bound', adjusted_prediction['prediction'] * 1.2),
confidence_lower=adjusted_prediction.get('lower_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 0.8)),
confidence_upper=adjusted_prediction.get('upper_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 1.2)),
model_id=model_data['model_id'],
expires_in_hours=24
)
@@ -652,8 +652,8 @@ class EnhancedForecastingService:
"location": request.location,
"forecast_date": forecast_datetime,
"predicted_demand": adjusted_prediction['prediction'],
"confidence_lower": adjusted_prediction.get('lower_bound', adjusted_prediction['prediction'] * 0.8),
"confidence_upper": adjusted_prediction.get('upper_bound', adjusted_prediction['prediction'] * 1.2),
"confidence_lower": adjusted_prediction.get('lower_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 0.8)),
"confidence_upper": adjusted_prediction.get('upper_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 1.2)),
"confidence_level": request.confidence_level,
"model_id": model_data['model_id'],
"model_version": str(model_data.get('version', '1.0')),
@@ -679,8 +679,8 @@ class EnhancedForecastingService:
location=request.location,
forecast_date=forecast_datetime,
predicted_demand=adjusted_prediction['prediction'],
confidence_lower=adjusted_prediction.get('lower_bound', adjusted_prediction['prediction'] * 0.8),
confidence_upper=adjusted_prediction.get('upper_bound', adjusted_prediction['prediction'] * 1.2),
confidence_lower=adjusted_prediction.get('lower_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 0.8)),
confidence_upper=adjusted_prediction.get('upper_bound', max(0.0, float(adjusted_prediction.get('prediction') or 0.0) * 1.2)),
model_id=model_data['model_id'],
expires_in_hours=24
)