Skip to content

Commit

Permalink
Fix broken footer stats on pages without panels
Browse files Browse the repository at this point in the history
-The update_stats() function is now called again from all pages even when not displaying panels to fix an oversight with the earlier change to allow removal of panels from any page
-Reorganized the structure and order of calls within the /ext/getsummary api to allow for returning just the connection and block counts for pages that have show_panels set to false
  • Loading branch information
joeuhren committed Apr 12, 2021
1 parent 29954f6 commit b0108cf
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 107 deletions.
119 changes: 64 additions & 55 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,68 +407,77 @@ app.use('/ext/getaddresstxs/:address/:start/:length', function(req, res) {
app.use('/ext/getsummary', function(req, res) {
// check if the getsummary api is enabled or else check the headers to see if it matches an internal ajax request from the explorer itself (TODO: come up with a more secure method of whitelisting ajax calls from the explorer)
if ((settings.api_page.enabled == true && settings.api_page.public_apis.ext.getsummary.enabled == true) || (req.headers['x-requested-with'] != null && req.headers['x-requested-with'].toLowerCase() == 'xmlhttprequest' && req.headers.referer != null && req.headers.accept.indexOf('text/javascript') > -1 && req.headers.accept.indexOf('application/json') > -1)) {
lib.get_difficulty(function(difficulty) {
difficultyHybrid = '';

if (difficulty && difficulty['proof-of-work']) {
if (settings.shared_pages.difficulty == 'Hybrid') {
difficultyHybrid = 'POS: ' + difficulty['proof-of-stake'];
difficulty = 'POW: ' + difficulty['proof-of-work'];
} else if (settings.shared_pages.difficulty == 'POW')
difficulty = difficulty['proof-of-work'];
else
difficulty = difficulty['proof-of-stake'];
}

lib.get_hashrate(function(hashrate) {
lib.get_connectioncount(function(connections) {
lib.get_blockcount(function(blockcount) {
lib.get_connectioncount(function(connections) {
lib.get_blockcount(function(blockcount) {
// check if this is a footer-only method that should only return the connection count and block count
if (req.headers['footer-only'] != null && req.headers['footer-only'] == 'true') {
// only return the connection count and block count
res.send({
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-')
});
} else {
lib.get_hashrate(function(hashrate) {
db.get_stats(settings.coin.name, function (stats) {
lib.get_masternodecount(function(masternodestotal) {
if (hashrate == 'There was an error. Check your console.')
hashrate = 0;

// check if the masternode count api is enabled
if (settings.api_page.public_apis.rpc.getmasternodecount.enabled == true && settings.api_cmds['getmasternodecount'] != null && settings.api_cmds['getmasternodecount'] != '') {
// masternode count api is available
var mn_total = 0;
var mn_enabled = 0;

if (masternodestotal) {
if (masternodestotal.total)
mn_total = masternodestotal.total;

if (masternodestotal.enabled)
mn_enabled = masternodestotal.enabled;
lib.get_difficulty(function(difficulty) {
difficultyHybrid = '';

if (difficulty && difficulty['proof-of-work']) {
if (settings.shared_pages.difficulty == 'Hybrid') {
difficultyHybrid = 'POS: ' + difficulty['proof-of-stake'];
difficulty = 'POW: ' + difficulty['proof-of-work'];
} else if (settings.shared_pages.difficulty == 'POW')
difficulty = difficulty['proof-of-work'];
else
difficulty = difficulty['proof-of-stake'];
}

res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
masternodeCountOnline: (masternodestotal ? mn_enabled : '-'),
masternodeCountOffline: (masternodestotal ? Math.floor(mn_total - mn_enabled) : '-'),
blockcount: (blockcount ? blockcount : '-')
});
} else {
// masternode count api is not available
res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-')
});
}
if (hashrate == 'There was an error. Check your console.')
hashrate = 0;

// check if the masternode count api is enabled
if (settings.api_page.public_apis.rpc.getmasternodecount.enabled == true && settings.api_cmds['getmasternodecount'] != null && settings.api_cmds['getmasternodecount'] != '') {
// masternode count api is available
var mn_total = 0;
var mn_enabled = 0;

if (masternodestotal) {
if (masternodestotal.total)
mn_total = masternodestotal.total;

if (masternodestotal.enabled)
mn_enabled = masternodestotal.enabled;
}

res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
masternodeCountOnline: (masternodestotal ? mn_enabled : '-'),
masternodeCountOffline: (masternodestotal ? Math.floor(mn_total - mn_enabled) : '-'),
blockcount: (blockcount ? blockcount : '-')
});
} else {
// masternode count api is not available
res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-')
});
}
});
});
});
});
});
}
});
});
} else
Expand Down
113 changes: 61 additions & 52 deletions views/layout.pug
Original file line number Diff line number Diff line change
Expand Up @@ -192,65 +192,74 @@ html(lang='en')
}
}
function update_stats() {
$.ajax({url: '/ext/getsummary', headers: {Accept: 'application/json, text/javascript, */*; q=0.01'}, success: function(json) {
if (json.masternodeCountOnline == null)
json.masternodeCountOnline = '-';
if (json.masternodeCountOffline == null)
json.masternodeCountOffline = '-';
var summary_headers = {
Accept: 'application/json, text/javascript, */*; q=0.01'
};

var mnOnlineText = json.masternodeCountOnline+" node"+(json.masternodeCountOnline == 1 ? "" : "s")+" online";
var mnOfflineText = json.masternodeCountOffline+" unreachable node"+(json.masternodeCountOffline == 1 ? "" : "s");
if (#{showPanels} == false)
summary_headers['footer-only'] = 'true';

$("#masternodeCountOnline").text(json.masternodeCountOnline).prop("alt", mnOnlineText).prop("title", mnOnlineText).attr("data-original-title", mnOnlineText);
$("#masternodeCountOffline").text(json.masternodeCountOffline).prop("alt", mnOfflineText).prop("title", mnOfflineText).attr("data-original-title", mnOfflineText);
$("#spnMasternodeCountOnline").prop("alt", mnOnlineText).prop("title", mnOnlineText).attr("data-original-title", mnOnlineText);
$("#spnMasternodeCountOffline").prop("alt", mnOfflineText).prop("title", mnOfflineText).attr("data-original-title", mnOfflineText);
showTopPanelData('masternodepanel', 'masternodePanelLoading');
$.ajax({url: '/ext/getsummary', headers: summary_headers, success: function(json) {
if (#{showPanels} == true) {
if (json.masternodeCountOnline == null)
json.masternodeCountOnline = '-';
if (json.masternodeCountOffline == null)
json.masternodeCountOffline = '-';

var supplyString = json.supply;
var diffString = json.difficulty;
var hashrateString = json.hashrate;
var splitValue, splitParts;
var mnOnlineText = json.masternodeCountOnline+" node"+(json.masternodeCountOnline == 1 ? "" : "s")+" online";
var mnOfflineText = json.masternodeCountOffline+" unreachable node"+(json.masternodeCountOffline == 1 ? "" : "s");

if (!isNaN(json.difficulty))
diffString = Number(json.difficulty).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
if (!isNaN(json.supply))
supplyString = parseInt(parseFloat(json.supply).toFixed(0)).toLocaleString('en');
if (!isNaN(json.hashrate))
hashrateString = Number(json.hashrate).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
$("#masternodeCountOnline").text(json.masternodeCountOnline).prop("alt", mnOnlineText).prop("title", mnOnlineText).attr("data-original-title", mnOnlineText);
$("#masternodeCountOffline").text(json.masternodeCountOffline).prop("alt", mnOfflineText).prop("title", mnOfflineText).attr("data-original-title", mnOfflineText);
$("#spnMasternodeCountOnline").prop("alt", mnOnlineText).prop("title", mnOnlineText).attr("data-original-title", mnOnlineText);
$("#spnMasternodeCountOffline").prop("alt", mnOfflineText).prop("title", mnOfflineText).attr("data-original-title", mnOfflineText);
showTopPanelData('masternodepanel', 'masternodePanelLoading');

$("#supply").text(supplyString);
splitValue = Number(parseFloat(json.lastPrice).toFixed(8) * parseInt(parseFloat(json.supply).toFixed(0))).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
splitParts = splitValue.split('.');
showTopPanelData('supplypanel', 'supplyPanelLoading');
var supplyString = json.supply;
var diffString = json.difficulty;
var hashrateString = json.hashrate;
var splitValue, splitParts;

$("#marketCap").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
showTopPanelData('marketCapPanel', 'marketCapPanelLoading');
if (!isNaN(json.difficulty))
diffString = Number(json.difficulty).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
if (!isNaN(json.supply))
supplyString = parseInt(parseFloat(json.supply).toFixed(0)).toLocaleString('en');
if (!isNaN(json.hashrate))
hashrateString = Number(json.hashrate).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});

splitParts = diffString.split('.');
$("#difficulty").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
$("#supply").text(supplyString);
splitValue = Number(parseFloat(json.lastPrice).toFixed(8) * parseInt(parseFloat(json.supply).toFixed(0))).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
splitParts = splitValue.split('.');
showTopPanelData('supplypanel', 'supplyPanelLoading');

if (json.difficultyHybrid == null || json.difficultyHybrid == '')
$("#difficultyHybrid").html('-');
else {
splitParts = json.difficultyHybrid.split('.');
$("#difficultyHybrid").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
}
$("#marketCap").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
showTopPanelData('marketCapPanel', 'marketCapPanelLoading');

showTopPanelData('difficultypanel', 'difficultyPanelLoading');
splitParts = diffString.split('.');
$("#difficulty").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');

if (hashrateString == null || hashrateString == '' || hashrateString == '-')
$("#hashrate").html('-');
else {
splitParts = hashrateString.split('.');
$("#hashrate").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
}
showTopPanelData('hashratepanel', 'hashratePanelLoading');
if (json.difficultyHybrid == null || json.difficultyHybrid == '')
$("#difficultyHybrid").html('-');
else {
splitParts = json.difficultyHybrid.split('.');
$("#difficultyHybrid").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
}

splitValue = Number(json.lastPrice).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
splitParts = splitValue.split('.');
$("#lastPrice").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
showTopPanelData('pricepanel', 'pricePanelLoading');
showTopPanelData('difficultypanel', 'difficultyPanelLoading');

if (hashrateString == null || hashrateString == '' || hashrateString == '-')
$("#hashrate").html('-');
else {
splitParts = hashrateString.split('.');
$("#hashrate").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
}
showTopPanelData('hashratepanel', 'hashratePanelLoading');

splitValue = Number(json.lastPrice).toLocaleString('en',{'minimumFractionDigits':2,'maximumFractionDigits':8,'useGrouping':true});
splitParts = splitValue.split('.');
$("#lastPrice").html(splitParts[0] + '.<span class="decimal">' + splitParts[1] + '</span>');
showTopPanelData('pricepanel', 'pricePanelLoading');
}

$("#lblConnections").text(json.connections + ' connections');
$("#lblBlockcount").text(json.blockcount + ' blocks');
Expand Down Expand Up @@ -356,11 +365,11 @@ html(lang='en')
hideShowPanel('#{settings.panel3}', 'pnlThree');
hideShowPanel('#{settings.panel4}', 'pnlFour');
hideShowPanel('#{settings.panel5}', 'pnlFive');
setInterval(function() {
update_stats();
}, 60000);
update_stats();
}
setInterval(function() {
update_stats();
}, 60000);
update_stats();
fixFooterHeightAndPosition();
enableTooltips();
});
Expand Down

0 comments on commit b0108cf

Please # to comment.