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:
@@ -4,12 +4,11 @@ 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 TelegramConfig, get_effective_telegram_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SEVERITY_ORDER = {"info": 10, "warning": 20, "high": 30, "critical": 40}
|
||||
|
||||
|
||||
class TelegramNotConfiguredError(Exception):
|
||||
"""Telegram disabled or missing token/chat_id."""
|
||||
@@ -21,10 +20,6 @@ class TelegramSendError(Exception):
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
def _severity_value(value: str) -> int:
|
||||
return SEVERITY_ORDER.get(value, 0)
|
||||
|
||||
|
||||
def send_telegram_text(message: str, *, config: TelegramConfig | None = None, force: bool = False) -> None:
|
||||
cfg = config or get_effective_telegram_config()
|
||||
if not force and not cfg.active:
|
||||
@@ -61,14 +56,11 @@ def send_telegram_test_message(*, config: TelegramConfig | None = None) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _should_notify_severity(severity: str, *, config: TelegramConfig) -> bool:
|
||||
min_level = _severity_value(config.min_severity)
|
||||
return _severity_value(severity) >= min_level
|
||||
|
||||
|
||||
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_telegram_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
|
||||
host = event.host.hostname if event.host else "unknown"
|
||||
message = (
|
||||
@@ -85,9 +77,11 @@ def notify_event(event: Event, *, db: Session | None = None) -> None:
|
||||
logger.exception("notify_event telegram 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_telegram_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
|
||||
host = problem.host.hostname if problem.host else "unknown"
|
||||
related = ""
|
||||
|
||||
Reference in New Issue
Block a user