ethstats-server/public/js/controllers.js

206 lines
4.9 KiB
JavaScript
Raw Normal View History

2015-01-20 19:29:31 +01:00
'use strict';
/* Controllers */
2015-02-17 11:14:48 +01:00
function StatsCtrl($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-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-03-28 00:27:40 +01:00
$scope.difficultyChange = [];
$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-02-08 16:03:05 +01:00
2015-04-06 04:57:13 +02:00
$scope.predicate = 'info.name';
2015-04-06 04:40:29 +02:00
$scope.reverse = false;
$scope.orderTable = function(predicate, reverse)
{
if(predicate != $scope.predicate)
{
$scope.reverse = reverse;
$scope.predicate = predicate;
}
else
{
$scope.reverse = !$scope.reverse;
}
}
2015-02-23 15:48:00 +01:00
$scope.timeout = setInterval(function(){
$scope.$apply();
}, 1000);
2015-01-20 19:29:31 +01:00
// Socket listeners
// ----------------
2015-02-17 06:12:44 +01:00
socket = new Primus();
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-02-17 10:30:41 +01:00
function socketAction(action, data)
2015-02-08 16:03:05 +01:00
{
2015-02-17 11:14:48 +01:00
console.log('Action: ', action);
console.log('Data: ', data);
2015-02-17 10:30:41 +01:00
switch(action) {
2015-02-17 11:14:48 +01:00
case "init":
$scope.nodes = data;
2015-02-17 19:14:38 +01:00
2015-02-17 21:45:30 +01:00
if($scope.nodes.length > 0) toastr['success']("Got nodes list", "Got nodes!");
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-02-17 10:30:41 +01:00
break;
case "update":
2015-04-05 18:22:49 +02:00
$scope.nodes[findIndex({id: data.id})].stats = data.stats;
2015-02-17 10:30:41 +01:00
break;
case "info":
$scope.nodes[findIndex({id: data.id})].info = data.info;
2015-02-17 11:14:48 +01:00
break;
2015-02-18 07:06:41 +01:00
case "inactive":
$scope.nodes[findIndex({id: data.id})].stats = data.stats;
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-02-18 07:06:41 +01:00
break;
2015-04-03 06:00:06 +02:00
case "latency":
$scope.nodes[findIndex({id: data.id})].stats.latency = data.latency;
break;
2015-02-17 10:30:41 +01:00
}
2015-01-29 17:50:16 +01: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-02-17 19:14:38 +01:00
function addNewNode(data)
{
var index = findIndex({id: data.id});
if(index < 0)
{
$scope.nodes.push(data);
return true;
}
$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);
}).stats.block.number;
2015-04-04 21:35:45 +02:00
if(bestBlock > $scope.bestBlock)
{
$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-04-06 01:28:42 +02:00
$scope.lastBlock = $scope.bestStats.block.received;
$scope.lastBlocksTime = $scope.bestStats.blockTimes;
$scope.difficultyChange = $scope.bestStats.difficulty;
$scope.transactionDensity = $scope.bestStats.txDensity;
$scope.gasSpending = $scope.bestStats.gasSpending;
$scope.miners = $scope.bestStats.miners;
2015-03-27 17:52:42 +01:00
2015-04-06 01:28:42 +02:00
$scope.getNumber = function(num) {
return new Array(num);
}
2015-04-05 18:22:49 +02:00
2015-04-06 01:28:42 +02:00
jQuery('.spark-blocktimes').sparkline($scope.lastBlocksTime.reverse(), {type: 'bar', tooltipSuffix: 's'});
2015-04-04 21:35:45 +02:00
jQuery('.spark-difficulty').sparkline($scope.difficultyChange.reverse(), {type: 'bar'});
jQuery('.spark-transactions').sparkline($scope.transactionDensity.reverse(), {type: 'bar'});
jQuery('.spark-gasspending').sparkline($scope.gasSpending.reverse(), {type: 'bar'});
}
2015-03-28 00:27:40 +01:00
2015-04-04 21:35:45 +02:00
$scope.lastDifficulty = _.max($scope.nodes, function(node) {
2015-03-28 19:07:15 +01:00
return parseInt(node.stats.block.number);
2015-04-04 21:35:45 +02:00
}).stats.block.difficulty;
2015-03-28 19:07:15 +01:00
2015-04-04 21:35:45 +02:00
$scope.avgBlockTime = _.max($scope.nodes, function(node) {
return parseInt(node.stats.block.number);
}).stats.blocktimeAvg;
$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) {
if(node.geo != null)
return {
radius: 3,
latitude: node.geo.ll[0],
longitude: node.geo.ll[1],
2015-02-18 03:26:30 +01:00
fillKey: $filter('bubbleClass')(node.stats, $scope.bestBlock)
2015-02-17 10:30:41 +01:00
};
else
return {
radius: 0,
latitude: 0,
longitude: 0
};
});
2015-02-18 03:26:30 +01:00
2015-03-27 17:52:42 +01:00
// console.log($scope.map);
2015-02-17 10:30:41 +01:00
}
2015-02-17 06:12:44 +01:00
$scope.$apply();
2015-01-29 17:50:16 +01:00
}
2015-01-20 19:29:31 +01:00
}