Compare commits

..

3 commits

Author SHA1 Message Date
caf3169829
update api 2024-12-08 00:13:56 +01:00
ebe17beb8b
test stuff 2024-12-07 22:57:27 +01:00
8ba2f944dd
feat: delete unused monitors 2024-12-07 20:02:06 +01:00
4 changed files with 65 additions and 11 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use nix

View file

@ -5,6 +5,8 @@ Classes to make the needed operations to reach the specified state.
import logging
import sys
from requests.api import delete
logger = logging.getLogger(__name__)
@ -13,6 +15,7 @@ class Manager:
self.api = api
self.prune_unused = prune_unused
self.target_tree = target_tree
self.to_prune = []
def process(self):
self.sync_tags()
@ -20,6 +23,7 @@ class Manager:
self.sync_monitors()
self.sync_status_pages()
self.sync_settings()
self.prune()
self.save()
def save(self):
@ -27,25 +31,34 @@ class Manager:
for i in v:
i.save() # this method should be safe to be called in whatever order
def prune(self):
for e in self.to_prune:
e.prune()
def sync_monitors(self):
logger.debug(f"Syncing monitors...")
old = self.api.get_monitors()
new = self.target_tree.get("monitors", [])
self.sync(new, old, "name")
self.sync(new, old, "name", Monitor)
def sync_notifications(self):
logger.debug(f"Syncing notifications...")
old = self.api.get_notifications()
new = self.target_tree.get("notifications", [])
self.sync(new, old, "name")
self.sync(new, old, "name", Notification)
def sync_tags(self):
logger.debug(f"Syncing tags...")
old = self.api.get_tags()
new = self.target_tree.get("tags", [])
self.sync(new, old, "name")
self.sync(new, old, "name", Tag)
def sync_status_pages(self):
logger.debug(f"Syncing status pages...")
old = self.api.get_status_pages()
new = self.target_tree.get("status_pages", [])
self.sync(new, old, "slug")
self.sync(new, old, "slug", StatusPage)
def sync_settings(self):
if "settings" not in self.target_tree:
@ -58,8 +71,9 @@ class Manager:
if k not in new.kwargs:
new.kwargs[k] = v
def sync(self, new, old, pk):
def sync(self, new, old, pk, item_t):
indexed_old = {elem[pk]: elem for elem in old}
indexed_new = {elem.name: elem for elem in new}
for k in new:
if k.name in indexed_old:
k.id = indexed_old[k.name]["id"]
@ -72,6 +86,14 @@ class Manager:
else:
k.id = None # Useless
logger.debug(f"Creating key {k}")
for k in indexed_old:
if k not in indexed_new:
if item_t == StatusPage:
self.to_prune.append(item_t(api=None, name=indexed_old[k]["slug"], id=indexed_old[k]["id"]))
else:
self.to_prune.append(item_t(api=self.api, name=indexed_old[k]["name"], id=indexed_old[k]["id"]))
class Item:
@ -85,6 +107,10 @@ class Item:
def save(self):
raise NotImplementedError()
def prune(self):
print(f"Would prune: {self}")
def __setattr__(self, name, value):
if name != "saved":
self.saved = False
@ -157,6 +183,12 @@ class Monitor(Item):
self.api.add_monitor_tag(tag_id=t.id, monitor_id=self.id, value=v)
self.saved = True
def prune(self):
logger.debug(f"Deleting monitor {self.name}")
self.api.delete_monitor(self.id)
def __repr__(self):
return f"Monitor({str(self)})"
@ -185,6 +217,9 @@ class Tag(Item):
)
self.saved = True
def prune(self):
pass
def __repr__(self):
return f"Tag({str(self)})"

17
test.json Normal file
View file

@ -0,0 +1,17 @@
{
"monitors": {
"mdr": {
"accepted_statuscodes": [
"200-299"
],
"headers": "{\n \"Host\": \"julienmalka.me\"\n}\n",
"keyword": "Ulm",
"type": "keyword",
"url": "https://82.67.34.230"
}
},
"notifications": {},
"settings": {},
"status_pages": {},
"tags": {}
}

View file

@ -1,7 +1,7 @@
{
lib,
buildPythonPackage,
fetchFromGitHub,
fetchFromGitea,
setuptools,
wheel,
packaging,
@ -11,14 +11,15 @@
buildPythonPackage rec {
pname = "uptime-kuma-api";
version = "1.2.1";
version = "2.0.0";
pyproject = true;
src = fetchFromGitHub {
owner = "lucasheld";
src = fetchFromGitea {
domain = "git.dgnum.eu";
owner = "Luj";
repo = "uptime-kuma-api";
rev = version;
hash = "sha256-Mgp4bSQPiEeulK9dAl+Di4Nj1HG3oVFGKr1bIdRZI44=";
rev = "782037348df18dd32c3904c812c289f3d2d79170";
hash = "sha256-YK4KMtKPlxqWwzB6lLyFqLvq1RCiUWrIILcVmuiirFw=";
};
nativeBuildInputs = [