ethstats-server/models/node.js

69 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-12-04 22:35:09 +01:00
var geoip = require('geoip-lite');
2015-02-17 06:12:44 +01:00
var Node = function Node(data)
2015-01-20 19:29:31 +01:00
{
2015-02-17 03:04:15 +01:00
this.id = null;
this.info = {};
this.geo = {}
this.stats = {
2015-01-20 19:29:31 +01:00
active: false,
2015-02-17 03:04:15 +01:00
listening: false,
2015-01-20 19:29:31 +01:00
mining: false,
2015-02-17 03:04:15 +01:00
peers: 0,
pending: 0,
gasPrice: 0,
2015-02-17 10:30:41 +01:00
block: {
difficulty: 0,
number: 0,
gasLimit: 0,
timestamp: 0,
blocktime: 0
},
2015-02-17 03:04:15 +01:00
blocktimeAvg: 0,
difficulty: [],
2015-02-17 06:12:44 +01:00
uptime: 0,
2015-02-17 03:04:15 +01:00
lastUpdate: 0
};
2015-01-20 19:29:31 +01:00
2015-02-17 06:12:44 +01:00
if(typeof data.id !== 'undefined')
this.id = data.id;
if(typeof data.info !== 'undefined')
this.info = data.info;
2015-02-17 10:30:41 +01:00
if(typeof data.ip !== 'undefined'){
this.info.ip = data.ip;
2015-02-17 06:12:44 +01:00
this.setGeo(data.ip);
2015-02-17 10:30:41 +01:00
}
2015-02-17 06:12:44 +01:00
2015-01-20 19:29:31 +01:00
return this;
}
2015-02-17 06:12:44 +01:00
Node.prototype.setGeo = function(ip)
2015-01-20 19:29:31 +01:00
{
2015-02-17 03:04:15 +01:00
this.geo = geoip.lookup(ip);
}
2014-12-04 22:35:09 +01:00
2015-02-17 10:30:41 +01:00
Node.prototype.setInfo = function(data)
{
if(typeof data.info !== 'undefined')
this.info = data.info;
if(typeof data.ip !== 'undefined'){
this.info.ip = data.ip;
this.setGeo(data.ip);
}
}
2015-02-17 06:12:44 +01:00
Node.prototype.getInfo = function()
{
2015-02-17 10:30:41 +01:00
return {id: this.id, info: this.info, geo: this.geo, stats: this.stats};
2015-02-17 06:12:44 +01:00
}
Node.prototype.getStats = function()
{
return {id: this.id, stats: this.stats};
}
module.exports = Node;