check if anything has changed to spare unnecessary requests
This commit is contained in:
parent
ad9998753c
commit
56716885b7
54
lib/node.js
54
lib/node.js
@ -28,7 +28,7 @@ function Node()
|
||||
os_v: os.release()
|
||||
};
|
||||
|
||||
this.info.id = _.camelCase(this.info.name);
|
||||
this.id = _.camelCase(this.info.name);
|
||||
|
||||
console.log(this.info);
|
||||
|
||||
@ -42,13 +42,13 @@ function Node()
|
||||
block: {},
|
||||
blocktimeAvg: 0,
|
||||
difficulty: [],
|
||||
uptime: {
|
||||
down: 0,
|
||||
inc: 0,
|
||||
total: 0
|
||||
},
|
||||
uptime: 0,
|
||||
errors: []
|
||||
}
|
||||
};
|
||||
this._lastStats = JSON.stringify(this.stats);
|
||||
|
||||
this._tries = 0;
|
||||
this._down = 0;
|
||||
|
||||
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')));
|
||||
|
||||
socket.on('open', function open() {
|
||||
self._socket = true;
|
||||
self.emit('hello', self.info);
|
||||
socket.emit('hello', { id: self.id, info: self.info});
|
||||
console.log('The connection has been opened.');
|
||||
}).on('end', function end() {
|
||||
self._socket = false;
|
||||
@ -73,6 +72,17 @@ function Node()
|
||||
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();
|
||||
|
||||
return this;
|
||||
@ -80,7 +90,7 @@ function Node()
|
||||
|
||||
Node.prototype.isActive = function()
|
||||
{
|
||||
this.stats.uptime.inc++;
|
||||
this._tries++;
|
||||
this.stats.errors = [];
|
||||
|
||||
try {
|
||||
@ -94,7 +104,7 @@ Node.prototype.isActive = function()
|
||||
this.stats.listening = false;
|
||||
this.stats.mining = false;
|
||||
this.stats.peers = 0;
|
||||
this.stats.uptime.down++;
|
||||
this._down++;
|
||||
|
||||
this.stats.errors.push({
|
||||
code: '1',
|
||||
@ -219,11 +229,14 @@ Node.prototype.difficultyChart = 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()
|
||||
{
|
||||
if(this._socket)
|
||||
this._lastStats = JSON.stringify(this.stats);
|
||||
|
||||
if(this.isActive())
|
||||
{
|
||||
this.stats.block = this.getBlock();
|
||||
@ -240,19 +253,32 @@ Node.prototype.getStats = function()
|
||||
this.uptime();
|
||||
}
|
||||
|
||||
Node.prototype.changed = function()
|
||||
{
|
||||
var changed = ! _.isEqual(this._lastStats, JSON.stringify(this.stats));
|
||||
console.log(changed);
|
||||
return changed;
|
||||
}
|
||||
|
||||
Node.prototype.prepareStats = function()
|
||||
{
|
||||
return {
|
||||
id: this.info.id,
|
||||
id: this.id,
|
||||
stats: this.stats
|
||||
};
|
||||
}
|
||||
|
||||
Node.prototype.sendUpdate = function()
|
||||
{
|
||||
if(this.changed())
|
||||
this.emit('update', this.prepareStats());
|
||||
}
|
||||
|
||||
Node.prototype.update = function()
|
||||
{
|
||||
this.getStats();
|
||||
|
||||
this.emit('update', this.prepareStats());
|
||||
this.sendUpdate();
|
||||
|
||||
return this.stats;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user