moved miner chart processing to server

This commit is contained in:
cubedro 2015-04-29 12:00:01 +03:00
parent 9d8b1483fe
commit e2f4369307
5 changed files with 73 additions and 41 deletions

4
app.js
View File

@ -62,7 +62,7 @@ api.on('connection', function(spark) {
data.spark = spark.id;
data.latency = spark.latency;
var info = Nodes.add(data);
var info = Nodes.add( data );
spark.emit('ready');
client.write({
@ -138,7 +138,7 @@ api.on('connection', function(spark) {
var range = Nodes.getHistory().getHistoryRequestRange();
console.log("asked " + data.id + " for history");
console.log('interval', range);
console.log('interval', range.max + " - " + range.min);
spark.emit('history', range);

View File

@ -160,4 +160,4 @@ Collection.prototype.requiresUpdate = function(id)
return ( this.canNodeUpdate(id) && this._blockchain.requiresUpdate() );
}
module.exports = Collection;
module.exports = Collection;

View File

@ -33,7 +33,7 @@ var History = function History(data)
History.prototype.add = function(block, id)
{
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.uncles) && !_.isUndefined(block.transactions) && !_.isUndefined(block.difficulty) )
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.uncles) && !_.isUndefined(block.transactions) && !_.isUndefined(block.difficulty) && block.number > 0 )
{
var historyBlock = this.search(block.number);
@ -44,7 +44,7 @@ History.prototype.add = function(block, id)
if( historyBlock )
{
var propIndex = _.findIndex( historyBlock.propagTimes, {node: id} );
var propIndex = _.findIndex( historyBlock.propagTimes, { node: id } );
if( propIndex === -1 )
{
@ -119,7 +119,7 @@ History.prototype._save = function(block)
History.prototype.search = function(number)
{
var index = _.findIndex( this._items, {height: number} );
var index = _.findIndex( this._items, { height: number } );
if(index < 0)
return false;
@ -129,7 +129,7 @@ History.prototype.search = function(number)
History.prototype.prevMaxBlock = function(number)
{
var index = _.findIndex(this._items, function(item) {
var index = _.findIndex(this._items, function (item) {
return item.height < number;
});
@ -339,6 +339,30 @@ History.prototype.getAvgHashrate = function()
return avgDifficulty / 1000 * 12 * ( 12 / avgBlocktime );
}
History.prototype.getMinersCount = function()
{
var miners = _( this._items )
.sortByOrder( 'height', false )
.slice(0, MAX_BINS)
.map(function (item)
{
return item.block.miner;
})
.value();
var minerCount = [];
_.forEach( _.countBy(miners), function (cnt, miner)
{
minerCount.push({ miner: miner, name: false, blocks: cnt });
});
return _(minerCount)
.sortByOrder( 'blocks', false )
.slice(0, 5)
.value();
}
History.prototype.getCharts = function()
{
var chartHistory = _( this._items )
@ -353,22 +377,24 @@ History.prototype.getCharts = function()
difficulty: item.block.difficulty,
uncles: item.block.uncles.length,
transactions: item.block.transactions.length,
gasSpending: item.block.gasUsed
gasSpending: item.block.gasUsed,
miner: item.block.miner
};
})
.value();
return {
height: _.pluck(chartHistory, 'height'),
blocktime: _.pluck(chartHistory, 'blocktime'),
avgBlocktime: _.sum(_.pluck(chartHistory, 'blocktime')) / (chartHistory.length === 0 ? 1 : chartHistory.length),
difficulty: _.pluck(chartHistory, 'difficulty'),
uncles: _.pluck(chartHistory, 'uncles'),
transactions: _.pluck(chartHistory, 'transactions'),
gasSpending: _.pluck(chartHistory, 'gasSpending'),
propagation: this.getBlockPropagation(),
uncleCount: this.getUncleCount(),
avgHashrate: this.getAvgHashrate()
height : _.pluck( chartHistory, 'height' ),
blocktime : _.pluck( chartHistory, 'blocktime' ),
avgBlocktime : _.sum(_.pluck( chartHistory, 'blocktime' )) / (chartHistory.length === 0 ? 1 : chartHistory.length),
difficulty : _.pluck( chartHistory, 'difficulty' ),
uncles : _.pluck( chartHistory, 'uncles' ),
transactions : _.pluck( chartHistory, 'transactions' ),
gasSpending : _.pluck( chartHistory, 'gasSpending' ),
miners : this.getMinersCount(),
propagation : this.getBlockPropagation(),
uncleCount : this.getUncleCount(),
avgHashrate : this.getAvgHashrate()
};
}
@ -391,7 +417,11 @@ History.prototype.getHistoryRequestRange = function()
var max = _.max(missing);
var min = max - Math.min( 50, (MAX_HISTORY - this._items.length + 1) ) + 1;
return {max: max, min: min};
return {
max: max,
min: min,
list: _( missing ).reverse().slice(0, 50).reverse().value()
};
}
module.exports = History;

View File

@ -27,10 +27,7 @@ var Node = function Node(data)
transactions: [],
uncles: []
},
blocktimeAvg: 0,
propagationAvg: 0,
blockTimes: [],
difficulty: [],
latency: 0,
uptime: 0,
lastUpdate: 0

View File

@ -155,16 +155,19 @@ function StatsCtrl($scope, $filter, socket, _, toastr) {
break;
case "charts":
$scope.lastBlocksTime = data.blocktime;
$scope.avgBlockTime = data.avgBlocktime;
$scope.avgHashrate = data.avgHashrate;
$scope.lastBlocksTime = data.blocktime;
$scope.difficultyChart = data.difficulty;
$scope.transactionDensity = data.transactions;
$scope.gasSpending = data.gasSpending;
$scope.blockPropagationChart = data.propagation.histogram;
$scope.blockPropagationAvg = data.propagation.avg;
$scope.uncleCountChart = data.uncleCount;
$scope.uncleCount = data.uncleCount[0] + data.uncleCount[1];
$scope.transactionDensity = data.transactions;
$scope.gasSpending = data.gasSpending;
$scope.miners = data.miners;
getMinersNames();
jQuery('.spark-blocktimes').sparkline($scope.lastBlocksTime, {type: 'bar', tooltipSuffix: ' s'});
jQuery('.spark-difficulty').sparkline($scope.difficultyChart, {type: 'bar'});
@ -225,6 +228,24 @@ function StatsCtrl($scope, $filter, socket, _, toastr) {
});
}
function getMinersNames()
{
if($scope.miners.length > 0) {
console.log('miners', $scope.miners);
_.forIn($scope.miners, function(value, key)
{
if(value.name !== false)
return;
var name = _.result(_.find(_.pluck($scope.nodes, 'info'), 'coinbase', value.miner), 'name');
if(typeof name !== 'undefined')
$scope.miners[key].name = name;
});
}
}
function addNewNode(data)
{
var index = findIndex({id: data.id});
@ -267,22 +288,6 @@ function StatsCtrl($scope, $filter, socket, _, toastr) {
$scope.lastBlock = $scope.bestStats.block.received;
$scope.lastDifficulty = $scope.bestStats.block.difficulty;
if(typeof $scope.bestStats.miners !== 'undefined') {
$scope.miners = $scope.bestStats.miners;
console.log($scope.miners);
_.forIn($scope.miners, function(value, key)
{
if(value.name !== false)
return;
var name = _.result(_.find(_.pluck($scope.nodes, 'info'), 'coinbase', value.miner), 'name');
if(typeof name !== 'undefined')
$scope.miners[key].name = name;
});
}
}
$scope.upTimeTotal = _.reduce($scope.nodes, function(total, node) {