Skip to content

Commit

Permalink
added stats auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
npq7721 committed Nov 15, 2019
1 parent c678742 commit c0e9c4a
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ See `btc-rpc-explorer --help` for the full list of CLI options.

# Support

* [3NPGpNyLLmVKCEcuipBs7G4KpQJoJXjDGe](bitcoin:3NPGpNyLLmVKCEcuipBs7G4KpQJoJXjDGe)
* [3Jym9QJLXQyjSSGKS1LCD9LCroPqCGo3Lq](bitcoin:3Jym9QJLXQyjSSGKS1LCD9LCroPqCGo3Lq)


[npm-ver-img]: https://img.shields.io/npm/v/btc-rpc-explorer.svg?style=flat
Expand Down
47 changes: 46 additions & 1 deletion app/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,50 @@ function buildQrCodeUrl(str, results) {
});
}

function updateElementValue(id, value) {
var element = $(`#${id}`);
if(element) {
element.text(value);
}
}

function updateElementAttr(id, attrName, value) {
var element = $(`#${id}`);
if(element) {
element.attr(attrName, value);
}
}

function updateStats() {
$.ajax({url: '/ext/summary', success: function(json){
var hashrateData = formatLargeNumber(json.miningInfo.networkhashps, 3);
var mempoolBytesData = formatLargeNumber(json.mempoolInfo.usage, 2);
var chainworkData = formatLargeNumber(parseInt("0x" + json.getblockchaininfo.chainwork), 2);
var difficultyData = formatLargeNumber(json.getblockchaininfo.difficulty, 3);
var sizeData = formatLargeNumber(json.getblockchaininfo.size_on_disk, 2);
var price = `${formatExchangedCurrency(1.0, "btc", "฿", 8)}/${formatExchangedCurrency(1.0, "usd", "$", 6)}`
updateElementValue("hashrate", hashrateData[0]);
updateElementAttr("hashUnit", "data-original-title", `${hashrateData[1].abbreviation}H = ${hashrateData[1].name}-hash (x10^${hashrateData[1].exponent})`);
updateElementValue("txStats", json.txStats.totalTxCount.toLocaleString());
updateElementValue("mempoolCount", json.mempoolInfo.size.toLocaleString() + " tx");
updateElementValue("mempoolSize", `(${mempoolBytesData[0]} ${mempoolBytesData[1].abbreviation}B)`);
updateElementValue("chainworkNum", chainworkData[0]);
updateElementValue("chainworkExp", chainworkData[1].exponent);
updateElementValue("diffNum", difficultyData[0]);
updateElementValue("diffExp", difficultyData[1].exponent);
updateElementValue("chainSize", `${sizeData[0]} ${sizeData[1].abbreviation}B`);
updateElementValue("price", price);
}});
}

function homePageRoutineScript() {
$(document).ready(function(){
setInterval( function() {
update_stats();
}, 60000);
});
}


