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 redisClients = [];
|
|
|
|
|
2014-03-19 23:43:15 -07:00
|
|
|
var algoMultipliers = {
|
2014-03-25 15:21:17 -07:00
|
|
|
'x11': Math.pow(2, 16),
|
2014-03-19 23:43:15 -07:00
|
|
|
'scrypt': Math.pow(2, 16),
|
2014-03-25 15:21:17 -07:00
|
|
|
'scrypt-jane': Math.pow(2,16),
|
2014-03-19 23:43:15 -07:00
|
|
|
'sha256': Math.pow(2, 32)
|
|
|
|
};
|
|
|
|
|
2014-03-19 13:24:29 -07:00
|
|
|
Object.keys(poolConfigs).forEach(function(coin){
|
|
|
|
var poolConfig = poolConfigs[coin];
|
|
|
|
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
|
|
|
|
|
|
|
|
2014-03-25 15:21:17 -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){
|
2014-03-19 23:43:15 -07:00
|
|
|
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,
|
2014-03-25 11:52:11 -07:00
|
|
|
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];
|
2014-03-19 23:43:15 -07:00
|
|
|
var hashratePre = shareMultiplier * coinStats.shares / portalConfig.website.hashrateWindow;
|
|
|
|
coinStats.hashrate = hashratePre / 1e3 | 0;
|
2014-03-19 13:24:29 -07:00
|
|
|
delete coinStats.hashrates;
|
2014-03-22 12:58:51 -07:00
|
|
|
delete coinStats.shares;
|
2014-03-19 13:24:29 -07:00
|
|
|
portalStats.global.hashrate += coinStats.hashrate;
|
|
|
|
portalStats.global.workers += Object.keys(coinStats.workers).length;
|
|
|
|
});
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|