Initial commit - production deployment
This commit is contained in:
0
services/forecasting/app/core/__init__.py
Normal file
0
services/forecasting/app/core/__init__.py
Normal file
81
services/forecasting/app/core/config.py
Normal file
81
services/forecasting/app/core/config.py
Normal file
@@ -0,0 +1,81 @@
|
||||
# ================================================================
|
||||
# FORECASTING SERVICE CONFIGURATION
|
||||
# services/forecasting/app/core/config.py
|
||||
# ================================================================
|
||||
|
||||
"""
|
||||
Forecasting service configuration
|
||||
Demand prediction and forecasting
|
||||
"""
|
||||
|
||||
from shared.config.base import BaseServiceSettings
|
||||
import os
|
||||
|
||||
class ForecastingSettings(BaseServiceSettings):
|
||||
"""Forecasting service specific settings"""
|
||||
|
||||
# Service Identity
|
||||
APP_NAME: str = "Forecasting Service"
|
||||
SERVICE_NAME: str = "forecasting-service"
|
||||
DESCRIPTION: str = "Demand prediction and forecasting service"
|
||||
|
||||
# Database configuration (secure approach - build from components)
|
||||
@property
|
||||
def DATABASE_URL(self) -> str:
|
||||
"""Build database URL from secure components"""
|
||||
# Try complete URL first (for backward compatibility)
|
||||
complete_url = os.getenv("FORECASTING_DATABASE_URL")
|
||||
if complete_url:
|
||||
return complete_url
|
||||
|
||||
# Build from components (secure approach)
|
||||
user = os.getenv("FORECASTING_DB_USER", "forecasting_user")
|
||||
password = os.getenv("FORECASTING_DB_PASSWORD", "forecasting_pass123")
|
||||
host = os.getenv("FORECASTING_DB_HOST", "localhost")
|
||||
port = os.getenv("FORECASTING_DB_PORT", "5432")
|
||||
name = os.getenv("FORECASTING_DB_NAME", "forecasting_db")
|
||||
|
||||
return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
|
||||
|
||||
# Redis Database (dedicated for prediction cache)
|
||||
REDIS_DB: int = 2
|
||||
|
||||
# Prediction Configuration
|
||||
MAX_FORECAST_DAYS: int = int(os.getenv("MAX_FORECAST_DAYS", "30"))
|
||||
MIN_HISTORICAL_DAYS: int = int(os.getenv("MIN_HISTORICAL_DAYS", "60"))
|
||||
PREDICTION_CONFIDENCE_THRESHOLD: float = float(os.getenv("PREDICTION_CONFIDENCE_THRESHOLD", "0.8"))
|
||||
|
||||
# Caching Configuration
|
||||
PREDICTION_CACHE_TTL_HOURS: int = int(os.getenv("PREDICTION_CACHE_TTL_HOURS", "6"))
|
||||
FORECAST_BATCH_SIZE: int = int(os.getenv("FORECAST_BATCH_SIZE", "100"))
|
||||
|
||||
# MinIO Configuration
|
||||
MINIO_ENDPOINT: str = os.getenv("MINIO_ENDPOINT", "minio.bakery-ia.svc.cluster.local:9000")
|
||||
MINIO_ACCESS_KEY: str = os.getenv("FORECASTING_MINIO_ACCESS_KEY", "forecasting-service")
|
||||
MINIO_SECRET_KEY: str = os.getenv("FORECASTING_MINIO_SECRET_KEY", "forecasting-secret-key")
|
||||
MINIO_USE_SSL: bool = os.getenv("MINIO_USE_SSL", "true").lower() == "true"
|
||||
MINIO_MODEL_BUCKET: str = os.getenv("MINIO_MODEL_BUCKET", "training-models")
|
||||
MINIO_CONSOLE_PORT: str = os.getenv("MINIO_CONSOLE_PORT", "9001")
|
||||
MINIO_API_PORT: str = os.getenv("MINIO_API_PORT", "9000")
|
||||
MINIO_REGION: str = os.getenv("MINIO_REGION", "us-east-1")
|
||||
MINIO_MODEL_LIFECYCLE_DAYS: int = int(os.getenv("MINIO_MODEL_LIFECYCLE_DAYS", "90"))
|
||||
MINIO_CACHE_TTL_SECONDS: int = int(os.getenv("MINIO_CACHE_TTL_SECONDS", "3600"))
|
||||
|
||||
# Real-time Forecasting
|
||||
REALTIME_FORECASTING_ENABLED: bool = os.getenv("REALTIME_FORECASTING_ENABLED", "true").lower() == "true"
|
||||
FORECAST_UPDATE_INTERVAL_HOURS: int = int(os.getenv("FORECAST_UPDATE_INTERVAL_HOURS", "6"))
|
||||
|
||||
# Business Rules for Spanish Bakeries
|
||||
BUSINESS_HOUR_START: int = 7 # 7 AM
|
||||
BUSINESS_HOUR_END: int = 20 # 8 PM
|
||||
WEEKEND_ADJUSTMENT_FACTOR: float = float(os.getenv("WEEKEND_ADJUSTMENT_FACTOR", "0.8"))
|
||||
HOLIDAY_ADJUSTMENT_FACTOR: float = float(os.getenv("HOLIDAY_ADJUSTMENT_FACTOR", "0.5"))
|
||||
|
||||
# Weather Impact Modeling
|
||||
WEATHER_IMPACT_ENABLED: bool = os.getenv("WEATHER_IMPACT_ENABLED", "true").lower() == "true"
|
||||
TEMPERATURE_THRESHOLD_COLD: float = float(os.getenv("TEMPERATURE_THRESHOLD_COLD", "10.0"))
|
||||
TEMPERATURE_THRESHOLD_HOT: float = float(os.getenv("TEMPERATURE_THRESHOLD_HOT", "30.0"))
|
||||
RAIN_IMPACT_FACTOR: float = float(os.getenv("RAIN_IMPACT_FACTOR", "0.7"))
|
||||
|
||||
|
||||
settings = ForecastingSettings()
|
||||
121
services/forecasting/app/core/database.py
Normal file
121
services/forecasting/app/core/database.py
Normal file
@@ -0,0 +1,121 @@
|
||||
# ================================================================
|
||||
# services/forecasting/app/core/database.py
|
||||
# ================================================================
|
||||
"""
|
||||
Database configuration for forecasting service
|
||||
"""
|
||||
|
||||
import structlog
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy import text
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from app.core.config import settings
|
||||
from shared.database.base import Base, DatabaseManager
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
# Create async engine
|
||||
async_engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=settings.DEBUG,
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=3600
|
||||
)
|
||||
|
||||
# Create async session factory
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
bind=async_engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False
|
||||
)
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
"""Get database session"""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
except Exception as e:
|
||||
await session.rollback()
|
||||
logger.error("Database session error", error=str(e))
|
||||
raise
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
async def init_database():
|
||||
"""Initialize database tables"""
|
||||
try:
|
||||
async with async_engine.begin() as conn:
|
||||
# Import all models to ensure they are registered
|
||||
from app.models.forecast import ForecastBatch, Forecast
|
||||
from app.models.prediction import PredictionBatch, Prediction
|
||||
|
||||
# Create all tables
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
logger.info("Forecasting database initialized successfully")
|
||||
except Exception as e:
|
||||
logger.error("Failed to initialize forecasting database", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_db_health() -> bool:
|
||||
"""Check database health"""
|
||||
try:
|
||||
async with async_engine.begin() as conn:
|
||||
await conn.execute(text("SELECT 1"))
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error("Database health check failed", error=str(e))
|
||||
return False
|
||||
|
||||
|
||||
async def get_connection_pool_stats() -> dict:
|
||||
"""
|
||||
Get current connection pool statistics for monitoring.
|
||||
|
||||
Returns:
|
||||
Dictionary with pool statistics including usage and capacity
|
||||
"""
|
||||
try:
|
||||
pool = async_engine.pool
|
||||
|
||||
# Get pool stats
|
||||
stats = {
|
||||
"pool_size": pool.size(),
|
||||
"checked_in_connections": pool.checkedin(),
|
||||
"checked_out_connections": pool.checkedout(),
|
||||
"overflow_connections": pool.overflow(),
|
||||
"total_connections": pool.size() + pool.overflow(),
|
||||
"max_capacity": 10 + 20, # pool_size + max_overflow
|
||||
"usage_percentage": round(((pool.size() + pool.overflow()) / 30) * 100, 2)
|
||||
}
|
||||
|
||||
# Add health status
|
||||
if stats["usage_percentage"] > 90:
|
||||
stats["status"] = "critical"
|
||||
stats["message"] = "Connection pool near capacity"
|
||||
elif stats["usage_percentage"] > 80:
|
||||
stats["status"] = "warning"
|
||||
stats["message"] = "Connection pool usage high"
|
||||
else:
|
||||
stats["status"] = "healthy"
|
||||
stats["message"] = "Connection pool healthy"
|
||||
|
||||
return stats
|
||||
except Exception as e:
|
||||
logger.error("Failed to get connection pool stats", error=str(e))
|
||||
return {
|
||||
"status": "error",
|
||||
"message": f"Failed to get pool stats: {str(e)}"
|
||||
}
|
||||
|
||||
# Database manager instance for service_base compatibility
|
||||
database_manager = DatabaseManager(
|
||||
database_url=settings.DATABASE_URL,
|
||||
service_name="forecasting-service",
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
echo=settings.DEBUG
|
||||
)
|
||||
Reference in New Issue
Block a user