forked from DGNum/uptime-kuma-api
fix monitors in status pages
This commit is contained in:
parent
464d0b9e71
commit
c6b3d59221
3 changed files with 51 additions and 17 deletions
|
@ -6,12 +6,25 @@ from uptime_kuma_test_case import UptimeKumaTestCase
|
|||
|
||||
class TestStatusPage(UptimeKumaTestCase):
|
||||
def test_status_page(self):
|
||||
monitor_id = self.add_monitor()
|
||||
|
||||
slug = "slug1"
|
||||
expected_status_page = {
|
||||
"slug": slug,
|
||||
"title": "status page 1",
|
||||
"description": "description 1",
|
||||
"showPoweredBy": False
|
||||
"showPoweredBy": False,
|
||||
"publicGroupList": [
|
||||
{
|
||||
'name': 'Services',
|
||||
'weight': 1,
|
||||
'monitorList': [
|
||||
{
|
||||
"id": monitor_id
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# slug must be unique
|
||||
|
@ -35,7 +48,9 @@ class TestStatusPage(UptimeKumaTestCase):
|
|||
status_pages = self.api.get_status_pages()
|
||||
status_page = self.find_by_id(status_pages, slug, "slug")
|
||||
self.assertIsNotNone(status_page)
|
||||
self.compare(status_page, expected_status_page)
|
||||
# publicGroupList and incident is not available in status pages
|
||||
expected_status_page_config = {i: expected_status_page[i] for i in expected_status_page if i != "publicGroupList"}
|
||||
self.compare(status_page, expected_status_page_config)
|
||||
|
||||
# edit status page
|
||||
expected_status_page["title"] = "status page 1 new"
|
||||
|
|
|
@ -6,6 +6,22 @@ from uptime_kuma_api import UptimeKumaApi, Event, MonitorType
|
|||
token = None
|
||||
|
||||
|
||||
def compare(subset, superset):
|
||||
for key, value in subset.items():
|
||||
value2 = superset.get(key)
|
||||
if type(value) == list:
|
||||
for i in range(len(value)):
|
||||
if not value2 or not compare(value[i], value2[i]):
|
||||
return False
|
||||
elif type(value) == dict:
|
||||
if not compare(value, value2):
|
||||
return False
|
||||
else:
|
||||
if value != value2:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class UptimeKumaTestCase(unittest.TestCase):
|
||||
api = None
|
||||
url = "http://127.0.0.1:3001"
|
||||
|
@ -40,7 +56,7 @@ class UptimeKumaTestCase(unittest.TestCase):
|
|||
self.api.disconnect()
|
||||
|
||||
def compare(self, superset, subset):
|
||||
self.assertTrue(subset.items() <= superset.items())
|
||||
self.assertTrue(compare(subset, superset))
|
||||
|
||||
def find_by_id(self, objects, value, key="id"):
|
||||
for obj in objects:
|
||||
|
@ -48,7 +64,7 @@ class UptimeKumaTestCase(unittest.TestCase):
|
|||
return obj
|
||||
|
||||
def add_monitor(self):
|
||||
r = self.api.add_monitor(type="http", name="monitor 1", url="http://127.0.0.1")
|
||||
r = self.api.add_monitor(type=MonitorType.HTTP, name="monitor 1", url="http://127.0.0.1")
|
||||
monitor_id = r["monitorID"]
|
||||
return monitor_id
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import json
|
||||
import time
|
||||
|
||||
import requests
|
||||
import socketio
|
||||
|
||||
from . import AuthMethod
|
||||
|
@ -226,18 +227,14 @@ def _build_status_page_data(
|
|||
showPoweredBy: bool = True,
|
||||
|
||||
icon: str = "/icon.svg",
|
||||
monitors: list = None
|
||||
publicGroupList: list = None
|
||||
):
|
||||
if theme not in ["light", "dark"]:
|
||||
raise ValueError
|
||||
if not domainNameList:
|
||||
domainNameList = []
|
||||
public_group_list = []
|
||||
if monitors:
|
||||
public_group_list.append({
|
||||
"name": "Services",
|
||||
"monitorList": monitors
|
||||
})
|
||||
if not publicGroupList:
|
||||
publicGroupList = []
|
||||
config = {
|
||||
"id": id,
|
||||
"slug": slug,
|
||||
|
@ -252,7 +249,7 @@ def _build_status_page_data(
|
|||
"footerText": footerText,
|
||||
"showPoweredBy": showPoweredBy
|
||||
}
|
||||
return slug, config, icon, public_group_list
|
||||
return slug, config, icon, publicGroupList
|
||||
|
||||
|
||||
def _check_missing_arguments(required_params, kwargs):
|
||||
|
@ -641,11 +638,16 @@ class UptimeKumaApi(object):
|
|||
return r
|
||||
|
||||
def get_status_page(self, slug: str):
|
||||
r = self._call('getStatusPage', slug)
|
||||
config = r["config"]
|
||||
del r["config"]
|
||||
r.update(config)
|
||||
return r
|
||||
r1 = self._call('getStatusPage', slug)
|
||||
r2 = requests.get(f"{self.url}/api/status-page/{slug}").json()
|
||||
|
||||
config = r1["config"]
|
||||
config.update(r2["config"])
|
||||
return {
|
||||
**config,
|
||||
"incident": r2["incident"],
|
||||
"publicGroupList": r2["publicGroupList"]
|
||||
}
|
||||
|
||||
def add_status_page(self, slug: str, title: str):
|
||||
return self._call('addStatusPage', (title, slug))
|
||||
|
@ -655,6 +657,7 @@ class UptimeKumaApi(object):
|
|||
|
||||
def save_status_page(self, slug: str, **kwargs):
|
||||
status_page = self.get_status_page(slug)
|
||||
status_page.pop("incident")
|
||||
status_page.update(kwargs)
|
||||
data = _build_status_page_data(**status_page)
|
||||
return self._call('saveStatusPage', data)
|
||||
|
|
Loading…
Reference in a new issue