Merge pull request #165 from cubedro/develop

AddBlock as a separate call
pull/5/head
Marian OANCΞA 10 years ago
commit c758da2312
  1. 21
      app.js
  2. 2
      dist/js/netstats.min.js
  3. 2
      dist/js/netstats.min.js.map
  4. 21
      models/collection.js
  5. 35
      models/node.js
  6. 26
      src/js/controllers.js

@ -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)
{
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

@ -39,6 +39,27 @@ Collection.prototype.update = function(id, stats)
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)
{
var node = this.getNode({ id: id });

@ -141,6 +141,31 @@ Node.prototype.setStats = function(stats, history)
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)
{
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)
{
this.stats.active = active;

@ -181,6 +181,32 @@ netStatsApp.controller('StatsCtrl', function($scope, $filter, socket, _, toastr)
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":
var index = findIndex({id: data.id});

Loading…
Cancel
Save