added latency update

This commit is contained in:
cubedro 2015-04-03 07:00:06 +03:00
parent 9487a06dbd
commit ec14cc4d67
4 changed files with 43 additions and 0 deletions

17
app.js
View File

@ -79,6 +79,23 @@ api.on('connection', function(spark) {
} }
}); });
spark.on('node-ping', function(data){
spark.emit('node-pong');
});
spark.on('latency', function(data)
{
console.log('Latency: ', data.latency);
console.log('got ping from ' + spark.id);
if(typeof data.id !== 'undefined')
{
var stats = Nodes.updateLatency(data.id, data.latency);
client.write({action: 'latency', data: stats});
}
});
spark.on('end', function(data) spark.on('end', function(data)
{ {
var stats = Nodes.inactive(spark.id); var stats = Nodes.inactive(spark.id);

View File

@ -26,6 +26,16 @@ Collection.prototype.update = function(id, stats)
return node.setStats(stats); return node.setStats(stats);
} }
Collection.prototype.updateLatency = function(id, latency)
{
var node = this.getNode({ id: id });
if(!node)
return false;
return node.setLatency(latency);
}
Collection.prototype.inactive = function(id) Collection.prototype.inactive = function(id)
{ {
var node = this.getNode({ spark: id }); var node = this.getNode({ spark: id });

View File

@ -104,6 +104,18 @@ Node.prototype.setStats = function(stats)
return false; return false;
} }
Node.prototype.setLatency = function(latency)
{
if(typeof latency !== 'undefined')
{
this.stats.latency = latency;
return { id: this.id, latency: latency };
}
return false;
}
Node.prototype.getStats = function() Node.prototype.getStats = function()
{ {
return {id: this.id, stats: this.stats}; return {id: this.id, stats: this.stats};

View File

@ -85,6 +85,10 @@ function StatsCtrl($scope, $filter, socket, _, toastr) {
$scope.nodes[findIndex({id: data.id})].stats = data.stats; $scope.nodes[findIndex({id: data.id})].stats = data.stats;
toastr['error']("Node "+ $scope.nodes[findIndex({id: data.id})].info.name +" went away!", "Node connection was lost!"); toastr['error']("Node "+ $scope.nodes[findIndex({id: data.id})].info.name +" went away!", "Node connection was lost!");
break; break;
case "latency":
$scope.nodes[findIndex({id: data.id})].stats.latency = data.latency;
break;
} }
updateStats(); updateStats();