fix: adjust monitor status type to allow all used values

BREAKING CHANGE: monitor `status` type changed from `bool` to `MonitorStatus`
This commit is contained in:
lucasheld 2023-05-02 20:34:26 +02:00
parent 7ef61f8ce1
commit b87eed2597
3 changed files with 49 additions and 11 deletions

View file

@ -1,5 +1,6 @@
from .__version__ import __title__, __version__, __author__, __copyright__ from .__version__ import __title__, __version__, __author__, __copyright__
from .auth_method import AuthMethod from .auth_method import AuthMethod
from .monitor_status import MonitorStatus
from .monitor_type import MonitorType from .monitor_type import MonitorType
from .notification_providers import NotificationType, notification_provider_options, notification_provider_conditions from .notification_providers import NotificationType, notification_provider_options, notification_provider_conditions
from .proxy_protocol import ProxyProtocol from .proxy_protocol import ProxyProtocol

View file

@ -13,11 +13,12 @@ import requests
import socketio import socketio
from packaging.version import parse as parse_version from packaging.version import parse as parse_version
from . import (AuthMethod, from . import (AuthMethod,
DockerType, DockerType,
Event, Event,
IncidentStyle, IncidentStyle,
MaintenanceStrategy, MaintenanceStrategy,
MonitorStatus,
MonitorType, MonitorType,
NotificationType, NotificationType,
ProxyProtocol, ProxyProtocol,
@ -33,6 +34,7 @@ from .docstrings import (append_docstring,
proxy_docstring, proxy_docstring,
tag_docstring) tag_docstring)
def int_to_bool(data, keys) -> None: def int_to_bool(data, keys) -> None:
if isinstance(data, list): if isinstance(data, list):
for d in data: for d in data:
@ -43,6 +45,20 @@ def int_to_bool(data, keys) -> None:
data[key] = True if data[key] == 1 else False data[key] = True if data[key] == 1 else False
def parse_value(data, func) -> None:
if isinstance(data, list):
for d in data:
parse_value(d, func)
else:
func(data)
def parse_monitor_status(data) -> None:
def parse(x):
x["status"] = MonitorStatus(x["status"])
parse_value(data, parse)
def gen_secret(length: int) -> str: def gen_secret(length: int) -> str:
chars = string.ascii_uppercase + string.ascii_lowercase + string.digits chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
return ''.join(random.choice(chars) for _ in range(length)) return ''.join(random.choice(chars) for _ in range(length))
@ -1123,7 +1139,7 @@ class UptimeKumaApi(object):
'monitor_id': 1, 'monitor_id': 1,
'msg': '200 - OK', 'msg': '200 - OK',
'ping': 201, 'ping': 201,
'status': True, 'status': <MonitorStatus.UP: 1>,
'time': '2022-12-15 12:38:42.661' 'time': '2022-12-15 12:38:42.661'
}, },
{ {
@ -1134,14 +1150,15 @@ class UptimeKumaApi(object):
'monitor_id': 1, 'monitor_id': 1,
'msg': '200 - OK', 'msg': '200 - OK',
'ping': 193, 'ping': 193,
'status': True, 'status': <MonitorStatus.UP: 1>,
'time': '2022-12-15 12:39:42.878' 'time': '2022-12-15 12:39:42.878'
}, },
... ...
] ]
""" """
r = self._call('getMonitorBeats', (id_, hours))["data"] r = self._call('getMonitorBeats', (id_, hours))["data"]
int_to_bool(r, ["important", "status"]) int_to_bool(r, ["important"])
parse_monitor_status(r)
return r return r
def get_game_list(self) -> list[dict]: def get_game_list(self) -> list[dict]:
@ -1945,7 +1962,7 @@ class UptimeKumaApi(object):
'monitor_id': 1, 'monitor_id': 1,
'msg': 'connect ECONNREFUSED 127.0.0.1:80', 'msg': 'connect ECONNREFUSED 127.0.0.1:80',
'ping': None, 'ping': None,
'status': False, 'status': <MonitorStatus.DOWN: 0>,
'time': '2022-12-15 16:51:41.782' 'time': '2022-12-15 16:51:41.782'
}, },
{ {
@ -1956,7 +1973,7 @@ class UptimeKumaApi(object):
'monitor_id': 1, 'monitor_id': 1,
'msg': 'connect ECONNREFUSED 127.0.0.1:80', 'msg': 'connect ECONNREFUSED 127.0.0.1:80',
'ping': None, 'ping': None,
'status': False, 'status': <MonitorStatus.DOWN: 0>,
'time': '2022-12-15 16:52:41.799' 'time': '2022-12-15 16:52:41.799'
}, },
... ...
@ -1967,7 +1984,8 @@ class UptimeKumaApi(object):
""" """
r = self._get_event_data(Event.HEARTBEAT_LIST) 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"])
parse_monitor_status(i["data"])
return r return r
def get_important_heartbeats(self) -> list[dict]: def get_important_heartbeats(self) -> list[dict]:
@ -1990,7 +2008,7 @@ class UptimeKumaApi(object):
'monitorID': 1, 'monitorID': 1,
'msg': 'connect ECONNREFUSED 127.0.0.1:80', 'msg': 'connect ECONNREFUSED 127.0.0.1:80',
'ping': None, 'ping': None,
'status': False, 'status': <MonitorStatus.DOWN: 0>,
'time': '2022-12-15 16:51:41.782' 'time': '2022-12-15 16:51:41.782'
} }
], ],
@ -2000,7 +2018,8 @@ class UptimeKumaApi(object):
""" """
r = self._get_event_data(Event.IMPORTANT_HEARTBEAT_LIST) 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"])
parse_monitor_status(i["data"])
return r return r
def get_heartbeat(self) -> list[dict]: def get_heartbeat(self) -> list[dict]:
@ -2019,13 +2038,14 @@ class UptimeKumaApi(object):
'important': False, 'important': False,
'monitorID': 1, 'monitorID': 1,
'msg': 'connect ECONNREFUSED 127.0.0.1:80', 'msg': 'connect ECONNREFUSED 127.0.0.1:80',
'status': False, 'status': <MonitorStatus.DOWN: 0>,
'time': '2022-12-15 17:17:42.099' 'time': '2022-12-15 17:17:42.099'
} }
] ]
""" """
r = self._get_event_data(Event.HEARTBEAT) r = self._get_event_data(Event.HEARTBEAT)
int_to_bool(r, ["important", "status"]) int_to_bool(r, ["important"])
parse_monitor_status(r)
return r return r
# avg ping # avg ping

View file

@ -0,0 +1,17 @@
from enum import Enum
class MonitorStatus(int, Enum):
"""Enumerate monitor statuses."""
DOWN = 0
"""DOWN"""
UP = 1
"""UP"""
PENDING = 2
"""PENDING"""
MAINTENANCE = 3
"""MAINTENANCE"""