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
|
2025-09-09 17:40:57 +02:00
|
|
|
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(
|
2025-09-09 17:40:57 +02:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-10-06 15:27:01 +02:00
|
|
|
start_date: date,
|
|
|
|
|
end_date: date
|
2025-09-09 17:40:57 +02:00
|
|
|
) -> 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-09-09 17:40:57 +02:00
|
|
|
}
|
2025-10-06 15:27:01 +02:00
|
|
|
return await self.post("forecasting/operations/validate-predictions", params=params, tenant_id=tenant_id)
|
|
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
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
|