ethstats-server/public/js/controllers.js

391 lines
9.0 KiB
JavaScript
Raw Normal View History

2015-01-20 19:29:31 +01:00
/* 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-05-19 21:07:06 +02:00
var timeout = setInterval(function ()
{
$scope.$apply();
}, 200);
2015-02-23 15:48:00 +01:00
2015-05-19 10:07:48 +02: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":
2015-05-19 07:08:52 +02:00
var oldNodes = [];
if( $scope.nodes.length > 0 ){
oldNodes = $scope.nodes;
}
2015-02-17 11:14:48 +01:00
$scope.nodes = data;
2015-04-17 11:10:20 +02:00
2015-05-19 10:07:48 +02:00
_.forEach($scope.nodes, function (node, index) {
2015-05-19 02:32:38 +02:00
// Init hashrate
2015-05-19 07:08:52 +02:00
if( _.isUndefined(node.stats.hashrate) )
2015-04-24 12:39:26 +02:00
$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 07:08:52 +02:00
// Init or recover pin
$scope.nodes[index].pinned = _.result(_.find(oldNodes, 'id', node.id), 'pinned', false);
2015-04-17 11:10:20 +02:00
});
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-05-19 10:07:48 +02:00
var index = findIndex({id: data.id});
2015-04-17 11:10:20 +02:00
2015-05-19 10:07:48 +02:00
if( addNewNode(data) )
2015-05-19 14:59:29 +02:00
toastr['success']("New node "+ $scope.nodes[findIndex({id: data.id})].info.name +" connected!", "New node!");
2015-05-19 10:07:48 +02:00
else
toastr['info']("Node "+ $scope.nodes[index].info.name +" reconnected!", "Node is back!");
2015-02-17 10:30:41 +01:00
break;
case "update":
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 14:59:29 +02:00
if( index >= 0 && !_.isUndefined($scope.nodes[index]) && !_.isUndefined($scope.nodes[index].stats) )
{
if( _.isUndefined(data.stats.hashrate) )
data.stats.hashrate = 0;
2015-05-18 17:33:20 +02:00
2015-05-19 10:07:48 +02:00
if( $scope.nodes[index].stats.block.number < data.stats.block.number )
2015-05-07 23:58:02 +02:00
{
2015-05-19 10:07:48 +02:00
var best = _.max($scope.nodes, function (node) {
2015-05-07 23:58:02 +02:00
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-05-19 10:07:48 +02:00
$scope.nodes[index].history = data.history;
2015-05-07 23:58:02 +02:00
}
2015-04-28 11:01:34 +02:00
$scope.nodes[index].stats = data.stats;
}
2015-04-17 11:10:20 +02:00
2015-02-17 10:30:41 +01:00
break;
case "info":
2015-05-19 10:07:48 +02:00
var index = findIndex({id: data.id});
2015-05-19 14:59:29 +02:00
if( index >= 0 )
{
$scope.nodes[index].info = data.info;
2015-04-17 11:10:20 +02:00
2015-05-19 14:59:29 +02:00
if( _.isUndefined($scope.nodes[index].pinned) )
$scope.nodes[index].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.uncleCount = data[0] + data[1];
2015-05-19 10:07:48 +02:00
$scope.uncleCountChart = data.reverse();
2015-04-24 05:23:26 +02:00
break;
2015-04-28 09:43:56 +02:00
case "charts":
2015-05-20 16:17:59 +02:00
if( !_.isEqual($scope.avgBlockTime, data.avgBlocktime) )
$scope.avgBlockTime = data.avgBlocktime;
if( !_.isEqual($scope.avgHashrate, data.avgHashrate) )
$scope.avgHashrate = data.avgHashrate;
if( !_.isEqual($scope.lastBlocksTime, data.blocktime) )
$scope.lastBlocksTime = data.blocktime;
if( !_.isEqual($scope.difficultyChart, data.difficulty) )
$scope.difficultyChart = data.difficulty;
if( !_.isEqual($scope.blockPropagationChart, data.propagation.histogram) ) {
$scope.blockPropagationChart = data.propagation.histogram;
$scope.blockPropagationAvg = data.propagation.avg;
}
if( !_.isEqual($scope.uncleCountChart, data.uncleCount.reverse()) ) {
$scope.uncleCount = data.uncleCount[0] + data.uncleCount[1];
$scope.uncleCountChart = data.uncleCount.reverse();
}
if( !_.isEqual($scope.transactionDensity, data.transactions) )
$scope.transactionDensity = data.transactions;
if( !_.isEqual($scope.gasSpending, data.gasSpending) )
$scope.gasSpending = data.gasSpending;
if( !_.isEqual($scope.miners, data.miners) ) {
$scope.miners = data.miners;
getMinersNames();
}
2015-04-28 09:43:56 +02:00
break;
2015-02-18 07:06:41 +01:00
case "inactive":
2015-05-19 10:07:48 +02:00
var index = findIndex({id: data.id});
2015-05-19 14:59:29 +02:00
if( index >= 0 )
{
if( !_.isUndefined(data.stats) )
$scope.nodes[index].stats = data.stats;
2015-04-24 12:39:26 +02:00
2015-05-19 14:59:29 +02:00
toastr['error']("Node "+ $scope.nodes[index].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-05-19 10:07:48 +02:00
var index = findIndex({id: data.id});
2015-04-28 11:01:34 +02:00
2015-05-19 14:59:29 +02:00
if( !_.isUndefined(data.id) && index >= 0 )
{
2015-05-19 10:07:48 +02:00
var node = $scope.nodes[index];
2015-04-17 11:10:20 +02:00
2015-05-19 14:59:29 +02:00
if( !_.isUndefined(node) && !_.isUndefined(node.stats) && !_.isUndefined(node.stats.latency) )
$scope.nodes[index].stats.latency = data.latency;
}
2015-05-19 02:32:38 +02:00
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 10:07:48 +02:00
if( action !== "latency" && action !== "client-ping" )
2015-05-19 02:32:38 +02:00
{
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-29 11:00:01 +02:00
function getMinersNames()
{
2015-05-19 10:07:48 +02:00
if( $scope.miners.length > 0 )
2015-04-29 11:03:05 +02:00
{
2015-05-19 10:07:48 +02:00
_.forIn($scope.miners, function (value, key)
2015-04-29 11:00:01 +02:00
{
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');
2015-05-19 10:07:48 +02:00
if( !_.isUndefined(name) )
2015-04-29 11:00:01 +02:00
$scope.miners[key].name = name;
});
}
}
2015-02-17 19:14:38 +01:00
function addNewNode(data)
{
var index = findIndex({id: data.id});
2015-04-24 12:39:26 +02:00
2015-05-19 10:07:48 +02:00
if( _.isUndefined(data.history) )
{
data.history = new Array(40);
_.fill(data.history, -1);
}
2015-05-19 01:41:14 +02:00
2015-05-19 10:07:48 +02:00
if( index < 0 )
{
if( !_.isUndefined(data.stats) && _.isUndefined(data.stats.hashrate) )
2015-05-19 02:32:38 +02:00
{
2015-05-19 10:07:48 +02:00
data.stats.hashrate = 0;
2015-05-19 02:32:38 +02:00
}
2015-05-19 10:07:48 +02:00
data.pinned = false;
2015-02-17 19:14:38 +01:00
$scope.nodes.push(data);
return true;
}
2015-05-19 10:07:48 +02:00
data.pinned = ( !_.isUndefined($scope.nodes[index].pinned) ? $scope.nodes[index].pinned : false);
2015-05-19 10:07:48 +02:00
if( !_.isUndefined($scope.nodes[index].history) )
2015-05-19 02:32:38 +02:00
{
2015-05-19 10:07:48 +02:00
data.history = $scope.nodes[index].history;
2015-05-19 02:32:38 +02:00
}
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-05-19 10:07:48 +02:00
if( $scope.nodes.length )
2015-02-17 10:30:41 +01:00
{
$scope.nodesTotal = $scope.nodes.length;
2015-05-19 10:07:48 +02:00
$scope.nodesActive = _.filter($scope.nodes, function (node) {
2015-02-17 10:30:41 +01:00
return node.stats.active == true;
}).length;
2015-05-19 10:07:48 +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-05-19 10:07:48 +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-05-19 10:07:48 +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-05-19 10:07:48 +02:00
$scope.upTimeTotal = _.reduce($scope.nodes, function (total, node) {
2015-04-04 21:35:45 +02:00
return total + node.stats.uptime;
}, 0) / $scope.nodes.length;
2015-03-28 19:07:15 +01:00
2015-05-19 10:07:48 +02: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-05-20 16:02:35 +02:00
// $scope.$apply();
2015-02-17 10:30:41 +01:00
}
2015-01-29 17:50:16 +01:00
}
2015-04-29 12:58:09 +02:00
});