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/librato: Use datetime_to_global_time and ISO 8601 syntax.

Niloth Pbe153f9fc74c
diff --git a/zerver/webhooks/librato/tests.py b/zerver/webhooks/librato/tests.py
index f30008fcc04..3038194aa76 100644
--- a/zerver/webhooks/librato/tests.py
+++ b/zerver/webhooks/librato/tests.py
@@ -21,7 +21,7 @@ class LibratoHookTests(WebhookTestCase):
def test_alert_message_with_default_topic(self) -> None:
expected_topic_name = "Alert alert.name"
- expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.google.pl):\n * Metric `librato.cpu.percent.idle`, sum was below 44 by 300s, recorded at <time:2016-03-31 09:11:42 UTC>.\n * Metric `librato.swap.swap.cached`, average was absent by 300s, recorded at <time:2016-03-31 09:11:42 UTC>.\n * Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at <time:2016-03-31 09:11:42 UTC>."
+ expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.google.pl):\n * Metric `librato.cpu.percent.idle`, sum was below 44 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, average was absent by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>."
self.check_webhook(
"alert",
expected_topic_name,
@@ -32,7 +32,7 @@ class LibratoHookTests(WebhookTestCase):
def test_alert_message_with_custom_topic(self) -> None:
custom_topic_name = "custom_name"
self.url = self.build_webhook_url(topic=custom_topic_name)
- expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.google.pl):\n * Metric `librato.cpu.percent.idle`, sum was below 44 by 300s, recorded at <time:2016-03-31 09:11:42 UTC>.\n * Metric `librato.swap.swap.cached`, average was absent by 300s, recorded at <time:2016-03-31 09:11:42 UTC>.\n * Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at <time:2016-03-31 09:11:42 UTC>."
+ expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.google.pl):\n * Metric `librato.cpu.percent.idle`, sum was below 44 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, average was absent by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>."
self.check_webhook(
"alert",
custom_topic_name,
@@ -41,7 +41,7 @@ class LibratoHookTests(WebhookTestCase):
)
def test_three_conditions_alert_message(self) -> None:
- expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.use.water.pl):\n * Metric `collectd.interface.eth0.if_octets.tx`, absolute_value was above 4 by 300s, recorded at <time:2016-04-11 20:40:14 UTC>.\n * Metric `collectd.load.load.longterm`, max was above 99, recorded at <time:2016-04-11 20:40:14 UTC>.\n * Metric `librato.swap.swap.cached`, average was absent by 60s, recorded at <time:2016-04-11 20:40:14 UTC>."
+ expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.use.water.pl):\n * Metric `collectd.interface.eth0.if_octets.tx`, absolute_value was above 4 by 300s, recorded at <time:2016-04-11T20:40:14+00:00>.\n * Metric `collectd.load.load.longterm`, max was above 99, recorded at <time:2016-04-11T20:40:14+00:00>.\n * Metric `librato.swap.swap.cached`, average was absent by 60s, recorded at <time:2016-04-11T20:40:14+00:00>."
expected_topic_name = "Alert TooHighTemperature"
self.check_webhook(
"three_conditions_alert",
@@ -52,7 +52,7 @@ class LibratoHookTests(WebhookTestCase):
def test_alert_clear(self) -> None:
expected_topic_name = "Alert Alert_name"
- expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6309313) has cleared at <time:2016-04-12 13:11:44 UTC>!"
+ expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6309313) has cleared at <time:2016-04-12T13:11:44+00:00>!"
self.check_webhook(
"alert_cleared",
expected_topic_name,
diff --git a/zerver/webhooks/librato/view.py b/zerver/webhooks/librato/view.py
index 5961b73630f..100328e5c43 100644
--- a/zerver/webhooks/librato/view.py
+++ b/zerver/webhooks/librato/view.py
@@ -1,5 +1,4 @@
from collections.abc import Callable, Mapping
-from datetime import datetime, timezone
from typing import Any
import orjson
@@ -10,6 +9,7 @@ from pydantic import Json
from zerver.decorator import webhook_view
from zerver.lib.exceptions import JsonableError
from zerver.lib.response import json_success
+from zerver.lib.timestamp import datetime_to_global_time, timestamp_to_datetime
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
@@ -43,9 +43,7 @@ class LibratoWebhookParser:
def parse_violation(self, violation: dict[str, Any]) -> tuple[str, str]:
metric_name = violation["metric"]
- recorded_at = datetime.fromtimestamp(violation["recorded_at"], tz=timezone.utc).strftime(
- "%Y-%m-%d %H:%M:%S"
- )
+ recorded_at = datetime_to_global_time(timestamp_to_datetime(violation["recorded_at"]))
return metric_name, recorded_at
def parse_conditions(self) -> list[dict[str, Any]]:
@@ -97,12 +95,8 @@ class LibratoWebhookHandler(LibratoWebhookParser):
return topic_template.format(alert_name=alert_name)
def handle_alert_clear_message(self) -> str:
- alert_clear_template = (
- "Alert [alert_name]({alert_url}) has cleared at <time:{trigger_time} UTC>!"
- )
- trigger_time = datetime.fromtimestamp(
- self.payload["trigger_time"], tz=timezone.utc
- ).strftime("%Y-%m-%d %H:%M:%S")
+ alert_clear_template = "Alert [alert_name]({alert_url}) has cleared at {trigger_time}!"
+ trigger_time = datetime_to_global_time(timestamp_to_datetime(self.payload["trigger_time"]))
_alert_id, alert_name, alert_url, _alert_runbook_url = self.parse_alert()
content = alert_clear_template.format(
alert_name=alert_name, alert_url=alert_url, trigger_time=trigger_time
@@ -157,7 +151,7 @@ class LibratoWebhookHandler(LibratoWebhookParser):
)
if duration:
content += f" by {duration}s"
- content += f", recorded at <time:{recorded_at} UTC>."
+ content += f", recorded at {recorded_at}."
return content