log.dateCreated didn't contain any UTC metadata so now log.dateCreatedUTC() return the datetime object with correct metadata

This commit is contained in:
2025-09-05 18:57:21 +02:00
parent 2ab46f1994
commit 3b87da9292
4 changed files with 30 additions and 13 deletions

15
app.py
View File

@@ -8,7 +8,7 @@ from pathlib import Path
from models import service, log from models import service, log
from typing import Any, Optional, cast from typing import Any, Optional, cast
import json import json
from datetime import timedelta from datetime import datetime, timezone, timedelta
from config import timeout from config import timeout
@@ -19,7 +19,7 @@ def prepare_chart_data(
if len(logs) <= 0: # Return empty if there are no logs if len(logs) <= 0: # Return empty if there are no logs
return ([], []) return ([], [])
x = [logs[0].dateCreated.isoformat()] x = [logs[0].dateCreatedUTC().isoformat()]
y = [logs[0].ping] y = [logs[0].ping]
for i in range(1, len(logs)): for i in range(1, len(logs)):
@@ -27,13 +27,13 @@ def prepare_chart_data(
log2 = logs[i - 1] log2 = logs[i - 1]
# Check if the gap in points exceeds a threshold # 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) milliseconds=1.5 * (timeout + 1000)
): ):
x.append(log2.dateCreated.isoformat()) x.append(log2.dateCreatedUTC().isoformat())
y.append(None) y.append(None)
x.append(log1.dateCreated.isoformat()) x.append(log1.dateCreatedUTC().isoformat())
y.append(log1.ping) y.append(log1.ping)
return (x, y) return (x, y)
@@ -83,10 +83,15 @@ def chart(id: int):
return abort(code=403) return abort(code=403)
x, y = prepare_chart_data(logs=logs) x, y = prepare_chart_data(logs=logs)
now = datetime.now(timezone.utc)
max_ = now
min_ = now - timedelta(hours=1)
return render_template( return render_template(
"chart.html", "chart.html",
dates=x, dates=x,
values=json.dumps(y), values=json.dumps(y),
min=min_.isoformat(),
max=max_.isoformat(),
) )

View File

@@ -22,12 +22,19 @@
const data = { const data = {
labels: chartDates, labels: chartDates,
datasets: [{ datasets: [{
label: 'Example Data', label: 'Ping',
data: {{ values }}, data: {{ values }},
}] }]
}; };
const ctx = document.getElementById('myChart').getContext('2d'); 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, { new Chart(ctx, {
type: 'line', type: 'line',
data: data, data: data,
@@ -36,8 +43,14 @@
x: { x: {
type: 'time', // Important for datetime axis type: 'time', // Important for datetime axis
time: { time: {
unit: 'day' unit: 'hour',
tooltipFormat: 'HH:mm:ss',
displayFormats: {
hour: 'HH:mm'
} }
},
min: min,
max: max
} }
} }
} }

View File

@@ -30,10 +30,13 @@ class log(db.Model):
"log_id": self.id, "log_id": self.id,
"service_id": self.service_id, "service_id": self.service_id,
"ping": self.ping, "ping": self.ping,
"dateCreated": self.dateCreated, "dateCreated": self.dateCreatedUTC(),
"timeout": self.timeout, "timeout": self.timeout,
} }
def dateCreatedUTC(self) -> datetime:
return self.dateCreated.replace(tzinfo=timezone.utc)
class service(db.Model): class service(db.Model):
id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID

View File

@@ -39,11 +39,7 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log:
try: try:
ctx = SimpleNamespace() ctx = SimpleNamespace()
status = await ping(client=client, s=s, ctx=ctx) status = await ping(client=client, s=s, ctx=ctx)
print(status)
print(vars(ctx))
if status == 200: if status == 200:
print("test")
print(ctx.duration_ms)
return log(service_id=s.id, ping=int(ctx.duration_ms)) return log(service_id=s.id, ping=int(ctx.duration_ms))
else: else:
return log(service_id=s.id, ping=None) return log(service_id=s.id, ping=None)