feat: add alert history

This commit is contained in:
Christian Krakau-Louis
2026-05-22 23:18:32 +02:00
parent c384200098
commit af66c93829
17 changed files with 469 additions and 12 deletions
+5 -5
View File
@@ -1,10 +1,8 @@
import os
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from sqlalchemy import engine_from_config, pool
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
@@ -22,8 +20,7 @@ if database_url:
config.set_main_option("sqlalchemy.url", _make_sync_db_url(database_url))
# Import all models so that autogenerate can detect them
from app.core.database import Base # noqa: E402
import app.models.alert # noqa: E402, F401
import app.models.domain # noqa: E402, F401
import app.models.mail_source # noqa: E402, F401
import app.models.mail_source_import # noqa: E402, F401
@@ -31,6 +28,9 @@ import app.models.report # noqa: E402, F401
import app.models.setting # noqa: E402, F401
import app.models.user # noqa: E402, F401
# Import all models so that autogenerate can detect them
from app.core.database import Base # noqa: E402
target_metadata = Base.metadata
@@ -0,0 +1,107 @@
"""add alert history
Revision ID: a7b8c9d0e1f2
Revises: f6a7b8c9d0e1
Create Date: 2026-05-22 23:12:00.000000
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "a7b8c9d0e1f2"
down_revision: Union[str, Sequence[str], None] = "f6a7b8c9d0e1"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Create persisted alert history rows."""
op.create_table(
"alert_history",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("fingerprint", sa.String(length=64), nullable=False),
sa.Column("rule", sa.String(), nullable=False),
sa.Column("severity", sa.String(), nullable=False),
sa.Column("domain", sa.String(), nullable=True),
sa.Column("title", sa.String(), nullable=False),
sa.Column("detail", sa.Text(), nullable=False),
sa.Column("payload", sa.Text(), nullable=True),
sa.Column("observed_count", sa.Integer(), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("first_seen_at", sa.DateTime(), nullable=False),
sa.Column("last_seen_at", sa.DateTime(), nullable=False),
sa.Column("resolved_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("fingerprint"),
)
op.create_index(op.f("ix_alert_history_id"), "alert_history", ["id"], unique=False)
op.create_index(
op.f("ix_alert_history_fingerprint"),
"alert_history",
["fingerprint"],
unique=False,
)
op.create_index(op.f("ix_alert_history_rule"), "alert_history", ["rule"], unique=False)
op.create_index(
op.f("ix_alert_history_severity"),
"alert_history",
["severity"],
unique=False,
)
op.create_index(op.f("ix_alert_history_domain"), "alert_history", ["domain"], unique=False)
op.create_index(
op.f("ix_alert_history_is_active"),
"alert_history",
["is_active"],
unique=False,
)
op.create_index(
op.f("ix_alert_history_first_seen_at"),
"alert_history",
["first_seen_at"],
unique=False,
)
op.create_index(
op.f("ix_alert_history_last_seen_at"),
"alert_history",
["last_seen_at"],
unique=False,
)
op.create_index(
op.f("ix_alert_history_resolved_at"),
"alert_history",
["resolved_at"],
unique=False,
)
op.create_index(
"ix_alert_history_active_last_seen",
"alert_history",
["is_active", "last_seen_at"],
unique=False,
)
op.create_index(
"ix_alert_history_rule_domain",
"alert_history",
["rule", "domain"],
unique=False,
)
def downgrade() -> None:
"""Drop persisted alert history rows."""
op.drop_index("ix_alert_history_rule_domain", table_name="alert_history")
op.drop_index("ix_alert_history_active_last_seen", table_name="alert_history")
op.drop_index(op.f("ix_alert_history_resolved_at"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_last_seen_at"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_first_seen_at"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_is_active"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_domain"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_severity"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_rule"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_fingerprint"), table_name="alert_history")
op.drop_index(op.f("ix_alert_history_id"), table_name="alert_history")
op.drop_table("alert_history")