2019-03-21 05:46:49 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('ContainerStatsController', [
|
|
|
|
'$q',
|
|
|
|
'$scope',
|
|
|
|
'$transition$',
|
|
|
|
'$document',
|
|
|
|
'$interval',
|
|
|
|
'ContainerService',
|
|
|
|
'ChartService',
|
|
|
|
'Notifications',
|
|
|
|
'HttpRequestHelper',
|
|
|
|
function ($q, $scope, $transition$, $document, $interval, ContainerService, ChartService, Notifications, HttpRequestHelper) {
|
|
|
|
$scope.state = {
|
|
|
|
refreshRate: '5',
|
|
|
|
networkStatsUnavailable: false,
|
2021-06-14 03:57:00 +00:00
|
|
|
ioStatsUnavailable: false,
|
2020-04-10 21:54:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
stopRepeater();
|
|
|
|
});
|
|
|
|
|
|
|
|
function stopRepeater() {
|
|
|
|
var repeater = $scope.repeater;
|
|
|
|
if (angular.isDefined(repeater)) {
|
|
|
|
$interval.cancel(repeater);
|
|
|
|
repeater = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateNetworkChart(stats, chart) {
|
|
|
|
if (stats.Networks.length > 0) {
|
|
|
|
var rx = stats.Networks[0].rx_bytes;
|
|
|
|
var tx = stats.Networks[0].tx_bytes;
|
|
|
|
var label = moment(stats.read).format('HH:mm:ss');
|
|
|
|
|
|
|
|
ChartService.UpdateNetworkChart(label, rx, tx, chart);
|
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function updateMemoryChart(stats, chart) {
|
2018-11-08 00:31:33 +00:00
|
|
|
var label = moment(stats.read).format('HH:mm:ss');
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
ChartService.UpdateMemoryChart(label, stats.MemoryUsage, stats.MemoryCache, chart);
|
2017-09-27 07:47:11 +00:00
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2021-06-14 03:57:00 +00:00
|
|
|
function updateIOChart(stats, chart) {
|
|
|
|
var label = moment(stats.read).format('HH:mm:ss');
|
|
|
|
if (stats.noIOData !== true) {
|
|
|
|
ChartService.UpdateIOChart(label, stats.BytesRead, stats.BytesWrite, chart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function updateCPUChart(stats, chart) {
|
|
|
|
var label = moment(stats.read).format('HH:mm:ss');
|
|
|
|
var value = stats.isWindows ? calculateCPUPercentWindows(stats) : calculateCPUPercentUnix(stats);
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
ChartService.UpdateCPUChart(label, value, chart);
|
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function calculateCPUPercentUnix(stats) {
|
|
|
|
var cpuPercent = 0.0;
|
|
|
|
var cpuDelta = stats.CurrentCPUTotalUsage - stats.PreviousCPUTotalUsage;
|
|
|
|
var systemDelta = stats.CurrentCPUSystemUsage - stats.PreviousCPUSystemUsage;
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (systemDelta > 0.0 && cpuDelta > 0.0) {
|
|
|
|
cpuPercent = (cpuDelta / systemDelta) * stats.CPUCores * 100.0;
|
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return cpuPercent;
|
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function calculateCPUPercentWindows(stats) {
|
|
|
|
var possIntervals =
|
|
|
|
stats.NumProcs * parseFloat(moment(stats.read, 'YYYY-MM-DDTHH:mm:ss.SSSSSSSSSZ').valueOf() - moment(stats.preread, 'YYYY-MM-DDTHH:mm:ss.SSSSSSSSSZ').valueOf());
|
|
|
|
var windowsCpuUsage = 0.0;
|
|
|
|
if (possIntervals > 0) {
|
|
|
|
windowsCpuUsage = parseFloat(stats.CurrentCPUTotalUsage - stats.PreviousCPUTotalUsage) / parseFloat(possIntervals * 100);
|
|
|
|
}
|
|
|
|
return windowsCpuUsage;
|
2017-09-09 16:49:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.changeUpdateRepeater = function () {
|
|
|
|
var networkChart = $scope.networkChart;
|
|
|
|
var cpuChart = $scope.cpuChart;
|
|
|
|
var memoryChart = $scope.memoryChart;
|
2021-06-14 03:57:00 +00:00
|
|
|
var ioChart = $scope.ioChart;
|
2017-09-09 16:49:21 +00:00
|
|
|
|
|
|
|
stopRepeater();
|
2021-06-14 03:57:00 +00:00
|
|
|
setUpdateRepeater(networkChart, cpuChart, memoryChart, ioChart);
|
2020-04-10 21:54:53 +00:00
|
|
|
$('#refreshRateChange').show();
|
|
|
|
$('#refreshRateChange').fadeOut(1500);
|
|
|
|
};
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2021-06-14 03:57:00 +00:00
|
|
|
function startChartUpdate(networkChart, cpuChart, memoryChart, ioChart) {
|
2017-09-09 16:49:21 +00:00
|
|
|
$q.all({
|
2017-09-21 14:00:53 +00:00
|
|
|
stats: ContainerService.containerStats($transition$.params().id),
|
2020-04-10 21:54:53 +00:00
|
|
|
top: ContainerService.containerTop($transition$.params().id),
|
2017-09-09 16:49:21 +00:00
|
|
|
})
|
2020-04-10 21:54:53 +00:00
|
|
|
.then(function success(data) {
|
|
|
|
var stats = data.stats;
|
|
|
|
$scope.processInfo = data.top;
|
|
|
|
if (stats.Networks.length === 0) {
|
|
|
|
$scope.state.networkStatsUnavailable = true;
|
|
|
|
}
|
2021-06-14 03:57:00 +00:00
|
|
|
if (stats.noIOData === true) {
|
|
|
|
$scope.state.ioStatsUnavailable = true;
|
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
updateNetworkChart(stats, networkChart);
|
|
|
|
updateMemoryChart(stats, memoryChart);
|
|
|
|
updateCPUChart(stats, cpuChart);
|
2021-06-14 03:57:00 +00:00
|
|
|
updateIOChart(stats, ioChart);
|
|
|
|
setUpdateRepeater(networkChart, cpuChart, memoryChart, ioChart);
|
2020-04-10 21:54:53 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
stopRepeater();
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve container statistics');
|
|
|
|
});
|
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2021-06-14 03:57:00 +00:00
|
|
|
function setUpdateRepeater(networkChart, cpuChart, memoryChart, ioChart) {
|
2020-04-10 21:54:53 +00:00
|
|
|
var refreshRate = $scope.state.refreshRate;
|
|
|
|
$scope.repeater = $interval(function () {
|
|
|
|
$q.all({
|
|
|
|
stats: ContainerService.containerStats($transition$.params().id),
|
|
|
|
top: ContainerService.containerTop($transition$.params().id),
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
var stats = data.stats;
|
|
|
|
$scope.processInfo = data.top;
|
|
|
|
updateNetworkChart(stats, networkChart);
|
|
|
|
updateMemoryChart(stats, memoryChart);
|
|
|
|
updateCPUChart(stats, cpuChart);
|
2021-06-14 03:57:00 +00:00
|
|
|
updateIOChart(stats, ioChart);
|
2020-04-10 21:54:53 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
stopRepeater();
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve container statistics');
|
|
|
|
});
|
|
|
|
}, refreshRate * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function initCharts() {
|
|
|
|
var networkChartCtx = $('#networkChart');
|
|
|
|
var networkChart = ChartService.CreateNetworkChart(networkChartCtx);
|
|
|
|
$scope.networkChart = networkChart;
|
|
|
|
|
|
|
|
var cpuChartCtx = $('#cpuChart');
|
|
|
|
var cpuChart = ChartService.CreateCPUChart(cpuChartCtx);
|
|
|
|
$scope.cpuChart = cpuChart;
|
|
|
|
|
|
|
|
var memoryChartCtx = $('#memoryChart');
|
|
|
|
var memoryChart = ChartService.CreateMemoryChart(memoryChartCtx);
|
|
|
|
$scope.memoryChart = memoryChart;
|
|
|
|
|
2021-06-14 03:57:00 +00:00
|
|
|
var ioChartCtx = $('#ioChart');
|
|
|
|
var ioChart = ChartService.CreateIOChart(ioChartCtx);
|
|
|
|
$scope.ioChart = ioChart;
|
|
|
|
|
|
|
|
startChartUpdate(networkChart, cpuChart, memoryChart, ioChart);
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initView() {
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader($transition$.params().nodeName);
|
|
|
|
ContainerService.container($transition$.params().id)
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.container = data;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve container information');
|
|
|
|
});
|
|
|
|
|
|
|
|
$document.ready(function () {
|
|
|
|
initCharts();
|
|
|
|
});
|
|
|
|
}
|
2017-09-09 16:49:21 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
initView();
|
|
|
|
},
|
|
|
|
]);
|