ethstats-server/models/node.js

81 lines
1.3 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,
2015-02-23 18:10:17 +01:00
timestamp: 0,
arrival: 0,
propagation: 0
2015-02-17 10:30:41 +01:00
},
2015-02-17 03:04:15 +01:00
blocktimeAvg: 0,
2015-02-23 15:48:00 +01:00
blockTimes: [],
2015-02-17 03:04:15 +01:00
difficulty: [],
2015-02-23 14:57:41 +01:00
latency: 0,
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-02-18 07:06:41 +01:00
if(typeof data.spark !== 'undefined')
this.spark = data.spark;
2015-02-23 15:20:05 +01:00
if(typeof data.latency !== 'undefined')
this.stats.latency = data.latency;
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-18 07:06:41 +01:00
if(typeof data.spark !== 'undefined')
this.spark = data.spark;
2015-02-17 10:30:41 +01:00
}
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;