parent
5076d6c33f
commit
8954fbae16
@ -0,0 +1,53 @@ |
||||
var _ = require('lodash'); |
||||
var Node = require('./node'); |
||||
|
||||
var Collection = function Collection() |
||||
{ |
||||
this._list = []; |
||||
|
||||
return this; |
||||
} |
||||
|
||||
Collection.prototype.add = function(data) |
||||
{ |
||||
if(typeof data.id == 'undefined') |
||||
return false; |
||||
|
||||
var node = this.getNodeOrNew({ id : data.id }); |
||||
|
||||
node.info = data.info; |
||||
} |
||||
|
||||
Collection.prototype.get = function(id) |
||||
{ |
||||
return this.getNode(id); |
||||
} |
||||
|
||||
Collection.prototype.getIndex = function(search) |
||||
{ |
||||
return _.findIndex(this._list, search); |
||||
|
||||
return (index >= 0 ? index : false); |
||||
} |
||||
|
||||
Collection.prototype.getNode = function(search) |
||||
{ |
||||
var index = this.getIndex(search); |
||||
|
||||
if(index) |
||||
return this._list[index]; |
||||
|
||||
return false; |
||||
} |
||||
|
||||
Collection.prototype.getIndexOrNew = function(search) |
||||
{ |
||||
var index = this.getIndex(search) || this._list.push(new Node()); |
||||
} |
||||
|
||||
Collection.prototype.getNodeOrNew = function(search) |
||||
{ |
||||
return this.getNode(this.getIndexOrNew(search)); |
||||
} |
||||
|
||||
module.exports = Collection; |
@ -1,68 +1,34 @@ |
||||
var geoip = require('geoip-lite'); |
||||
|
||||
var Node = function Node(options, id) |
||||
var Node = function Node() |
||||
{ |
||||
this.options = options; |
||||
this.info = { |
||||
name: options.name, |
||||
ip: options.rpcHost, |
||||
type: options.type, |
||||
os: options.os |
||||
}; |
||||
|
||||
this.info.geo = geoip.lookup(this.info.ip); |
||||
this.info.id = parseInt(id); |
||||
this.info.stats = { |
||||
this.id = null; |
||||
this.info = {}; |
||||
this.geo = {} |
||||
this.stats = { |
||||
active: false, |
||||
peers: 0, |
||||
listening: false, |
||||
mining: false, |
||||
block: { |
||||
number: 0, |
||||
hash: '?', |
||||
timestamp: 0 |
||||
}, |
||||
peers: 0, |
||||
pending: 0, |
||||
gasPrice: 0, |
||||
block: {}, |
||||
blocktimeAvg: 0, |
||||
difficulty: [], |
||||
uptime: { |
||||
down: 0, |
||||
inc: 0, |
||||
total: 0 |
||||
} |
||||
} |
||||
|
||||
this.web3 = require('ethereum.js'); |
||||
}, |
||||
lastUpdate: 0 |
||||
}; |
||||
|
||||
return this; |
||||
} |
||||
|
||||
Node.prototype.update = function() |
||||
Node.prototype.setGeo = function() |
||||
{ |
||||
var sock = new this.web3.providers.HttpSyncProvider('http://' + this.options.rpcHost + ':' + this.options.rpcPort); |
||||
this.web3.setProvider(sock); |
||||
|
||||
var eth = this.web3.eth; |
||||
|
||||
try { |
||||
this.info.stats.peers = eth.peerCount; |
||||
} |
||||
catch (err) { |
||||
this.info.stats.peers = null; |
||||
} |
||||
|
||||
if(this.info.stats.peers != null) { |
||||
this.info.stats.block = eth.block(parseInt(eth.number)); |
||||
if(this.info.stats.block.hash != '?' && typeof this.info.stats.block.difficulty !== 'undefined'){ |
||||
this.info.stats.block.difficulty = this.web3.toDecimal(this.info.stats.block.difficulty); |
||||
} |
||||
this.info.stats.mining = eth.mining; |
||||
this.info.stats.active = true; |
||||
} else { |
||||
this.info.stats.active = false; |
||||
this.info.stats.uptime.down++; |
||||
} |
||||
|
||||
this.info.stats.uptime.inc++; |
||||
this.info.stats.uptime.total = ((this.info.stats.uptime.inc - this.info.stats.uptime.down) / this.info.stats.uptime.inc) * 100; |
||||
|
||||
return this.info; |
||||
}; |
||||
this.geo = geoip.lookup(ip); |
||||
} |
||||
|
||||
module.exports = Node; |
||||
|
Loading…
Reference in new issue