From e2f43693076bd7af66833791b323c04d3d75aefc Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 29 Apr 2015 12:00:01 +0300 Subject: [PATCH] moved miner chart processing to server --- app.js | 4 +-- models/collection.js | 2 +- models/history.js | 62 +++++++++++++++++++++++++++++----------- models/node.js | 3 -- public/js/controllers.js | 43 ++++++++++++++++------------ 5 files changed, 73 insertions(+), 41 deletions(-) diff --git a/app.js b/app.js index bb36aee..6afaeb8 100644 --- a/app.js +++ b/app.js @@ -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); diff --git a/models/collection.js b/models/collection.js index 728d9c6..180f73f 100644 --- a/models/collection.js +++ b/models/collection.js @@ -160,4 +160,4 @@ Collection.prototype.requiresUpdate = function(id) return ( this.canNodeUpdate(id) && this._blockchain.requiresUpdate() ); } -module.exports = Collection; \ No newline at end of file +module.exports = Collection; diff --git a/models/history.js b/models/history.js index 0628bcb..a075df9 100644 --- a/models/history.js +++ b/models/history.js @@ -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; diff --git a/models/node.js b/models/node.js index 5002af2..1de6db9 100644 --- a/models/node.js +++ b/models/node.js @@ -27,10 +27,7 @@ var Node = function Node(data) transactions: [], uncles: [] }, - blocktimeAvg: 0, propagationAvg: 0, - blockTimes: [], - difficulty: [], latency: 0, uptime: 0, lastUpdate: 0 diff --git a/public/js/controllers.js b/public/js/controllers.js index f584cb8..f200f13 100644 --- a/public/js/controllers.js +++ b/public/js/controllers.js @@ -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) {