ethstats-server/models/node.js

119 lines
2.2 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,
2015-03-27 10:44:07 +01:00
propagation: 0,
2015-03-27 20:05:43 +01:00
received: 0,
transactions: []
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-03-27 11:28:55 +01:00
this.uptime = {
started: null,
history: []
};
if(this.id === null) {
this.uptime.started = (new Date()).getTime();
this.setState(true);
}
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-03-27 11:28:55 +01:00
if(this.uptime.history.length > 0 && this.uptime.history[this.uptime.history.length - 1].status == 'down')
this.setState(true);
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
}
2015-03-27 10:44:07 +01:00
Node.prototype.setStats = function(stats)
{
2015-03-27 12:44:35 +01:00
if(typeof stats !== 'undefined' && typeof stats.block !== 'undefined' && typeof stats.block.number !== 'undefined')
2015-03-27 10:44:07 +01:00
{
2015-03-27 13:22:28 +01:00
if(stats.block.number !== this.stats.block.number) {
2015-03-27 13:19:36 +01:00
stats.block.received = (new Date()).getTime() - stats.block.arrival;
2015-03-27 10:44:07 +01:00
} else {
stats.block.received = this.stats.block.received;
}
this.stats = stats;
return this.getStats();
}
return false;
}
2015-02-17 06:12:44 +01:00
Node.prototype.getStats = function()
{
return {id: this.id, stats: this.stats};
}
2015-03-27 11:28:55 +01:00
Node.prototype.setState = function(active)
{
this.stats.active = active;
this.uptime.history.push({status: (active ? 'up' : 'down'), time: (new Date()).getTime()});
}
module.exports = Node;