z-nomp/libs/stats.js

156 lines
5.3 KiB
JavaScript
Raw Normal View History

2014-03-19 13:24:29 -07:00
var redis = require('redis');
var async = require('async');
var os = require('os');
module.exports = function(logger, portalConfig, poolConfigs){
var _this = this;
var logSystem = 'Stats';
2014-03-19 13:24:29 -07:00
var redisClients = [];
var algoMultipliers = {
'x11': Math.pow(2, 16),
'scrypt': Math.pow(2, 16),
'scrypt-jane': Math.pow(2,16),
'sha256': Math.pow(2, 32)
};
var canDoStats = true;
2014-03-19 13:24:29 -07:00
Object.keys(poolConfigs).forEach(function(coin){
if (!canDoStats) return;
2014-03-19 13:24:29 -07:00
var poolConfig = poolConfigs[coin];
if (!poolConfig.shareProcessing || !poolConfig.shareProcessing.internal){
logger.error(logSystem, coin, 'Cannot do stats without internal share processing setup');
canDoStats = false;
return;
}
2014-03-19 13:24:29 -07:00
var internalConfig = poolConfig.shareProcessing.internal;
var redisConfig = internalConfig.redis;
for (var i = 0; i < redisClients.length; i++){
var client = redisClients[i];
if (client.client.port === redisConfig.port && client.client.host === redisConfig.host){
client.coins.push(coin);
return;
}
}
redisClients.push({
coins: [coin],
client: redis.createClient(redisConfig.port, redisConfig.host)
});
});
this.stats = {};
2014-03-22 12:58:51 -07:00
this.statsString = '';
2014-03-19 13:24:29 -07:00
this.getGlobalStats = function(callback){
2014-03-19 13:24:29 -07:00
2014-03-22 12:58:51 -07:00
var allCoinStats = {};
2014-03-19 13:24:29 -07:00
async.each(redisClients, function(client, callback){
var windowTime = (((Date.now() / 1000) - portalConfig.website.hashrateWindow) | 0).toString();
2014-03-19 13:24:29 -07:00
var redisCommands = [];
2014-03-22 12:58:51 -07:00
var redisComamndTemplates = [
['zremrangebyscore', '_hashrate', '-inf', '(' + windowTime],
['zrangebyscore', '_hashrate', windowTime, '+inf'],
['hgetall', '_stats'],
['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);
});
});
2014-03-19 13:24:29 -07:00
client.client.multi(redisCommands).exec(function(err, replies){
if (err){
console.log('error with getting hashrate stats ' + JSON.stringify(err));
callback(err);
}
else{
for(var i = 0; i < replies.length; i += commandsPerCoin){
2014-03-22 12:58:51 -07:00
var coinName = client.coins[i / commandsPerCoin | 0];
2014-03-19 13:24:29 -07:00
var coinStats = {
2014-03-22 12:58:51 -07:00
name: coinName,
symbol: poolConfigs[coinName].coin.symbol.toUpperCase(),
2014-03-22 12:58:51 -07:00
algorithm: poolConfigs[coinName].coin.algorithm,
2014-03-19 13:24:29 -07:00
hashrates: replies[i + 1],
poolStats: replies[i + 2],
2014-03-22 12:58:51 -07:00
blocks: {
pending: replies[i + 3],
confirmed: replies[i + 4],
orphaned: replies[i + 5]
}
2014-03-19 13:24:29 -07:00
};
2014-03-22 12:58:51 -07:00
allCoinStats[coinStats.name] = (coinStats);
2014-03-19 13:24:29 -07:00
}
callback();
}
});
}, function(err){
if (err){
console.log('error getting all stats' + JSON.stringify(err));
callback();
return;
}
var portalStats = {
global:{
workers: 0,
hashrate: 0
},
pools: allCoinStats
};
2014-03-22 12:58:51 -07:00
Object.keys(allCoinStats).forEach(function(coin){
var coinStats = allCoinStats[coin];
2014-03-19 13:24:29 -07:00
coinStats.workers = {};
coinStats.shares = 0;
coinStats.hashrates.forEach(function(ins){
var parts = ins.split(':');
var workerShares = parseInt(parts[0]);
coinStats.shares += workerShares;
var worker = parts[1];
if (worker in coinStats.workers)
coinStats.workers[worker] += workerShares
else
coinStats.workers[worker] = workerShares
});
2014-03-22 12:58:51 -07:00
var shareMultiplier = algoMultipliers[coinStats.algorithm];
var hashratePre = shareMultiplier * coinStats.shares / portalConfig.website.hashrateWindow;
coinStats.hashrate = hashratePre / 1e3 | 0;
2014-03-19 13:24:29 -07:00
portalStats.global.hashrate += coinStats.hashrate;
portalStats.global.workers += Object.keys(coinStats.workers).length;
2014-03-26 19:53:30 -07:00
delete coinStats.hashrates;
delete coinStats.shares;
2014-03-19 13:24:29 -07:00
});
2014-03-22 12:58:51 -07:00
2014-03-19 13:24:29 -07:00
_this.stats = portalStats;
2014-03-22 12:58:51 -07:00
_this.statsString = JSON.stringify(portalStats);
2014-03-19 13:24:29 -07:00
callback();
});
};
};