Add customizable email templates and remove alert recipients
All checks were successful
PostgreSQL Compatibility Matrix / PG14 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG15 smoke (push) Successful in 8s
PostgreSQL Compatibility Matrix / PG16 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG17 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG18 smoke (push) Successful in 7s
All checks were successful
PostgreSQL Compatibility Matrix / PG14 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG15 smoke (push) Successful in 8s
PostgreSQL Compatibility Matrix / PG16 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG17 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG18 smoke (push) Successful in 7s
Replaced the fixed `alert_recipients` list with customizable subject and body templates for alerts and warnings. This allows for more flexible and dynamic email notifications using placeholder variables. Updated relevant backend and frontend components to support this feature.
This commit is contained in:
@@ -29,7 +29,6 @@ async def _get_or_create_settings(db: AsyncSession) -> EmailNotificationSettings
|
||||
|
||||
|
||||
def _to_out(settings: EmailNotificationSettings) -> EmailSettingsOut:
|
||||
recipients = settings.alert_recipients if isinstance(settings.alert_recipients, list) else []
|
||||
return EmailSettingsOut(
|
||||
enabled=settings.enabled,
|
||||
smtp_host=settings.smtp_host,
|
||||
@@ -39,7 +38,10 @@ def _to_out(settings: EmailNotificationSettings) -> EmailSettingsOut:
|
||||
from_email=settings.from_email,
|
||||
use_starttls=settings.use_starttls,
|
||||
use_ssl=settings.use_ssl,
|
||||
alert_recipients=recipients,
|
||||
warning_subject_template=settings.warning_subject_template,
|
||||
alert_subject_template=settings.alert_subject_template,
|
||||
warning_body_template=settings.warning_body_template,
|
||||
alert_body_template=settings.alert_body_template,
|
||||
has_password=bool(settings.encrypted_smtp_password),
|
||||
updated_at=settings.updated_at,
|
||||
)
|
||||
@@ -70,7 +72,10 @@ async def update_email_settings(
|
||||
settings.from_email = str(payload.from_email) if payload.from_email else None
|
||||
settings.use_starttls = payload.use_starttls
|
||||
settings.use_ssl = payload.use_ssl
|
||||
settings.alert_recipients = [str(item) for item in payload.alert_recipients]
|
||||
settings.warning_subject_template = payload.warning_subject_template.strip() if payload.warning_subject_template else None
|
||||
settings.alert_subject_template = payload.alert_subject_template.strip() if payload.alert_subject_template else None
|
||||
settings.warning_body_template = payload.warning_body_template.strip() if payload.warning_body_template else None
|
||||
settings.alert_body_template = payload.alert_body_template.strip() if payload.alert_body_template else None
|
||||
|
||||
if payload.clear_smtp_password:
|
||||
settings.encrypted_smtp_password = None
|
||||
|
||||
@@ -134,7 +134,10 @@ class EmailNotificationSettings(Base):
|
||||
from_email: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
use_starttls: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||
use_ssl: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
||||
alert_recipients: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
|
||||
warning_subject_template: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
alert_subject_template: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
warning_body_template: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
alert_body_template: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
|
||||
@@ -12,7 +12,10 @@ class EmailSettingsOut(BaseModel):
|
||||
from_email: EmailStr | None
|
||||
use_starttls: bool
|
||||
use_ssl: bool
|
||||
alert_recipients: list[EmailStr]
|
||||
warning_subject_template: str | None
|
||||
alert_subject_template: str | None
|
||||
warning_body_template: str | None
|
||||
alert_body_template: str | None
|
||||
has_password: bool
|
||||
updated_at: datetime | None
|
||||
|
||||
@@ -28,7 +31,10 @@ class EmailSettingsUpdate(BaseModel):
|
||||
from_email: EmailStr | None = None
|
||||
use_starttls: bool = True
|
||||
use_ssl: bool = False
|
||||
alert_recipients: list[EmailStr] = []
|
||||
warning_subject_template: str | None = None
|
||||
alert_subject_template: str | None = None
|
||||
warning_body_template: str | None = None
|
||||
alert_body_template: str | None = None
|
||||
|
||||
@field_validator("smtp_port")
|
||||
@classmethod
|
||||
|
||||
@@ -81,6 +81,32 @@ def _render_body(item) -> str:
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _template_context(item) -> dict[str, str]:
|
||||
return {
|
||||
"severity": str(item.severity),
|
||||
"target_name": str(item.target_name),
|
||||
"target_id": str(item.target_id),
|
||||
"alert_name": str(item.name),
|
||||
"category": str(item.category),
|
||||
"description": str(item.description),
|
||||
"message": str(item.message),
|
||||
"value": "" if item.value is None else str(item.value),
|
||||
"warning_threshold": "" if item.warning_threshold is None else str(item.warning_threshold),
|
||||
"alert_threshold": "" if item.alert_threshold is None else str(item.alert_threshold),
|
||||
"checked_at": item.checked_at.isoformat() if item.checked_at else "",
|
||||
"alert_key": str(item.alert_key),
|
||||
}
|
||||
|
||||
|
||||
def _safe_format(template: str | None, context: dict[str, str], fallback: str) -> str:
|
||||
if not template:
|
||||
return fallback
|
||||
rendered = template
|
||||
for key, value in context.items():
|
||||
rendered = rendered.replace("{" + key + "}", value)
|
||||
return rendered
|
||||
|
||||
|
||||
async def process_target_owner_notifications(db: AsyncSession, status: AlertStatusResponse) -> None:
|
||||
settings = await db.scalar(select(EmailNotificationSettings).limit(1))
|
||||
if not settings or not settings.enabled:
|
||||
@@ -125,8 +151,15 @@ async def process_target_owner_notifications(db: AsyncSession, status: AlertStat
|
||||
should_send = existing is None or (now - existing.last_sent_at) >= _NOTIFICATION_COOLDOWN
|
||||
|
||||
if should_send:
|
||||
subject = _render_subject(item)
|
||||
body = _render_body(item)
|
||||
fallback_subject = _render_subject(item)
|
||||
fallback_body = _render_body(item)
|
||||
context = _template_context(item)
|
||||
if item.severity == "alert":
|
||||
subject = _safe_format(settings.alert_subject_template, context, fallback_subject)
|
||||
body = _safe_format(settings.alert_body_template, context, fallback_body)
|
||||
else:
|
||||
subject = _safe_format(settings.warning_subject_template, context, fallback_subject)
|
||||
body = _safe_format(settings.warning_body_template, context, fallback_body)
|
||||
for recipient in recipients:
|
||||
try:
|
||||
await _smtp_send(
|
||||
|
||||
Reference in New Issue
Block a user