From 9304407348f61b1f38cc88a7a29d3f7620f89f8f Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Mon, 12 May 2025 23:03:35 +0200 Subject: [PATCH] scan log files for timestamp as creating timestamp --- app/src/web/log_files.py | 41 +++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/app/src/web/log_files.py b/app/src/web/log_files.py index 772e292..d736209 100644 --- a/app/src/web/log_files.py +++ b/app/src/web/log_files.py @@ -1,26 +1,57 @@ from quart import render_template -from quart_babel import format_datetime, format_decimal +from quart_babel import format_datetime, format_decimal, _ from quart.helpers import send_from_directory from werkzeug.utils import secure_filename from cnf.config import Config +from datetime import datetime +from os import DirEntry import os from . import web -def _get_file(file): +def _get_birth_from_log(path: str) -> None | datetime: + '''read timestamp from the first line of a log file''' + dt = None + try: + with open(path) as f: + first_line = f.readline() + first_line = first_line.lstrip("'") + fmt = "%Y-%m-%d %H:%M:%S" if first_line[4] == '-' \ + else "%d-%m-%Y %H:%M:%S" + dt = datetime.strptime(first_line[0:19], fmt) + except Exception: + pass + # print(f"except: '{e}' for {first_line}") + return dt + + +def _get_file(file: DirEntry) -> dict: '''build one row for the connection table''' entry = {} entry['name'] = file.name stat = file.stat() entry['size'] = format_decimal(stat.st_size) - entry['date'] = stat.st_mtime - entry['created'] = format_datetime(stat.st_ctime, format="short") + try: + dt = stat.st_birthtime + + except Exception: + dt = _get_birth_from_log(file.path) + + if dt: + entry['created'] = format_datetime(dt, format="short") + + # sort by creating date, if available + entry['date'] = dt if isinstance(dt, float) else dt.timestamp() + else: + entry['created'] = _('n/a') + entry['date'] = stat.st_mtime + entry['modified'] = format_datetime(stat.st_mtime, format="short") return entry -def get_list_data(): +def get_list_data() -> list: '''build the connection table''' file_list = [] with os.scandir(Config.get_log_path()) as it: