add event enum

This commit is contained in:
lucasheld 2022-08-05 15:52:19 +02:00
parent ed4a6130fe
commit 8f108a700b
4 changed files with 79 additions and 60 deletions

View file

@ -5,4 +5,5 @@ from .notification_providers import NotificationType, notification_provider_opti
from .proxy_protocol import ProxyProtocol from .proxy_protocol import ProxyProtocol
from .incident_style import IncidentStyle from .incident_style import IncidentStyle
from .exceptions import UptimeKumaException from .exceptions import UptimeKumaException
from .event import Event
from .api import UptimeKumaApi from .api import UptimeKumaApi

View file

@ -8,6 +8,7 @@ from . import MonitorType
from . import NotificationType, notification_provider_options from . import NotificationType, notification_provider_options
from . import ProxyProtocol from . import ProxyProtocol
from . import IncidentStyle from . import IncidentStyle
from . import Event
from . import UptimeKumaException from . import UptimeKumaException
@ -368,40 +369,40 @@ class UptimeKumaApi(object):
self.sio = socketio.Client() self.sio = socketio.Client()
self._event_data: dict = { self._event_data: dict = {
"monitorList": None, Event.MONITOR_LIST: None,
"notificationList": None, Event.NOTIFICATION_LIST: None,
"proxyList": None, Event.PROXY_LIST: None,
"statusPageList": None, Event.STATUS_PAGE_LIST: None,
"heartbeatList": None, Event.HEARTBEAT_LIST: None,
"importantHeartbeatList": None, Event.IMPORTANT_HEARTBEAT_LIST: None,
"avgPing": None, Event.AVG_PING: None,
"uptime": None, Event.UPTIME: None,
"heartbeat": None, Event.HEARTBEAT: None,
"info": None, Event.INFO: None,
"certInfo": None Event.CERT_INFO: None
} }
self.sio.on("connect", self._event_connect) self.sio.on(Event.CONNECT, self._event_connect)
self.sio.on("disconnect", self._event_disconnect) self.sio.on(Event.DISCONNECT, self._event_disconnect)
self.sio.on("monitorList", self._event_monitor_list) self.sio.on(Event.MONITOR_LIST, self._event_monitor_list)
self.sio.on("notificationList", self._event_notification_list) self.sio.on(Event.NOTIFICATION_LIST, self._event_notification_list)
self.sio.on("proxyList", self._event_proxy_list) self.sio.on(Event.PROXY_LIST, self._event_proxy_list)
self.sio.on("statusPageList", self._event_status_page_list) self.sio.on(Event.STATUS_PAGE_LIST, self._event_status_page_list)
self.sio.on("heartbeatList", self._event_heartbeat_list) self.sio.on(Event.HEARTBEAT_LIST, self._event_heartbeat_list)
self.sio.on("importantHeartbeatList", self._event_important_heartbeat_list) self.sio.on(Event.IMPORTANT_HEARTBEAT_LIST, self._event_important_heartbeat_list)
self.sio.on("avgPing", self._event_avg_ping) self.sio.on(Event.AVG_PING, self._event_avg_ping)
self.sio.on("uptime", self._event_uptime) self.sio.on(Event.UPTIME, self._event_uptime)
self.sio.on("heartbeat", self._event_heartbeat) self.sio.on(Event.HEARTBEAT, self._event_heartbeat)
self.sio.on("info", self._event_info) self.sio.on(Event.INFO, self._event_info)
self.sio.on("certInfo", self._event_cert_info) self.sio.on(Event.CERT_INFO, self._event_cert_info)
self.connect(url) self.connect(url)
def _get_event_data(self, event): def _get_event_data(self, event):
monitor_events = ["avgPing", "uptime", "heartbeatList", "importantHeartbeatList", "certInfo", "heartbeat"] monitor_events = [Event.AVG_PING, Event.UPTIME, Event.HEARTBEAT_LIST, Event.IMPORTANT_HEARTBEAT_LIST, Event.CERT_INFO, Event.HEARTBEAT]
while self._event_data[event] is None: while self._event_data[event] is None:
# do not wait for events that are not sent # do not wait for events that are not sent
if self._event_data["monitorList"] == {} and event in monitor_events: if self._event_data[Event.MONITOR_LIST] == {} and event in monitor_events:
return [] return []
time.sleep(0.01) time.sleep(0.01)
time.sleep(0.01) # wait for multiple messages time.sleep(0.01) # wait for multiple messages
@ -424,64 +425,64 @@ class UptimeKumaApi(object):
pass pass
def _event_monitor_list(self, data): def _event_monitor_list(self, data):
self._event_data["monitorList"] = data self._event_data[Event.MONITOR_LIST] = data
def _event_notification_list(self, data): def _event_notification_list(self, data):
self._event_data["notificationList"] = data self._event_data[Event.NOTIFICATION_LIST] = data
def _event_proxy_list(self, data): def _event_proxy_list(self, data):
self._event_data["proxyList"] = data self._event_data[Event.PROXY_LIST] = data
def _event_status_page_list(self, data): def _event_status_page_list(self, data):
self._event_data["statusPageList"] = data self._event_data[Event.STATUS_PAGE_LIST] = data
def _event_heartbeat_list(self, id_, data, bool_): def _event_heartbeat_list(self, id_, data, bool_):
if self._event_data["heartbeatList"] is None: if self._event_data[Event.HEARTBEAT_LIST] is None:
self._event_data["heartbeatList"] = [] self._event_data[Event.HEARTBEAT_LIST] = []
self._event_data["heartbeatList"].append({ self._event_data[Event.HEARTBEAT_LIST].append({
"id": id_, "id": id_,
"data": data, "data": data,
"bool": bool_, "bool": bool_,
}) })
def _event_important_heartbeat_list(self, id_, data, bool_): def _event_important_heartbeat_list(self, id_, data, bool_):
if self._event_data["importantHeartbeatList"] is None: if self._event_data[Event.IMPORTANT_HEARTBEAT_LIST] is None:
self._event_data["importantHeartbeatList"] = [] self._event_data[Event.IMPORTANT_HEARTBEAT_LIST] = []
self._event_data["importantHeartbeatList"].append({ self._event_data[Event.IMPORTANT_HEARTBEAT_LIST].append({
"id": id_, "id": id_,
"data": data, "data": data,
"bool": bool_, "bool": bool_,
}) })
def _event_avg_ping(self, id_, data): def _event_avg_ping(self, id_, data):
if self._event_data["avgPing"] is None: if self._event_data[Event.AVG_PING] is None:
self._event_data["avgPing"] = [] self._event_data[Event.AVG_PING] = []
self._event_data["avgPing"].append({ self._event_data[Event.AVG_PING].append({
"id": id_, "id": id_,
"data": data, "data": data,
}) })
def _event_uptime(self, id_, hours_24, days_30): def _event_uptime(self, id_, hours_24, days_30):
if self._event_data["uptime"] is None: if self._event_data[Event.UPTIME] is None:
self._event_data["uptime"] = [] self._event_data[Event.UPTIME] = []
self._event_data["uptime"].append({ self._event_data[Event.UPTIME].append({
"id": id_, "id": id_,
"hours_24": hours_24, "hours_24": hours_24,
"days_30": days_30, "days_30": days_30,
}) })
def _event_heartbeat(self, data): def _event_heartbeat(self, data):
if self._event_data["heartbeat"] is None: if self._event_data[Event.HEARTBEAT] is None:
self._event_data["heartbeat"] = [] self._event_data[Event.HEARTBEAT] = []
self._event_data["heartbeat"].append(data) self._event_data[Event.HEARTBEAT].append(data)
def _event_info(self, data): def _event_info(self, data):
self._event_data["info"] = data self._event_data[Event.INFO] = data
def _event_cert_info(self, id_, data): def _event_cert_info(self, id_, data):
if self._event_data["certInfo"] is None: if self._event_data[Event.CERT_INFO] is None:
self._event_data["certInfo"] = [] self._event_data[Event.CERT_INFO] = []
self._event_data["certInfo"].append({ self._event_data[Event.CERT_INFO].append({
"id": id_, "id": id_,
"data": data, "data": data,
}) })
@ -498,7 +499,7 @@ class UptimeKumaApi(object):
# monitors # monitors
def get_monitors(self): def get_monitors(self):
r = list(self._get_event_data("monitorList").values()) r = list(self._get_event_data(Event.MONITOR_LIST).values())
int_to_bool(r, ["active"]) int_to_bool(r, ["active"])
return r return r
@ -549,7 +550,7 @@ class UptimeKumaApi(object):
# notifications # notifications
def get_notifications(self): def get_notifications(self):
notifications = self._get_event_data("notificationList") notifications = self._get_event_data(Event.NOTIFICATION_LIST)
r = [] r = []
for notification_raw in notifications: for notification_raw in notifications:
notification = notification_raw.copy() notification = notification_raw.copy()
@ -603,7 +604,7 @@ class UptimeKumaApi(object):
# proxy # proxy
def get_proxies(self): def get_proxies(self):
r = self._get_event_data("proxyList") r = self._get_event_data(Event.PROXY_LIST)
int_to_bool(r, ["auth", "active", "default", "applyExisting"]) int_to_bool(r, ["auth", "active", "default", "applyExisting"])
return r return r
@ -632,7 +633,7 @@ class UptimeKumaApi(object):
# status page # status page
def get_status_pages(self): def get_status_pages(self):
r = list(self._get_event_data("statusPageList").values()) r = list(self._get_event_data(Event.STATUS_PAGE_LIST).values())
return r return r
def get_status_page(self, slug: str): def get_status_page(self, slug: str):
@ -678,41 +679,41 @@ class UptimeKumaApi(object):
# heartbeat # heartbeat
def get_heartbeats(self): def get_heartbeats(self):
r = self._get_event_data("heartbeatList") r = self._get_event_data(Event.HEARTBEAT_LIST)
for i in r: for i in r:
int_to_bool(i["data"], ["important", "status"]) int_to_bool(i["data"], ["important", "status"])
return r return r
def get_important_heartbeats(self): def get_important_heartbeats(self):
r = self._get_event_data("importantHeartbeatList") r = self._get_event_data(Event.IMPORTANT_HEARTBEAT_LIST)
for i in r: for i in r:
int_to_bool(i["data"], ["important", "status"]) int_to_bool(i["data"], ["important", "status"])
return r return r
def get_heartbeat(self): def get_heartbeat(self):
r = self._get_event_data("heartbeat") r = self._get_event_data(Event.HEARTBEAT)
int_to_bool(r, ["important", "status"]) int_to_bool(r, ["important", "status"])
return r return r
# avg ping # avg ping
def avg_ping(self): def avg_ping(self):
return self._get_event_data("avgPing") return self._get_event_data(Event.AVG_PING)
# cert info # cert info
def cert_info(self): def cert_info(self):
return self._get_event_data("certInfo") return self._get_event_data(Event.CERT_INFO)
# uptime # uptime
def uptime(self): def uptime(self):
return self._get_event_data("uptime") return self._get_event_data(Event.UPTIME)
# info # info
def info(self): def info(self):
r = self._get_event_data("info") r = self._get_event_data(Event.INFO)
return r return r
# clear # clear

17
uptime_kuma_api/event.py Normal file
View file

@ -0,0 +1,17 @@
from enum import Enum
class Event(str, Enum):
CONNECT = "connect"
DISCONNECT = "disconnect"
MONITOR_LIST = "monitorList"
NOTIFICATION_LIST = "notificationList"
PROXY_LIST = "proxyList"
STATUS_PAGE_LIST = "statusPageList"
HEARTBEAT_LIST = "heartbeatList"
IMPORTANT_HEARTBEAT_LIST = "importantHeartbeatList"
AVG_PING = "avgPing"
UPTIME = "uptime"
HEARTBEAT = "heartbeat"
INFO = "info"
CERT_INFO = "certInfo"