feat: global notification policy severity to channels (notif-22)

Singleton notification_policy table, NOTIFY_MIN_SEVERITY/CHANNELS env,
central dispatch gate, Settings policy UI, migration 007.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
PTah
2026-05-29 16:19:26 +10:00
parent 9b177c145b
commit ebb450be92
19 changed files with 577 additions and 126 deletions
+9 -13
View File
@@ -9,8 +9,8 @@ import httpx
from sqlalchemy.orm import Session
from app.models import Event, Problem
from app.services.notification_severity import severity_meets_minimum
from app.services.notification_settings import WebhookConfig, get_effective_webhook_config
from app.services.telegram_notify import SEVERITY_ORDER
logger = logging.getLogger(__name__)
@@ -25,14 +25,6 @@ class WebhookSendError(Exception):
self.status_code = status_code
def _severity_value(value: str) -> int:
return SEVERITY_ORDER.get(value, 0)
def _should_notify_severity(severity: str, *, config: WebhookConfig) -> bool:
return _severity_value(severity) >= _severity_value(config.min_severity)
def _host_payload(entity: Event | Problem) -> dict[str, Any]:
host = entity.host
if host is None:
@@ -120,9 +112,11 @@ def send_webhook_test_message(*, config: WebhookConfig | None = None) -> None:
)
def notify_event(event: Event, *, db: Session | None = None) -> None:
def notify_event(event: Event, *, db: Session | None = None, apply_policy_gate: bool = True) -> None:
cfg = get_effective_webhook_config(db)
if not cfg.active or not _should_notify_severity(event.severity, config=cfg):
if not cfg.active:
return
if apply_policy_gate and not severity_meets_minimum(event.severity, cfg.min_severity):
return
try:
send_webhook_payload(build_event_payload(event), config=cfg)
@@ -130,9 +124,11 @@ def notify_event(event: Event, *, db: Session | None = None) -> None:
logger.exception("notify_event webhook failed event_id=%s", event.event_id)
def notify_problem(problem: Problem, event: Event | None = None, *, db: Session | None = None) -> None:
def notify_problem(problem: Problem, event: Event | None = None, *, db: Session | None = None, apply_policy_gate: bool = True) -> None:
cfg = get_effective_webhook_config(db)
if not cfg.active or not _should_notify_severity(problem.severity, config=cfg):
if not cfg.active:
return
if apply_policy_gate and not severity_meets_minimum(problem.severity, cfg.min_severity):
return
try:
send_webhook_payload(build_problem_payload(problem, event), config=cfg)