From 55db862438a520a851a3dcd55ea5287c38fc921a Mon Sep 17 00:00:00 2001 From: Roy Wiggins Date: Wed, 2 Oct 2024 23:17:06 +0000 Subject: [PATCH] webgui logs: use local time for display and time filters --- common/helper.py | 23 ++++++++++++++++++++++- webgui.py | 19 ++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/common/helper.py b/common/helper.py index 09a4a9c..393257e 100755 --- a/common/helper.py +++ b/common/helper.py @@ -10,7 +10,8 @@ import inspect from pathlib import Path import threading -from typing import Callable, Optional, Tuple +from typing import Callable, List, Optional, Tuple +import dateutil import graphyte import os import common.influxdb @@ -31,6 +32,26 @@ def validate_folders(config) -> Tuple[bool, str]: return False, f"No read/write access to {folder}" return True, "" +def localize_log_timestamps(loglines: List[str], config) -> None: + if config.mercure.local_time == "UTC": + return + + try: + local_tz: datetime.tzinfo = dateutil.tz.gettz(config.mercure.local_time) + except: + return + + for i, line in enumerate(loglines): + split = line.split(" ") + timestamp = split[0] + try: + parsed_dt = dateutil.parser.isoparse(timestamp) + dt_localtime:datetime.datetime = parsed_dt.astimezone(local_tz) + split[0] = dt_localtime.isoformat(timespec='seconds') + loglines[i] = " ".join(split) + except: + pass + def get_now_str() -> str: """Returns the current time as string with mercure-wide formatting""" return datetime.now().strftime("%Y-%m-%d %H:%M:%S") diff --git a/webgui.py b/webgui.py index 623e625..c9f40d5 100755 --- a/webgui.py +++ b/webgui.py @@ -13,6 +13,8 @@ import subprocess from tempfile import tempdir import traceback + +import dateutil from common.generate_test_series import generate_series, generate_several_protocols from common.types import DicomTarget, Rule, Module import uvicorn @@ -289,9 +291,9 @@ async def show_log(request) -> Response: start_date_cmd = "" end_date_cmd = "" if start_timestamp: - start_date_cmd = f'--since "{start_timestamp}"' + start_date_cmd = f'--since "{start_timestamp} {config.mercure.local_time}"' if end_timestamp: - end_date_cmd = f'--until "{end_timestamp}"' + end_date_cmd = f'--until "{end_timestamp} {config.mercure.local_time}"' service_name_or_list = services.services_list[requested_service]["systemd_service"] if isinstance(service_name_or_list, list): @@ -300,10 +302,12 @@ async def show_log(request) -> Response: else: service_name = service_name_or_list sub_services = [] + run_result = await async_run( f"sudo journalctl -n 1000 -u " f'{service_name} ' - f"{start_date_cmd} {end_date_cmd}" + f"{start_date_cmd} {end_date_cmd} " + "-o short-iso" ) return_code = -1 if run_result[0] is None else run_result[0] raw_logs = run_result[1] @@ -321,6 +325,12 @@ async def show_log(request) -> Response: container = client.containers.get(service_name) container.reload() + try: + local_tz: datetime.tzinfo = dateutil.tz.gettz(config.mercure.local_time) + start_dt = start_dt.replace(tzinfo=local_tz) + end_dt = end_dt.replace(tzinfo=local_tz) + except: + pass raw_logs = container.logs(since=start_dt, until=end_dt, timestamps=True, tail=1000) return_code = 0 except (docker.errors.NotFound, docker.errors.APIError) as e: # type: ignore @@ -333,6 +343,9 @@ async def show_log(request) -> Response: line_list = log_content.split("\n") if len(line_list) and (not line_list[-1]): del line_list[-1] + + helper.localize_log_timestamps(line_list, config) + log_content = "
".join(line_list) else: log_content = f"Error reading log information"