1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 23:09:41 +02:00
portainer/app/controllers.js

248 lines
7.3 KiB
JavaScript
Raw Normal View History

function newLineChart(id, data, getkey) {
var chart = getChart(id);
var map = {};
for (var i = 0; i < data.length; i++) {
var c = data[i];
var key = getkey(c);
var count = map[key];
if (count === undefined) {
count = 0;
}
count += 1;
map[key] = count;
}
var labels = [];
var data = [];
var keys = Object.keys(map);
for (var i = keys.length - 1; i > -1; i--) {
var k = keys[i];
labels.push(k);
data.push(map[k]);
}
var dataset = {
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
data : data
};
chart.Line({
labels: labels,
datasets: [dataset]
},
{
scaleStepWidth: 1,
pointDotRadius:1,
scaleOverride: true,
scaleSteps: labels.length
});
}
2013-09-02 16:54:53 -07:00
function getChart(id) {
var ctx = $(id).get(0).getContext("2d");
return new Chart(ctx);
}
function SideBarController($scope, Container, Settings) {
$scope.template = 'partials/sidebar.html';
$scope.containers = [];
$scope.endpoint = Settings.endpoint;
Container.query({all: 0}, function(d) {
2013-06-18 18:01:22 -09:00
$scope.containers = d;
2013-06-19 17:40:58 -09:00
});
}
2013-06-22 09:23:25 -09:00
function SettingsController($scope, System, Docker, Settings, Messages) {
2013-06-09 16:31:05 -09:00
$scope.info = {};
$scope.docker = {};
2013-06-10 15:10:43 -09:00
$scope.endpoint = Settings.endpoint;
$scope.apiVersion = Settings.version;
2013-06-09 16:31:05 -09:00
2013-06-19 18:10:08 -09:00
Docker.get({}, function(d) { $scope.docker = d; });
System.get({}, function(d) { $scope.info = d; });
2013-06-08 16:20:29 -09:00
}
// Controller for the list of images
2013-06-19 17:40:58 -09:00
function ImagesController($scope, Image, ViewSpinner, Messages) {
2013-06-18 19:08:17 -09:00
$scope.toggle = false;
2013-06-22 09:23:25 -09:00
$scope.predicate = '-Created';
2013-06-18 16:21:27 -09:00
$scope.showBuilder = function() {
$('#build-modal').modal('show');
};
2013-06-18 19:08:17 -09:00
$scope.removeAction = function() {
2013-06-19 17:40:58 -09:00
ViewSpinner.spin();
var counter = 0;
var complete = function() {
counter = counter - 1;
if (counter === 0) {
ViewSpinner.stop();
}
};
2013-06-22 09:23:25 -09:00
angular.forEach($scope.images, function(i) {
2013-06-18 19:08:17 -09:00
if (i.Checked) {
2013-06-19 17:40:58 -09:00
counter = counter + 1;
2013-06-18 19:08:17 -09:00
Image.remove({id: i.Id}, function(d) {
2013-06-19 17:40:58 -09:00
angular.forEach(d, function(resource) {
2013-08-08 19:18:51 +00:00
Messages.send("Image deleted", resource.Deleted);
2013-06-19 17:40:58 -09:00
});
var index = $scope.images.indexOf(i);
$scope.images.splice(index, 1);
complete();
}, function(e) {
2013-08-08 19:18:51 +00:00
Messages.error("Failure", e.data);
2013-06-19 17:40:58 -09:00
complete();
2013-06-18 19:08:17 -09:00
});
}
});
};
2013-06-22 09:23:25 -09:00
2013-06-18 19:08:17 -09:00
$scope.toggleSelectAll = function() {
angular.forEach($scope.images, function(i) {
i.Checked = $scope.toggle;
});
};
2013-06-18 18:01:22 -09:00
ViewSpinner.spin();
2013-06-09 14:11:40 -09:00
Image.query({}, function(d) {
2013-06-18 19:08:17 -09:00
$scope.images = d.map(function(item) { return new ImageViewModel(item); });
2013-06-18 18:01:22 -09:00
ViewSpinner.stop();
}, function (e) {
2013-08-08 19:18:51 +00:00
Messages.error("Failure", e.data);
2013-06-18 18:01:22 -09:00
ViewSpinner.stop();
2013-06-18 16:21:27 -09:00
});
2013-06-09 14:11:40 -09:00
}
// Controller for a single image and actions on that image
2013-09-02 16:54:53 -07:00
function ImageController($scope, $q, $routeParams, $location, Image, Container, Messages) {
2013-06-09 14:11:40 -09:00
$scope.history = [];
2013-06-09 14:56:54 -09:00
$scope.tag = {repo: '', force: false};
2013-06-22 09:23:25 -09:00
2013-06-09 14:11:40 -09:00
$scope.remove = function() {
2013-06-22 09:23:25 -09:00
Image.remove({id: $routeParams.id}, function(d) {
2013-08-08 19:18:51 +00:00
Messages.send("Image Removed", $routeParams.id);
2013-06-22 09:23:25 -09:00
}, function(e) {
2013-08-08 19:18:51 +00:00
$scope.error = e.data;
$('#error-message').show();
2013-06-22 09:23:25 -09:00
});
2013-06-09 14:11:40 -09:00
};
$scope.getHistory = function() {
Image.history({id: $routeParams.id}, function(d) {
2013-06-18 14:28:22 -09:00
$scope.history = d;
});
2013-06-09 14:11:40 -09:00
};
$scope.updateTag = function() {
var tag = $scope.tag;
2013-06-09 14:56:54 -09:00
Image.tag({id: $routeParams.id, repo: tag.repo, force: tag.force ? 1 : 0}, function(d) {
2013-08-08 19:18:51 +00:00
Messages.send("Tag Added", $routeParams.id);
}, function(e) {
2013-08-08 19:18:51 +00:00
$scope.error = e.data;
$('#error-message').show();
2013-06-09 14:11:40 -09:00
});
};
2013-06-18 14:28:22 -09:00
2013-06-09 14:11:40 -09:00
Image.get({id: $routeParams.id}, function(d) {
$scope.image = d;
2013-09-02 17:08:06 -07:00
$scope.tag = d.id;
var t = $routeParams.tag;
if (t && t !== ":") {
$scope.tag = t;
var promise = getContainersFromImage($q, Container, t);
2013-09-02 16:54:53 -07:00
promise.then(function(containers) {
newLineChart('#containers-started-chart', containers, function(c) { return new Date(c.Created * 1000).toLocaleDateString(); });
2013-09-02 16:54:53 -07:00
});
}
2013-06-09 16:31:05 -09:00
}, function(e) {
if (e.status === 404) {
$('.detail').hide();
2013-08-08 19:18:51 +00:00
$scope.error = "Image not found.<br />" + $routeParams.id;
} else {
$scope.error = e.data;
}
2013-08-08 19:18:51 +00:00
$('#error-message').show();
2013-06-09 14:11:40 -09:00
});
$scope.getHistory();
}
2013-06-19 18:10:08 -09:00
function StartContainerController($scope, $routeParams, $location, Container, Messages) {
$scope.template = 'partials/startcontainer.html';
2013-06-14 16:46:41 -09:00
$scope.config = {
name: '',
2013-06-14 16:46:41 -09:00
memory: 0,
memorySwap: 0,
cpuShares: 1024,
2013-06-14 16:46:41 -09:00
env: '',
commands: '',
volumesFrom: ''
};
$scope.commandPlaceholder = '["/bin/echo", "Hello world"]';
2013-06-18 14:28:22 -09:00
$scope.create = function() {
var cmds = null;
2013-06-14 16:46:41 -09:00
if ($scope.config.commands !== '') {
cmds = angular.fromJson($scope.config.commands);
}
var id = $routeParams.id;
var ctor = Container;
var loc = $location;
var s = $scope;
Container.create({
2013-06-22 09:23:25 -09:00
Image: id,
name: $scope.config.name,
2013-06-22 09:23:25 -09:00
Memory: $scope.config.memory,
MemorySwap: $scope.config.memorySwap,
CpuShares: $scope.config.cpuShares,
2013-06-22 09:23:25 -09:00
Cmd: cmds,
2013-06-14 16:46:41 -09:00
VolumesFrom: $scope.config.volumesFrom
}, function(d) {
if (d.Id) {
ctor.start({id: d.Id}, function(cd) {
2013-06-18 14:28:22 -09:00
$('#create-modal').modal('hide');
loc.path('/containers/' + d.Id + '/');
}, function(e) {
2013-06-19 18:10:08 -09:00
failedRequestHandler(e, Messages);
});
}
}, function(e) {
2013-06-19 18:10:08 -09:00
failedRequestHandler(e, Messages);
});
};
}
2013-06-10 15:42:34 -09:00
2013-06-19 17:40:58 -09:00
function BuilderController($scope, Dockerfile, Messages) {
$scope.template = 'partials/builder.html';
2013-06-18 15:10:13 -09:00
}
2013-06-19 18:10:08 -09:00
function failedRequestHandler(e, Messages) {
Messages.send({class: 'text-error', data: e.data});
2013-06-10 15:42:34 -09:00
}
2013-09-02 16:54:53 -07:00
// This gonna get messy but we don't have a good way to do this right now
function getContainersFromImage($q, Container, tag) {
var defer = $q.defer();
Container.query({all:1, notruc:1}, function(d) {
var containers = [];
for (var i = 0; i < d.length; i++) {
var c = d[i];
if (c.Image == tag) {
containers.push(new ContainerViewModel(c));
}
}
defer.resolve(containers);
});
return defer.promise;
}