🎨 — Make each handler build its own message

This commit is contained in:
Luc Didry 2023-12-11 13:24:39 +01:00
parent 2ab9afabc9
commit 9ed58220f4
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C
3 changed files with 31 additions and 19 deletions

View file

@ -22,10 +22,18 @@ def handle_alert(config: Config, result, task, severity, request):
result.status,
severity)
if config.general.mail is not None or \
config.general.gotify is not None:
if config.general.mail is not None and \
'mail' in getattr(config.general.alerts, severity):
notify_by_mail(result, task, severity, config.general.mail, request)
if config.general.gotify is not None and \
'gotify' in getattr(config.general.alerts, severity):
notify_with_gotify(result, task, severity, config.general.gotify, request)
def notify_by_mail(result, task, severity, config: Mail, request) -> None:
logger.debug('Will send mail notification')
subject = f"{urlparse(task.url).netloc}: status {severity}"
msg = f"""\
URL: {task.url}
Check: {task.check}
@ -35,18 +43,8 @@ Time: {result.submitted_at}
See results of task on {request.url_for('get_task_results', task_id=task.id)}
"""
if config.general.mail is not None and \
'mail' in getattr(config.general.alerts, severity):
notify_by_mail(subject, msg, config.general.mail)
if config.general.gotify is not None and \
'gotify' in getattr(config.general.alerts, severity):
notify_with_gotify(subject, msg, severity, config.general.gotify)
def notify_by_mail(subject: str, msg: str, config: Mail) -> None:
logger.debug('Will send mail notification')
mail = f"""\
Subject: [Argos] {subject}
Subject: [Argos] {urlparse(task.url).netloc}: status {severity}
{msg}"""
@ -75,16 +73,29 @@ Subject: [Argos] {subject}
smtp.sendmail(config.mailfrom, address, mail)
def notify_with_gotify(subject: str, msg: str, severity: str, config: List[GotifyUrl]) -> None:
def notify_with_gotify(result, task, severity: str, config: List[GotifyUrl], request) -> None:
logger.debug('Will send gotify notification')
headers = {'accept': 'application/json',
'content-type': 'application/json'}
priority = 9
icon = ''
if severity == Severity.OK:
priority = 1
icon = ''
elif severity == Severity.WARNING:
priority = 5
icon = '⚠️'
subject = f"{icon} {urlparse(task.url).netloc}: status {severity}"
msg = f"""\
URL: {task.url}
Check: {task.check}
Status: {severity}
Time: {result.submitted_at}
See results of task on {request.url_for('get_task_results', task_id=task.id)}
"""
payload = {'title': subject,
'message': msg,
@ -99,7 +110,7 @@ def notify_with_gotify(subject: str, msg: str, severity: str, config: List[Gotif
headers=headers,
json=payload)
res.raise_for_status()
except httpx.RequestError as exc:
except httpx.RequestError as err:
logger.error('An error occurred while sending a message to %s with token %s',
exc.request.url,
err.request.url,
token)

View file

@ -59,6 +59,7 @@ async def create_results(
# Dont create an alert if the severity has not changed
if last_severity != severity:
# XXX Use a job queue or make it async
handle_alert(config, result, task, severity, request)
db_results.append(result)

View file

@ -12,7 +12,7 @@
{{ counts_dict['ok'] }}
</article>
<article>
<header> ⚠️ Warning</header>
<header>⚠️ Warning</header>
{{ counts_dict['warning'] }}
</article>
<article>