Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

webhooks/grafana: Use datetime_to_global_time and ISO 8601 syntax.

Niloth Pf8cdc4c9b9cd
diff --git a/zerver/webhooks/grafana/tests.py b/zerver/webhooks/grafana/tests.py
index 2d6fe42b5bb..a39032d1106 100644
--- a/zerver/webhooks/grafana/tests.py
+++ b/zerver/webhooks/grafana/tests.py
@@ -134,9 +134,9 @@ Someone is testing the alert notification within grafana.
**TestAlert**
-This alert was fired at <time:2022-08-31T05:54:04.52289368Z>.
+This alert was fired at <time:2022-08-31T05:54:04+00:00>.
-This alert was resolved at <time:2022-08-31T10:30:00.52288431Z>.
+This alert was resolved at <time:2022-08-31T10:30:00+00:00>.
Labels:
- alertname: TestAlert
@@ -166,7 +166,7 @@ Annotations:
**High memory usage**
-This alert was fired at <time:2021-10-12T09:51:03.157076+02:00>.
+This alert was fired at <time:2021-10-12T09:51:03+02:00>.
Labels:
- alertname: High memory usage
@@ -189,7 +189,7 @@ Annotations:
**High CPU usage**
-This alert was fired at <time:2021-10-12T09:56:03.157076+02:00>.
+This alert was fired at <time:2021-10-12T09:56:03+02:00>.
Labels:
- alertname: High CPU usage
@@ -241,7 +241,7 @@ Annotations:
**Memory (copy)**
-This alert was fired at <time:2024-03-01T02:09:00Z>.
+This alert was fired at <time:2024-03-01T02:09:00+00:00>.
Labels:
- alertname: Memory (copy)
@@ -274,7 +274,7 @@ Annotations:
expected_message = """
:alert: **FIRING**
-This alert was fired at <time:2024-03-01T02:09:00Z>.
+This alert was fired at <time:2024-03-01T02:09:00+00:00>.
Labels:
- debug: true
diff --git a/zerver/webhooks/grafana/view.py b/zerver/webhooks/grafana/view.py
index b7935d277a3..d2c5cc7d3cf 100644
--- a/zerver/webhooks/grafana/view.py
+++ b/zerver/webhooks/grafana/view.py
@@ -1,7 +1,9 @@
+import dateutil.parser
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.response import json_success
+from zerver.lib.timestamp import datetime_to_global_time
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
from zerver.lib.validator import (
WildValue,
@@ -24,9 +26,9 @@ OLD_MESSAGE_TEMPLATE = "{alert_status}[{rule_name}]({rule_url})\n\n{alert_messag
NEW_TOPIC_TEMPLATE = "[{alertname}]"
-START_TIME_TEMPLATE = "This alert was fired at <time:{start_time}>."
+START_TIME_TEMPLATE = "This alert was fired at {start_time}."
-END_TIME_TEMPLATE = "\n\nThis alert was resolved at <time:{end_time}>."
+END_TIME_TEMPLATE = "\n\nThis alert was resolved at {end_time}."
MESSAGE_LABELS_TEMPLATE = "\n\nLabels:\n{label_information}\n"
@@ -47,6 +49,11 @@ NEW_EVENT_TYPES = ["firing", "resolved"]
ALL_EVENT_TYPES = LEGACY_EVENT_TYPES + NEW_EVENT_TYPES
+def get_global_time(dt_str: str) -> str:
+ dt = dateutil.parser.parse(dt_str)
+ return datetime_to_global_time(dt)
+
+
@webhook_view("Grafana", all_event_types=ALL_EVENT_TYPES)
@typed_endpoint
def api_grafana_webhook(
@@ -83,11 +90,13 @@ def api_grafana_webhook(
alertname=alert["fingerprint"].tame(check_string)
)
- body += START_TIME_TEMPLATE.format(start_time=alert["startsAt"].tame(check_string))
+ body += START_TIME_TEMPLATE.format(
+ start_time=get_global_time(alert["startsAt"].tame(check_string))
+ )
end_time = alert["endsAt"].tame(check_string)
if end_time != "0001-01-01T00:00:00Z":
- body += END_TIME_TEMPLATE.format(end_time=end_time)
+ body += END_TIME_TEMPLATE.format(end_time=get_global_time(end_time))
if alert["labels"]:
label_information = ""