Update workers.js

This commit is contained in:
DJObleezy 2025-03-30 21:02:45 -07:00 committed by GitHub
parent a2a661797d
commit e21b7be7a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -184,37 +184,38 @@ function updateServerTime() {
}); });
} }
// Fetch worker data from API // Utility functions to show/hide loader
function showLoader() {
$("#loader").show();
}
function hideLoader() {
$("#loader").hide();
}
// Fetch worker data from API with pagination, limiting to 10 pages
function fetchWorkerData(forceRefresh = false) { function fetchWorkerData(forceRefresh = false) {
console.log("Fetching worker data..."); console.log("Fetching worker data...");
lastManualRefreshTime = Date.now(); lastManualRefreshTime = Date.now();
$('#worker-grid').addClass('loading-fade'); $('#worker-grid').addClass('loading-fade');
const apiUrl = `/api/workers${forceRefresh ? '?force=true' : ''}`; let allWorkers = [];
let aggregatedData = null; // Used to preserve summary stats from page 1
const maxPages = 10;
$.ajax({ function fetchPage(page) {
url: apiUrl, if (page > maxPages) {
method: 'GET', // Deduplicate workers based on a unique field (e.g., worker.name)
dataType: 'json', const uniqueWorkers = allWorkers.filter((worker, index, self) =>
timeout: 15000, index === self.findIndex((w) => w.name === worker.name)
success: function (data) { );
if (!data || !data.workers || data.workers.length === 0) {
console.warn("No workers found in data response");
$('#worker-grid').html(`
<div class="text-center p-5">
<p>No workers found. Try refreshing the page.</p>
</div>
`);
return;
}
workerData = data;
// After fetching up to maxPages, update the UI with aggregated (and deduplicated) data
workerData = aggregatedData || {};
workerData.workers = uniqueWorkers;
if (typeof BitcoinMinuteRefresh !== 'undefined' && BitcoinMinuteRefresh.notifyRefresh) { if (typeof BitcoinMinuteRefresh !== 'undefined' && BitcoinMinuteRefresh.notifyRefresh) {
BitcoinMinuteRefresh.notifyRefresh(); BitcoinMinuteRefresh.notifyRefresh();
} }
updateWorkerGrid(); updateWorkerGrid();
updateSummaryStats(); updateSummaryStats();
updateMiniChart(); updateMiniChart();
@ -222,35 +223,45 @@ function fetchWorkerData(forceRefresh = false) {
$('#retry-button').hide(); $('#retry-button').hide();
connectionRetryCount = 0; connectionRetryCount = 0;
console.log("Worker data updated successfully"); console.log("Worker data updated successfully");
},
error: function (xhr, status, error) {
console.error("Error fetching worker data:", error);
$('#worker-grid').html(`
<div class="text-center p-5 text-danger">
<i class="fas fa-exclamation-triangle"></i>
<p>Error loading worker data: ${error || 'Unknown error'}</p>
</div>
`);
$('#retry-button').show();
connectionRetryCount++;
const delay = Math.min(30000, 1000 * Math.pow(1.5, Math.min(5, connectionRetryCount)));
console.log(`Will retry in ${delay / 1000} seconds (attempt ${connectionRetryCount})`);
setTimeout(() => {
fetchWorkerData(true);
}, delay);
},
complete: function () {
$('#worker-grid').removeClass('loading-fade'); $('#worker-grid').removeClass('loading-fade');
return;
} }
});
// Build URL with pagination
const apiUrl = `/api/workers?page=${page}${forceRefresh ? '&force=true' : ''}`;
$.ajax({
url: apiUrl,
method: 'GET',
dataType: 'json',
timeout: 15000,
success: function (data) {
if (data && data.workers && data.workers.length > 0) {
allWorkers = allWorkers.concat(data.workers);
// On the first page, preserve other properties (e.g., summary stats)
if (page === 1) {
aggregatedData = data;
}
} else {
console.warn(`No workers found on page ${page}`);
// Optionally, stop fetching early if no data is returned:
page = maxPages;
}
},
error: function (xhr, status, error) {
console.error(`Error fetching worker data on page ${page}:`, error);
},
complete: function () {
// Continue to the next page regardless of success or error
fetchPage(page + 1);
}
});
}
// Start fetching from page 1
fetchPage(1);
} }
// Refresh worker data every 60 seconds // Refresh worker data every 60 seconds
setInterval(function () { setInterval(function () {
console.log("Refreshing worker data at " + new Date().toLocaleTimeString()); console.log("Refreshing worker data at " + new Date().toLocaleTimeString());