Log slack door open time
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2024-10-31 21:20:13 +02:00
parent 7b6921e44d
commit 4fa4edb637

View File

@ -1,6 +1,7 @@
import os import os
import re import re
import const import const
import time
import threading import threading
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import wraps from functools import wraps
@ -62,6 +63,7 @@ def get_group_cards():
@page_api.route("/api/slack/doorboy", methods=['POST']) @page_api.route("/api/slack/doorboy", methods=['POST'])
def view_slack_doorboy(): def view_slack_doorboy():
begin_time = time.perf_counter()
if request.form.get("token") != const.SLACK_VERIFICATION_TOKEN: if request.form.get("token") != const.SLACK_VERIFICATION_TOKEN:
return "Invalid token was supplied" return "Invalid token was supplied"
if request.form.get("channel_id") not in ("C01CWPF5H8W", "CDL9H8Q9W"): if request.form.get("channel_id") not in ("C01CWPF5H8W", "CDL9H8Q9W"):
@ -98,12 +100,13 @@ def view_slack_doorboy():
threading.Thread(target=handle_slack_door_event, args=(doors, approved, member, door, status, subject)).start() threading.Thread(target=handle_slack_door_event, args=(doors, approved, member, door, status, subject)).start()
if approved: return_message = "Opening %s for %s" % (door, subject) if approved else "Permission denied"
return "Opening %s for %s" % (door, subject) end_time = time.perf_counter()
else: print(f"view_slack_doorboy done in {end_time - begin_time:.4f} seconds")
return "Permission denied" return return_message
def handle_slack_door_event(doors, approved, member, door, status, subject): def handle_slack_door_event(doors, approved, member, door, status, subject):
begin_time = time.perf_counter()
for d in doors: for d in doors:
db.eventlog.insert_one({ db.eventlog.insert_one({
"method": "slack", "method": "slack",
@ -119,3 +122,5 @@ def handle_slack_door_event(doors, approved, member, door, status, subject):
msg = "%s %s door access for %s via Slack bot" % (status, door, subject) msg = "%s %s door access for %s via Slack bot" % (status, door, subject)
slack_post(msg, "doorboy") slack_post(msg, "doorboy")
end_time = time.perf_counter()
print(f"handle_slack_door_event done in {end_time - begin_time:.4f} seconds")