From 010bbc950730d55cd1298d1c9e1623ea726339f0 Mon Sep 17 00:00:00 2001 From: James Kirsop Date: Fri, 27 Mar 2020 09:30:26 +1100 Subject: [PATCH 1/2] Sample work for review --- hc/api/models.py | 26 +++++++++++++++++++++++++- hc/api/views.py | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/hc/api/models.py b/hc/api/models.py index 38fc2afd..16dba4a2 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -16,6 +16,7 @@ from hc.api import transports from hc.lib import emails from hc.lib.date import month_boundaries import pytz +import re STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New"), ("paused", "Paused")) DEFAULT_TIMEOUT = td(days=1) @@ -205,7 +206,7 @@ class Check(models.Model): code_half = self.code.hex[:16] return hashlib.sha1(code_half.encode()).hexdigest() - def to_dict(self, readonly=False): + def to_dict(self, readonly=False, history=None): result = { "name": self.name, @@ -221,6 +222,29 @@ class Check(models.Model): if self.last_duration: result["last_duration"] = int(self.last_duration.total_seconds()) + if history: + split = re.split(r'(h|d|w)$',history,maxsplit=0) + if len(split) == 3: # re.split should return a list of 3 items if the parameter is set correctly + zone = pytz.timezone(self.tz) + current_now = datetime.now(tz=zone) + + if split[1] == 'd': + cutoff = current_now - td(days=int(split[0])) + elif split[1] == 'h': + cutoff = current_now - td(hours=int(split[0])) + elif split[1] == 'w': + cutoff = current_now - td(weeks=int(split[0])) + + pings = Ping.objects.filter(owner=self, created__gte=cutoff).order_by("-id")#[:limit] + pings = list(pings) + + alerts = Notification.objects.select_related("channel").filter( + owner=self, check_status="down", created__gt=cutoff + ) + + events = pings + list(alerts) + events.sort(key=lambda el: el.created, reverse=True) + result['history'] = list(map(lambda x: {'timestamp':x.created,'status':x.kind}, events)) if readonly: result["unique_key"] = self.unique_key else: diff --git a/hc/api/views.py b/hc/api/views.py index fed66d6c..9e2ecb6a 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -185,6 +185,8 @@ def get_check(request, code): if check.project != request.project: return HttpResponseForbidden() + if 'history' in request.GET: + return JsonResponse(check.to_dict(readonly=request.readonly, history=request.GET['history'])) return JsonResponse(check.to_dict(readonly=request.readonly)) From 74f4744c62ced0c8649613b3932da07084b6abbe Mon Sep 17 00:00:00 2001 From: James Kirsop Date: Fri, 27 Mar 2020 14:19:57 +1100 Subject: [PATCH 2/2] Implementation of history using Flips model statuses for a check --- hc/api/models.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 16dba4a2..1efe7487 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -235,16 +235,11 @@ class Check(models.Model): elif split[1] == 'w': cutoff = current_now - td(weeks=int(split[0])) - pings = Ping.objects.filter(owner=self, created__gte=cutoff).order_by("-id")#[:limit] - pings = list(pings) - - alerts = Notification.objects.select_related("channel").filter( - owner=self, check_status="down", created__gt=cutoff - ) - - events = pings + list(alerts) - events.sort(key=lambda el: el.created, reverse=True) - result['history'] = list(map(lambda x: {'timestamp':x.created,'status':x.kind}, events)) + flips = Flip.objects.select_related("owner").filter( + owner=self, new_status__in=("down","up"), created__gt=cutoff + ).order_by("created") + dictStatus = {"up":1,"down":0} + result['history'] = list(map(lambda x: {'timestamp':x.created,'status':dictStatus[x.new_status]}, flips)) if readonly: result["unique_key"] = self.unique_key else: