feat: better logs view

This commit is contained in:
Kevin Restaino
2024-01-10 18:18:10 -05:00
parent 6a2f1a59cf
commit 6294427aeb
+47 -3
View File
@@ -4,6 +4,8 @@ import threading
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
import functools import functools
import re
import datetime
load_dotenv() load_dotenv()
@@ -60,13 +62,55 @@ def list_logs():
try: try:
logs = os.listdir(LOG_DIR) logs = os.listdir(LOG_DIR)
logs = [log for log in logs if log.endswith(".log")] # Filter log files logs = [log for log in logs if log.endswith(".log")] # Filter log files
full_paths = [os.path.join(LOG_DIR, log) for log in logs]
logs_sorted = sorted(full_paths, key=os.path.getmtime, reverse=True)
log_links = "".join(f'<li><a href="/logs/{log}">{log}</a></li>' for log in logs) table_rows = []
return f"<h1>Log Files</h1><ul>{log_links}</ul>" for log_path in logs_sorted:
with open(log_path, "r") as file:
last_line = file.readlines()[-1]
tracks, found, not_found, match, time = parse_log_data(last_line)
log_name = os.path.basename(log_path)
timestamp = int(log_name.split("_")[1].split(".")[0])
log_datetime = datetime.datetime.fromtimestamp(timestamp)
formatted_datetime = log_datetime.strftime("%Y-%m-%d %H:%M:%S")
table_rows.append(
f"<tr><td><a href='/logs/{log_name}'>{log_name}</a></td><td>{formatted_datetime}</td><td>{tracks}</td><td>{found}</td><td>{not_found}</td><td>{match}</td><td>{time}</td></tr>"
)
table_html = f"<table border='1'><tr><th>Log File</th><th>Date & Time</th><th>Tracks</th><th>Found</th><th>Not Found</th><th>Match</th><th>Time</th></tr>{''.join(table_rows)}</table>"
return table_html
except Exception as e: except Exception as e:
return f"An error occurred: {e}", 500 return f"An error occurred: {e}", 500
def parse_log_data(line):
# Regular expressions to extract the required data
tracks_pattern = r"Tracks: (\d+)"
found_pattern = r"Found: (\d+)"
not_found_pattern = r"Not Found: (\d+)"
match_pattern = r"Match: ([\d.]+%)"
time_pattern = r"Time: ([\ds]+)"
# Extracting data using regular expressions
tracks = re.search(tracks_pattern, line)
found = re.search(found_pattern, line)
not_found = re.search(not_found_pattern, line)
match = re.search(match_pattern, line)
time = re.search(time_pattern, line)
# Getting the values or "N/A" if not found
tracks = tracks.group(1) if tracks else "N/A"
found = found.group(1) if found else "N/A"
not_found = not_found.group(1) if not_found else "N/A"
match = match.group(1) if match else "N/A"
time = time.group(1) if time else "N/A"
return tracks, found, not_found, match, time
@sptnr.route("/logs/<filename>") @sptnr.route("/logs/<filename>")
@api_key_required @api_key_required
def view_log(filename): def view_log(filename):
@@ -80,7 +124,7 @@ def view_log(filename):
# Convert content to HTML-friendly format # Convert content to HTML-friendly format
content = content.replace("\n", "<br>") content = content.replace("\n", "<br>")
return f'<h1>{filename}</h1><div style="white-space: pre-wrap;">{content}</div>' return f"<pre>{content}</pre>"
except Exception as e: except Exception as e:
return f"An error occurred: {e}", 500 return f"An error occurred: {e}", 500