2020-01-14 22:06:24 +01:00
|
|
|
import time
|
2018-01-29 11:33:21 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-01-14 22:06:24 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_int, check_string
|
2020-01-14 22:06:24 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2019-04-17 21:32:23 +02:00
|
|
|
MESSAGE_TEMPLATE = """
|
|
|
|
State changed to **{state}**:
|
|
|
|
* **URL**: {url}
|
|
|
|
* **Response time**: {response_time} ms
|
|
|
|
* **Timestamp**: {timestamp}
|
|
|
|
""".strip()
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Insping")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2018-01-29 11:33:21 +01:00
|
|
|
def api_insping_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2018-01-29 11:33:21 +01:00
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = payload["webhook_event_data"]
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2022-10-08 19:23:35 +02:00
|
|
|
state_name = data["check_state_name"].tame(check_string)
|
|
|
|
url_tested = data["request_url"].tame(check_string)
|
|
|
|
response_time = data["response_time"].tame(check_int)
|
|
|
|
timestamp = data["request_start_time"].tame(check_string)
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
time_formatted = time.strftime("%c", time.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f+00:00"))
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2019-04-17 21:32:23 +02:00
|
|
|
body = MESSAGE_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
state=state_name,
|
|
|
|
url=url_tested,
|
|
|
|
response_time=response_time,
|
|
|
|
timestamp=time_formatted,
|
2019-04-17 21:32:23 +02:00
|
|
|
)
|
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = "insping"
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body)
|
2018-01-29 11:33:21 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|