diff --git a/TwitchChannelPointsMiner/classes/AnalyticsServer.py b/TwitchChannelPointsMiner/classes/AnalyticsServer.py index e3feff28..0aabbe10 100644 --- a/TwitchChannelPointsMiner/classes/AnalyticsServer.py +++ b/TwitchChannelPointsMiner/classes/AnalyticsServer.py @@ -222,6 +222,7 @@ def check_assets(): download_assets(assets_folder, required_files) break +last_sent_log_index = 0 class AnalyticsServer(Thread): def __init__( @@ -243,12 +244,23 @@ def __init__( self.username = username def generate_log(): + global last_sent_log_index # Use the global variable + + # Get the last received log index from the client request parameters + last_received_index = int(request.args.get("lastIndex", last_sent_log_index)) + logs_path = os.path.join(Path().absolute(), "logs") log_file_path = os.path.join(logs_path, f"{username}.log") try: with open(log_file_path, "r") as log_file: log_content = log_file.read() - return Response(log_content, status=200, mimetype="text/plain") + + # Extract new log entries since the last received index + new_log_entries = log_content[last_received_index:] + last_sent_log_index = len(log_content) # Update the last sent index + + return Response(new_log_entries, status=200, mimetype="text/plain") + except FileNotFoundError: return Response("Log file not found.", status=404, mimetype="text/plain") diff --git a/assets/charts.html b/assets/charts.html index 3afb4c80..844bc630 100644 --- a/assets/charts.html +++ b/assets/charts.html @@ -185,7 +185,7 @@
diff --git a/assets/script.js b/assets/script.js index 2a4a7e5f..5fc1d3ee 100644 --- a/assets/script.js +++ b/assets/script.js @@ -95,35 +95,23 @@ $(document).ready(function () { // Variable to keep track of whether log checkbox is checked var isLogCheckboxChecked = $('#log').prop('checked'); - // Function to get the full log content - function getFullLog() { - $.get("/log", function (data) { - // Update the log content with the full log - $("#log-content").text(data); - // Scroll to the bottom of the log content - $("#log-content").scrollTop($("#log-content")[0].scrollHeight); - }); - } + // Variable to keep track of the last received log index + var lastReceivedLogIndex = 0; - // Function to continuously update the log content - function updateLogContent() { + // Function to get the full log content + function getLog() { if (isLogCheckboxChecked) { - // Get the current log content - var currentLogContent = $("#log-content").text(); - - // Fetch the updated log content - $.get("/log", function (data) { - // If the log checkbox is still checked, update the displayed log - if (isLogCheckboxChecked && data !== currentLogContent) { - // Update the log content with the new log data - $("#log-content").text(data); - // Scroll to the bottom of the log content - $("#log-content").scrollTop($("#log-content")[0].scrollHeight); - } - // Schedule the next update after 1 second if the log checkbox is still checked - if (isLogCheckboxChecked) { - setTimeout(updateLogContent, 1000); - } + $.get(`/log?lastIndex=${lastReceivedLogIndex}`, function (data) { + // Process and display the new log entries received + $("#log-content").append(data); + // Scroll to the bottom of the log content + $("#log-content").scrollTop($("#log-content")[0].scrollHeight); + + // Update the last received log index + lastReceivedLogIndex += data.length; + + // Call getLog() again after a certain interval (e.g., 1 second) + setTimeout(getLog, 1000); }); } } @@ -196,10 +184,8 @@ $(document).ready(function () { if (logCheckboxState === 'true') { isLogCheckboxChecked = true; $('#log-box').show(); - // Get the full log content when the document is ready - getFullLog(); // Start continuously updating the log content - updateLogContent(); + getLog(); } // Handle the log checkbox change event @@ -209,8 +195,7 @@ $(document).ready(function () { if (isLogCheckboxChecked) { $('#log-box').show(); - getFullLog(); - updateLogContent(); + getLog(); } else { $('#log-box').hide(); // Clear log content when checkbox is unchecked