Initial commit - production deployment

This commit is contained in:
2026-01-21 17:17:16 +01:00
commit c23d00dd92
2289 changed files with 638440 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
"""Configuration settings for AI Insights Service."""
from shared.config.base import BaseServiceSettings
import os
from typing import Optional
class Settings(BaseServiceSettings):
"""Application settings."""
# Service Info
SERVICE_NAME: str = "ai-insights"
SERVICE_VERSION: str = "1.0.0"
API_V1_PREFIX: str = "/api/v1"
# 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("AI_INSIGHTS_DATABASE_URL")
if complete_url:
return complete_url
# Also check for generic DATABASE_URL (for migration compatibility)
generic_url = os.getenv("DATABASE_URL")
if generic_url:
return generic_url
# Build from components (secure approach)
user = os.getenv("AI_INSIGHTS_DB_USER", "ai_insights_user")
password = os.getenv("AI_INSIGHTS_DB_PASSWORD", "ai_insights_pass123")
host = os.getenv("AI_INSIGHTS_DB_HOST", "localhost")
port = os.getenv("AI_INSIGHTS_DB_PORT", "5432")
name = os.getenv("AI_INSIGHTS_DB_NAME", "ai_insights_db")
return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
DB_POOL_SIZE: int = 20
DB_MAX_OVERFLOW: int = 10
# Redis (inherited from BaseServiceSettings but can override)
REDIS_CACHE_TTL: int = 900 # 15 minutes
REDIS_DB: int = 3 # Dedicated Redis database for AI Insights
# Service URLs
FORECASTING_SERVICE_URL: str = "http://forecasting-service:8000"
PROCUREMENT_SERVICE_URL: str = "http://procurement-service:8000"
PRODUCTION_SERVICE_URL: str = "http://production-service:8000"
SALES_SERVICE_URL: str = "http://sales-service:8000"
INVENTORY_SERVICE_URL: str = "http://inventory-service:8000"
# Circuit Breaker Settings
CIRCUIT_BREAKER_FAILURE_THRESHOLD: int = 5
CIRCUIT_BREAKER_TIMEOUT: int = 60
# Insight Settings
MIN_CONFIDENCE_THRESHOLD: int = 60
DEFAULT_INSIGHT_TTL_DAYS: int = 7
MAX_INSIGHTS_PER_REQUEST: int = 100
# Feedback Settings
FEEDBACK_PROCESSING_ENABLED: bool = True
FEEDBACK_PROCESSING_SCHEDULE: str = "0 6 * * *" # Daily at 6 AM
# Logging
LOG_LEVEL: str = "INFO"
# CORS
ALLOWED_ORIGINS: list[str] = ["http://localhost:3000", "http://localhost:5173"]
class Config:
env_file = ".env"
case_sensitive = True
settings = Settings()

View File

@@ -0,0 +1,58 @@
"""Database configuration and session management."""
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import declarative_base
from sqlalchemy.pool import NullPool
from typing import AsyncGenerator
from app.core.config import settings
# Create async engine
engine = create_async_engine(
settings.DATABASE_URL,
pool_size=settings.DB_POOL_SIZE,
max_overflow=settings.DB_MAX_OVERFLOW,
echo=False,
future=True,
)
# Create async session factory
AsyncSessionLocal = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
autocommit=False,
autoflush=False,
)
# Create declarative base
Base = declarative_base()
async def get_db() -> AsyncGenerator[AsyncSession, None]:
"""
Dependency for getting async database sessions.
Yields:
AsyncSession: Database session
"""
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
async def init_db():
"""Initialize database tables."""
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def close_db():
"""Close database connections."""
await engine.dispose()