65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
# services/training/app/services/data_client.py
|
|
"""
|
|
Training Service Data Client
|
|
Migrated to use shared service clients - much simpler now!
|
|
"""
|
|
|
|
import structlog
|
|
from typing import Dict, Any, List, Optional
|
|
from datetime import datetime
|
|
|
|
# Import the shared clients
|
|
from shared.clients import get_sales_client, get_external_client, get_service_clients
|
|
from app.core.config import settings
|
|
|
|
logger = structlog.get_logger()
|
|
|
|
class DataClient:
|
|
"""
|
|
Data client for training service
|
|
Now uses the shared data service client under the hood
|
|
"""
|
|
|
|
def __init__(self):
|
|
# Get the new specialized clients
|
|
self.sales_client = get_sales_client(settings, "forecasting")
|
|
self.external_client = get_external_client(settings, "forecasting")
|
|
|
|
# Or alternatively, get all clients at once:
|
|
# self.clients = get_service_clients(settings, "forecasting")
|
|
# Then use: self.clients.sales.get_sales_data(...) and self.clients.external.get_weather_forecast(...)
|
|
|
|
|
|
async def fetch_weather_forecast(
|
|
self,
|
|
tenant_id: str,
|
|
days: str,
|
|
latitude: Optional[float] = None,
|
|
longitude: Optional[float] = None
|
|
) -> List[Dict[str, Any]]:
|
|
"""
|
|
Fetch weather data for forecats
|
|
All the error handling and retry logic is now in the base client!
|
|
"""
|
|
try:
|
|
weather_data = await self.external_client.get_weather_forecast(
|
|
tenant_id=tenant_id,
|
|
days=days,
|
|
latitude=latitude,
|
|
longitude=longitude
|
|
)
|
|
|
|
if weather_data:
|
|
logger.info(f"Fetched {len(weather_data)} weather records",
|
|
tenant_id=tenant_id)
|
|
return weather_data
|
|
else:
|
|
logger.warning("No weather data returned", tenant_id=tenant_id)
|
|
return []
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error fetching weather data: {e}", tenant_id=tenant_id)
|
|
return []
|
|
|
|
# Global instance - same as before, but much simpler implementation
|
|
data_client = DataClient() |