nodes model refactoring

This commit is contained in:
cubedro 2015-06-03 03:51:19 +03:00
parent b0117b2490
commit e7408a3594
7 changed files with 217 additions and 141 deletions

24
app.js
View File

@ -88,6 +88,7 @@ api.on('connection', function (spark)
{
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Open:', spark.address.ip);
spark.on('hello', function (data)
{
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Hello', data['id']);
@ -104,7 +105,7 @@ api.on('connection', function (spark)
{
data.ip = spark.address.ip;
data.spark = spark.id;
data.latency = spark.latency;
data.latency = spark.latency || 0;
var info = Nodes.add( data );
spark.emit('ready');
@ -128,6 +129,7 @@ api.on('connection', function (spark)
}
});
spark.on('update', function (data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
@ -161,6 +163,7 @@ api.on('connection', function (spark)
}
});
spark.on('block', function (data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
@ -193,6 +196,7 @@ api.on('connection', function (spark)
}
});
spark.on('pending', function (data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
@ -215,6 +219,7 @@ api.on('connection', function (spark)
}
});
spark.on('stats', function (data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
@ -238,6 +243,7 @@ api.on('connection', function (spark)
}
});
spark.on('history', function (data)
{
console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Got from:', data.id);
@ -256,6 +262,7 @@ api.on('connection', function (spark)
});
spark.on('node-ping', function (data)
{
var start = (!_.isUndefined(data) && !_.isUndefined(data.clientTime) ? data.clientTime : null);
@ -268,16 +275,20 @@ api.on('connection', function (spark)
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Ping from:', data['id']);
});
spark.on('latency', function (data)
{
if( !_.isUndefined(data.id) )
{
var latency = Nodes.updateLatency(data.id, data.latency);
client.write({
action: 'latency',
data: latency
});
if(latency)
{
client.write({
action: 'latency',
data: latency
});
}
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Latency:', latency, 'from:', data.id);
@ -294,6 +305,7 @@ api.on('connection', function (spark)
}
});
spark.on('end', function (data)
{
var stats = Nodes.inactive(spark.id);
@ -307,6 +319,8 @@ api.on('connection', function (spark)
});
});
client.on('connection', function (clientSpark)
{
clientSpark.on('ready', function (data)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -167,9 +167,7 @@ Collection.prototype.removeOldNodes = function()
for(var i = this._items.length - 1; i >= 0; i--)
{
var node = this._items[i];
if( node.isInactiveAndOld() )
if( this._items[i].isInactiveAndOld() )
{
deleteList.push(i);
}

View File

@ -386,7 +386,7 @@ History.prototype.getMinersCount = function()
.value();
}
History.prototype.getCharts = function()
History.prototype.getCharts = function(callback)
{
var chartHistory = _( this._items )
.sortByOrder( 'height', false )

View File

@ -1,10 +1,11 @@
var util = require('util');
var geoip = require('geoip-lite');
var _ = require('lodash');
var MAX_HISTORY = 40;
var MAX_INACTIVE_TIME = 1000*60*60*4;
var Node = function Node(data)
var Node = function(data)
{
this.id = null;
this.info = {};
@ -17,28 +18,32 @@ var Node = function Node(data)
pending: 0,
gasPrice: 0,
block: {
difficulty: 0,
number: 0,
hash: '0x0000000000000000000000000000000000000000000000000000000000000000',
difficulty: 0,
totalDifficulty: 0,
gasLimit: 0,
timestamp: 0,
time: 0,
arrival: 0,
propagation: 0,
received: 0,
propagation: 0,
transactions: [],
uncles: []
},
propagationAvg: 0,
latency: 0,
uptime: 0,
lastUpdate: 0
uptime: 100
};
this.history = new Array(MAX_HISTORY);
this.uptime = {
started: null,
history: []
up: 0,
down: 0,
lastStatus: null,
lastUpdate: null
};
this.init(data);
@ -48,23 +53,18 @@ var Node = function Node(data)
Node.prototype.init = function(data)
{
console.info(util.inspect( data, { depth: null, colors: true } ));
_.fill(this.history, -1);
if( this.id === null )
{
this.uptime.started = _.now();
if( this.id === null && this.uptime.started === null )
this.setState(true);
}
if( !_.isUndefined(data.id) )
this.id = data.id;
this.setInfo(data);
this.id = _.result(data, 'id', this.id);
if( !_.isUndefined(data.latency) )
this.stats.latency = data.latency;
return this;
this.setInfo(data);
}
Node.prototype.setInfo = function(data)
@ -73,35 +73,26 @@ Node.prototype.setInfo = function(data)
{
this.info = data.info;
if( _.isUndefined(data.info.canUpdateHistory) )
data.info.canUpdateHistory = false;
if( !_.isUndefined(data.info.canUpdateHistory) )
{
this.info.canUpdateHistory = _.result(data, 'info.canUpdateHistory', false);
}
}
if( !_.isUndefined(data.ip) )
{
if(data.ip === '::ffff:127.0.0.1')
data.ip = '84.117.82.122';
this.info.ip = data.ip;
this.setGeo();
this.setGeo(data.ip);
}
if( !_.isUndefined(data.spark) )
this.spark = data.spark;
this.spark = _.result(data, 'spark', null);
var uptimeCnt = this.uptime.history.length;
if( uptimeCnt > 0 && this.uptime.history[uptimeCnt - 1].status === 'down' )
this.setState(true);
return this;
this.setState(true);
}
Node.prototype.setGeo = function()
Node.prototype.setGeo = function(ip)
{
this.geo = geoip.lookup(this.info.ip);
return this;
this.info.ip = ip;
this.geo = geoip.lookup(ip);
}
Node.prototype.getInfo = function()
@ -109,31 +100,32 @@ Node.prototype.getInfo = function()
return {
id: this.id,
info: this.info,
geo: this.geo,
stats: this.stats,
history: this.history
stats: {
active: this.stats.active,
mining: this.stats.mining,
hashrate: this.stats.hashrate,
peers: this.stats.peers,
gasPrice: this.stats.gasPrice,
block: this.stats.block,
propagationAvg: this.stats.propagationAvg,
uptime: this.stats.uptime,
latency: this.stats.latency,
pending: this.stats.pending,
},
history: this.history,
geo: this.geo
};
}
Node.prototype.setStats = function(stats, history)
{
if( !_.isUndefined(stats) && !_.isUndefined(stats.block) && !_.isUndefined(stats.block.number) )
if( !_.isUndefined(stats) )
{
this.history = history;
this.setBlock( _.result(stats, 'block', this.stats.block), history );
var positives = _.filter(history, function(p) {
return p >= 0;
});
this.setBasicStats(stats);
if(positives.length > 0)
stats.propagationAvg = Math.round( _.sum(positives) / positives.length );
else
stats.propagationAvg = 0;
if( !_.isUndefined(this.stats.latency) )
stats.latency = this.stats.latency;
this.stats = stats;
this.setPending( _.result(stats, 'pending', this.stats.pending) );
return this.getStats();
}
@ -143,22 +135,14 @@ Node.prototype.setStats = function(stats, history)
Node.prototype.setBlock = function(block, history)
{
if( !_.isUndefined(block) && !_.isUndefined(block.number) )
if( !_.isUndefined(block) && !_.isUndefined(block.number) && ( !_.isEqual(history, this.history) || !_.isEqual(block, this.stats.block) ))
{
this.history = history;
var propagationAvg = 0;
if(block.number !== this.stats.block.number && block.hash !== this.stats.block.hash)
{
this.stats.block = block;
}
var positives = _.filter(history, function(p) {
return p >= 0;
});
if(positives.length > 0)
propagationAvg = Math.round( _.sum(positives) / positives.length );
else
propagationAvg = 0;
this.stats.block = block;
this.stats.propagationAvg = propagationAvg;
this.setHistory(history);
return this.getBlockStats();
}
@ -166,9 +150,36 @@ Node.prototype.setBlock = function(block, history)
return false;
}
Node.prototype.setHistory = function(history)
{
if( _.isEqual(history, this.history) )
{
return false;
}
if( !_.isArray(history) )
{
this.history = _.fill( new Array(MAX_HISTORY), -1 );
this.stats.propagationAvg = 0;
return true;
}
this.history = history;
var positives = _.filter(history, function(p) {
return p >= 0;
});
this.stats.propagationAvg = ( positives.length > 0 ? Math.round( _.sum(positives) / positives.length ) : 0 );
positives = null;
return true;
}
Node.prototype.setPending = function(stats)
{
if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) )
if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) && !_.isEqual(stats.pending, this.stats.pending))
{
this.stats.pending = stats.pending;
@ -183,7 +194,14 @@ Node.prototype.setPending = function(stats)
Node.prototype.setBasicStats = function(stats)
{
if( !_.isUndefined(stats) )
if( !_.isUndefined(stats) && !_.isEqual(stats, {
active: this.stats.active,
mining: this.stats.mining,
hashrate: this.stats.hashrate,
peers: this.stats.peers,
gasPrice: this.stats.gasPrice,
uptime: this.stats.uptime
}) )
{
this.stats.active = stats.active;
this.stats.mining = stats.mining;
@ -200,7 +218,7 @@ Node.prototype.setBasicStats = function(stats)
Node.prototype.setLatency = function(latency)
{
if( !_.isUndefined(latency) )
if( !_.isUndefined(latency) && !_.isEqual(latency, this.stats.latency) )
{
this.stats.latency = latency;
@ -217,7 +235,17 @@ Node.prototype.getStats = function()
{
return {
id: this.id,
stats: this.stats,
stats: {
active: this.stats.active,
mining: this.stats.mining,
hashrate: this.stats.hashrate,
peers: this.stats.peers,
gasPrice: this.stats.gasPrice,
block: this.stats.block,
propagationAvg: this.stats.propagationAvg,
uptime: this.stats.uptime,
pending: this.stats.pending
},
history: this.history
};
}
@ -249,11 +277,41 @@ Node.prototype.getBasicStats = function()
Node.prototype.setState = function(active)
{
var now = _.now();
if(this.uptime.started !== null)
{
if(this.uptime.lastStatus === active)
{
this.uptime[(active ? 'up' : 'down')] += now - this.uptime.lastUpdate;
}
else
{
this.uptime[(active ? 'down' : 'up')] += now - this.uptime.lastUpdate;
}
}
else
{
this.uptime.started = now;
}
this.stats.active = active;
this.uptime.history.push({
status: (active ? 'up' : 'down'),
time: _.now()
});
this.uptime.lastStatus = active;
this.uptime.lastUpdate = now;
this.stats.uptime = this.calculateUptime();
now = undefined;
}
Node.prototype.calculateUptime = function()
{
if(this.uptime.lastUpdate === this.uptime.started)
{
return 100;
}
return Math.round( this.uptime.up / (this.uptime.lastUpdate - this.uptime.started) * 100);
}
Node.prototype.getBlockNumber = function()
@ -268,15 +326,9 @@ Node.prototype.canUpdate = function()
Node.prototype.isInactiveAndOld = function()
{
if(this.stats.active)
return false;
var lastState = this.uptime.history[this.uptime.history.length -1];
if( !_.isUndefined(lastState) && !_.isUndefined(lastState.status) && !_.isUndefined(lastState.time) )
if( this.uptime.lastStatus === false && this.uptime.lastUpdate !== null && (_.now() - this.uptime.lastUpdate) > MAX_INACTIVE_TIME )
{
if( lastState.status === 'down' && (_.now() - lastState.time) > MAX_INACTIVE_TIME )
return true;
return true;
}
return false;

View File

@ -134,14 +134,9 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
// console.log('Action: ', action);
// console.log('Data: ', data);
switch(action) {
switch(action)
{
case "init":
var oldNodes = [];
if( $scope.nodes.length > 0 ){
oldNodes = $scope.nodes;
}
$scope.nodes = data;
_.forEach($scope.nodes, function (node, index) {
@ -160,9 +155,13 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
$scope.nodes[index].pinned = ($scope.pinned.indexOf(node.id) >= 0 ? true : false);
});
if($scope.nodes.length > 0)
if( $scope.nodes.length > 0 )
{
toastr['success']("Got nodes list", "Got nodes!");
updateActiveNodes();
}
break;
case "add":
@ -170,8 +169,8 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
if( addNewNode(data) )
toastr['success']("New node "+ $scope.nodes[findIndex({id: data.id})].info.name +" connected!", "New node!");
// else
// toastr['info']("Node "+ $scope.nodes[index].info.name +" reconnected!", "Node is back!");
else
toastr['info']("Node "+ $scope.nodes[index].info.name +" reconnected!", "Node is back!");
break;
@ -203,6 +202,8 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
}
$scope.nodes[index].stats = data.stats;
updateBestBlock();
}
break;
@ -229,6 +230,8 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
$scope.nodes[index].stats.block = data.block;
$scope.nodes[index].stats.propagationAvg = data.propagationAvg;
updateBestBlock();
}
break;
@ -261,6 +264,8 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
$scope.nodes[index].stats.peers = data.stats.peers;
$scope.nodes[index].stats.gasPrice = data.stats.gasPrice;
$scope.nodes[index].stats.uptime = data.stats.uptime;
updateActiveNodes();
}
}
@ -275,6 +280,8 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
if( _.isUndefined($scope.nodes[index].pinned) )
$scope.nodes[index].pinned = false;
updateActiveNodes();
}
break;
@ -338,7 +345,9 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
if( !_.isUndefined(data.stats) )
$scope.nodes[index].stats = data.stats;
// toastr['error']("Node "+ $scope.nodes[index].info.name +" went away!", "Node connection was lost!");
toastr['error']("Node "+ $scope.nodes[index].info.name +" went away!", "Node connection was lost!");
updateActiveNodes();
}
break;
@ -351,8 +360,9 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
var node = $scope.nodes[index];
if( !_.isUndefined(node) && !_.isUndefined(node.stats) && !_.isUndefined(node.stats.latency) )
// console.log(data.latency);
{
$scope.nodes[index].stats.latency = data.latency;
}
}
break;
@ -366,10 +376,7 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
break;
}
if( action !== "latency" && action !== "pending" && action !== "client-ping" )
{
updateStats();
}
$scope.$apply();
}
function findIndex(search)
@ -430,19 +437,50 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
$scope.nodes[index] = data;
updateActiveNodes();
return false;
}
function updateStats()
function updateActiveNodes()
{
updateBestBlock();
$scope.nodesTotal = $scope.nodes.length;
$scope.nodesActive = _.filter($scope.nodes, function (node) {
return node.stats.active == true;
}).length;
$scope.upTimeTotal = _.reduce($scope.nodes, function (total, node) {
return total + node.stats.uptime;
}, 0) / $scope.nodes.length;
$scope.map = _.map($scope.nodes, function (node) {
var fill = $filter('bubbleClass')(node.stats, $scope.bestBlock);
if(node.geo != null)
return {
radius: 3,
latitude: node.geo.ll[0],
longitude: node.geo.ll[1],
nodeName: node.info.name,
fillClass: "text-" + fill,
fillKey: fill,
};
else
return {
radius: 0,
latitude: 0,
longitude: 0
};
});
}
function updateBestBlock()
{
if( $scope.nodes.length )
{
$scope.nodesTotal = $scope.nodes.length;
$scope.nodesActive = _.filter($scope.nodes, function (node) {
return node.stats.active == true;
}).length;
var bestBlock = _.max($scope.nodes, function (node) {
return parseInt(node.stats.block.number);
}).stats.block.number;
@ -457,32 +495,6 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, $localStorage, soc
$scope.lastBlock = $scope.bestStats.block.arrived;
$scope.lastDifficulty = $scope.bestStats.block.difficulty;
}
$scope.upTimeTotal = _.reduce($scope.nodes, function (total, node) {
return total + node.stats.uptime;
}, 0) / $scope.nodes.length;
$scope.map = _.map($scope.nodes, function (node) {
var fill = $filter('bubbleClass')(node.stats, $scope.bestBlock);
if(node.geo != null)
return {
radius: 3,
latitude: node.geo.ll[0],
longitude: node.geo.ll[1],
nodeName: node.info.name,
fillClass: "text-" + fill,
fillKey: fill,
};
else
return {
radius: 0,
latitude: 0,
longitude: 0
};
});
// $scope.$apply();
}
}
});