ethstats-server/public/js/controllers.js

406 lines
9.4 KiB
JavaScript
Raw Normal View History

2015-01-20 19:29:31 +01:00
'use strict';
/* Controllers */
2015-04-29 12:58:09 +02:00
netStatsApp.controller('StatsCtrl', function($scope, $filter, socket, _, toastr) {
2015-01-20 19:29:31 +01:00
2015-02-05 16:54:04 +01:00
// Main Stats init
// ---------------
$scope.nodesTotal = 0;
$scope.nodesActive = 0;
$scope.bestBlock = 0;
$scope.lastBlock = 0;
2015-02-18 08:54:04 +01:00
$scope.lastDifficulty = 0;
2015-02-05 16:54:04 +01:00
$scope.upTimeTotal = 0;
2015-02-18 08:54:04 +01:00
$scope.avgBlockTime = 0;
2015-04-28 17:44:41 +02:00
$scope.blockPropagationAvg = 0;
2015-04-28 11:54:32 +02:00
$scope.avgHashrate = 0;
2015-04-24 07:13:28 +02:00
$scope.uncleCount = 0;
2015-04-06 01:28:42 +02:00
$scope.bestStats = {};
2015-02-05 16:54:04 +01:00
2015-03-27 17:52:42 +01:00
$scope.lastBlocksTime = [];
2015-04-24 07:37:03 +02:00
$scope.difficultyChart = [];
2015-03-28 00:27:40 +01:00
$scope.transactionDensity = [];
2015-03-28 19:07:15 +01:00
$scope.gasSpending = [];
2015-04-06 01:28:42 +02:00
$scope.miners = [];
2015-03-27 17:52:42 +01:00
2015-02-17 11:14:48 +01:00
$scope.nodes = [];
2015-02-08 16:28:19 +01:00
$scope.map = [];
2015-04-23 15:17:31 +02:00
$scope.blockPropagationChart = [];
2015-04-24 07:13:28 +02:00
$scope.uncleCountChart = [];
$scope.coinbases = [];
2015-02-08 16:03:05 +01:00
2015-04-06 07:33:01 +02:00
$scope.latency = 0;
2015-05-05 09:19:32 +02:00
$scope.currentApiVersion = "0.0.8";
2015-04-28 03:25:49 +02:00
2015-05-19 01:41:14 +02:00
$scope.predicate = ['-pinned', '-stats.active', '-stats.block.number', 'stats.block.propagation'];
2015-04-06 04:40:29 +02:00
$scope.reverse = false;
2015-05-19 01:41:14 +02:00
$scope.prefixPredicate = ['-pinned', '-stats.active'];
$scope.originalPredicate = ['-stats.block.number', 'stats.block.propagation'];
$scope.orderTable = function(predicate, reverse)
{
2015-05-19 01:41:14 +02:00
if(!_.isEqual(predicate, $scope.originalPredicate))
{
$scope.reverse = reverse;
2015-05-19 01:41:14 +02:00
$scope.originalPredicate = predicate;
$scope.predicate = _.union($scope.prefixPredicate, predicate);
}
else
{
$scope.reverse = !$scope.reverse;
2015-05-19 01:41:14 +02:00
if($scope.reverse === true){
_.forEach(predicate, function (value, key) {
predicate[key] = (value[0] === '-' ? value.replace('-', '') : '-' + value);
});
}
$scope.predicate = _.union($scope.prefixPredicate, predicate);
}
}
2015-04-29 12:58:09 +02:00
$scope.timeout = setInterval(function ()
{
2015-02-23 15:48:00 +01:00
$scope.$apply();
2015-04-29 12:58:09 +02:00
}, 200);
2015-02-23 15:48:00 +01:00
$scope.getNumber = function(num) {
return new Array(num);
}
2015-01-20 19:29:31 +01:00
// Socket listeners
// ----------------
2015-02-17 06:12:44 +01:00
socket.on('open', function open() {
socket.emit('ready');
console.log('The connection has been opened.');
2015-02-17 10:30:41 +01:00
})
.on('end', function end() {
2015-02-17 11:14:48 +01:00
console.log('Socket connection ended.')
2015-02-17 10:30:41 +01:00
})
.on('error', function error(err) {
2015-02-17 06:12:44 +01:00
console.log(err);
2015-02-17 10:30:41 +01:00
})
.on('reconnecting', function reconnecting(opts) {
2015-02-17 06:12:44 +01:00
console.log('We are scheduling a reconnect operation', opts);
2015-02-17 10:30:41 +01:00
})
.on('data', function incoming(data) {
socketAction(data.action, data.data);
2015-02-17 06:12:44 +01:00
});
2015-02-08 16:03:05 +01:00
socket.on('init', function(data)
{
2015-02-17 11:14:48 +01:00
socketAction("init", data.nodes);
2015-01-29 17:50:16 +01:00
});
2015-04-06 07:33:01 +02:00
socket.on('client-latency', function(data)
{
$scope.latency = data.latency;
})
2015-02-17 10:30:41 +01:00
function socketAction(action, data)
2015-02-08 16:03:05 +01:00
{
2015-04-29 11:03:05 +02:00
// console.log('Action: ', action);
// console.log('Data: ', data);
2015-02-17 11:14:48 +01:00
2015-02-17 10:30:41 +01:00
switch(action) {
2015-02-17 11:14:48 +01:00
case "init":
$scope.nodes = data;
2015-04-17 11:10:20 +02:00
_.forEach($scope.nodes, function(node, index) {
2015-05-19 02:32:38 +02:00
// Init hashrate
2015-04-24 12:39:26 +02:00
if(typeof node.stats.hashrate === 'undefined')
$scope.nodes[index].stats.hashrate = 0;
2015-05-19 01:41:14 +02:00
2015-05-19 02:32:38 +02:00
// Init history
if( _.isUndefined(data.history) )
{
data.history = new Array(40);
_.fill(data.history, -1);
}
2015-05-19 01:41:14 +02:00
// Init pin
$scope.nodes[index].pinned = false;
2015-05-19 02:32:38 +02:00
$scope.$apply();
2015-04-17 11:10:20 +02:00
makePeerPropagationChart($scope.nodes[index]);
});
if($scope.nodes.length > 0)
toastr['success']("Got nodes list", "Got nodes!");
2015-02-17 19:14:38 +01:00
2015-02-17 11:14:48 +01:00
break;
2015-02-17 10:30:41 +01:00
case "add":
2015-02-17 19:14:38 +01:00
if(addNewNode(data))
2015-02-19 21:49:44 +01:00
toastr['success']("New node "+ $scope.nodes[findIndex({id: data.id})].info.name +" connected!", "New node!");
2015-02-17 21:45:30 +01:00
else
2015-02-19 21:49:44 +01:00
toastr['info']("Node "+ $scope.nodes[findIndex({id: data.id})].info.name +" reconnected!", "Node is back!");
2015-04-17 11:10:20 +02:00
$scope.$apply();
makePeerPropagationChart($scope.nodes[findIndex({id: data.id})]);
2015-02-17 10:30:41 +01:00
break;
case "update":
2015-04-24 12:39:26 +02:00
if(typeof data.stats.hashrate === 'undefined')
data.stats.hashrate = 0;
2015-04-06 16:08:36 +02:00
var index = findIndex({id: data.id});
2015-04-28 11:01:34 +02:00
2015-05-19 02:32:38 +02:00
if( !_.isUndefined($scope.nodes[index].stats) ) {
2015-05-18 17:33:20 +02:00
2015-05-07 23:58:02 +02:00
if($scope.nodes[index].stats.block.number < data.stats.block.number)
{
var best = _.max($scope.nodes, function(node) {
return parseInt(node.stats.block.number);
}).stats.block;
if (data.stats.block.number > best.number) {
data.stats.block.arrived = _.now();
} else {
data.stats.block.arrived = best.arrived;
}
}
2015-04-28 11:01:34 +02:00
$scope.nodes[index].stats = data.stats;
$scope.nodes[index].history = data.history;
2015-05-19 02:32:38 +02:00
$scope.$apply();
2015-04-28 11:01:34 +02:00
makePeerPropagationChart($scope.nodes[index]);
}
2015-04-17 11:10:20 +02:00
2015-02-17 10:30:41 +01:00
break;
case "info":
$scope.nodes[findIndex({id: data.id})].info = data.info;
2015-04-17 11:10:20 +02:00
if(_.isUndefined($scope.nodes[findIndex({id: data.id})].pinned))
$scope.nodes[findIndex({id: data.id})].pinned = false;
2015-04-17 11:10:20 +02:00
break;
case "blockPropagationChart":
2015-04-28 17:44:41 +02:00
$scope.blockPropagationChart = data.histogram;
$scope.blockPropagationAvg = data.avg;
2015-04-17 11:10:20 +02:00
2015-02-17 11:14:48 +01:00
break;
2015-02-18 07:06:41 +01:00
2015-04-24 05:23:26 +02:00
case "uncleCount":
2015-04-24 07:13:28 +02:00
$scope.uncleCountChart = data;
$scope.uncleCount = data[0] + data[1];
2015-04-24 05:23:26 +02:00
2015-04-24 07:13:28 +02:00
jQuery('.spark-uncles').sparkline($scope.uncleCountChart.reverse(), {type: 'bar', barSpacing: 1});
2015-04-24 05:23:26 +02:00
break;
2015-04-28 09:43:56 +02:00
case "charts":
2015-04-28 11:31:49 +02:00
$scope.avgBlockTime = data.avgBlocktime;
2015-04-28 13:04:39 +02:00
$scope.avgHashrate = data.avgHashrate;
2015-04-29 11:00:01 +02:00
$scope.lastBlocksTime = data.blocktime;
2015-04-28 09:43:56 +02:00
$scope.difficultyChart = data.difficulty;
2015-04-28 17:44:41 +02:00
$scope.blockPropagationChart = data.propagation.histogram;
$scope.blockPropagationAvg = data.propagation.avg;
2015-04-28 09:43:56 +02:00
$scope.uncleCountChart = data.uncleCount;
$scope.uncleCount = data.uncleCount[0] + data.uncleCount[1];
2015-04-29 11:00:01 +02:00
$scope.transactionDensity = data.transactions;
$scope.gasSpending = data.gasSpending;
$scope.miners = data.miners;
2015-05-19 02:32:38 +02:00
$scope.$apply();
2015-04-29 11:00:01 +02:00
getMinersNames();
2015-04-28 09:43:56 +02:00
jQuery('.spark-blocktimes').sparkline($scope.lastBlocksTime, {type: 'bar', tooltipSuffix: ' s'});
jQuery('.spark-difficulty').sparkline($scope.difficultyChart, {type: 'bar'});
jQuery('.spark-transactions').sparkline($scope.transactionDensity, {type: 'bar'});
jQuery('.spark-gasspending').sparkline($scope.gasSpending, {type: 'bar'});
jQuery('.spark-uncles').sparkline($scope.uncleCountChart.reverse(), {type: 'bar', barSpacing: 1});
break;
2015-02-18 07:06:41 +01:00
case "inactive":
2015-04-29 12:14:18 +02:00
if( !_.isUndefined(data.stats) )
2015-04-24 12:33:50 +02:00
$scope.nodes[findIndex({id: data.id})].stats = data.stats;
2015-04-24 12:39:26 +02:00
2015-02-19 21:49:44 +01:00
toastr['error']("Node "+ $scope.nodes[findIndex({id: data.id})].info.name +" went away!", "Node connection was lost!");
2015-04-17 11:10:20 +02:00
2015-02-18 07:06:41 +01:00
break;
2015-04-03 06:00:06 +02:00
case "latency":
2015-04-29 12:14:18 +02:00
if( !_.isUndefined(data.id) )
var node = $scope.nodes[findIndex({id: data.id})];
2015-04-28 11:01:34 +02:00
2015-04-29 12:14:18 +02:00
if( !_.isUndefined(node) && !_.isUndefined(node.stats) && !_.isUndefined(node.stats.latency))
2015-04-28 11:01:34 +02:00
$scope.nodes[findIndex({id: data.id})].stats.latency = data.latency;
2015-04-17 11:10:20 +02:00
2015-05-19 02:32:38 +02:00
$scope.$apply();
2015-04-03 06:00:06 +02:00
break;
2015-04-06 07:33:01 +02:00
case "client-ping":
socket.emit('client-pong');
2015-04-17 11:10:20 +02:00
2015-04-06 07:33:01 +02:00
break;
2015-02-17 10:30:41 +01:00
}
2015-01-29 17:50:16 +01:00
2015-05-19 02:32:38 +02:00
if(action !== "latency")
{
updateStats();
}
2015-02-17 10:30:41 +01:00
}
function findIndex(search)
{
return _.findIndex($scope.nodes, search);
}
2015-01-29 17:50:16 +01:00
2015-04-17 11:10:20 +02:00
function makePeerPropagationChart(node)
2015-04-06 16:56:48 +02:00
{
2015-04-17 11:10:20 +02:00
jQuery('.' + node.id).sparkline(node.history, {
type: 'bar',
negBarColor: '#7f7f7f',
zeroAxis: false,
2015-04-23 21:28:38 +02:00
height: 20,
2015-04-17 11:10:20 +02:00
barWidth : 2,
barSpacing : 1,
tooltipSuffix: ' ms',
2015-04-17 12:12:25 +02:00
chartRangeMax: 8000,
2015-04-17 11:10:20 +02:00
colorMap: jQuery.range_map({
'0:1': '#10a0de',
'1:1000': '#7bcc3a',
'1001:3000': '#FFD162',
'3001:7000': '#ff8a00',
'7001:': '#F74B4B'
})
2015-04-06 16:56:48 +02:00
});
2015-04-17 11:10:20 +02:00
}
2015-04-06 16:56:48 +02:00
2015-04-29 11:00:01 +02:00
function getMinersNames()
{
2015-04-29 11:03:05 +02:00
if($scope.miners.length > 0)
{
2015-04-29 11:00:01 +02:00
_.forIn($scope.miners, function(value, key)
{
if(value.name !== false)
return;
2015-05-18 17:33:20 +02:00
if(value.miner === "0x0000000000000000000000000000000000000000")
return;
2015-04-29 11:00:01 +02:00
var name = _.result(_.find(_.pluck($scope.nodes, 'info'), 'coinbase', value.miner), 'name');
if(typeof name !== 'undefined')
$scope.miners[key].name = name;
});
}
}
2015-02-17 19:14:38 +01:00
function addNewNode(data)
{
var index = findIndex({id: data.id});
if(index < 0)
{
2015-04-24 12:39:26 +02:00
if(typeof data.stats !== 'undefined' && typeof data.stats.hashrate === 'undefined')
data.stats.hashrate = 0;
2015-05-19 01:41:14 +02:00
data.pinned = false;
2015-05-19 02:32:38 +02:00
if( _.isUndefined(data.history) )
{
data.history = new Array(40);
_.fill(data.history, -1);
}
2015-02-17 19:14:38 +01:00
$scope.nodes.push(data);
return true;
}
2015-05-19 02:32:38 +02:00
if( !_.isUndefined($scope.nodes[index].pinned) )
{
data.pinned = $scope.nodes[index].pinned
2015-05-19 02:32:38 +02:00
}
else
{
data.pinned = false;
}
2015-05-19 02:32:38 +02:00
if( !_.isUndefined($scope.nodes[index].pinned) )
{
data.history = $scope.nodes[index].history
}
else
{
if( _.isUndefined(data.history) )
{
data.history = new Array(40);
_.fill(data.history, -1);
}
}
2015-02-17 19:14:38 +01:00
$scope.nodes[index] = data;
return false;
}
2015-01-29 17:50:16 +01:00
function updateStats()
{
2015-02-17 10:30:41 +01:00
if($scope.nodes.length)
{
$scope.nodesTotal = $scope.nodes.length;
$scope.nodesActive = _.filter($scope.nodes, function(node) {
return node.stats.active == true;
}).length;
2015-04-04 21:35:45 +02:00
var bestBlock = _.max($scope.nodes, function(node) {
2015-02-17 10:30:41 +01:00
return parseInt(node.stats.block.number);
2015-04-24 10:44:21 +02:00
}).stats.block.number;
2015-02-17 10:30:41 +01:00
2015-04-24 10:44:21 +02:00
if(bestBlock > $scope.bestBlock)
2015-04-04 21:35:45 +02:00
{
2015-04-24 10:44:21 +02:00
$scope.bestBlock = bestBlock;
2015-04-06 01:28:42 +02:00
$scope.bestStats = _.max($scope.nodes, function(node) {
2015-04-04 21:35:45 +02:00
return parseInt(node.stats.block.number);
2015-04-06 01:28:42 +02:00
}).stats;
2015-02-17 10:30:41 +01:00
2015-05-07 23:58:02 +02:00
$scope.lastBlock = $scope.bestStats.block.arrived;
2015-04-24 18:49:39 +02:00
$scope.lastDifficulty = $scope.bestStats.block.difficulty;
2015-04-04 21:35:45 +02:00
}
2015-03-28 00:27:40 +01:00
2015-04-04 21:35:45 +02:00
$scope.upTimeTotal = _.reduce($scope.nodes, function(total, node) {
return total + node.stats.uptime;
}, 0) / $scope.nodes.length;
2015-03-28 19:07:15 +01:00
2015-02-17 10:30:41 +01:00
$scope.map = _.map($scope.nodes, function(node) {
2015-04-17 02:12:15 +02:00
var fill = $filter('bubbleClass')(node.stats, $scope.bestBlock);
2015-02-17 10:30:41 +01:00
if(node.geo != null)
return {
2015-04-17 02:12:15 +02:00
radius: 3,
2015-02-17 10:30:41 +01:00
latitude: node.geo.ll[0],
longitude: node.geo.ll[1],
2015-04-17 02:12:15 +02:00
nodeName: node.info.name,
fillClass: "text-" + fill,
fillKey: fill,
2015-02-17 10:30:41 +01:00
};
else
return {
radius: 0,
latitude: 0,
longitude: 0
};
});
}
2015-02-17 06:12:44 +01:00
$scope.$apply();
2015-01-29 17:50:16 +01:00
}
2015-04-29 12:58:09 +02:00
});