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,134 @@
"""Alembic environment configuration for alert_processor service"""
import asyncio
import os
import sys
from logging.config import fileConfig
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
# Add the service directory to the Python path
service_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if service_path not in sys.path:
sys.path.insert(0, service_path)
# Add shared modules to path
shared_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "shared"))
if shared_path not in sys.path:
sys.path.insert(0, shared_path)
try:
from shared.database.base import Base
# Import all models to ensure they are registered with Base.metadata
from app.models import * # noqa: F401, F403
except ImportError as e:
print(f"Import error in migrations env.py: {e}")
print(f"Current Python path: {sys.path}")
raise
# this is the Alembic Config object
config = context.config
# Determine service name from file path
service_name = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
service_name_upper = service_name.upper().replace('-', '_')
# Set database URL from environment variables with multiple fallback strategies
database_url = (
os.getenv(f'{service_name_upper}_DATABASE_URL') or # Service-specific
os.getenv('DATABASE_URL') # Generic fallback
)
# If DATABASE_URL is not set, construct from individual components
if not database_url:
# Try generic PostgreSQL environment variables first
postgres_host = os.getenv('POSTGRES_HOST')
postgres_port = os.getenv('POSTGRES_PORT', '5432')
postgres_db = os.getenv('POSTGRES_DB')
postgres_user = os.getenv('POSTGRES_USER')
postgres_password = os.getenv('POSTGRES_PASSWORD')
if all([postgres_host, postgres_db, postgres_user, postgres_password]):
database_url = f"postgresql+asyncpg://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
else:
# Try service-specific environment variables
db_host = os.getenv(f'{service_name_upper}_DB_HOST', f'{service_name}-db-service')
db_port = os.getenv(f'{service_name_upper}_DB_PORT', '5432')
db_name = os.getenv(f'{service_name_upper}_DB_NAME', f'{service_name.replace("-", "_")}_db')
db_user = os.getenv(f'{service_name_upper}_DB_USER', f'{service_name.replace("-", "_")}_user')
db_password = os.getenv(f'{service_name_upper}_DB_PASSWORD')
if db_password:
database_url = f"postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
if not database_url:
error_msg = f"ERROR: No database URL configured for {service_name} service"
print(error_msg)
raise Exception(error_msg)
config.set_main_option("sqlalchemy.url", database_url)
# Interpret the config file for Python logging
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Set target metadata
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
compare_type=True,
compare_server_default=True,
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
"""Execute migrations with the given connection."""
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True,
compare_server_default=True,
)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
"""Run migrations in 'online' mode with async support."""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,97 @@
"""
Clean unified events table schema.
Revision ID: 20251205_unified
Revises:
Create Date: 2025-12-05
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers
revision = '20251205_unified'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
"""
Create unified events table with JSONB enrichment contexts.
"""
# Create events table
op.create_table(
'events',
# Core fields
sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
# Classification
sa.Column('event_class', sa.String(50), nullable=False),
sa.Column('event_domain', sa.String(50), nullable=False),
sa.Column('event_type', sa.String(100), nullable=False),
sa.Column('service', sa.String(50), nullable=False),
# i18n content (NO hardcoded title/message)
sa.Column('i18n_title_key', sa.String(200), nullable=False),
sa.Column('i18n_title_params', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
sa.Column('i18n_message_key', sa.String(200), nullable=False),
sa.Column('i18n_message_params', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
# Priority
sa.Column('priority_score', sa.Integer, nullable=False, server_default='50'),
sa.Column('priority_level', sa.String(20), nullable=False),
sa.Column('type_class', sa.String(50), nullable=False),
# Enrichment contexts (JSONB)
sa.Column('orchestrator_context', postgresql.JSONB, nullable=True),
sa.Column('business_impact', postgresql.JSONB, nullable=True),
sa.Column('urgency', postgresql.JSONB, nullable=True),
sa.Column('user_agency', postgresql.JSONB, nullable=True),
sa.Column('trend_context', postgresql.JSONB, nullable=True),
# Smart actions
sa.Column('smart_actions', postgresql.JSONB, nullable=False, server_default=sa.text("'[]'::jsonb")),
# AI reasoning
sa.Column('ai_reasoning_summary_key', sa.String(200), nullable=True),
sa.Column('ai_reasoning_summary_params', postgresql.JSONB, nullable=True),
sa.Column('ai_reasoning_details', postgresql.JSONB, nullable=True),
sa.Column('confidence_score', sa.Float, nullable=True),
# Entity references
sa.Column('entity_links', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
# Status
sa.Column('status', sa.String(20), nullable=False, server_default='active'),
sa.Column('resolved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('acknowledged_at', sa.DateTime(timezone=True), nullable=True),
# Metadata
sa.Column('event_metadata', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb"))
)
# Create indexes for efficient queries (matching SQLAlchemy model)
op.create_index('idx_events_tenant_status', 'events', ['tenant_id', 'status'])
op.create_index('idx_events_tenant_priority', 'events', ['tenant_id', 'priority_score'])
op.create_index('idx_events_tenant_class', 'events', ['tenant_id', 'event_class'])
op.create_index('idx_events_tenant_created', 'events', ['tenant_id', 'created_at'])
op.create_index('idx_events_type_class_status', 'events', ['type_class', 'status'])
def downgrade():
"""
Drop events table and all indexes.
"""
op.drop_index('idx_events_type_class_status', 'events')
op.drop_index('idx_events_tenant_created', 'events')
op.drop_index('idx_events_tenant_class', 'events')
op.drop_index('idx_events_tenant_priority', 'events')
op.drop_index('idx_events_tenant_status', 'events')
op.drop_table('events')