Fix new services implementation 3

This commit is contained in:
Urtzi Alfaro
2025-08-14 16:47:34 +02:00
parent 0951547e92
commit 03737430ee
51 changed files with 657 additions and 982 deletions

View File

@@ -223,7 +223,7 @@ class EnhancedTrainingService:
"training_results": training_results,
"stored_models": [{
"id": str(model.id),
"product_name": model.product_name,
"inventory_product_id": str(model.inventory_product_id),
"model_type": model.model_type,
"model_path": model.model_path,
"is_active": model.is_active,
@@ -292,11 +292,11 @@ class EnhancedTrainingService:
models_trained_type=type(models_trained).__name__,
models_trained_keys=list(models_trained.keys()) if isinstance(models_trained, dict) else "not_dict")
for product_name, model_result in models_trained.items():
for inventory_product_id, model_result in models_trained.items():
# Defensive check: ensure model_result is a dictionary
if not isinstance(model_result, dict):
logger.warning("Skipping invalid model_result for product",
product_name=product_name,
inventory_product_id=inventory_product_id,
model_result_type=type(model_result).__name__,
model_result_value=str(model_result)[:100])
continue
@@ -306,12 +306,12 @@ class EnhancedTrainingService:
metrics = model_result.get("metrics", {})
if not isinstance(metrics, dict):
logger.warning("Invalid metrics object, using empty dict",
product_name=product_name,
inventory_product_id=inventory_product_id,
metrics_type=type(metrics).__name__)
metrics = {}
model_data = {
"tenant_id": tenant_id,
"product_name": product_name,
"inventory_product_id": inventory_product_id,
"job_id": job_id,
"model_type": "prophet_optimized",
"model_path": model_result.get("model_path"),
@@ -371,14 +371,14 @@ class EnhancedTrainingService:
"""Create performance metrics for stored models"""
try:
for model in stored_models:
model_result = training_results.get("models_trained", {}).get(model.product_name)
model_result = training_results.get("models_trained", {}).get(str(model.inventory_product_id))
if model_result and model_result.get("metrics"):
metrics = model_result["metrics"]
metric_data = {
"model_id": str(model.id),
"tenant_id": tenant_id,
"product_name": model.product_name,
"inventory_product_id": str(model.inventory_product_id),
"mae": metrics.get("mae"),
"mse": metrics.get("mse"),
"rmse": metrics.get("rmse"),
@@ -556,14 +556,14 @@ class EnhancedTrainingService:
async def start_single_product_training(self,
tenant_id: str,
product_name: str,
inventory_product_id: str,
job_id: str,
bakery_location: tuple = (40.4168, -3.7038)) -> Dict[str, Any]:
"""Start enhanced single product training using repository pattern"""
try:
logger.info("Starting enhanced single product training",
tenant_id=tenant_id,
product_name=product_name,
inventory_product_id=inventory_product_id,
job_id=job_id)
# This would use the data client to fetch data for the specific product
@@ -573,7 +573,7 @@ class EnhancedTrainingService:
return {
"job_id": job_id,
"tenant_id": tenant_id,
"product_name": product_name,
"inventory_product_id": inventory_product_id,
"status": "completed",
"message": "Enhanced single product training completed successfully",
"created_at": datetime.now(),
@@ -582,9 +582,9 @@ class EnhancedTrainingService:
"successful_trainings": 1,
"failed_trainings": 0,
"products": [{
"product_name": product_name,
"inventory_product_id": inventory_product_id,
"status": "completed",
"model_id": f"model_{product_name}_{job_id[:8]}",
"model_id": f"model_{inventory_product_id}_{job_id[:8]}",
"data_points": 100,
"metrics": {"mape": 15.5, "mae": 2.3, "rmse": 3.1, "r2_score": 0.85}
}],
@@ -597,7 +597,7 @@ class EnhancedTrainingService:
except Exception as e:
logger.error("Enhanced single product training failed",
product_name=product_name,
inventory_product_id=inventory_product_id,
error=str(e))
raise
@@ -611,7 +611,7 @@ class EnhancedTrainingService:
products = []
for model in stored_models:
products.append({
"product_name": model.get("product_name"),
"inventory_product_id": model.get("inventory_product_id"),
"status": "completed",
"model_id": model.get("id"),
"data_points": model.get("training_samples", 0),