1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-08 23:35:31 +02:00

feat(log-viewer): introduce the log viewer component (#1666)

This commit is contained in:
Anthony Lapenna 2018-02-28 07:19:28 +01:00 committed by GitHub
parent 81de2a5afb
commit 0c5152fb5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 458 additions and 304 deletions

View file

@ -1,78 +1,69 @@
angular.module('portainer.docker')
.controller('ServiceLogsController', ['$scope', '$transition$', '$anchorScroll', 'ServiceLogs', 'Service',
function ($scope, $transition$, $anchorScroll, ServiceLogs, Service) {
$scope.state = {};
$scope.state.displayTimestampsOut = false;
$scope.state.displayTimestampsErr = false;
$scope.stdout = '';
$scope.stderr = '';
$scope.tailLines = 2000;
.controller('ServiceLogsController', ['$scope', '$transition$', '$interval', 'ServiceService', 'Notifications',
function ($scope, $transition$, $interval, ServiceService, Notifications) {
$scope.state = {
refreshRate: 3,
lineCount: 2000
};
function getLogs() {
getLogsStdout();
getLogsStderr();
$scope.changeLogCollection = function(logCollectionStatus) {
if (!logCollectionStatus) {
stopRepeater();
} else {
setUpdateRepeater();
}
};
$scope.$on('$destroy', function() {
stopRepeater();
});
function stopRepeater() {
var repeater = $scope.repeater;
if (angular.isDefined(repeater)) {
$interval.cancel(repeater);
repeater = null;
}
}
function getLogsStderr() {
ServiceLogs.get($transition$.params().id, {
stdout: 0,
stderr: 1,
timestamps: $scope.state.displayTimestampsErr,
tail: $scope.tailLines
}, function (data, status, headers, config) {
// Replace carriage returns with newlines to clean up output
data = data.replace(/[\r]/g, '\n');
// Strip 8 byte header from each line of output
data = data.substring(8);
data = data.replace(/\n(.{8})/g, '\n');
$scope.stderr = data;
});
function setUpdateRepeater() {
var refreshRate = $scope.state.refreshRate;
$scope.repeater = $interval(function() {
ServiceService.logs($transition$.params().id, 1, 1, 0, $scope.state.lineCount)
.then(function success(data) {
$scope.logs = data;
})
.catch(function error(err) {
stopRepeater();
Notifications.error('Failure', err, 'Unable to retrieve service logs');
});
}, refreshRate * 1000);
}
function getLogsStdout() {
ServiceLogs.get($transition$.params().id, {
stdout: 1,
stderr: 0,
timestamps: $scope.state.displayTimestampsOut,
tail: $scope.tailLines
}, function (data, status, headers, config) {
// Replace carriage returns with newlines to clean up output
data = data.replace(/[\r]/g, '\n');
// Strip 8 byte header from each line of output
data = data.substring(8);
data = data.replace(/\n(.{8})/g, '\n');
$scope.stdout = data;
});
}
function getService() {
Service.get({id: $transition$.params().id}, function (d) {
$scope.service = d;
}, function (e) {
Notifications.error('Failure', e, 'Unable to retrieve service info');
function startLogPolling() {
ServiceService.logs($transition$.params().id, 1, 1, 0, $scope.state.lineCount)
.then(function success(data) {
$scope.logs = data;
console.log(JSON.stringify(data, null, 4));
setUpdateRepeater();
})
.catch(function error(err) {
stopRepeater();
Notifications.error('Failure', err, 'Unable to retrieve service logs');
});
}
function initView() {
getService();
getLogs();
var logIntervalId = window.setInterval(getLogs, 5000);
$scope.$on('$destroy', function () {
// clearing interval when view changes
clearInterval(logIntervalId);
ServiceService.service($transition$.params().id)
.then(function success(data) {
$scope.service = data;
startLogPolling();
})
.catch(function error(err) {
Notifications.error('Failure', err, 'Unable to retrieve service information');
});
$scope.toggleTimestampsOut = function () {
getLogsStdout();
};
$scope.toggleTimestampsErr = function () {
getLogsStderr();
};
}
initView();
}]);