Files
zulip/zerver/webhooks/grafana/view.py
Anders Kaseorg 365fe0b3d5 python: Sort imports with isort.
Fixes #2665.

Regenerated by tabbott with `lint --fix` after a rebase and change in
parameters.

Note from tabbott: In a few cases, this converts technical debt in the
form of unsorted imports into different technical debt in the form of
our largest files having very long, ugly import sequences at the
start.  I expect this change will increase pressure for us to split
those files, which isn't a bad thing.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-06-11 16:45:32 -07:00

48 lines
1.7 KiB
Python

from typing import Any, Dict
from django.http import HttpRequest, HttpResponse
from zerver.decorator import api_key_only_webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
GRAFANA_TOPIC_TEMPLATE = '{alert_title}'
GRAFANA_MESSAGE_TEMPLATE = '[{rule_name}]({rule_url})\n\n{alert_message}{eval_matches}'
@api_key_only_webhook_view('Grafana')
@has_request_variables
def api_grafana_webhook(
request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
) -> HttpResponse:
topic = GRAFANA_TOPIC_TEMPLATE.format(alert_title=payload['title'])
eval_matches_text = ''
eval_matches = payload.get('evalMatches')
if eval_matches is not None:
for match in eval_matches:
eval_matches_text += '**{}:** {}\n'.format(match['metric'], match['value'])
message_text = ''
if payload.get('message') is not None:
message_text = payload['message'] + "\n\n"
body = GRAFANA_MESSAGE_TEMPLATE.format(alert_message=message_text,
rule_name=payload['ruleName'],
rule_url=payload['ruleUrl'],
eval_matches=eval_matches_text)
if payload.get('imageUrl') is not None:
body += "\n[Click to view visualization]({visualization})".format(visualization=payload['imageUrl'])
body = body.strip()
# send the message
check_send_webhook_message(request, user_profile, topic, body)
return json_success()