Skip to content

Commit

Permalink
Merge pull request rdavydov#395 from rdavydov/analytics-log
Browse files Browse the repository at this point in the history
Log file reader on the Analytics webpage
  • Loading branch information
rdavydov authored Nov 2, 2023
2 parents 35d33b9 + 7147b43 commit 9ecacf4
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions 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 @@ -230,6 +231,7 @@ def __init__(
port: int = 5000,
refresh: int = 5,
days_ago: int = 7,
username: str = None
):
super(AnalyticsServer, self).__init__()

Expand All @@ -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__,
Expand All @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions assets/charts.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@
<input class="input" type="date" id="endDate">
</div>
<div class="column has-text-right" style="margin: auto">
<label class="checkbox checkbox-label">
Log
<input type="checkbox" id="log">
</label>
<label class="checkbox checkbox-label">
Annotations
<input type="checkbox" checked="true" id="annotations">
Expand All @@ -177,6 +181,14 @@
<div class="box" id="chart" style="padding: 0.30rem;"></div>
</div>
</div>
<div class="columns">
<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.&#10;&#13;</pre>
</div>
</div>
</div>
</div>
</body>

Expand Down
4 changes: 4 additions & 0 deletions assets/dark-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ a:hover {
.checkbox:hover{
color: #f9826c;
}
#log-content {
color: #fff;
background-color: #2B2D3E;
}
49 changes: 49 additions & 0 deletions assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 9ecacf4

Please sign in to comment.