Files
bakery-ia/shared/clients/forecast_client.py

332 lines
12 KiB
Python
Raw Normal View History

2025-07-29 15:08:55 +02:00
# shared/clients/forecast_client.py
"""
2025-10-06 15:27:01 +02:00
Forecast Service Client - Updated for refactored backend structure
2025-07-29 15:08:55 +02:00
Handles all API calls to the forecasting service
2025-10-06 15:27:01 +02:00
Backend structure:
- ATOMIC: /forecasting/forecasts (CRUD)
- BUSINESS: /forecasting/operations/* (single, multi-day, batch, etc.)
- ANALYTICS: /forecasting/analytics/* (predictions-performance)
2025-07-29 15:08:55 +02:00
"""
from typing import Dict, Any, Optional, List
from datetime import date
2025-07-29 15:08:55 +02:00
from .base_service_client import BaseServiceClient
from shared.config.base import BaseServiceSettings
class ForecastServiceClient(BaseServiceClient):
"""Client for communicating with the forecasting service"""
2025-10-06 15:27:01 +02:00
2025-07-29 15:08:55 +02:00
def __init__(self, config: BaseServiceSettings, calling_service_name: str = "unknown"):
super().__init__(calling_service_name, config)
2025-10-06 15:27:01 +02:00
2025-07-29 15:08:55 +02:00
def get_service_base_path(self) -> str:
return "/api/v1"
2025-10-06 15:27:01 +02:00
2025-07-29 15:08:55 +02:00
# ================================================================
2025-10-06 15:27:01 +02:00
# ATOMIC: Forecast CRUD Operations
2025-07-29 15:08:55 +02:00
# ================================================================
2025-10-06 15:27:01 +02:00
2025-07-29 15:08:55 +02:00
async def get_forecast(self, tenant_id: str, forecast_id: str) -> Optional[Dict[str, Any]]:
2025-10-06 15:27:01 +02:00
"""Get forecast details by ID"""
return await self.get(f"forecasting/forecasts/{forecast_id}", tenant_id=tenant_id)
2025-07-29 15:08:55 +02:00
async def list_forecasts(
self,
tenant_id: str,
2025-10-06 15:27:01 +02:00
inventory_product_id: Optional[str] = None,
start_date: Optional[date] = None,
end_date: Optional[date] = None,
limit: int = 50,
offset: int = 0
2025-07-29 15:08:55 +02:00
) -> Optional[List[Dict[str, Any]]]:
2025-10-06 15:27:01 +02:00
"""List forecasts for a tenant with optional filters"""
params = {"limit": limit, "offset": offset}
if inventory_product_id:
params["inventory_product_id"] = inventory_product_id
if start_date:
params["start_date"] = start_date.isoformat()
if end_date:
params["end_date"] = end_date.isoformat()
return await self.get("forecasting/forecasts", tenant_id=tenant_id, params=params)
2025-07-29 15:08:55 +02:00
async def delete_forecast(self, tenant_id: str, forecast_id: str) -> Optional[Dict[str, Any]]:
"""Delete a forecast"""
2025-10-06 15:27:01 +02:00
return await self.delete(f"forecasting/forecasts/{forecast_id}", tenant_id=tenant_id)
2025-07-29 15:08:55 +02:00
# ================================================================
2025-10-06 15:27:01 +02:00
# BUSINESS: Forecasting Operations
2025-07-29 15:08:55 +02:00
# ================================================================
2025-10-06 15:27:01 +02:00
async def generate_single_forecast(
2025-07-29 15:08:55 +02:00
self,
tenant_id: str,
2025-10-06 15:27:01 +02:00
inventory_product_id: str,
forecast_date: date,
include_recommendations: bool = False
) -> Optional[Dict[str, Any]]:
"""Generate a single product forecast"""
data = {
"inventory_product_id": inventory_product_id,
"forecast_date": forecast_date.isoformat(),
"include_recommendations": include_recommendations
}
return await self.post("forecasting/operations/single", data=data, tenant_id=tenant_id)
async def generate_multi_day_forecast(
2025-07-29 15:08:55 +02:00
self,
tenant_id: str,
2025-10-06 15:27:01 +02:00
inventory_product_id: str,
forecast_date: date,
forecast_days: int = 7,
include_recommendations: bool = False
) -> Optional[Dict[str, Any]]:
"""Generate multiple daily forecasts for the specified period"""
data = {
"inventory_product_id": inventory_product_id,
"forecast_date": forecast_date.isoformat(),
"forecast_days": forecast_days,
"include_recommendations": include_recommendations
}
return await self.post("forecasting/operations/multi-day", data=data, tenant_id=tenant_id)
async def generate_batch_forecast(
self,
tenant_id: str,
inventory_product_ids: List[str],
forecast_date: date,
forecast_days: int = 1
) -> Optional[Dict[str, Any]]:
"""Generate forecasts for multiple products in batch"""
data = {
"inventory_product_ids": inventory_product_ids,
"forecast_date": forecast_date.isoformat(),
"forecast_days": forecast_days
}
return await self.post("forecasting/operations/batch", data=data, tenant_id=tenant_id)
async def generate_realtime_prediction(
self,
tenant_id: str,
inventory_product_id: str,
2025-07-29 15:08:55 +02:00
model_id: str,
features: Dict[str, Any],
2025-10-06 15:27:01 +02:00
model_path: Optional[str] = None,
confidence_level: float = 0.8
2025-07-29 15:08:55 +02:00
) -> Optional[Dict[str, Any]]:
2025-10-06 15:27:01 +02:00
"""Generate real-time prediction"""
2025-07-29 15:08:55 +02:00
data = {
2025-10-06 15:27:01 +02:00
"inventory_product_id": inventory_product_id,
2025-07-29 15:08:55 +02:00
"model_id": model_id,
"features": features,
2025-10-06 15:27:01 +02:00
"confidence_level": confidence_level
2025-07-29 15:08:55 +02:00
}
2025-10-06 15:27:01 +02:00
if model_path:
data["model_path"] = model_path
return await self.post("forecasting/operations/realtime", data=data, tenant_id=tenant_id)
async def validate_predictions(
self,
tenant_id: str,
2025-10-06 15:27:01 +02:00
start_date: date,
end_date: date
) -> Optional[Dict[str, Any]]:
2025-10-06 15:27:01 +02:00
"""Validate predictions against actual sales data"""
params = {
"start_date": start_date.isoformat(),
"end_date": end_date.isoformat()
}
2025-10-06 15:27:01 +02:00
return await self.post("forecasting/operations/validate-predictions", params=params, tenant_id=tenant_id)
2025-11-05 13:34:56 +01:00
async def validate_forecasts(
self,
tenant_id: str,
date: date
) -> Optional[Dict[str, Any]]:
"""
Validate forecasts for a specific date against actual sales.
Calculates MAPE, RMSE, MAE and identifies products with poor accuracy.
Args:
tenant_id: Tenant UUID
2025-11-18 07:17:17 +01:00
date: Date to validate (validates this single day)
2025-11-05 13:34:56 +01:00
Returns:
Dict with overall metrics and poor accuracy products list
"""
2025-11-18 07:17:17 +01:00
from datetime import datetime, timezone
# Convert date to datetime with timezone for start/end of day
start_datetime = datetime.combine(date, datetime.min.time()).replace(tzinfo=timezone.utc)
end_datetime = datetime.combine(date, datetime.max.time()).replace(tzinfo=timezone.utc)
# Call the new validation endpoint
result = await self.post(
"forecasting/validation/validate-yesterday",
params={"orchestration_run_id": None},
tenant_id=tenant_id
)
if not result:
return None
# Transform the new response format to match the expected format
overall_metrics = result.get("overall_metrics", {})
# Get poor accuracy products from the result
poor_accuracy_products = result.get("poor_accuracy_products", [])
return {
"overall_mape": overall_metrics.get("mape", 0),
"overall_rmse": overall_metrics.get("rmse", 0),
"overall_mae": overall_metrics.get("mae", 0),
"overall_r2_score": overall_metrics.get("r2_score", 0),
"overall_accuracy_percentage": overall_metrics.get("accuracy_percentage", 0),
"products_validated": result.get("forecasts_with_actuals", 0),
"poor_accuracy_products": poor_accuracy_products,
"validation_run_id": result.get("validation_run_id"),
"forecasts_evaluated": result.get("forecasts_evaluated", 0),
"forecasts_with_actuals": result.get("forecasts_with_actuals", 0),
"forecasts_without_actuals": result.get("forecasts_without_actuals", 0)
2025-11-05 13:34:56 +01:00
}
2025-10-06 15:27:01 +02:00
async def get_forecast_statistics(
2025-07-29 15:08:55 +02:00
self,
tenant_id: str,
2025-10-06 15:27:01 +02:00
start_date: Optional[date] = None,
end_date: Optional[date] = None
2025-07-29 15:08:55 +02:00
) -> Optional[Dict[str, Any]]:
2025-10-06 15:27:01 +02:00
"""Get forecast statistics"""
2025-07-29 15:08:55 +02:00
params = {}
if start_date:
2025-10-06 15:27:01 +02:00
params["start_date"] = start_date.isoformat()
2025-07-29 15:08:55 +02:00
if end_date:
2025-10-06 15:27:01 +02:00
params["end_date"] = end_date.isoformat()
return await self.get("forecasting/operations/statistics", tenant_id=tenant_id, params=params)
async def clear_prediction_cache(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Clear prediction cache"""
return await self.delete("forecasting/operations/cache", tenant_id=tenant_id)
# ================================================================
# ANALYTICS: Forecasting Analytics
# ================================================================
async def get_predictions_performance(
2025-07-29 15:08:55 +02:00
self,
tenant_id: str,
2025-10-06 15:27:01 +02:00
start_date: Optional[date] = None,
end_date: Optional[date] = None
2025-07-29 15:08:55 +02:00
) -> Optional[Dict[str, Any]]:
2025-10-06 15:27:01 +02:00
"""Get predictions performance analytics"""
params = {}
if start_date:
params["start_date"] = start_date.isoformat()
if end_date:
params["end_date"] = end_date.isoformat()
return await self.get("forecasting/analytics/predictions-performance", tenant_id=tenant_id, params=params)
2025-11-05 13:34:56 +01:00
# ================================================================
# ML INSIGHTS: Dynamic Rules Generation
# ================================================================
async def trigger_rules_generation(
self,
tenant_id: str,
product_ids: Optional[List[str]] = None,
lookback_days: int = 90,
min_samples: int = 10
) -> Optional[Dict[str, Any]]:
"""
Trigger dynamic business rules learning for demand forecasting.
Args:
tenant_id: Tenant UUID
product_ids: Specific product IDs to analyze. If None, analyzes all products
lookback_days: Days of historical data to analyze (30-365)
min_samples: Minimum samples required for rule learning (5-100)
Returns:
Dict with rules generation results including insights posted
"""
data = {
"product_ids": product_ids,
"lookback_days": lookback_days,
"min_samples": min_samples
}
return await self.post("forecasting/ml/insights/generate-rules", data=data, tenant_id=tenant_id)
2025-07-29 15:08:55 +02:00
# ================================================================
2025-10-06 15:27:01 +02:00
# Legacy/Compatibility Methods (deprecated)
2025-07-29 15:08:55 +02:00
# ================================================================
2025-10-06 15:27:01 +02:00
2025-11-05 13:34:56 +01:00
async def generate_forecasts(
self,
tenant_id: str,
forecast_days: int = 7,
inventory_product_ids: Optional[List[str]] = None
) -> Optional[Dict[str, Any]]:
"""
COMPATIBILITY: Orchestrator-friendly method to generate forecasts
This method is called by the orchestrator service and generates batch forecasts
for either specified products or all products.
Args:
tenant_id: Tenant UUID
forecast_days: Number of days to forecast (default 7)
inventory_product_ids: Optional list of product IDs. If None, forecasts all products.
Returns:
Dict with forecast results
"""
from datetime import datetime
# If no product IDs specified, let the backend handle it
if not inventory_product_ids:
# Call the batch operation endpoint to forecast all products
# The forecasting service will handle fetching all products internally
data = {
"batch_name": f"orchestrator-batch-{datetime.now().strftime('%Y%m%d')}",
"inventory_product_ids": [], # Empty list will trigger fetching all products
"forecast_days": forecast_days
}
return await self.post("forecasting/operations/batch", data=data, tenant_id=tenant_id)
# Otherwise use the standard batch forecast
return await self.generate_batch_forecast(
tenant_id=tenant_id,
inventory_product_ids=inventory_product_ids,
forecast_date=datetime.now().date(),
forecast_days=forecast_days
)
2025-10-06 15:27:01 +02:00
async def create_forecast(
2025-07-29 15:08:55 +02:00
self,
tenant_id: str,
model_id: str,
start_date: str,
end_date: str,
2025-10-06 15:27:01 +02:00
product_ids: Optional[List[str]] = None,
include_confidence_intervals: bool = True,
2025-07-29 15:08:55 +02:00
**kwargs
) -> Optional[Dict[str, Any]]:
2025-10-06 15:27:01 +02:00
"""
DEPRECATED: Use generate_single_forecast or generate_batch_forecast instead
Legacy method for backward compatibility
"""
# Map to new batch forecast operation
if product_ids:
return await self.generate_batch_forecast(
tenant_id=tenant_id,
inventory_product_ids=product_ids,
forecast_date=date.fromisoformat(start_date),
forecast_days=1
)
return None