ethstats-server/public/js/controllers.js

117 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-01-20 19:29:31 +01:00
'use strict';
/* Controllers */
2015-02-08 16:03:05 +01:00
function StatsCtrl($scope, $filter, socket, _) {
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;
$scope.upTimeTotal = 0;
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 06:12:44 +01:00
self._socket = false;
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) {
2015-02-17 06:12:44 +01:00
console.log('Received some data', data);
2015-02-17 10:30:41 +01:00
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 06:12:44 +01:00
console.log(data);
2015-01-20 19:29:31 +01:00
$scope.nodes = data.nodes;
2015-01-29 17:50:16 +01:00
updateStats();
});
2015-02-17 10:30:41 +01:00
function socketAction(action, data)
2015-02-08 16:03:05 +01:00
{
2015-02-17 10:30:41 +01:00
switch(action) {
case "add":
console.log(data);
$scope.nodes.push(data);
break;
case "update":
console.log(data);
$scope.nodes[findIndex({id: data.id})].stats = data.stats;
break;
case "info":
console.log(data);
$scope.nodes[findIndex({id: data.id})].info = data.info;
}
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
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);
}).stats.block.timestamp;
$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],
fillKey: $filter('bubbleClass')(node, $scope.bestBlock)
};
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-01-20 19:29:31 +01:00
}