0bf4e71b79
When a container is opened from a stack, the detail tab kept the stack trail (PR #7) but the attribute sub-tabs (Logs, Stats, Inspect, Console, Attach) dropped it: those tabs were registered only under the global docker.containers.container.* tree, so navigating to one left the stack state (and its inherited params) behind, and each sub-view set a hardcoded "Containers > ..." breadcrumb. - Register stack-scoped child states docker.stacks.stack.container.{attach, exec,inspect,logs,stats} mirroring the global ones, so the inherited stack params survive and the trail can be kept. - Centralize the breadcrumb logic in containerBreadcrumbs.ts (moved out of ItemView, which re-exports it) and add isStackContainerState + getContainerSubTabBreadcrumbs + buildStackContainerLinkParams. - ActionLinksRow links sub-tabs into the stack tree (with stack+container params) when opened from a stack, else the global states unchanged. - InspectView + the logs/stats/console controllers render the stack-aware trail; set up-front (no name) so it survives the load window and errors. Covers regular/external/orphaned stacks and the non-stack fallback, matching the existing ItemView breadcrumb behavior. New unit tests in containerBreadcrumbs.test.ts. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
187 lines
6.6 KiB
JavaScript
187 lines
6.6 KiB
JavaScript
import moment from 'moment';
|
|
|
|
import { trimContainerName } from '@/docker/filters/utils';
|
|
import { getContainerSubTabBreadcrumbs } from '@/react/docker/containers/ItemView/containerBreadcrumbs';
|
|
|
|
angular.module('portainer.docker').controller('ContainerStatsController', [
|
|
'$q',
|
|
'$scope',
|
|
'$transition$',
|
|
'$document',
|
|
'$interval',
|
|
'ContainerService',
|
|
'ChartService',
|
|
'Notifications',
|
|
'HttpRequestHelper',
|
|
'endpoint',
|
|
function ($q, $scope, $transition$, $document, $interval, ContainerService, ChartService, Notifications, HttpRequestHelper, endpoint) {
|
|
$scope.state = {
|
|
refreshRate: '5',
|
|
networkStatsUnavailable: false,
|
|
ioStatsUnavailable: false,
|
|
};
|
|
|
|
$scope.$on('$destroy', function () {
|
|
stopRepeater();
|
|
});
|
|
|
|
function stopRepeater() {
|
|
var repeater = $scope.repeater;
|
|
if (angular.isDefined(repeater)) {
|
|
$interval.cancel(repeater);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
function updateMemoryChart(stats, chart) {
|
|
var label = moment(stats.read).format('HH:mm:ss');
|
|
|
|
ChartService.UpdateMemoryChart(label, stats.MemoryUsage, stats.MemoryCache, chart);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
function updateCPUChart(stats, chart) {
|
|
var label = moment(stats.read).format('HH:mm:ss');
|
|
var value = stats.isWindows ? calculateCPUPercentWindows(stats) : calculateCPUPercentUnix(stats);
|
|
|
|
ChartService.UpdateCPUChart(label, value, chart);
|
|
}
|
|
|
|
function calculateCPUPercentUnix(stats) {
|
|
var cpuPercent = 0.0;
|
|
var cpuDelta = stats.CurrentCPUTotalUsage - stats.PreviousCPUTotalUsage;
|
|
var systemDelta = stats.CurrentCPUSystemUsage - stats.PreviousCPUSystemUsage;
|
|
|
|
if (systemDelta > 0.0 && cpuDelta > 0.0) {
|
|
cpuPercent = (cpuDelta / systemDelta) * stats.CPUCores * 100.0;
|
|
}
|
|
|
|
return cpuPercent;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
$scope.changeUpdateRepeater = function () {
|
|
var networkChart = $scope.networkChart;
|
|
var cpuChart = $scope.cpuChart;
|
|
var memoryChart = $scope.memoryChart;
|
|
var ioChart = $scope.ioChart;
|
|
|
|
stopRepeater();
|
|
setUpdateRepeater(networkChart, cpuChart, memoryChart, ioChart);
|
|
$('#refreshRateChange').show();
|
|
$('#refreshRateChange').fadeOut(1500);
|
|
};
|
|
|
|
function startChartUpdate(networkChart, cpuChart, memoryChart, ioChart) {
|
|
$q.all({
|
|
stats: ContainerService.containerStats(endpoint.Id, $transition$.params().id),
|
|
})
|
|
.then(function success(data) {
|
|
var stats = data.stats;
|
|
if (stats.Networks.length === 0) {
|
|
$scope.state.networkStatsUnavailable = true;
|
|
}
|
|
if (stats.noIOData === true) {
|
|
$scope.state.ioStatsUnavailable = true;
|
|
}
|
|
updateNetworkChart(stats, networkChart);
|
|
updateMemoryChart(stats, memoryChart);
|
|
updateCPUChart(stats, cpuChart);
|
|
updateIOChart(stats, ioChart);
|
|
setUpdateRepeater(networkChart, cpuChart, memoryChart, ioChart);
|
|
})
|
|
.catch(function error(err) {
|
|
stopRepeater();
|
|
Notifications.error('Failure', err, 'Unable to retrieve container statistics');
|
|
});
|
|
}
|
|
|
|
function setUpdateRepeater(networkChart, cpuChart, memoryChart, ioChart) {
|
|
var refreshRate = $scope.state.refreshRate;
|
|
$scope.repeater = $interval(function () {
|
|
$q.all({
|
|
stats: ContainerService.containerStats(endpoint.Id, $transition$.params().id),
|
|
})
|
|
.then(function success(data) {
|
|
var stats = data.stats;
|
|
updateNetworkChart(stats, networkChart);
|
|
updateMemoryChart(stats, memoryChart);
|
|
updateCPUChart(stats, cpuChart);
|
|
updateIOChart(stats, ioChart);
|
|
})
|
|
.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;
|
|
|
|
var ioChartCtx = $('#ioChart');
|
|
var ioChart = ChartService.CreateIOChart(ioChartCtx);
|
|
$scope.ioChart = ioChart;
|
|
|
|
startChartUpdate(networkChart, cpuChart, memoryChart, ioChart);
|
|
}
|
|
|
|
function initView() {
|
|
HttpRequestHelper.setPortainerAgentTargetHeader($transition$.params().nodeName);
|
|
// Set the trail up-front (without the container name) so it survives the
|
|
// load window and a load error; the success path fills in the name.
|
|
$scope.breadcrumbs = getContainerSubTabBreadcrumbs($transition$.to().name, $transition$.params(), '', 'Stats');
|
|
ContainerService.container(endpoint.Id, $transition$.params().id)
|
|
.then(function success(data) {
|
|
$scope.container = data;
|
|
// Stack-aware breadcrumb: keeps the stack trail when the container was
|
|
// opened from a stack, falls back to the global Containers trail otherwise.
|
|
$scope.breadcrumbs = getContainerSubTabBreadcrumbs($transition$.to().name, $transition$.params(), trimContainerName(data.Name), 'Stats');
|
|
})
|
|
.catch(function error(err) {
|
|
Notifications.error('Failure', err, 'Unable to retrieve container information');
|
|
});
|
|
|
|
$document.ready(function () {
|
|
initCharts();
|
|
});
|
|
}
|
|
|
|
initView();
|
|
},
|
|
]);
|