diff --git a/server.py b/server.py index c64d2dd..f5b914b 100644 --- a/server.py +++ b/server.py @@ -4,6 +4,8 @@ import threading import os from dotenv import load_dotenv import functools +import re +import datetime load_dotenv() @@ -60,13 +62,55 @@ def list_logs(): try: logs = os.listdir(LOG_DIR) 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'
  • {log}
  • ' for log in logs) - return f"

    Log Files

    " + table_rows = [] + 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"{log_name}{formatted_datetime}{tracks}{found}{not_found}{match}{time}" + ) + + table_html = f"{''.join(table_rows)}
    Log FileDate & TimeTracksFoundNot FoundMatchTime
    " + return table_html except Exception as e: 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/") @api_key_required def view_log(filename): @@ -80,7 +124,7 @@ def view_log(filename): # Convert content to HTML-friendly format content = content.replace("\n", "
    ") - return f'

    {filename}

    {content}
    ' + return f"
    {content}
    " except Exception as e: return f"An error occurred: {e}", 500