diff --git a/app.py b/app.py index 57e5d50..8d18b42 100644 --- a/app.py +++ b/app.py @@ -8,7 +8,7 @@ from pathlib import Path from models import service, log from typing import Any, Optional, cast import json -from datetime import timedelta +from datetime import datetime, timezone, timedelta from config import timeout @@ -19,7 +19,7 @@ def prepare_chart_data( if len(logs) <= 0: # Return empty if there are no logs return ([], []) - x = [logs[0].dateCreated.isoformat()] + x = [logs[0].dateCreatedUTC().isoformat()] y = [logs[0].ping] for i in range(1, len(logs)): @@ -27,13 +27,13 @@ def prepare_chart_data( log2 = logs[i - 1] # Check if the gap in points exceeds a threshold - if (abs(log1.dateCreated - log2.dateCreated)) > timedelta( + if (abs(log1.dateCreatedUTC() - log2.dateCreatedUTC())) > timedelta( milliseconds=1.5 * (timeout + 1000) ): - x.append(log2.dateCreated.isoformat()) + x.append(log2.dateCreatedUTC().isoformat()) y.append(None) - x.append(log1.dateCreated.isoformat()) + x.append(log1.dateCreatedUTC().isoformat()) y.append(log1.ping) return (x, y) @@ -83,10 +83,15 @@ def chart(id: int): return abort(code=403) x, y = prepare_chart_data(logs=logs) + now = datetime.now(timezone.utc) + max_ = now + min_ = now - timedelta(hours=1) return render_template( "chart.html", dates=x, values=json.dumps(y), + min=min_.isoformat(), + max=max_.isoformat(), ) diff --git a/mem/templates/chart.html b/mem/templates/chart.html index 146c368..4690859 100644 --- a/mem/templates/chart.html +++ b/mem/templates/chart.html @@ -22,12 +22,19 @@ const data = { labels: chartDates, datasets: [{ - label: 'Example Data', + label: 'Ping', data: {{ values }}, }] }; const ctx = document.getElementById('myChart').getContext('2d'); + // Current time in UTC + const nowUTC = new Date(); + + // One hour ago in UTC + const oneDayAgoUTC = new Date(nowUTC.getTime() - 24 * 60 * 60 * 1000); + const min = "{{ min }}" + const max = "{{ max }}" new Chart(ctx, { type: 'line', data: data, @@ -36,8 +43,14 @@ x: { type: 'time', // Important for datetime axis time: { - unit: 'day' - } + unit: 'hour', + tooltipFormat: 'HH:mm:ss', + displayFormats: { + hour: 'HH:mm' + } + }, + min: min, + max: max } } } diff --git a/models.py b/models.py index c641e23..3818ebd 100644 --- a/models.py +++ b/models.py @@ -30,10 +30,13 @@ class log(db.Model): "log_id": self.id, "service_id": self.service_id, "ping": self.ping, - "dateCreated": self.dateCreated, + "dateCreated": self.dateCreatedUTC(), "timeout": self.timeout, } + def dateCreatedUTC(self) -> datetime: + return self.dateCreated.replace(tzinfo=timezone.utc) + class service(db.Model): id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID diff --git a/poll_services.py b/poll_services.py index 525a320..505ceed 100644 --- a/poll_services.py +++ b/poll_services.py @@ -39,11 +39,7 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log: try: ctx = SimpleNamespace() status = await ping(client=client, s=s, ctx=ctx) - print(status) - print(vars(ctx)) if status == 200: - print("test") - print(ctx.duration_ms) return log(service_id=s.id, ping=int(ctx.duration_ms)) else: return log(service_id=s.id, ping=None)