2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2018-01-29 11:33:21 +01:00
|
|
|
from zerver.decorator import REQ, has_request_variables, \
|
|
|
|
api_key_only_webhook_view
|
|
|
|
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2018-01-29 11:33:21 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2019-02-02 23:53:55 +01:00
|
|
|
from typing import Dict, Any
|
2018-01-29 11:33:21 +01:00
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Insping')
|
|
|
|
@has_request_variables
|
|
|
|
def api_insping_webhook(
|
|
|
|
request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[str, Dict[str, Any]]=REQ(argument_type='body')
|
2018-01-29 11:33:21 +01:00
|
|
|
) -> HttpResponse:
|
|
|
|
|
|
|
|
data = payload['webhook_event_data']
|
|
|
|
|
|
|
|
state_name = data['check_state_name']
|
|
|
|
url_tested = data['request_url']
|
|
|
|
response_time = data['response_time']
|
|
|
|
timestamp = data['request_start_time']
|
|
|
|
|
|
|
|
time_formatted = time.strftime("%c", time.strptime(timestamp,
|
|
|
|
"%Y-%m-%dT%H:%M:%S.%f+00:00"))
|
|
|
|
|
|
|
|
body = """State changed: {}
|
|
|
|
URL: {}
|
|
|
|
Response time: {} ms
|
|
|
|
Timestamp: {}
|
|
|
|
""".format(state_name, url_tested, response_time, time_formatted)
|
2018-03-16 22:53:50 +01:00
|
|
|
topic = 'insping'
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2018-01-29 11:33:21 +01:00
|
|
|
|
|
|
|
return json_success()
|