mirror of
https://github.com/krestaino/sptnr.git
synced 2026-06-12 22:15:22 -04:00
feat: better logs view
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user