Store log timestamps in ISO format

This commit is contained in:
Jordan Wages 2025-07-18 00:47:45 -05:00
commit 1140a02377

View file

@ -3,7 +3,7 @@
import os import os
import re import re
import sqlite3 import sqlite3
from datetime import datetime from datetime import datetime, timezone
LOG_DIR = "/var/log/nginx" LOG_DIR = "/var/log/nginx"
DB_FILE = "database/ngxstat.db" DB_FILE = "database/ngxstat.db"
@ -42,10 +42,16 @@ cursor.execute("SELECT time FROM logs ORDER BY id DESC LIMIT 1")
row = cursor.fetchone() row = cursor.fetchone()
last_dt = None last_dt = None
if row and row[0]: if row and row[0]:
try: # Support both legacy log date format and ISO timestamps
last_dt = datetime.strptime(row[0], DATE_FMT) for fmt in ("%Y-%m-%d %H:%M:%S", DATE_FMT):
except ValueError: try:
last_dt = None parsed = datetime.strptime(row[0], fmt)
if fmt == DATE_FMT:
parsed = parsed.astimezone(timezone.utc).replace(tzinfo=None)
last_dt = parsed
break
except ValueError:
continue
try: try:
log_files = [] log_files = []
@ -74,6 +80,7 @@ for log_file in log_files:
entry_dt = datetime.strptime(data["time"], DATE_FMT) entry_dt = datetime.strptime(data["time"], DATE_FMT)
except ValueError: except ValueError:
continue continue
entry_dt = entry_dt.astimezone(timezone.utc).replace(tzinfo=None)
if last_dt and entry_dt <= last_dt: if last_dt and entry_dt <= last_dt:
continue continue
cursor.execute( cursor.execute(
@ -86,7 +93,7 @@ for log_file in log_files:
( (
data["ip"], data["ip"],
data["host"], data["host"],
data["time"], entry_dt.strftime("%Y-%m-%d %H:%M:%S"),
data["request"], data["request"],
int(data["status"]), int(data["status"]),
int(data["bytes_sent"]), int(data["bytes_sent"]),