More stats added

This commit is contained in:
Matt 2014-03-22 13:58:51 -06:00
parent 02fbf69896
commit ceb6f0dae0
5 changed files with 48 additions and 19 deletions

View File

@ -374,7 +374,7 @@ BTC: 1KRotMnQpxu3sePQnsVLRy3EraRFYfJQFR
Credits Credits
------- -------
* [vekexasia](https://github.com/vekexasia) - co-developer & great tester * [vekexasia](https://github.com/vekexasia) - co-developer & great tester
* [TheSeven](https://github.com/TheSeven) - answering an absurd amount of my questions and being a very helpful and king gentleman * [TheSeven](https://github.com/TheSeven) - answering an absurd amount of my questions and being a very helpful gentleman
* Those that contributed to [node-stratum](/zone117x/node-stratum) * Those that contributed to [node-stratum](/zone117x/node-stratum)

1
libs/api.js Normal file
View File

@ -0,0 +1 @@
//create the stats object in here. then let the website use this object. that way we can have a config for stats and website separate :D

View File

@ -335,7 +335,7 @@ function SetupForPool(logger, poolOptions){
); );
finalRedisCommands.push(deleteRoundsCommand); finalRedisCommands.push(deleteRoundsCommand);
finalRedisCommands.push(['hincrby', coin + '_stats', 'totalPaid', toBePaid]); finalRedisCommands.push(['hincrby', coin + '_stats', 'totalPaid', toBePaid / magnitude]);
callback(null, magnitude, workerPayments, finalRedisCommands); callback(null, magnitude, workerPayments, finalRedisCommands);
@ -356,18 +356,21 @@ function SetupForPool(logger, poolOptions){
console.log(JSON.stringify(workerPayments, null, 4)); console.log(JSON.stringify(workerPayments, null, 4));
console.log(JSON.stringify(sendManyCmd, null, 4)); console.log(JSON.stringify(sendManyCmd, null, 4));
//return callback('not yet...');
daemon.cmd('sendmany', sendManyCmd, function(results){ daemon.cmd('sendmany', sendManyCmd, function(results){
if (results[0].error){ if (results[0].error){
callback('done - error with sendmany ' + JSON.stringify(results[0].error)); callback('done - error with sendmany ' + JSON.stringify(results[0].error));
return; return;
} }
//This does the final all-or-nothing atom transaction if block deamon sent payments
redisClient.multi(finalRedisCommands).exec(function(error, results){ redisClient.multi(finalRedisCommands).exec(function(error, results){
if (error){ if (error){
callback('done - error with final redis commands for cleaning up ' + JSON.stringify(error)); callback('done - error with final redis commands for cleaning up ' + JSON.stringify(error));
return; return;
} }
callback(null, 'Payments sent'); var totalWorkers = Object.keys(workerPayments).length;
var totalAmount = Object.keys(workerPayments).reduce(function(p, c){return p + workerPayments[c]}, 0);
callback(null, 'Payments sent, a total of ' + totalAmount + ' was sent to ' + totalWorkers);
}); });
}); });

View File

@ -35,26 +35,37 @@ module.exports = function(logger, portalConfig, poolConfigs){
this.stats = {}; this.stats = {};
this.statsString = '';
this.getStats = function(callback){ this.getStats = function(callback){
var allCoinStats = []; var allCoinStats = {};
async.each(redisClients, function(client, callback){ async.each(redisClients, function(client, callback){
var windowTime = (((Date.now() / 1000) - portalConfig.website.hashrateWindow) | 0).toString(); var windowTime = (((Date.now() / 1000) - portalConfig.website.hashrateWindow) | 0).toString();
var redisCommands = []; var redisCommands = [];
var commandsPerCoin = 4;
//Clear out old hashrate stats for each coin from redis
client.coins.forEach(function(coin){ var redisComamndTemplates = [
redisCommands.push(['zremrangebyscore', coin + '_hashrate', '-inf', '(' + windowTime]); ['zremrangebyscore', '_hashrate', '-inf', '(' + windowTime],
redisCommands.push(['zrangebyscore', coin + '_hashrate', windowTime, '+inf']); ['zrangebyscore', '_hashrate', windowTime, '+inf'],
redisCommands.push(['hgetall', coin + '_stats']); ['hgetall', '_stats'],
redisCommands.push(['scard', coin + '_blocksPending']); ['scard', '_blocksPending'],
['scard', '_blocksConfirmed'],
['scard', '_blocksOrphaned']
];
var commandsPerCoin = redisComamndTemplates.length;
client.coins.map(function(coin){
redisComamndTemplates.map(function(t){
var clonedTemplates = t.slice(0);
clonedTemplates[1] = coin + clonedTemplates [1];
redisCommands.push(clonedTemplates);
});
}); });
client.client.multi(redisCommands).exec(function(err, replies){ client.client.multi(redisCommands).exec(function(err, replies){
if (err){ if (err){
console.log('error with getting hashrate stats ' + JSON.stringify(err)); console.log('error with getting hashrate stats ' + JSON.stringify(err));
@ -62,14 +73,20 @@ module.exports = function(logger, portalConfig, poolConfigs){
} }
else{ else{
for(var i = 0; i < replies.length; i += commandsPerCoin){ for(var i = 0; i < replies.length; i += commandsPerCoin){
var coinName = client.coins[i / commandsPerCoin | 0];
var coinStats = { var coinStats = {
coinName: client.coins[i / commandsPerCoin | 0], name: coinName,
symbol: poolConfigs[coinName].coin.symbol,
algorithm: poolConfigs[coinName].coin.algorithm,
hashrates: replies[i + 1], hashrates: replies[i + 1],
poolStats: replies[i + 2], poolStats: replies[i + 2],
poolPendingBlocks: replies[i + 3] blocks: {
pending: replies[i + 3],
confirmed: replies[i + 4],
orphaned: replies[i + 5]
}
}; };
allCoinStats.push(coinStats) allCoinStats[coinStats.name] = (coinStats);
} }
callback(); callback();
} }
@ -89,7 +106,8 @@ module.exports = function(logger, portalConfig, poolConfigs){
pools: allCoinStats pools: allCoinStats
}; };
allCoinStats.forEach(function(coinStats){ Object.keys(allCoinStats).forEach(function(coin){
var coinStats = allCoinStats[coin];
coinStats.workers = {}; coinStats.workers = {};
coinStats.shares = 0; coinStats.shares = 0;
coinStats.hashrates.forEach(function(ins){ coinStats.hashrates.forEach(function(ins){
@ -102,14 +120,18 @@ module.exports = function(logger, portalConfig, poolConfigs){
else else
coinStats.workers[worker] = workerShares coinStats.workers[worker] = workerShares
}); });
var shareMultiplier = algoMultipliers[poolConfigs[coinStats.coinName].coin.algorithm]; var shareMultiplier = algoMultipliers[coinStats.algorithm];
var hashratePre = shareMultiplier * coinStats.shares / portalConfig.website.hashrateWindow; var hashratePre = shareMultiplier * coinStats.shares / portalConfig.website.hashrateWindow;
coinStats.hashrate = hashratePre / 1e3 | 0; coinStats.hashrate = hashratePre / 1e3 | 0;
delete coinStats.hashrates; delete coinStats.hashrates;
delete coinStats.shares;
portalStats.global.hashrate += coinStats.hashrate; portalStats.global.hashrate += coinStats.hashrate;
portalStats.global.workers += Object.keys(coinStats.workers).length; portalStats.global.workers += Object.keys(coinStats.workers).length;
}); });
_this.stats = portalStats; _this.stats = portalStats;
_this.statsString = JSON.stringify(portalStats);
console.log(JSON.stringify(portalStats, null, 4));
callback(); callback();
}); });

View File

@ -183,6 +183,9 @@ module.exports = function(logger){
res.end(requestedPage); res.end(requestedPage);
return; return;
} }
case 'stats':
res.end(portalStats.statsString);
return;
case 'live_stats': case 'live_stats':
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'text/event-stream', 'Content-Type': 'text/event-stream',