2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-01-12 15:10:02 +00:00
|
|
|
.controller('SwarmVisualizerController', ['$q', '$scope', '$document', '$interval', 'NodeService', 'ServiceService', 'TaskService', 'Notifications',
|
|
|
|
function ($q, $scope, $document, $interval, NodeService, ServiceService, TaskService, Notifications) {
|
2017-09-14 06:04:59 +00:00
|
|
|
|
|
|
|
$scope.state = {
|
|
|
|
ShowInformationPanel: true,
|
2018-01-12 15:10:02 +00:00
|
|
|
DisplayOnlyRunningTasks: false,
|
|
|
|
refreshRate: '5'
|
2017-09-14 06:04:59 +00:00
|
|
|
};
|
|
|
|
|
2018-01-12 15:10:02 +00:00
|
|
|
$scope.$on('$destroy', function() {
|
|
|
|
stopRepeater();
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.changeUpdateRepeater = function() {
|
|
|
|
stopRepeater();
|
|
|
|
setUpdateRepeater();
|
|
|
|
$('#refreshRateChange').show();
|
|
|
|
$('#refreshRateChange').fadeOut(1500);
|
|
|
|
};
|
|
|
|
|
|
|
|
function stopRepeater() {
|
|
|
|
var repeater = $scope.repeater;
|
|
|
|
if (angular.isDefined(repeater)) {
|
|
|
|
$interval.cancel(repeater);
|
|
|
|
repeater = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setUpdateRepeater() {
|
|
|
|
var refreshRate = $scope.state.refreshRate;
|
|
|
|
$scope.repeater = $interval(function() {
|
|
|
|
$q.all({
|
|
|
|
nodes: NodeService.nodes(),
|
|
|
|
services: ServiceService.services(),
|
|
|
|
tasks: TaskService.tasks()
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
var nodes = data.nodes;
|
|
|
|
$scope.nodes = nodes;
|
|
|
|
var services = data.services;
|
|
|
|
$scope.services = services;
|
|
|
|
var tasks = data.tasks;
|
|
|
|
$scope.tasks = tasks;
|
|
|
|
prepareVisualizerData(nodes, services, tasks);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
stopRepeater();
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve cluster information');
|
|
|
|
});
|
|
|
|
}, refreshRate * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-01 22:10:14 +00:00
|
|
|
function assignServiceInfo(services, tasks) {
|
2017-09-14 06:04:59 +00:00
|
|
|
for (var i = 0; i < services.length; i++) {
|
|
|
|
var service = services[i];
|
|
|
|
|
|
|
|
for (var j = 0; j < tasks.length; j++) {
|
|
|
|
var task = tasks[j];
|
|
|
|
|
|
|
|
if (task.ServiceId === service.Id) {
|
|
|
|
task.ServiceName = service.Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function assignTasksToNode(nodes, tasks) {
|
|
|
|
for (var i = 0; i < nodes.length; i++) {
|
|
|
|
var node = nodes[i];
|
|
|
|
node.Tasks = [];
|
|
|
|
|
|
|
|
for (var j = 0; j < tasks.length; j++) {
|
|
|
|
var task = tasks[j];
|
|
|
|
|
|
|
|
if (task.NodeId === node.Id) {
|
|
|
|
node.Tasks.push(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareVisualizerData(nodes, services, tasks) {
|
|
|
|
var visualizerData = {};
|
|
|
|
|
2018-03-01 22:10:14 +00:00
|
|
|
assignServiceInfo(services, tasks);
|
2017-09-14 06:04:59 +00:00
|
|
|
assignTasksToNode(nodes, tasks);
|
|
|
|
|
|
|
|
visualizerData.nodes = nodes;
|
|
|
|
$scope.visualizerData = visualizerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initView() {
|
|
|
|
$q.all({
|
|
|
|
nodes: NodeService.nodes(),
|
|
|
|
services: ServiceService.services(),
|
|
|
|
tasks: TaskService.tasks()
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
var nodes = data.nodes;
|
|
|
|
$scope.nodes = nodes;
|
|
|
|
var services = data.services;
|
|
|
|
$scope.services = services;
|
|
|
|
var tasks = data.tasks;
|
|
|
|
$scope.tasks = tasks;
|
|
|
|
prepareVisualizerData(nodes, services, tasks);
|
2018-01-12 15:10:02 +00:00
|
|
|
setUpdateRepeater();
|
2017-09-14 06:04:59 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to initialize cluster visualizer');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initView();
|
|
|
|
}]);
|