Skip to content

Commit

Permalink
webgui logs: use local time for display and time filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Wiggins authored and Roy Wiggins committed Oct 2, 2024
1 parent be3e3d6 commit 55db862
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
23 changes: 22 additions & 1 deletion common/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
19 changes: 16 additions & 3 deletions webgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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 = "<br />".join(line_list)
else:
log_content = f"Error reading log information"
Expand Down

0 comments on commit 55db862

Please sign in to comment.