module.exports = {
reflectPromise: reflectPromise,
Expand Down Expand Up @@ -581,5 +625,6 @@ module.exports = {
colorHexToHsl: colorHexToHsl,
logError: logError,
buildQrCodeUrls: buildQrCodeUrls,
ellipsize: ellipsize
ellipsize: ellipsize,
homePageRoutineScript: homePageRoutineScript
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"private": false,
"bin": "bin/cli.js",
"scripts": {
"start": "node ./bin/www",
"start": "node --max_old_space_size=8096 ./bin/www",
"refresh-mining-pool-configs": "node ./bin/refresh-mining-pool-configs.js"
},
"keywords": [
"bitcoin",
"litecoin",
"pigeoncoin",
"blockchain"
],
"author": "Dan Janosik <[email protected]>",
Expand Down
16 changes: 15 additions & 1 deletion routes/baseActionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ var Session = require("./session.js");

const forceCsrf = csurf({ ignoreMethods: [] });

var routing = function(path, method, sessionMethod, hashNext = true) {
if(hashNext) {
router[method](path, (req, res, next) => {
var session = new Session(req, res, next);
session[sessionMethod]();
});
} else {
router[method](path, (req, res) => {
var session = new Session(req, res);
session[sessionMethod]();
});
}
}

router.get("/", function(req, res, next) {
var session = new Session(req,res,next);
if(session.isRenderConnect()) {
Expand Down Expand Up @@ -1131,5 +1145,5 @@ router.get("/fun", function(req, res, next) {

next();
});

routing("/ext/summary", "get", "getNetworkSummary", false);
module.exports = router;
55 changes: 55 additions & 0 deletions routes/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,61 @@ class Session {
self.next();
});
}

getNetworkSummary() {
var promises = [];

promises.push(coreApi.getMempoolInfo());
promises.push(coreApi.getMiningInfo());
var self = this;
var result = {};
coreApi.getBlockchainInfo().then(function(getblockchaininfo) {
result.getblockchaininfo = getblockchaininfo;

if (getblockchaininfo.chain !== 'regtest') {
var targetBlocksPerDay = 24 * 60 * 60 / global.coinConfig.targetBlockTimeSeconds;

promises.push(coreApi.getTxCountStats(targetBlocksPerDay / 4, -targetBlocksPerDay, "latest"));

var chainTxStatsIntervals = [ targetBlocksPerDay, targetBlocksPerDay * 7, targetBlocksPerDay * 30, targetBlocksPerDay * 365 ]
.filter(numBlocks => numBlocks <= getblockchaininfo.blocks);

result.chainTxStatsLabels = [ "24 hours", "1 week", "1 month", "1 year" ]
.slice(0, chainTxStatsIntervals.length)
.concat("All time");

for (var i = 0; i < chainTxStatsIntervals.length; i++) {
promises.push(coreApi.getChainTxStats(chainTxStatsIntervals[i]));
}
}
Promise.all(promises).then(function(promiseResults) {
result.mempoolInfo = promiseResults[0];
result.miningInfo = promiseResults[1];

if (getblockchaininfo.chain !== 'regtest') {
result.txStats = promiseResults[2];

var chainTxStats = [];
for (var i = 0; i < self.res.locals.chainTxStatsLabels.length; i++) {
chainTxStats.push(promiseResults[i + 3]);
}

result.chainTxStats = chainTxStats;
}
self.res.send(result);
if(self.next) {
self.next();
}
});
}).catch(function(err) {
result.userMessage = "Error loading recent blocks: " + err;

self.res.send(result);
if(self.next) {
self.next();
}
});
}
}

module.exports = Session;
27 changes: 14 additions & 13 deletions views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ block content
li If you just started your node, it may still be initializing.
li If your node is already initialized, check your RPC connection info.
else

- utils.homePageRoutineScript();
if (config.demoSite && session.hideHomepageBanner != "true")
div(class="alert alert-primary alert-dismissible shadow-sm mb-4", role="alert")
span
Expand Down Expand Up @@ -86,8 +86,8 @@ block content
span(class="font-weight-bold") Hashrate

p(class="lead")
span #{hashrateData[0]}
span.border-dotted(title=`${hashrateData[1].abbreviation}H = ${hashrateData[1].name}-hash (x10^${hashrateData[1].exponent})`, data-toggle="tooltip") #{hashrateData[1].abbreviation}H/s
span(id="hashrate") #{hashrateData[0]}
span(id="hashUnit").border-dotted(title=`${hashrateData[1].abbreviation}H = ${hashrateData[1].name}-hash (x10^${hashrateData[1].exponent})`, data-toggle="tooltip") #{hashrateData[1].abbreviation}H/s

if (txStats)
div(class=networkSummaryColumnClass)
Expand All @@ -97,7 +97,7 @@ block content

span(class="font-weight-bold") Total Transactions

p(class="lead") #{txStats.totalTxCount.toLocaleString()}
p(id="txStats" class="lead") #{txStats.totalTxCount.toLocaleString()}

div(class=networkSummaryColumnClass)
div(class="float-left", style="height: 40px; width: 40px;")
Expand All @@ -106,9 +106,10 @@ block content

span(class="font-weight-bold") Unconfirmed Transactions

p(class="lead") #{mempoolInfo.size.toLocaleString()} tx
p(class="lead")
label(id="mempoolCount") #{mempoolInfo.size.toLocaleString()} tx
- var mempoolBytesData = utils.formatLargeNumber(mempoolInfo.usage, 2);
small(class="text-muted font-weight-light") (#{mempoolBytesData[0]} #{mempoolBytesData[1].abbreviation}B)
small(id="mempoolSize" class="text-muted font-weight-light") (#{mempoolBytesData[0]} #{mempoolBytesData[1].abbreviation}B)

div(class=networkSummaryColumnClass)
div(class="float-left", style="height: 40px; width: 40px;")
Expand All @@ -120,9 +121,9 @@ block content

p(class="lead")
span.mr-2.border-dotted(data-toggle="tooltip", title=getblockchaininfo.chainwork.replace(/^0+/, ''))
span #{chainworkData[0]}
span(id="chainworkNum") #{chainworkData[0]}
span x 10
sup #{chainworkData[1].exponent}
sup(id="chainworkExp") #{chainworkData[1].exponent}
span hashes

div(class=networkSummaryColumnClass)
Expand All @@ -136,9 +137,9 @@ block content

p(class="lead")
span.mr-2.border-dotted(data-toggle="tooltip", title=parseFloat(getblockchaininfo.difficulty).toLocaleString())
span #{difficultyData[0]}
span(id="diffNum") #{difficultyData[0]}
span x 10
sup #{difficultyData[1].exponent}
sup(id="diffExp") #{difficultyData[1].exponent}

if (getblockchaininfo.size_on_disk)
div(class=networkSummaryColumnClass)
Expand All @@ -148,7 +149,7 @@ block content
span(class="font-weight-bold") Blockchain Size

- var sizeData = utils.formatLargeNumber(getblockchaininfo.size_on_disk, 2);
p(class="lead") #{sizeData[0]} #{sizeData[1].abbreviation}B
p(id="chainSize" class="lead") #{sizeData[0]} #{sizeData[1].abbreviation}B

if (global.exchangeRates)
div(class=networkSummaryColumnClass)
Expand All @@ -159,9 +160,9 @@ block content
span.font-weight-bold.border-dotted(data-toggle="tooltip", title=("Exchange-rate data from: " + coinConfig.exchangeRateData.jsonUrl)) Exchange Rate

if (global.exchangeRates)
p(class="lead") #{utils.formatExchangedCurrency(1.0, "btc", "฿", 8)}/#{utils.formatExchangedCurrency(1.0, "usd", "$", 6)}
p(id="price" class="lead") #{utils.formatExchangedCurrency(1.0, "btc", "฿", 8)}/#{utils.formatExchangedCurrency(1.0, "usd", "$", 6)}
else
p(class="lead") -
p(id="price" class="lead") -


include includes/tools-card.pug
Expand Down

0 comments on commit c0e9c4a

Please sign in to comment.