Merge pull request #165 from cubedro/develop

AddBlock as a separate call
This commit is contained in:
Marian OANCΞA 2015-06-01 23:09:24 +03:00
commit c758da2312
6 changed files with 105 additions and 2 deletions

21
app.js
View File

@ -144,6 +144,27 @@ api.on('connection', function(spark) {
} }
}); });
spark.on('block', function(data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
{
var stats = Nodes.addBlock(data.id, data.block);
if(stats !== false)
{
client.write({
action: 'block',
data: stats
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}
}
});
spark.on('pending', function(data) spark.on('pending', function(data)
{ {
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -39,6 +39,27 @@ Collection.prototype.update = function(id, stats)
return node.setStats(stats, propagationHistory); return node.setStats(stats, propagationHistory);
} }
Collection.prototype.addBlock = function(id, block)
{
var node = this.getNode({ id: id });
if (!node)
return false;
var block = this._blockchain.add(block, id);
if (!block)
return false;
var propagationHistory = this._blockchain.getNodePropagation(id);
block.arrived = block.arrived;
block.received = block.received;
block.propagation = block.propagation;
return node.setBlock(block, propagationHistory);
}
Collection.prototype.updatePending = function(id, stats) Collection.prototype.updatePending = function(id, stats)
{ {
var node = this.getNode({ id: id }); var node = this.getNode({ id: id });

View File

@ -141,6 +141,31 @@ Node.prototype.setStats = function(stats, history)
return false; return false;
} }
Node.prototype.setBlock = function(block, history)
{
if( !_.isUndefined(block) && !_.isUndefined(block.block.number) )
{
this.history = history;
var propagationAvg = 0;
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;
return this.getBlockStats();
}
return false;
}
Node.prototype.setPending = function(stats) Node.prototype.setPending = function(stats)
{ {
if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) ) if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) )
@ -180,6 +205,16 @@ Node.prototype.getStats = function()
}; };
} }
Node.prototype.getBlockStats = function()
{
return {
id: this.id,
block: this.stats.block,
propagationAvg: this.stats.propagationAvg,
history: this.history
};
}
Node.prototype.setState = function(active) Node.prototype.setState = function(active)
{ {
this.stats.active = active; this.stats.active = active;

View File

@ -181,6 +181,32 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, socket, _, toastr)
break; break;
case "block":
var index = findIndex({id: data.id});
if( index >= 0 && !_.isUndefined($scope.nodes[index]) && !_.isUndefined($scope.nodes[index].stats) )
{
if( $scope.nodes[index].stats.block.number < data.block.number )
{
var best = _.max($scope.nodes, function (node) {
return parseInt(node.stats.block.number);
}).stats.block;
if (data.block.number > best.number) {
data.block.arrived = _.now();
} else {
data.block.arrived = best.arrived;
}
$scope.nodes[index].history = data.history;
}
$scope.nodes[index].stats.block = data.block;
$scope.nodes[index].stats.propagationAvg = data.propagationAvg;
}
break;
case "pending": case "pending":
var index = findIndex({id: data.id}); var index = findIndex({id: data.id});