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 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(),
)

View File

@@ -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
}
}
}

View File

@@ -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

View File

@@ -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)