BIN
										
									
								
								static/images/integrations/librato/001.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.4 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/002.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.2 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/003.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 27 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/004.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 3.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/005.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 88 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/006.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 29 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/007.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.7 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/008.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 33 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/librato/009.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 12 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								static/images/integrations/logos/librato.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 3.1 KiB  | 
@@ -1156,6 +1156,80 @@
 | 
			
		||||
      <img class="screenshot" src="/static/images/integrations/jira/001.png" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="librato" class="integration-instructions">
 | 
			
		||||
 | 
			
		||||
    <p>Get Zulip notifications for your Librato alerts or snapshots!</p>
 | 
			
		||||
 | 
			
		||||
    <p>First, create the stream you'd like to use for Librato notifications, and
 | 
			
		||||
    subscribe all interested parties to this stream. We recommend the
 | 
			
		||||
    name <code>librato</code>.</p>
 | 
			
		||||
 | 
			
		||||
    <p>Next, on your <a href="/#settings" target="_blank">Zulip settings
 | 
			
		||||
    page</a>, create a Librato bot. Please note the bot name and API key. Then:</p>
 | 
			
		||||
 | 
			
		||||
      <p><b>Alerts configuration</b></p>
 | 
			
		||||
 | 
			
		||||
      <p>1. Login into your Librato account and switch to the integrations page:</p>
 | 
			
		||||
      <p><img src="/static/images/integrations/librato/001.png" /></p>
 | 
			
		||||
 | 
			
		||||
      <p>2. From there, select <b>"Webhook"</b> integration:</p>
 | 
			
		||||
      <p><img src="/static/images/integrations/librato/002.png" /></p>
 | 
			
		||||
 | 
			
		||||
      <p>3. Fill in the title and URL fields using your bot's API key</p>
 | 
			
		||||
      <p><img class="screenshot" src="/static/images/integrations/librato/003.png" /></p>
 | 
			
		||||
 | 
			
		||||
      <p>The default stream name is <b>librato</b> and default topic name is <b>Alert alert_name</b>.</p>
 | 
			
		||||
 | 
			
		||||
      <div class="codehilite"><pre>
 | 
			
		||||
      URL = {{ external_api_uri }}/v1/external/librato?api_key=<b>your_api_key</b></pre></div>
 | 
			
		||||
 | 
			
		||||
      <p>You can customize the stream name and topic name:</p>
 | 
			
		||||
 | 
			
		||||
      <div class="codehilite"><pre>
 | 
			
		||||
      URL = {{ external_api_uri }}/v1/external/librato?stream=<b>your_stream_name</b>&topic=<b>your_topic_name</b>&api_key=<b>your_api_key</b></pre></div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      <p>4. Next, go to your alerts page:</p>
 | 
			
		||||
      <p><img class="screenshot" src="/static/images/integrations/librato/004.png" /></p>
 | 
			
		||||
 | 
			
		||||
      <p>5. Choose the alert conditions and enable the your new
 | 
			
		||||
      webhook under "Notification Services":</p>
 | 
			
		||||
      <p><img class="screenshot" src="/static/images/integrations/librato/005.png" /></p>
 | 
			
		||||
 | 
			
		||||
      <p>
 | 
			
		||||
        <b>Congratulations! You're done!</b><br />
 | 
			
		||||
        When an alert triggers, you'll get a Zulip notification that looks like this:
 | 
			
		||||
      </p>
 | 
			
		||||
 | 
			
		||||
      <p><img class="screenshot" src="/static/images/integrations/librato/006.png" /></p>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      <p><b>Snapshot configuration</b></p>
 | 
			
		||||
 | 
			
		||||
      <p>
 | 
			
		||||
        Because of limitations in Librato's API, you need to use the
 | 
			
		||||
        Slack integration to get Librato snapshots sent into Zulip.
 | 
			
		||||
      </p>
 | 
			
		||||
      <p><img src="/static/images/integrations/librato/007.png" /></p>
 | 
			
		||||
      <p>Default stream name is <b>librato</b> and default topic name is <b>snapshots</b>.</p>
 | 
			
		||||
 | 
			
		||||
      <p>
 | 
			
		||||
        To send a snapshot, just click at one of your chart, use
 | 
			
		||||
        the <b>send a snapshot</b> option and add the proper
 | 
			
		||||
        integration.
 | 
			
		||||
      </p>
 | 
			
		||||
 | 
			
		||||
      <p><img class="screenshot" src="/static/images/integrations/librato/008.png" /></p>
 | 
			
		||||
 | 
			
		||||
      <p>
 | 
			
		||||
        <b>Congratulations! You're done!</b><br />
 | 
			
		||||
        When a snapshot comes, you'll get a Zulip notification that looks like this:
 | 
			
		||||
      </p>
 | 
			
		||||
 | 
			
		||||
      <p><img class="screenshot" src="/static/images/integrations/librato/009.png" /></p>
 | 
			
		||||
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="mercurial" class="integration-instructions">
 | 
			
		||||
 | 
			
		||||
      <p>Get Zulip notifications when you <code>hg push</code>!</p>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										56
									
								
								zerver/fixtures/librato/librato_alert.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,56 @@
 | 
			
		||||
{
 | 
			
		||||
  "alert": {
 | 
			
		||||
    "id": 6294535,
 | 
			
		||||
    "name": "alert.name",
 | 
			
		||||
    "runbook_url": "http://www.google.pl",
 | 
			
		||||
    "version": 2,
 | 
			
		||||
    "description": "descrription"
 | 
			
		||||
  },
 | 
			
		||||
  "account": "lizonr@gmail.com",
 | 
			
		||||
  "trigger_time": 1459415502,
 | 
			
		||||
  "conditions": [
 | 
			
		||||
    {
 | 
			
		||||
      "id": 3543146,
 | 
			
		||||
      "type": "below",
 | 
			
		||||
      "threshold": 44,
 | 
			
		||||
      "summary_function": "sum",
 | 
			
		||||
      "duration": 300
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "id": 3651148,
 | 
			
		||||
      "type": "absent",
 | 
			
		||||
      "summary_function": "average",
 | 
			
		||||
      "duration": 300
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "id": 3651902,
 | 
			
		||||
      "type": "above",
 | 
			
		||||
      "threshold": 9,
 | 
			
		||||
      "summary_function": "derivative",
 | 
			
		||||
      "duration": 300
 | 
			
		||||
    }
 | 
			
		||||
  ],
 | 
			
		||||
  "violations": {
 | 
			
		||||
    "test-source": [
 | 
			
		||||
      {
 | 
			
		||||
        "metric": "librato.cpu.percent.idle",
 | 
			
		||||
        "value": 2,
 | 
			
		||||
        "recorded_at": 1459415502,
 | 
			
		||||
        "condition_violated": 3543146
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "metric": "librato.swap.swap.cached",
 | 
			
		||||
        "value": 42,
 | 
			
		||||
        "recorded_at": 1459415502,
 | 
			
		||||
        "condition_violated": 3651148
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "metric": "librato.swap.swap.cached",
 | 
			
		||||
        "value": 51,
 | 
			
		||||
        "recorded_at": 1459415502,
 | 
			
		||||
        "condition_violated": 3651902
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  },
 | 
			
		||||
  "triggered_by_user_test": true
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								zerver/fixtures/librato/librato_alert_cleared.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,11 @@
 | 
			
		||||
{
 | 
			
		||||
  "alert": {
 | 
			
		||||
    "id": 6309313,
 | 
			
		||||
    "name": "Alert_name",
 | 
			
		||||
    "runbook_url": "",
 | 
			
		||||
    "version": 2
 | 
			
		||||
  },
 | 
			
		||||
  "account": "mail@gmail.com",
 | 
			
		||||
  "trigger_time": 1460466704,
 | 
			
		||||
  "clear": "normal"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								zerver/fixtures/librato/librato_snapshot.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,15 @@
 | 
			
		||||
{
 | 
			
		||||
  "attachments": [
 | 
			
		||||
    {
 | 
			
		||||
      "author_name": "Hamlet",
 | 
			
		||||
      "author_link": "mailto:hamlet@zulip  .com",
 | 
			
		||||
      "author_icon": "https://secure.gravatar.com/avatar/99ca91cc7c0d8ef72fbd69a03a871a12?s=32&d=mm",
 | 
			
		||||
      "title": "https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409",
 | 
			
		||||
      "title_link": "https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409",
 | 
			
		||||
      "fallback": "https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409 by Rafal: http://snapshots.librato.com/chart/nr5l3n0c-82162.png",
 | 
			
		||||
      "image_url": "http://snapshots.librato.com/chart/nr5l3n0c-82162.png",
 | 
			
		||||
      "text": null,
 | 
			
		||||
      "color": "#0881AE"
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										55
									
								
								zerver/fixtures/librato/librato_three_conditions_alert.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,55 @@
 | 
			
		||||
{
 | 
			
		||||
  "alert": {
 | 
			
		||||
    "id": 6294535,
 | 
			
		||||
    "name": "ToHighTemeprature",
 | 
			
		||||
    "runbook_url": "http://www.use.water.pl",
 | 
			
		||||
    "version": 2,
 | 
			
		||||
    "description": "Measurment of temperature in your computer"
 | 
			
		||||
  },
 | 
			
		||||
  "account": "lizonr@gmail.com",
 | 
			
		||||
  "trigger_time": 1460407214,
 | 
			
		||||
  "conditions": [
 | 
			
		||||
    {
 | 
			
		||||
      "id": 4158783,
 | 
			
		||||
      "type": "above",
 | 
			
		||||
      "threshold": 4,
 | 
			
		||||
      "summary_function": "absolute_value",
 | 
			
		||||
      "duration": 300
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "id": 4299396,
 | 
			
		||||
      "type": "above",
 | 
			
		||||
      "threshold": 99,
 | 
			
		||||
      "summary_function": "max"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "id": 3651148,
 | 
			
		||||
      "type": "absent",
 | 
			
		||||
      "summary_function": "average",
 | 
			
		||||
      "duration": 60
 | 
			
		||||
    }
 | 
			
		||||
  ],
 | 
			
		||||
  "violations": {
 | 
			
		||||
    "test-source": [
 | 
			
		||||
      {
 | 
			
		||||
        "metric": "collectd.interface.eth0.if_octets.tx",
 | 
			
		||||
        "value": 46,
 | 
			
		||||
        "recorded_at": 1460407214,
 | 
			
		||||
        "condition_violated": 4158783
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "metric": "collectd.load.load.longterm",
 | 
			
		||||
        "value": 141,
 | 
			
		||||
        "recorded_at": 1460407214,
 | 
			
		||||
        "condition_violated": 4299396
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "metric": "librato.swap.swap.cached",
 | 
			
		||||
        "value": 42,
 | 
			
		||||
        "recorded_at": 1460407214,
 | 
			
		||||
        "condition_violated": 3651148
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  },
 | 
			
		||||
  "triggered_by_user_test": true
 | 
			
		||||
}
 | 
			
		||||
@@ -87,6 +87,7 @@ WEBHOOK_INTEGRATIONS = [
 | 
			
		||||
    WebhookIntegration('helloworld', display_name='Hello World'),
 | 
			
		||||
    WebhookIntegration('ifttt', function='zerver.views.webhooks.ifttt.api_iftt_app_webhook', display_name='IFTTT'),
 | 
			
		||||
    WebhookIntegration('jira', secondary_line_text='(hosted or v5.2+)', display_name='JIRA'),
 | 
			
		||||
    WebhookIntegration('librato'),
 | 
			
		||||
    WebhookIntegration('newrelic', display_name='New Relic'),
 | 
			
		||||
    WebhookIntegration('pagerduty'),
 | 
			
		||||
    WebhookIntegration('pingdom'),
 | 
			
		||||
 
 | 
			
		||||
@@ -2003,3 +2003,56 @@ class SentryHookTests(WebhookTestCase):
 | 
			
		||||
            expected_subject,
 | 
			
		||||
            expected_message
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
class LibratoHookTests(WebhookTestCase):
 | 
			
		||||
    STREAM_NAME = 'librato'
 | 
			
		||||
    URL_TEMPLATE = u"/api/v1/external/librato?api_key={api_key}&stream=librato"
 | 
			
		||||
    FIXTURE_DIR_NAME = 'librato'
 | 
			
		||||
    IS_ATTACHMENT = False
 | 
			
		||||
 | 
			
		||||
    def get_body(self, fixture_name):
 | 
			
		||||
        # type: (text_type) -> text_type
 | 
			
		||||
        if self.IS_ATTACHMENT:
 | 
			
		||||
            return self.fixture_data("librato", fixture_name, file_type='json')
 | 
			
		||||
        return urllib.parse.urlencode({'payload': self.fixture_data("librato", fixture_name, file_type='json')})
 | 
			
		||||
 | 
			
		||||
    def build_webhook_url(self, topic=None):
 | 
			
		||||
        # type: (Optional[text_type]) -> text_type
 | 
			
		||||
        api_key = self.get_api_key(self.TEST_USER_EMAIL)
 | 
			
		||||
        url = self.URL_TEMPLATE.format(stream=self.STREAM_NAME, api_key=api_key)
 | 
			
		||||
        if topic:
 | 
			
		||||
            url = u"{}&topic={}".format(url, topic)
 | 
			
		||||
        return url
 | 
			
		||||
 | 
			
		||||
    def test_alert_message_with_default_topic(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        expected_subject = '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 2016-03-31 05:11:42\n>Metric `librato.swap.swap.cached`, average was absent  by 300s, recorded at 2016-03-31 05:11:42\n>Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at 2016-03-31 05:11:42"
 | 
			
		||||
        self.send_and_test_stream_message('alert', expected_subject, expected_message, content_type="application/x-www-form-urlencoded")
 | 
			
		||||
 | 
			
		||||
    def test_alert_message_with_custom_topic(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        custom_topic = 'custom_name'
 | 
			
		||||
        self.url = self.build_webhook_url(custom_topic)
 | 
			
		||||
        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 2016-03-31 05:11:42\n>Metric `librato.swap.swap.cached`, average was absent  by 300s, recorded at 2016-03-31 05:11:42\n>Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at 2016-03-31 05:11:42"
 | 
			
		||||
        self.send_and_test_stream_message('alert', custom_topic, expected_message, content_type="application/x-www-form-urlencoded")
 | 
			
		||||
 | 
			
		||||
    def test_three_conditions_alert_message(self):
 | 
			
		||||
        # type: () -> 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 2016-04-11 16:40:14\n>Metric `collectd.load.load.longterm`, max was above 99, recorded at 2016-04-11 16:40:14\n>Metric `librato.swap.swap.cached`, average was absent  by 60s, recorded at 2016-04-11 16:40:14"
 | 
			
		||||
        expected_subject = 'Alert ToHighTemeprature'
 | 
			
		||||
        self.send_and_test_stream_message('three_conditions_alert', expected_subject, expected_message, content_type="application/x-www-form-urlencoded")
 | 
			
		||||
 | 
			
		||||
    def test_alert_clear(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        expected_subject = 'Alert Alert_name'
 | 
			
		||||
        expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6309313) has cleared at 2016-04-12 09:11:44!"
 | 
			
		||||
        self.send_and_test_stream_message('alert_cleared', expected_subject, expected_message, content_type="application/x-www-form-urlencoded")
 | 
			
		||||
 | 
			
		||||
    def test_snapshot(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        self.IS_ATTACHMENT = True
 | 
			
		||||
        expected_subject = 'Snapshots'
 | 
			
		||||
        expected_message = "**Hamlet** sent a [snapshot](http://snapshots.librato.com/chart/nr5l3n0c-82162.png) of [metric](https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409)"
 | 
			
		||||
        self.send_and_test_stream_message('snapshot', expected_subject, expected_message, content_type="application/x-www-form-urlencoded")
 | 
			
		||||
        self.IS_ATTACHMENT = False
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										183
									
								
								zerver/views/webhooks/librato.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,183 @@
 | 
			
		||||
from __future__ import absolute_import
 | 
			
		||||
 | 
			
		||||
from typing import Any, Optional, Callable, Tuple
 | 
			
		||||
from six import text_type
 | 
			
		||||
from six.moves import zip
 | 
			
		||||
 | 
			
		||||
from django.utils.translation import ugettext as _
 | 
			
		||||
from django.utils.datetime_safe import datetime
 | 
			
		||||
from django.http import HttpRequest, HttpResponse
 | 
			
		||||
 | 
			
		||||
from zerver.decorator import api_key_only_webhook_view, REQ, has_request_variables
 | 
			
		||||
from zerver.lib.response import json_success, json_error
 | 
			
		||||
from zerver.lib.actions import check_send_message
 | 
			
		||||
from zerver.models import Client, UserProfile
 | 
			
		||||
 | 
			
		||||
import ujson
 | 
			
		||||
 | 
			
		||||
ALERT_CLEAR = 'clear'
 | 
			
		||||
ALERT_VIOLATION = 'violations'
 | 
			
		||||
SNAPSHOT = 'image_url'
 | 
			
		||||
 | 
			
		||||
class LibratoWebhookParser(object):
 | 
			
		||||
    ALERT_URL_TEMPLATE = "https://metrics.librato.com/alerts#/{alert_id}"
 | 
			
		||||
 | 
			
		||||
    def __init__(self, payload, attachments):
 | 
			
		||||
        # type: (Dict[str, Any], List[Dict[str, Any]]) -> None
 | 
			
		||||
        self.payload = payload
 | 
			
		||||
        self.attachments = attachments
 | 
			
		||||
 | 
			
		||||
    def generate_alert_url(self, alert_id):
 | 
			
		||||
        # type: (int) -> text_type
 | 
			
		||||
        return self.ALERT_URL_TEMPLATE.format(alert_id=alert_id)
 | 
			
		||||
 | 
			
		||||
    def parse_alert(self):
 | 
			
		||||
        # type: () -> Tuple[int, text_type, text_type, text_type]
 | 
			
		||||
        alert = self.payload['alert']
 | 
			
		||||
        alert_id = alert['id']
 | 
			
		||||
        return alert_id, alert['name'], self.generate_alert_url(alert_id), alert['runbook_url']
 | 
			
		||||
 | 
			
		||||
    def parse_condition(self, condition):
 | 
			
		||||
        # type: (Dict[str, Any]) -> Tuple[text_type, text_type, text_type, text_type]
 | 
			
		||||
        summary_function = condition['summary_function']
 | 
			
		||||
        threshold = condition.get('threshold', '')
 | 
			
		||||
        condition_type = condition['type']
 | 
			
		||||
        duration = condition.get('duration', '')
 | 
			
		||||
        return summary_function, threshold, condition_type, duration
 | 
			
		||||
 | 
			
		||||
    def parse_violation(self, violation):
 | 
			
		||||
        # type: (Dict[str, Any]) -> Tuple[text_type, text_type]
 | 
			
		||||
        metric_name = violation['metric']
 | 
			
		||||
        recorded_at = datetime.fromtimestamp((violation['recorded_at']))
 | 
			
		||||
        return metric_name, recorded_at
 | 
			
		||||
 | 
			
		||||
    def parse_conditions(self):
 | 
			
		||||
        # type: () -> List[Dict[str, Any]]
 | 
			
		||||
        conditions = self.payload['conditions']
 | 
			
		||||
        return conditions
 | 
			
		||||
 | 
			
		||||
    def parse_violations(self):
 | 
			
		||||
        # type: () -> List[Dict[str, Any]]
 | 
			
		||||
        violations = self.payload['violations']['test-source']
 | 
			
		||||
        return violations
 | 
			
		||||
 | 
			
		||||
    def parse_snapshot(self, snapshot):
 | 
			
		||||
        # type: (Dict[str, Any]) -> Tuple[text_type, text_type, text_type]
 | 
			
		||||
        author_name, image_url, title = snapshot['author_name'], snapshot['image_url'], snapshot['title']
 | 
			
		||||
        return author_name, image_url, title
 | 
			
		||||
 | 
			
		||||
class LibratoWebhookHandler(LibratoWebhookParser):
 | 
			
		||||
    def __init__(self, payload, attachments):
 | 
			
		||||
        # type: (Dict[str, Any], List[Dict[str, Any]]) -> None
 | 
			
		||||
        super(LibratoWebhookHandler, self).__init__(payload, attachments)
 | 
			
		||||
        self.payload_available_types = {
 | 
			
		||||
            ALERT_CLEAR: self.handle_alert_clear_message,
 | 
			
		||||
            ALERT_VIOLATION: self.handle_alert_violation_message
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        self.attachments_available_types = {
 | 
			
		||||
            SNAPSHOT: self.handle_snapshots
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def find_handle_method(self):
 | 
			
		||||
        # type: () -> Callable
 | 
			
		||||
        for available_type in self.payload_available_types:
 | 
			
		||||
            if self.payload.get(available_type):
 | 
			
		||||
                return self.payload_available_types[available_type]
 | 
			
		||||
        for available_type in self.attachments_available_types:
 | 
			
		||||
            if self.attachments[0].get(available_type):
 | 
			
		||||
                return self.attachments_available_types[available_type]
 | 
			
		||||
        raise Exception("Unexcepted message type")
 | 
			
		||||
 | 
			
		||||
    def handle(self):
 | 
			
		||||
        # type: () -> text_type
 | 
			
		||||
        return self.find_handle_method()()
 | 
			
		||||
 | 
			
		||||
    def generate_topic(self):
 | 
			
		||||
        # type: () -> text_type
 | 
			
		||||
        if self.attachments:
 | 
			
		||||
            return "Snapshots"
 | 
			
		||||
        topic_template = "Alert {alert_name}"
 | 
			
		||||
        alert_id, alert_name, alert_url, alert_runbook_url = self.parse_alert()
 | 
			
		||||
        return topic_template.format(alert_name=alert_name)
 | 
			
		||||
 | 
			
		||||
    def handle_alert_clear_message(self):
 | 
			
		||||
        # type: () -> text_type
 | 
			
		||||
        alert_clear_template = "Alert [alert_name]({alert_url}) has cleared at {trigger_time}!"
 | 
			
		||||
        trigger_time = datetime.fromtimestamp((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)
 | 
			
		||||
        return content
 | 
			
		||||
 | 
			
		||||
    def handle_snapshots(self):
 | 
			
		||||
        # type: () -> text_type
 | 
			
		||||
        content = u''
 | 
			
		||||
        for attachment in self.attachments:
 | 
			
		||||
            content += self.handle_snapshot(attachment)
 | 
			
		||||
        return content
 | 
			
		||||
 | 
			
		||||
    def handle_snapshot(self, snapshot):
 | 
			
		||||
        # type: (Dict[str, Any]) -> text_type
 | 
			
		||||
        snapshot_template = u"**{author_name}** sent a [snapshot]({image_url}) of [metric]({title})"
 | 
			
		||||
        author_name, image_url, title = self.parse_snapshot(snapshot)
 | 
			
		||||
        content = snapshot_template.format(author_name=author_name, image_url=image_url, title=title)
 | 
			
		||||
        return content
 | 
			
		||||
 | 
			
		||||
    def handle_alert_violation_message(self):
 | 
			
		||||
        # type: () -> text_type
 | 
			
		||||
        alert_violation_template = u"Alert [alert_name]({alert_url}) has triggered! "
 | 
			
		||||
        alert_id, alert_name, alert_url, alert_runbook_url = self.parse_alert()
 | 
			
		||||
        content = alert_violation_template.format(alert_name=alert_name, alert_url=alert_url)
 | 
			
		||||
        if alert_runbook_url:
 | 
			
		||||
            alert_runbook_template = u"[Reaction steps]({alert_runbook_url})"
 | 
			
		||||
            content += alert_runbook_template.format(alert_runbook_url=alert_runbook_url)
 | 
			
		||||
        content += self.generate_conditions_and_violations()
 | 
			
		||||
        return content
 | 
			
		||||
 | 
			
		||||
    def generate_conditions_and_violations(self):
 | 
			
		||||
        # type: () -> text_type
 | 
			
		||||
        conditions = self.parse_conditions()
 | 
			
		||||
        violations = self.parse_violations()
 | 
			
		||||
        content = u""
 | 
			
		||||
        for condition, violation in zip(conditions, violations):
 | 
			
		||||
            content += self.generate_violated_metric_condition(violation, condition)
 | 
			
		||||
        return content
 | 
			
		||||
 | 
			
		||||
    def generate_violated_metric_condition(self, violation, condition):
 | 
			
		||||
        # type: (Dict[str, Any], Dict[str, Any]) -> text_type
 | 
			
		||||
        summary_function, threshold, condition_type, duration = self.parse_condition(condition)
 | 
			
		||||
        metric_name, recorded_at = self.parse_violation(violation)
 | 
			
		||||
        metric_condition_template = u"\n>Metric `{metric_name}`, {summary_function} was {condition_type} {threshold}"
 | 
			
		||||
        content = metric_condition_template.format(
 | 
			
		||||
                metric_name=metric_name, summary_function=summary_function, condition_type=condition_type,
 | 
			
		||||
                threshold=threshold)
 | 
			
		||||
        if duration:
 | 
			
		||||
            content += u" by {duration}s".format(duration=duration)
 | 
			
		||||
        content += u", recorded at {recorded_at}".format(recorded_at=recorded_at)
 | 
			
		||||
        return content
 | 
			
		||||
 | 
			
		||||
@api_key_only_webhook_view('Librato')
 | 
			
		||||
@has_request_variables
 | 
			
		||||
def api_librato_webhook(request, user_profile, client, payload=REQ(converter=ujson.loads, default={}),
 | 
			
		||||
                        stream=REQ(default='librato'), topic=REQ(default=None)):
 | 
			
		||||
    # type: (HttpRequest, UserProfile, Client, Dict[str, Any], text_type, text_type) -> HttpResponse
 | 
			
		||||
    try:
 | 
			
		||||
        attachments = ujson.loads(request.body).get('attachments', [])
 | 
			
		||||
    except ValueError:
 | 
			
		||||
        attachments = []
 | 
			
		||||
 | 
			
		||||
    if not attachments and not payload:
 | 
			
		||||
        return json_error(_("Malformed JSON input"))
 | 
			
		||||
 | 
			
		||||
    message_handler = LibratoWebhookHandler(payload, attachments)
 | 
			
		||||
 | 
			
		||||
    if not topic:
 | 
			
		||||
        topic = message_handler.generate_topic()
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        content = message_handler.handle()
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        return json_error(_(str(e)))
 | 
			
		||||
 | 
			
		||||
    check_send_message(user_profile, client, "stream", [stream], topic, content)
 | 
			
		||||
    return json_success()
 | 
			
		||||