made stats calls async

This commit is contained in:
cubedro 2015-05-04 23:16:02 +03:00
parent b8bbb4150a
commit 28821abc89
2 changed files with 225 additions and 102 deletions

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var web3 = require('web3'); var web3 = require('web3');
var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var os = require('os'); var os = require('os');
var shelljs = require('shelljs'); var shelljs = require('shelljs');
@ -97,7 +98,7 @@ function Node()
this.pingInterval = false; this.pingInterval = false;
this.connectionInterval = false; this.connectionInterval = false;
this._lastLatestLog = 0; this._lastChainLog = 0;
this._lastPendingLog = 0; this._lastPendingLog = 0;
this._called = 0 this._called = 0
@ -293,11 +294,15 @@ Node.prototype.getBlock = function(number)
try { try {
block = web3.eth.getBlock(number, true); block = web3.eth.getBlock(number, true);
console.log('====> block:', block);
if( block.hash != '?' && !_.isUndefined(block.difficulty) ) if( block.hash != '?' && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
{ {
block.difficulty = web3.toDecimal( block.difficulty ); block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
} }
console.log('====> block:', block);
} }
catch (err) { catch (err) {
console.error("getBlock(" + number + "):", err); console.error("getBlock(" + number + "):", err);
@ -309,9 +314,10 @@ Node.prototype.getBlock = function(number)
block = web3.eth.getBlock(number, true); block = web3.eth.getBlock(number, true);
if(block.hash !== '?' && !_.isUndefined(block.difficulty) ) if( block.hash !== '?' && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
{ {
block.difficulty = web3.toDecimal( block.difficulty ); block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
} }
} }
catch (err) { catch (err) {
@ -357,69 +363,168 @@ Node.prototype.uptime = function()
Node.prototype.getStats = function() Node.prototype.getStats = function()
{ {
var start = _.now(); var self = this;
console.log('==> statsstart: ', start);
if(this._socket) if(this._socket)
this._lastStats = JSON.stringify(this.stats); this._lastStats = JSON.stringify(this.stats);
if(this._web3) if(this._web3)
{ {
var blockStart = _.now(); async.parallel({
console.log('==> blockstart: ', blockStart); start: function (callback)
console.log('==> blockstart diff: ', (blockStart - start)); {
callback(null, _.now());
},
peers: function (callback)
{
setTimeout(function () {
var peers;
try {
peers = web3.toDecimal(web3.net.peerCount);
}
catch (err) {
console.error('xx> PeerCount failed: ', err);
callback(err, null);
}
callback(null, peers);
}, 1);
},
pending: function (callback)
{
setTimeout(function() {
try {
web3.eth.getBlockTransactionCount('pending', callback);
}
catch (err) {
console.error('xx> Pending failed: ', err);
callback(err, null);
}
}, 1);
},
mining: function (callback)
{
setTimeout(function () {
var mining;
try {
mining = web3.eth.mining;
}
catch (err) {
console.error('xx> Mining failed: ', err);
callback(err, null);
}
callback(null, mining);
}, 1);
},
hashrate: function (callback)
{
setTimeout(function () {
var hashrate;
try {
hashrate = web3.eth.hashrate;
}
catch (err) {
console.error('xx> Hashrate failed: ', err);
callback(err, null);
}
callback(null, hashrate);
}, 1);
},
gasprice: function (callback)
{
setTimeout(function () {
var gasPrice;
try {
gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10);
}
catch (err) {
console.error('xx> gasPrice failed: ', err);
callback(err, null);
}
callback(null, gasPrice);
}, 1);
}
},
function (err, results)
{
self._tries++;
if (err) {
console.error('xx> getStats error: ', err);
self.stats.peers = 0;
self.stats.active = false;
self.stats.pending = 0;
self.stats.mining = false;
self.stats.hashrate = 0;
self.stats.gasPrice = 0;
self._down++;
return false;
}
results.end = _.now();
results.diff = results.end - results.start;
// console.log('==> Got getStats results: ', results);
console.log('==> Got getStats results in', results.diff, 'ms');
if(results.peers !== null)
{
self.stats.peers = results.peers;
self.stats.active = true;
self.stats.pending = results.pending;
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
self.stats.gasPrice = results.gasPrice;
}
else {
self.stats.peers = 0;
self.stats.active = false;
self.stats.pending = 0;
self.stats.mining = false;
self.stats.hashrate = 0;
self.stats.gasPrice = 0;
self._down++;
}
self.uptime();
self.sendUpdate();
});
}
}
Node.prototype.getStatsBlock = function ()
{
if(this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this._web3)
{
console.log("==> Getting block");
var block = this.getBlock(); var block = this.getBlock();
var blockEnd = _.now();
console.log('==> blockend: ', blockEnd);
console.log('==> block diff: ', (blockEnd - blockStart));
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' ) if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' )
{ {
console.log("==> Got block:", block.number);
this.stats.block = block; this.stats.block = block;
this.sendUpdate();
this.isActive();
if(PENDING_WORKS) {
try
{
this.stats.pending = web3.eth.getBlockTransactionCount('pending');
}
catch (err)
{
PENDING_WORKS = false;
console.error("getBlockTransactionCount('pending'):", err);
}
}
this.stats.mining = web3.eth.mining;
if(this.stats.mining)
{
try {
this.stats.hashrate = web3.eth.hashrate;
}
catch (err)
{
console.error('hashrate: ', err);
this.stats.hashrate = 0;
}
}
else
this.stats.hashrate = 0;
this.stats.gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10);
} }
else else
{ {
console.error("getStats: couldn't fetch block..."); console.error("xx> getStatsBlock: couldn't fetch block...");
} }
} }
this.uptime(); this.uptime();
this.sendUpdate();
var end = _.now();
console.log('==> end: ', end);
console.log('==> stats diff: ', (end - start));
} }
Node.prototype.getHistory = function (range) Node.prototype.getHistory = function (range)
@ -456,17 +561,28 @@ Node.prototype.getHistory = function(range)
return history.reverse(); return history.reverse();
} }
Node.prototype.updateBlock = function()
{
console.log('==> Getting block stats');
this.getStatsBlock();
return this;
};
Node.prototype.update = function()
{
console.log('==> Getting stats');
this.getStats();
return this;
};
Node.prototype.changed = function () Node.prototype.changed = function ()
{ {
var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) ); var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) );
if(this._tries - this._lastSent > 5)
{
this._lastSent = this._tries;
return true;
}
return changed; return changed;
} }
@ -484,30 +600,6 @@ Node.prototype.sendUpdate = function(force)
this.emit('update', this.prepareStats()); this.emit('update', this.prepareStats());
} }
Node.prototype.update = function()
{
this.getStats();
this.sendUpdate();
return this.stats;
};
Node.prototype.updatePending = function()
{
if(PENDING_WORKS)
{
try {
this.stats.pending = web3.eth.getBlockTransactionCount('pending');
this.sendUpdate();
}
catch (err) {
PENDING_WORKS = false;
console.error("getBlockTransactionCount('pending'):", err);
}
}
}
Node.prototype.ping = function() Node.prototype.ping = function()
{ {
this._latency = _.now(); this._latency = _.now();
@ -519,34 +611,63 @@ Node.prototype.setWatches = function()
var self = this; var self = this;
try { try {
this.pendingFilter = web3.eth.filter('chain'); this.chainFilter = web3.eth.filter('chain');
this.pendingFilter.watch( function (log) this.chainFilter.watch( function (log)
{ {
if(PENDING_WORKS) {
var now = _.now(); var now = _.now();
var time = now - self._lastPendingLog; var time = now - self._lastChainLog;
self._lastChainLog = now;
console.log('>>> Chain Filter triggered: ', now);
console.log('>>> Last chain Filter triggered: ', time);
if(time > 50) if(time > 50)
{ {
self.update(); self.updateBlock();
} }
else else
{ {
debounce(function() { debounce(function() {
self.updatePending(); self.updateBlock();
}, 50); }, 50);
} }
self._lastPendingLog = now;
}
}); });
} }
catch (err) catch (err)
{ {
console.error("Couldn't set up pending filter"); console.error("Couldn't set up chain filter");
console.error(err); console.error(err);
} }
// try {
// this.pendingFilter = web3.eth.filter('pending');
// this.pendingFilter.watch( function (log)
// {
// var now = _.now();
// var time = now - self._lastPendingLog;
// console.log('>>> Pending Filter triggered: ', now);
// if(time > 50)
// {
// self.update();
// }
// else
// {
// debounce(function() {
// self.update();
// }, 50);
// }
// self._lastPendingLog = now;
// });
// }
// catch (err)
// {
// console.error("Couldn't set up pending filter");
// console.error(err);
// }
this.updateInterval = setInterval( function(){ this.updateInterval = setInterval( function(){
self.update(); self.update();
}, UPDATE_INTERVAL); }, UPDATE_INTERVAL);
@ -575,6 +696,7 @@ Node.prototype.init = function()
this.startSocketConnection(); this.startSocketConnection();
this.installContract(); this.installContract();
this.setWatches(); this.setWatches();
this.updateBlock();
this.update(); this.update();
} }

View File

@ -8,6 +8,7 @@
"lib": "./lib" "lib": "./lib"
}, },
"dependencies": { "dependencies": {
"async": "^0.9.0",
"debounce": "1.0.0", "debounce": "1.0.0",
"debug": "2.1.3", "debug": "2.1.3",
"lodash": "3.7.0", "lodash": "3.7.0",