diff --git a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py index 950c272a..f937351a 100644 --- a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py +++ b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py @@ -191,7 +191,7 @@ def analytics( from TwitchChannelPointsMiner.classes.AnalyticsServer import AnalyticsServer http_server = AnalyticsServer( - host=host, port=port, refresh=refresh, days_ago=days_ago + host=host, port=port, refresh=refresh, days_ago=days_ago, username=self.username ) http_server.daemon = True http_server.name = "Analytics Thread" diff --git a/TwitchChannelPointsMiner/classes/AnalyticsServer.py b/TwitchChannelPointsMiner/classes/AnalyticsServer.py index 7b59c586..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__( @@ -230,6 +231,7 @@ def __init__( port: int = 5000, refresh: int = 5, days_ago: int = 7, + username: str = None ): super(AnalyticsServer, self).__init__() @@ -239,6 +241,28 @@ def __init__( self.port = port self.refresh = refresh self.days_ago = days_ago + 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() + + # 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") self.app = Flask( __name__, @@ -259,6 +283,8 @@ def __init__( ) self.app.add_url_rule("/json_all", "json_all", json_all, methods=["GET"]) + self.app.add_url_rule( + "/log", "log", generate_log, methods=["GET"]) def run(self): logger.info( diff --git a/assets/charts.html b/assets/charts.html index fbae91be..844bc630 100644 --- a/assets/charts.html +++ b/assets/charts.html @@ -153,6 +153,10 @@
+
+
+
+ +
+
diff --git a/assets/dark-theme.css b/assets/dark-theme.css index 85270c06..437a6769 100644 --- a/assets/dark-theme.css +++ b/assets/dark-theme.css @@ -33,3 +33,7 @@ a:hover { .checkbox:hover{ color: #f9826c; } +#log-content { + color: #fff; + background-color: #2B2D3E; +} \ No newline at end of file diff --git a/assets/script.js b/assets/script.js index 48b67ae6..5fc1d3ee 100644 --- a/assets/script.js +++ b/assets/script.js @@ -92,6 +92,30 @@ startDate.setDate(startDate.getDate() - daysAgo); var endDate = new Date(); $(document).ready(function () { + // Variable to keep track of whether log checkbox is checked + var isLogCheckboxChecked = $('#log').prop('checked'); + + // Variable to keep track of the last received log index + var lastReceivedLogIndex = 0; + + // Function to get the full log content + function getLog() { + if (isLogCheckboxChecked) { + $.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); + }); + } + } + // Retrieve the saved header visibility preference from localStorage var headerVisibility = localStorage.getItem('headerVisibility'); @@ -153,6 +177,31 @@ $(document).ready(function () { updateAnnotations(); toggleDarkMode(); + + // Retrieve log checkbox state from localStorage and update UI accordingly + var logCheckboxState = localStorage.getItem('logCheckboxState'); + $('#log').prop('checked', logCheckboxState === 'true'); + if (logCheckboxState === 'true') { + isLogCheckboxChecked = true; + $('#log-box').show(); + // Start continuously updating the log content + getLog(); + } + + // Handle the log checkbox change event + $('#log').change(function () { + isLogCheckboxChecked = $(this).prop('checked'); + localStorage.setItem('logCheckboxState', isLogCheckboxChecked); + + if (isLogCheckboxChecked) { + $('#log-box').show(); + getLog(); + } else { + $('#log-box').hide(); + // Clear log content when checkbox is unchecked + // $("#log-content").text(''); + } + }); }); function formatDate(date) { diff --git a/assets/style.css b/assets/style.css index 34a78e05..e11f2d9a 100644 --- a/assets/style.css +++ b/assets/style.css @@ -53,3 +53,10 @@ a { ::-webkit-scrollbar-thumb:hover { background-color: #a8bbbf; } + +#log-content { + text-align: left; + white-space: pre-wrap; + max-height: 500px; + padding: 0; +} \ No newline at end of file