|
|
@ -1,6 +1,8 @@ |
|
|
|
var redis = require('redis'); |
|
|
|
var async = require('async'); |
|
|
|
|
|
|
|
var filterIterate = require('./filterIterate.js'); |
|
|
|
|
|
|
|
var stats = require('./stats.js'); |
|
|
|
|
|
|
|
module.exports = function(logger, portalConfig, poolConfigs){ |
|
|
@ -15,8 +17,13 @@ module.exports = function(logger, portalConfig, poolConfigs){ |
|
|
|
|
|
|
|
switch(req.params.method){ |
|
|
|
case 'stats': |
|
|
|
//Anonymize
|
|
|
|
let anonymizeJSON = JSON.parse(portalStats.statsString); |
|
|
|
for (pool in anonymizeJSON.pools) { |
|
|
|
filterIterate(anonymizeJSON.pools[pool].workers, {key: true, prop: ['name']}, 'miner-'); |
|
|
|
} |
|
|
|
res.header('Content-Type', 'application/json'); |
|
|
|
res.end(portalStats.statsString); |
|
|
|
res.end(JSON.stringify(anonymizeJSON)); |
|
|
|
return; |
|
|
|
case 'pool_stats': |
|
|
|
res.header('Content-Type', 'application/json'); |
|
|
@ -25,97 +32,105 @@ module.exports = function(logger, portalConfig, poolConfigs){ |
|
|
|
case 'blocks': |
|
|
|
case 'getblocksstats': |
|
|
|
portalStats.getBlocks(function(data){ |
|
|
|
//Anonymize
|
|
|
|
filterIterate(data, {split:{by:':', index:3}}, 'miner-'); |
|
|
|
res.header('Content-Type', 'application/json'); |
|
|
|
res.end(JSON.stringify(data)); |
|
|
|
res.end(JSON.stringify(data)); |
|
|
|
}); |
|
|
|
break; |
|
|
|
case 'payments': |
|
|
|
var poolBlocks = []; |
|
|
|
for(var pool in portalStats.stats.pools) { |
|
|
|
filterIterate(portalStats.stats.pools[pool].pending.blocks, {split:{by:':', index:3}}, 'miner-'); |
|
|
|
for (payment in portalStats.stats.pools[pool].payments) { |
|
|
|
filterIterate(portalStats.stats.pools[pool].payments[payment].amounts, {key: true}, 'miner-', ); |
|
|
|
filterIterate(portalStats.stats.pools[pool].payments[payment].balances, {key: true}, 'miner-', ); |
|
|
|
filterIterate(portalStats.stats.pools[pool].payments[payment].work, {key: true}, 'miner-', ); |
|
|
|
} |
|
|
|
poolBlocks.push({name: pool, pending: portalStats.stats.pools[pool].pending, payments: portalStats.stats.pools[pool].payments}); |
|
|
|
} |
|
|
|
res.header('Content-Type', 'application/json'); |
|
|
|
res.end(JSON.stringify(poolBlocks)); |
|
|
|
return; |
|
|
|
case 'worker_stats': |
|
|
|
res.header('Content-Type', 'application/json'); |
|
|
|
if (req.url.indexOf("?")>0) { |
|
|
|
var url_parms = req.url.split("?"); |
|
|
|
if (url_parms.length > 0) { |
|
|
|
var history = {}; |
|
|
|
var workers = {}; |
|
|
|
var address = url_parms[1] || null; |
|
|
|
//res.end(portalStats.getWorkerStats(address));
|
|
|
|
if (address != null && address.length > 0) { |
|
|
|
// make sure it is just the miners address
|
|
|
|
address = address.split(".")[0]; |
|
|
|
// get miners balance along with worker balances
|
|
|
|
portalStats.getBalanceByAddress(address, function(balances) { |
|
|
|
// get current round share total
|
|
|
|
portalStats.getTotalSharesByAddress(address, function(shares) { |
|
|
|
var totalHash = parseFloat(0.0); |
|
|
|
var totalShares = shares; |
|
|
|
var networkSols = 0; |
|
|
|
for (var h in portalStats.statHistory) { |
|
|
|
for(var pool in portalStats.statHistory[h].pools) { |
|
|
|
for(var w in portalStats.statHistory[h].pools[pool].workers){ |
|
|
|
if (w.startsWith(address)) { |
|
|
|
if (history[w] == null) { |
|
|
|
history[w] = []; |
|
|
|
} |
|
|
|
if (portalStats.statHistory[h].pools[pool].workers[w].hashrate) { |
|
|
|
history[w].push({time: portalStats.statHistory[h].time, hashrate:portalStats.statHistory[h].pools[pool].workers[w].hashrate}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// order check...
|
|
|
|
//console.log(portalStats.statHistory[h].time);
|
|
|
|
} |
|
|
|
} |
|
|
|
for(var pool in portalStats.stats.pools) { |
|
|
|
for(var w in portalStats.stats.pools[pool].workers){ |
|
|
|
if (w.startsWith(address)) { |
|
|
|
workers[w] = portalStats.stats.pools[pool].workers[w]; |
|
|
|
for (var b in balances.balances) { |
|
|
|
if (w == balances.balances[b].worker) { |
|
|
|
case 'worker_stats': |
|
|
|
res.header('Content-Type', 'application/json'); |
|
|
|
if (req.url.indexOf("?")>0) { |
|
|
|
var url_parms = req.url.split("?"); |
|
|
|
if (url_parms.length > 0) { |
|
|
|
var history = {}; |
|
|
|
var workers = {}; |
|
|
|
var address = url_parms[1] || null; |
|
|
|
//res.end(portalStats.getWorkerStats(address));
|
|
|
|
if (address != null && address.length > 0) { |
|
|
|
// make sure it is just the miners address
|
|
|
|
address = address.split(".")[0]; |
|
|
|
// get miners balance along with worker balances
|
|
|
|
portalStats.getBalanceByAddress(address, function(balances) { |
|
|
|
// get current round share total
|
|
|
|
portalStats.getTotalSharesByAddress(address, function(shares) { |
|
|
|
var totalHash = parseFloat(0.0); |
|
|
|
var totalShares = shares; |
|
|
|
var networkSols = 0; |
|
|
|
for (var h in portalStats.statHistory) { |
|
|
|
for(var pool in portalStats.statHistory[h].pools) { |
|
|
|
for(var w in portalStats.statHistory[h].pools[pool].workers){ |
|
|
|
if (w.startsWith(address)) { |
|
|
|
if (history[w] == null) { |
|
|
|
history[w] = []; |
|
|
|
} |
|
|
|
if (portalStats.statHistory[h].pools[pool].workers[w].hashrate) { |
|
|
|
history[w].push({time: portalStats.statHistory[h].time, hashrate:portalStats.statHistory[h].pools[pool].workers[w].hashrate}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// order check...
|
|
|
|
//console.log(portalStats.statHistory[h].time);
|
|
|
|
} |
|
|
|
} |
|
|
|
for(var pool in portalStats.stats.pools) { |
|
|
|
for(var w in portalStats.stats.pools[pool].workers){ |
|
|
|
if (w.startsWith(address)) { |
|
|
|
workers[w] = portalStats.stats.pools[pool].workers[w]; |
|
|
|
for (var b in balances.balances) { |
|
|
|
if (w == balances.balances[b].worker) { |
|
|
|
workers[w].paid = balances.balances[b].paid; |
|
|
|
workers[w].balance = balances.balances[b].balance; |
|
|
|
} |
|
|
|
} |
|
|
|
workers[w].balance = (workers[w].balance || 0); |
|
|
|
workers[w].paid = (workers[w].paid || 0); |
|
|
|
totalHash += portalStats.stats.pools[pool].workers[w].hashrate; |
|
|
|
networkSols = portalStats.stats.pools[pool].poolStats.networkSols; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
res.end(JSON.stringify({miner: address, totalHash: totalHash, totalShares: totalShares, networkSols: networkSols, immature: balances.totalImmature, balance: balances.totalHeld, paid: balances.totalPaid, workers: workers, history: history})); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
res.end(JSON.stringify({result: "error"})); |
|
|
|
} |
|
|
|
} else { |
|
|
|
res.end(JSON.stringify({result: "error"})); |
|
|
|
} |
|
|
|
} else { |
|
|
|
res.end(JSON.stringify({result: "error"})); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
workers[w].balance = (workers[w].balance || 0); |
|
|
|
workers[w].paid = (workers[w].paid || 0); |
|
|
|
totalHash += portalStats.stats.pools[pool].workers[w].hashrate; |
|
|
|
networkSols = portalStats.stats.pools[pool].poolStats.networkSols; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
res.end(JSON.stringify({miner: address, totalHash: totalHash, totalShares: totalShares, networkSols: networkSols, immature: balances.totalImmature, balance: balances.totalHeld, paid: balances.totalPaid, workers: workers, history: history})); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
res.end(JSON.stringify({result: "error"})); |
|
|
|
} |
|
|
|
} else { |
|
|
|
res.end(JSON.stringify({result: "error"})); |
|
|
|
} |
|
|
|
} else { |
|
|
|
res.end(JSON.stringify({result: "error"})); |
|
|
|
} |
|
|
|
return; |
|
|
|
case 'live_stats': |
|
|
|
res.writeHead(200, { |
|
|
|
'Content-Type': 'text/event-stream', |
|
|
|
'Cache-Control': 'no-cache', |
|
|
|
'Connection': 'keep-alive' |
|
|
|
}); |
|
|
|
res.write('\n'); |
|
|
|
var uid = Math.random().toString(); |
|
|
|
_this.liveStatConnections[uid] = res; |
|
|
|
res.flush(); |
|
|
|
req.on("close", function() { |
|
|
|
delete _this.liveStatConnections[uid]; |
|
|
|
}); |
|
|
|
return; |
|
|
|
res.writeHead(200, { |
|
|
|
'Content-Type': 'text/event-stream', |
|
|
|
'Cache-Control': 'no-cache', |
|
|
|
'Connection': 'keep-alive' |
|
|
|
}); |
|
|
|
res.write('\n'); |
|
|
|
var uid = Math.random().toString(); |
|
|
|
_this.liveStatConnections[uid] = res; |
|
|
|
res.flush(); |
|
|
|
req.on("close", function() { |
|
|
|
delete _this.liveStatConnections[uid]; |
|
|
|
}); |
|
|
|
return; |
|
|
|
default: |
|
|
|
next(); |
|
|
|
} |
|
|
|