check if anything has changed to spare unnecessary requests

This commit is contained in:
cubedro 2015-02-17 06:08:25 +02:00
parent ad9998753c
commit 56716885b7

View File

@ -28,7 +28,7 @@ function Node()
os_v: os.release() os_v: os.release()
}; };
this.info.id = _.camelCase(this.info.name); this.id = _.camelCase(this.info.name);
console.log(this.info); console.log(this.info);
@ -42,13 +42,13 @@ function Node()
block: {}, block: {},
blocktimeAvg: 0, blocktimeAvg: 0,
difficulty: [], difficulty: [],
uptime: { uptime: 0,
down: 0,
inc: 0,
total: 0
},
errors: [] errors: []
} };
this._lastStats = JSON.stringify(this.stats);
this._tries = 0;
this._down = 0;
this.blocks = []; this.blocks = [];
@ -60,8 +60,7 @@ function Node()
web3.setProvider(new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080'))); web3.setProvider(new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')));
socket.on('open', function open() { socket.on('open', function open() {
self._socket = true; socket.emit('hello', { id: self.id, info: self.info});
self.emit('hello', self.info);
console.log('The connection has been opened.'); console.log('The connection has been opened.');
}).on('end', function end() { }).on('end', function end() {
self._socket = false; self._socket = false;
@ -73,6 +72,17 @@ function Node()
console.log('Received some data', data); console.log('Received some data', data);
}); });
socket.on('ready', function()
{
if(self.changed())
{
self._socket = true;
self.sendUpdate();
}
console.log('The connection has been established.');
})
this.init(); this.init();
return this; return this;
@ -80,7 +90,7 @@ function Node()
Node.prototype.isActive = function() Node.prototype.isActive = function()
{ {
this.stats.uptime.inc++; this._tries++;
this.stats.errors = []; this.stats.errors = [];
try { try {
@ -94,7 +104,7 @@ Node.prototype.isActive = function()
this.stats.listening = false; this.stats.listening = false;
this.stats.mining = false; this.stats.mining = false;
this.stats.peers = 0; this.stats.peers = 0;
this.stats.uptime.down++; this._down++;
this.stats.errors.push({ this.stats.errors.push({
code: '1', code: '1',
@ -219,11 +229,14 @@ Node.prototype.difficultyChart = function()
Node.prototype.uptime = function() Node.prototype.uptime = function()
{ {
this.stats.uptime.total = ((this.stats.uptime.inc - this.stats.uptime.down) / this.stats.uptime.inc) * 100; this.stats.uptime = ((this._tries - this._down) / this._tries) * 100;
} }
Node.prototype.getStats = function() Node.prototype.getStats = function()
{ {
if(this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this.isActive()) if(this.isActive())
{ {
this.stats.block = this.getBlock(); this.stats.block = this.getBlock();
@ -240,19 +253,32 @@ Node.prototype.getStats = function()
this.uptime(); this.uptime();
} }
Node.prototype.changed = function()
{
var changed = ! _.isEqual(this._lastStats, JSON.stringify(this.stats));
console.log(changed);
return changed;
}
Node.prototype.prepareStats = function() Node.prototype.prepareStats = function()
{ {
return { return {
id: this.info.id, id: this.id,
stats: this.stats stats: this.stats
}; };
} }
Node.prototype.sendUpdate = function()
{
if(this.changed())
this.emit('update', this.prepareStats());
}
Node.prototype.update = function() Node.prototype.update = function()
{ {
this.getStats(); this.getStats();
this.emit('update', this.prepareStats()); this.sendUpdate();
return this.stats; return this.stats;
}; };