5d41ccac1c
Closes the action loop — instead of polling the dashboard, the platform pushes actionable signals to Telegram. New hourly 'alerts' job dispatches four toggleable triggers, deduped via a new alert_log table (cooldown-based for qualified/S-R/digest, watermark-based for score deterioration). Admin → Settings gains a Telegram panel (write-only bot token, chat ID, per-trigger toggles, Send Test). Credentials follow DB > env precedence (TELEGRAM_BOT_TOKEN / _CHAT_ID). Backend: alert_service + AlertLog model + migration 005, scheduler job, admin endpoints/schema. Frontend: AlertSettings panel, hooks, api, types. Deploy: run alembic upgrade (new alert_log table). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, Float, Index, String
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class AlertLog(Base):
|
|
"""Append-only log of alerts sent (and score watermarks).
|
|
|
|
Two uses, distinguished by ``alert_type``:
|
|
- notification dedup: a row records that ``dedup_key`` was alerted at
|
|
``created_at``; the dispatcher suppresses re-sending the same key within
|
|
a cooldown window.
|
|
- score watermark: rows of type ``score_watermark`` carry the last
|
|
observed composite in ``value``; the latest row per key is the baseline
|
|
for score-deterioration alerts.
|
|
"""
|
|
|
|
__tablename__ = "alert_log"
|
|
__table_args__ = (
|
|
Index("ix_alert_log_type_key_created", "alert_type", "dedup_key", "created_at"),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
alert_type: Mapped[str] = mapped_column(String(30), nullable=False)
|
|
dedup_key: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
value: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=datetime.utcnow, nullable=False
|
|
)
|