From ff5d8bd31a8a026cf10b18b1f522b4881213c009 Mon Sep 17 00:00:00 2001 From: cubedro Date: Sun, 5 Apr 2015 00:59:24 +0300 Subject: [PATCH] moved popagation to backend fixes #38 --- models/collection.js | 30 ++++++++++++++++++++++++++++++ models/node.js | 11 ++++++----- public/js/controllers.js | 10 +++++----- public/js/filters.js | 7 ++----- views/index.jade | 2 +- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/models/collection.js b/models/collection.js index b945693..ed68c68 100644 --- a/models/collection.js +++ b/models/collection.js @@ -4,6 +4,7 @@ var Node = require('./node'); var Collection = function Collection() { this._list = []; + this._bestBlock = null; return this; } @@ -23,6 +24,35 @@ Collection.prototype.update = function(id, stats) if(!node) return false; + if(this._bestBlock === null) + { + stats.block.received = (new Date()).getTime(); + stats.block.propagation = 0; + this._bestBlock = stats.block; + } + else + { + var oldStats = node.getStats(); + + if(stats.block.number !== oldStats.stats.block.number) + { + stats.block.received = (new Date()).getTime(); + + if(this._bestBlock.number < oldStats.stats.block.number) + { + stats.block.propagation = 0; + this._bestBlock = stats.block; + } + else + { + stats.block.propagation = stats.block.received - this._bestBlock.received; + } + } else { + stats.block.received = oldStats.stats.block.received; + stats.block.propagation = oldStats.stats.block.propagation; + } + } + return node.setStats(stats); } diff --git a/models/node.js b/models/node.js index 8edc66b..2fd3bf8 100644 --- a/models/node.js +++ b/models/node.js @@ -90,11 +90,12 @@ Node.prototype.setStats = function(stats) { if(typeof stats !== 'undefined' && typeof stats.block !== 'undefined' && typeof stats.block.number !== 'undefined') { - if(stats.block.number !== this.stats.block.number) { - stats.block.received = (new Date()).getTime(); - } else { - stats.block.received = this.stats.block.received; - } + // if(stats.block.number !== this.stats.block.number) { + // stats.block.received = (new Date()).getTime(); + // } else { + // stats.block.received = this.stats.block.received; + // } + stats.block.hash = stats.block.hash.replace('0x', ''); this.stats = stats; diff --git a/public/js/controllers.js b/public/js/controllers.js index 799ef61..fb3110d 100644 --- a/public/js/controllers.js +++ b/public/js/controllers.js @@ -76,11 +76,11 @@ function StatsCtrl($scope, $filter, socket, _, toastr) { case "update": var index = findIndex({id: data.id}); - if(data.stats.block.number > $scope.nodes[index].stats.block.number) { - data.stats.block.firstarrived = (data.stats.block.number > $scope.bestBlock ? data.stats.block.received : $scope.lastBlock); - } else { - data.stats.block.firstarrived = $scope.nodes[index].stats.block.firstarrived; - } + // if(data.stats.block.number > $scope.nodes[index].stats.block.number) { + // data.stats.block.firstarrived = (data.stats.block.number > $scope.bestBlock ? data.stats.block.received : $scope.lastBlock); + // } else { + // data.stats.block.firstarrived = $scope.nodes[index].stats.block.firstarrived; + // } $scope.nodes[index].stats = data.stats; diff --git a/public/js/filters.js b/public/js/filters.js index f38d23b..db436c7 100644 --- a/public/js/filters.js +++ b/public/js/filters.js @@ -75,9 +75,7 @@ angular.module('netStatsApp.filters', []) }; }) .filter('propagationTimeClass', function() { - return function(arrival, best) { - var propagation = arrival - best; - + return function(propagation) { if(propagation <= 3000) return 'text-success'; @@ -125,8 +123,7 @@ angular.module('netStatsApp.filters', []) }; }) .filter('blockPropagationFilter', function() { - return function(arrival, best) { - var ms = arrival - best; + return function(ms) { var result = 0; if(ms < 1000) { diff --git a/views/index.jade b/views/index.jade index ef885a7..d23803b 100644 --- a/views/index.jade +++ b/views/index.jade @@ -140,5 +140,5 @@ block content //- div.small Difficulty: {{node.stats.block.difficulty | gasFilter}} | Gas used: {{node.stats.block.gasUsed | gasFilter}} | Min gas price: {{node.stats.block.minGasPrice | gasFilter}} | Gas limit: {{node.stats.block.gasLimit | gasFilter}} td(style="padding-left: 18px;") {{node.stats.block.transactions.length || 0}} td(class="{{ node.stats.block.timestamp | timeClass }}") {{node.stats.block.received | blockTimeFilter }} - td(class="{{ node.stats.block.received | propagationTimeClass : node.stats.block.firstarrived }}") {{node.stats.block.received | blockPropagationFilter : node.stats.block.firstarrived}} + td(class="{{ node.stats.block.propagation | propagationTimeClass }}") {{node.stats.block.propagation | blockPropagationFilter}} td(class="{{ node.stats.uptime | upTimeClass }}") {{ node.stats.uptime | upTimeFilter }}