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.

newrelic: refactor of time input handling.

Max Zawisa57e847ab89af
diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.py
index b5a2b87497e..0c4e34d9da6 100644
--- a/zerver/lib/webhooks/common.py
+++ b/zerver/lib/webhooks/common.py
@@ -1,4 +1,5 @@
import importlib
+from datetime import datetime
from typing import Any, Callable, Dict, Optional, Union
from urllib.parse import unquote
@@ -158,3 +159,16 @@ def get_http_headers_from_filename(http_header_key: str) -> Callable[[str], Dict
event_type = filename
return {http_header_key: event_type}
return fixture_to_headers
+
+def unix_milliseconds_to_timestamp(milliseconds: Any, webhook: str) -> datetime:
+ """If an integration requires time input in unix milliseconds, this helper
+ checks to ensure correct type and will catch any errors related to type or
+ value and raise a JsonableError.
+ Returns a datetime representing the time."""
+ try:
+ # timestamps are in milliseconds so divide by 1000
+ return datetime.fromtimestamp((milliseconds) / 1000)
+ except ValueError:
+ raise JsonableError(_("The {} webhook expects time in milleseconds.").format(webhook))
+ except TypeError:
+ raise JsonableError(_("The {} webhook expects time in milleseconds.").format(webhook))
diff --git a/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json b/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json
index d8d93a39105..21a0ba6ebf6 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_closed.json b/zerver/webhooks/newrelic/fixtures/incident_closed.json
index 56ec7095296..30d2c0ff8e3 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_closed.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_closed.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_default_fields.json b/zerver/webhooks/newrelic/fixtures/incident_default_fields.json
index a8460403532..4f0f54724c4 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_default_fields.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_default_fields.json
@@ -1,5 +1,4 @@
{
- "duration": 5,
"timestamp": 1605133931151,
"current_state": "open"
}
diff --git a/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json b/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json
index eaedbceae60..746892df619 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json b/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json
index 9b3b9088f6b..545d302a84d 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_missing_duration.json b/zerver/webhooks/newrelic/fixtures/incident_missing_duration.json
deleted file mode 100644
index c9e32b7be6c..00000000000
--- a/zerver/webhooks/newrelic/fixtures/incident_missing_duration.json
+++ /dev/null
@@ -1,10 +0,0 @@
-{
- "incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "incident_id": 1234,
- "details": "Violation description test.",
- "policy_name": "Test policy name",
- "condition_name": "Server Down",
- "timestamp": 1605133931151,
- "current_state": "open",
- "incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"
-}
diff --git a/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json b/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json
index 4afd7f35047..f9bece876c3 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_opened.json b/zerver/webhooks/newrelic/fixtures/incident_opened.json
index 0c9be3e14fb..c9e32b7be6c 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_opened.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_opened.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json b/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json
index f431cfd7b8a..559a7f2ccd3 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json b/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json
index 201f800ec32..fbff28b9eeb 100644
--- a/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json
+++ b/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json
@@ -1,6 +1,5 @@
{
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234/acknowledge",
- "duration": 5,
"incident_id": 1234,
"details": "Violation description test.",
"policy_name": "Test policy name",
diff --git a/zerver/webhooks/newrelic/tests.py b/zerver/webhooks/newrelic/tests.py
index df06ded44e5..13290c9c8aa 100644
--- a/zerver/webhooks/newrelic/tests.py
+++ b/zerver/webhooks/newrelic/tests.py
@@ -9,7 +9,7 @@ class NewRelicHookTests(WebhookTestCase):
def test_open(self) -> None:
expected_topic = "Test policy name (1234)"
expected_message = """
-[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **opened** for condition: **Server Down** at <time:2020-11-11 22:32:11.146000>
+[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **opened** for condition: **Server Down** at <time:2020-11-11 22:32:11.151000>
``` quote
Violation description test.
```
@@ -61,7 +61,7 @@ Violation description test.
def test_missing_fields(self) -> None:
expected_topic = "Unknown Policy (Unknown ID)"
expected_message = """
-[Incident](https://alerts.newrelic.com) **opened** for condition: **Unknown condition** at <time:2020-11-11 22:32:11.146000>
+[Incident](https://alerts.newrelic.com) **opened** for condition: **Unknown condition** at <time:2020-11-11 22:32:11.151000>
``` quote
No details.
```
@@ -102,7 +102,7 @@ No details.
"",
content_type="application/json",
)
- self.assertIn("The newrelic webhook expects timestamp and duration in milliseconds", e.exception.args[0])
+ self.assertIn("The newrelic webhook expects time in milleseconds.", e.exception.args[0])
def test_time_too_large(self) -> None:
with self.assertRaises(AssertionError) as e:
@@ -112,4 +112,4 @@ No details.
"",
content_type="application/json",
)
- self.assertIn("The newrelic webhook expects timestamp and duration in milliseconds", e.exception.args[0])
+ self.assertIn("The newrelic webhook expects time in milleseconds.", e.exception.args[0])
diff --git a/zerver/webhooks/newrelic/view.py b/zerver/webhooks/newrelic/view.py
index 12f062f4ba1..188c9651a1b 100644
--- a/zerver/webhooks/newrelic/view.py
+++ b/zerver/webhooks/newrelic/view.py
@@ -1,5 +1,4 @@
# Webhooks for external integrations.
-from datetime import datetime
from typing import Any, Dict
from django.http import HttpRequest, HttpResponse
@@ -8,7 +7,7 @@ from django.utils.translation import ugettext as _
from zerver.decorator import webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
-from zerver.lib.webhooks.common import check_send_webhook_message
+from zerver.lib.webhooks.common import check_send_webhook_message, unix_milliseconds_to_timestamp
from zerver.models import UserProfile
OPEN_TEMPLATE = """
@@ -43,19 +42,7 @@ def api_newrelic_webhook(
if unix_time is None:
return json_error(_("The newrelic webhook requires timestamp in milliseconds"))
- duration = payload.get('duration', None)
- if duration is None:
- return json_error(_("The newrelic webhook requires duration in milliseconds"))
-
- try:
- # Timestamp - duration to get time alert started
- # timestamps from NewRelic are in ms
- iso_timestamp = datetime.fromtimestamp((unix_time - duration) / 1000)
- info['iso_timestamp'] = iso_timestamp
- except ValueError:
- return json_error(_("The newrelic webhook expects timestamp and duration in milliseconds"))
- except TypeError:
- return json_error(_("The newrelic webhook expects timestamp and duration in milliseconds"))
+ info['iso_timestamp'] = unix_milliseconds_to_timestamp(unix_time, "newrelic")
# These are the three promised current_state values
if 'open' in info['status']: