ethstats-server/public/js/controllers.js

155 lines
3.4 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-02-05 16:54:04 +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-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":
$scope.nodes[findIndex({id: data.id})].stats = data.stats;
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-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;
$scope.bestBlock = _.max($scope.nodes, function(node) {
return parseInt(node.stats.block.number);
}).stats.block.number;
$scope.lastBlock = _.max($scope.nodes, function(node) {
return parseInt(node.stats.block.timestamp);
2015-02-18 10:10:33 +01:00
}).stats.block.timestamp;
2015-02-17 10:30:41 +01:00
2015-02-18 08:54:04 +01:00
$scope.lastDifficulty = _.max($scope.nodes, function(node) {
return parseInt(node.stats.block.timestamp);
}).stats.block.difficulty;
$scope.avgBlockTime = _.max($scope.nodes, function(node) {
return parseInt(node.stats.block.timestamp);
}).stats.blocktimeAvg;
2015-02-17 10:30:41 +01:00
$scope.upTimeTotal = _.reduce($scope.nodes, function(total, node) {
return total + node.stats.uptime;
}, 0) / $scope.nodes.length;
$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
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
}