New alert system and panel de control page

This commit is contained in:
Urtzi Alfaro
2025-11-27 15:52:40 +01:00
parent 1a2f4602f3
commit e902419b6e
178 changed files with 20982 additions and 6944 deletions

View File

@@ -1,100 +0,0 @@
"""initial_schema_20251015_1230
Revision ID: 5ad7a76c1b10
Revises:
Create Date: 2025-10-15 12:30:29.410300+02:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '5ad7a76c1b10'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('alerts',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('item_type', sa.String(length=50), nullable=False),
sa.Column('alert_type', sa.String(length=100), nullable=False),
sa.Column('severity', sa.Enum('low', 'medium', 'high', 'urgent', name='alertseverity'), nullable=False),
sa.Column('status', sa.Enum('active', 'resolved', 'acknowledged', 'ignored', name='alertstatus'), nullable=True),
sa.Column('service', sa.String(length=100), nullable=False),
sa.Column('title', sa.String(length=255), nullable=False),
sa.Column('message', sa.Text(), nullable=False),
sa.Column('actions', sa.JSON(), nullable=True),
sa.Column('alert_metadata', sa.JSON(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('resolved_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_alerts_created_at'), 'alerts', ['created_at'], unique=False)
op.create_index(op.f('ix_alerts_severity'), 'alerts', ['severity'], unique=False)
op.create_index(op.f('ix_alerts_status'), 'alerts', ['status'], unique=False)
op.create_index(op.f('ix_alerts_tenant_id'), 'alerts', ['tenant_id'], unique=False)
op.create_table('audit_logs',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('action', sa.String(length=100), nullable=False),
sa.Column('resource_type', sa.String(length=100), nullable=False),
sa.Column('resource_id', sa.String(length=255), nullable=True),
sa.Column('severity', sa.String(length=20), nullable=False),
sa.Column('service_name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('changes', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('audit_metadata', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('ip_address', sa.String(length=45), nullable=True),
sa.Column('user_agent', sa.Text(), nullable=True),
sa.Column('endpoint', sa.String(length=255), nullable=True),
sa.Column('method', sa.String(length=10), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_audit_resource_type_action', 'audit_logs', ['resource_type', 'action'], unique=False)
op.create_index('idx_audit_service_created', 'audit_logs', ['service_name', 'created_at'], unique=False)
op.create_index('idx_audit_severity_created', 'audit_logs', ['severity', 'created_at'], unique=False)
op.create_index('idx_audit_tenant_created', 'audit_logs', ['tenant_id', 'created_at'], unique=False)
op.create_index('idx_audit_user_created', 'audit_logs', ['user_id', 'created_at'], unique=False)
op.create_index(op.f('ix_audit_logs_action'), 'audit_logs', ['action'], unique=False)
op.create_index(op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'], unique=False)
op.create_index(op.f('ix_audit_logs_resource_id'), 'audit_logs', ['resource_id'], unique=False)
op.create_index(op.f('ix_audit_logs_resource_type'), 'audit_logs', ['resource_type'], unique=False)
op.create_index(op.f('ix_audit_logs_service_name'), 'audit_logs', ['service_name'], unique=False)
op.create_index(op.f('ix_audit_logs_severity'), 'audit_logs', ['severity'], unique=False)
op.create_index(op.f('ix_audit_logs_tenant_id'), 'audit_logs', ['tenant_id'], unique=False)
op.create_index(op.f('ix_audit_logs_user_id'), 'audit_logs', ['user_id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_audit_logs_user_id'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_tenant_id'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_severity'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_service_name'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_resource_type'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_resource_id'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_created_at'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_action'), table_name='audit_logs')
op.drop_index('idx_audit_user_created', table_name='audit_logs')
op.drop_index('idx_audit_tenant_created', table_name='audit_logs')
op.drop_index('idx_audit_severity_created', table_name='audit_logs')
op.drop_index('idx_audit_service_created', table_name='audit_logs')
op.drop_index('idx_audit_resource_type_action', table_name='audit_logs')
op.drop_table('audit_logs')
op.drop_index(op.f('ix_alerts_tenant_id'), table_name='alerts')
op.drop_index(op.f('ix_alerts_status'), table_name='alerts')
op.drop_index(op.f('ix_alerts_severity'), table_name='alerts')
op.drop_index(op.f('ix_alerts_created_at'), table_name='alerts')
op.drop_table('alerts')
# ### end Alembic commands ###

View File

@@ -1,51 +0,0 @@
"""add_alert_interactions
Revision ID: a1b2c3d4e5f6
Revises: 5ad7a76c1b10
Create Date: 2025-10-19 14:30:00.000000+02:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = 'a1b2c3d4e5f6'
down_revision: Union[str, None] = '5ad7a76c1b10'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create alert_interactions table
op.create_table('alert_interactions',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('alert_id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('interaction_type', sa.String(length=50), nullable=False),
sa.Column('interacted_at', sa.DateTime(), nullable=False),
sa.Column('response_time_seconds', sa.Integer(), nullable=True),
sa.Column('interaction_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['alert_id'], ['alerts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
# Create indexes for efficient queries
op.create_index('idx_alert_interactions_tenant_alert', 'alert_interactions', ['tenant_id', 'alert_id'], unique=False)
op.create_index('idx_alert_interactions_user', 'alert_interactions', ['user_id'], unique=False)
op.create_index('idx_alert_interactions_time', 'alert_interactions', ['interacted_at'], unique=False)
op.create_index('idx_alert_interactions_type', 'alert_interactions', ['interaction_type'], unique=False)
op.create_index('idx_alert_interactions_tenant_time', 'alert_interactions', ['tenant_id', 'interacted_at'], unique=False)
def downgrade() -> None:
op.drop_index('idx_alert_interactions_tenant_time', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_type', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_time', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_user', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_tenant_alert', table_name='alert_interactions')
op.drop_table('alert_interactions')

View File

@@ -0,0 +1,275 @@
"""Unified initial schema for alert-processor service
Revision ID: 20251125_unified_initial_schema
Revises:
Create Date: 2025-11-25
This is a unified migration that includes:
- All enum types (alertstatus, prioritylevel, alerttypeclass)
- Alerts table with full enrichment capabilities
- Alert interactions table for user engagement tracking
- Audit logs table for compliance and debugging
- All enhancements from incremental migrations:
- event_domain column
- action_created_at, superseded_by_action_id, hidden_from_ui columns
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '20251125_unified_initial_schema'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ============================================================
# Create Enum Types
# ============================================================
op.execute("""
CREATE TYPE alertstatus AS ENUM (
'active',
'resolved',
'acknowledged',
'ignored',
'in_progress',
'dismissed'
);
""")
op.execute("""
CREATE TYPE prioritylevel AS ENUM (
'critical',
'important',
'standard',
'info'
);
""")
op.execute("""
CREATE TYPE alerttypeclass AS ENUM (
'action_needed',
'prevented_issue',
'trend_warning',
'escalation',
'information'
);
""")
# ============================================================
# Create Alerts Table
# ============================================================
op.create_table('alerts',
# Core alert fields
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('item_type', sa.String(length=50), nullable=False),
sa.Column('event_domain', sa.String(50), nullable=True), # Added from 20251125_add_event_domain_column
sa.Column('alert_type', sa.String(length=100), nullable=False),
sa.Column('status', postgresql.ENUM('active', 'resolved', 'acknowledged', 'ignored', 'in_progress', 'dismissed', name='alertstatus', create_type=False), nullable=False),
sa.Column('service', sa.String(length=100), nullable=False),
sa.Column('title', sa.String(length=500), nullable=False), # Increased from 255 to match model
sa.Column('message', sa.Text(), nullable=False),
sa.Column('alert_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
# Priority scoring fields
sa.Column('priority_score', sa.Integer(), nullable=False),
sa.Column('priority_level', postgresql.ENUM('critical', 'important', 'standard', 'info', name='prioritylevel', create_type=False), nullable=False),
# Alert classification
sa.Column('type_class', postgresql.ENUM('action_needed', 'prevented_issue', 'trend_warning', 'escalation', 'information', name='alerttypeclass', create_type=False), nullable=False),
# Context enrichment (JSONB)
sa.Column('orchestrator_context', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('business_impact', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('urgency_context', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('user_agency', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('trend_context', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
# Smart actions
sa.Column('smart_actions', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
# AI reasoning
sa.Column('ai_reasoning_summary', sa.Text(), nullable=True),
sa.Column('confidence_score', sa.Float(), nullable=False, server_default='0.8'),
# Timing intelligence
sa.Column('timing_decision', sa.String(50), nullable=False, server_default='send_now'),
sa.Column('scheduled_send_time', sa.DateTime(timezone=True), nullable=True),
# Placement hints for frontend
sa.Column('placement', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
# Escalation & chaining (Added from 20251123_add_alert_enhancements)
sa.Column('action_created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('superseded_by_action_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('hidden_from_ui', sa.Boolean(), nullable=False, server_default='false'),
# Timestamps
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('resolved_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id'),
# Constraints
sa.CheckConstraint('priority_score >= 0 AND priority_score <= 100', name='chk_priority_score_range')
)
# ============================================================
# Create Indexes for Alerts Table
# ============================================================
op.create_index(op.f('ix_alerts_created_at'), 'alerts', ['created_at'], unique=False)
op.create_index(op.f('ix_alerts_status'), 'alerts', ['status'], unique=False)
op.create_index(op.f('ix_alerts_tenant_id'), 'alerts', ['tenant_id'], unique=False)
# Enrichment indexes
op.create_index(
'idx_alerts_priority_score',
'alerts',
['tenant_id', 'priority_score', 'created_at'],
postgresql_using='btree'
)
op.create_index(
'idx_alerts_type_class',
'alerts',
['tenant_id', 'type_class', 'status'],
postgresql_using='btree'
)
op.create_index(
'idx_alerts_priority_level',
'alerts',
['priority_level', 'status'],
postgresql_using='btree'
)
op.create_index(
'idx_alerts_timing',
'alerts',
['timing_decision', 'scheduled_send_time'],
postgresql_using='btree',
postgresql_where=sa.text("timing_decision != 'send_now'")
)
# Domain index (from 20251125_add_event_domain_column)
op.create_index('idx_alerts_domain', 'alerts', ['tenant_id', 'event_domain', 'status'], unique=False)
# Escalation indexes (from 20251123_add_alert_enhancements)
op.create_index('idx_alerts_action_created', 'alerts', ['tenant_id', 'action_created_at'], unique=False)
op.create_index('idx_alerts_superseded', 'alerts', ['superseded_by_action_id'], unique=False)
op.create_index('idx_alerts_hidden', 'alerts', ['tenant_id', 'hidden_from_ui', 'status'], unique=False)
# ============================================================
# Create Alert Interactions Table
# ============================================================
op.create_table('alert_interactions',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('alert_id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('interaction_type', sa.String(length=50), nullable=False),
sa.Column('interacted_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('response_time_seconds', sa.Integer(), nullable=True),
sa.Column('interaction_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['alert_id'], ['alerts.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
# Create indexes for alert_interactions
op.create_index('idx_alert_interactions_tenant_alert', 'alert_interactions', ['tenant_id', 'alert_id'], unique=False)
op.create_index('idx_alert_interactions_user', 'alert_interactions', ['user_id'], unique=False)
op.create_index('idx_alert_interactions_time', 'alert_interactions', ['interacted_at'], unique=False)
op.create_index('idx_alert_interactions_type', 'alert_interactions', ['interaction_type'], unique=False)
op.create_index('idx_alert_interactions_tenant_time', 'alert_interactions', ['tenant_id', 'interacted_at'], unique=False)
# ============================================================
# Create Audit Logs Table
# ============================================================
op.create_table('audit_logs',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('action', sa.String(length=100), nullable=False),
sa.Column('resource_type', sa.String(length=100), nullable=False),
sa.Column('resource_id', sa.String(length=255), nullable=True),
sa.Column('severity', sa.String(length=20), nullable=False),
sa.Column('service_name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('changes', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('audit_metadata', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('ip_address', sa.String(length=45), nullable=True),
sa.Column('user_agent', sa.Text(), nullable=True),
sa.Column('endpoint', sa.String(length=255), nullable=True),
sa.Column('method', sa.String(length=10), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# Create indexes for audit_logs
op.create_index('idx_audit_resource_type_action', 'audit_logs', ['resource_type', 'action'], unique=False)
op.create_index('idx_audit_service_created', 'audit_logs', ['service_name', 'created_at'], unique=False)
op.create_index('idx_audit_severity_created', 'audit_logs', ['severity', 'created_at'], unique=False)
op.create_index('idx_audit_tenant_created', 'audit_logs', ['tenant_id', 'created_at'], unique=False)
op.create_index('idx_audit_user_created', 'audit_logs', ['user_id', 'created_at'], unique=False)
op.create_index(op.f('ix_audit_logs_action'), 'audit_logs', ['action'], unique=False)
op.create_index(op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'], unique=False)
op.create_index(op.f('ix_audit_logs_resource_id'), 'audit_logs', ['resource_id'], unique=False)
op.create_index(op.f('ix_audit_logs_resource_type'), 'audit_logs', ['resource_type'], unique=False)
op.create_index(op.f('ix_audit_logs_service_name'), 'audit_logs', ['service_name'], unique=False)
op.create_index(op.f('ix_audit_logs_severity'), 'audit_logs', ['severity'], unique=False)
op.create_index(op.f('ix_audit_logs_tenant_id'), 'audit_logs', ['tenant_id'], unique=False)
op.create_index(op.f('ix_audit_logs_user_id'), 'audit_logs', ['user_id'], unique=False)
# Remove server defaults after table creation (for new inserts)
op.alter_column('alerts', 'confidence_score', server_default=None)
op.alter_column('alerts', 'timing_decision', server_default=None)
op.alter_column('alerts', 'hidden_from_ui', server_default=None)
def downgrade() -> None:
# Drop audit_logs table and indexes
op.drop_index(op.f('ix_audit_logs_user_id'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_tenant_id'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_severity'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_service_name'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_resource_type'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_resource_id'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_created_at'), table_name='audit_logs')
op.drop_index(op.f('ix_audit_logs_action'), table_name='audit_logs')
op.drop_index('idx_audit_user_created', table_name='audit_logs')
op.drop_index('idx_audit_tenant_created', table_name='audit_logs')
op.drop_index('idx_audit_severity_created', table_name='audit_logs')
op.drop_index('idx_audit_service_created', table_name='audit_logs')
op.drop_index('idx_audit_resource_type_action', table_name='audit_logs')
op.drop_table('audit_logs')
# Drop alert_interactions table and indexes
op.drop_index('idx_alert_interactions_tenant_time', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_type', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_time', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_user', table_name='alert_interactions')
op.drop_index('idx_alert_interactions_tenant_alert', table_name='alert_interactions')
op.drop_table('alert_interactions')
# Drop alerts table and indexes
op.drop_index('idx_alerts_hidden', table_name='alerts')
op.drop_index('idx_alerts_superseded', table_name='alerts')
op.drop_index('idx_alerts_action_created', table_name='alerts')
op.drop_index('idx_alerts_domain', table_name='alerts')
op.drop_index('idx_alerts_timing', table_name='alerts')
op.drop_index('idx_alerts_priority_level', table_name='alerts')
op.drop_index('idx_alerts_type_class', table_name='alerts')
op.drop_index('idx_alerts_priority_score', table_name='alerts')
op.drop_index(op.f('ix_alerts_tenant_id'), table_name='alerts')
op.drop_index(op.f('ix_alerts_status'), table_name='alerts')
op.drop_index(op.f('ix_alerts_created_at'), table_name='alerts')
op.drop_table('alerts')
# Drop enum types
op.execute('DROP TYPE IF EXISTS alerttypeclass;')
op.execute('DROP TYPE IF EXISTS prioritylevel;')
op.execute('DROP TYPE IF EXISTS alertstatus;')