Skip to content

Commit

Permalink
get only new log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavydov committed Nov 2, 2023
1 parent 115cb7c commit 7147b43
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
14 changes: 13 additions & 1 deletion TwitchChannelPointsMiner/classes/AnalyticsServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def check_assets():
download_assets(assets_folder, required_files)
break

last_sent_log_index = 0

class AnalyticsServer(Thread):
def __init__(
Expand All @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion assets/charts.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
<div class="column is-12">
<div class="box" id="log-box" style="padding: 0.30rem; display: none;">
<pre
id="log-content">In your run.py file set save=True in logger_settings to save logs to a file</pre>
id="log-content">In your run.py file set save=True in logger_settings to save logs to a file.&#10;&#13;</pre>
</div>
</div>
</div>
Expand Down
49 changes: 17 additions & 32 deletions assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7147b43

Please sign in to comment.