2015-08-25 05:55:54 +00:00
|
|
|
angular.module('stats', [])
|
2015-08-27 06:41:45 +00:00
|
|
|
.controller('StatsController', ['Settings', '$scope', 'Messages', '$timeout', 'Container', '$routeParams', 'humansizeFilter', function (Settings, $scope, Messages, $timeout, Container, $routeParams, humansizeFilter) {
|
|
|
|
// TODO: Implement memory chart, force scale to 0-100 for cpu, 0 to limit for memory, fix charts on dashboard,
|
|
|
|
// TODO: Force memory scale to 0 - max memory
|
|
|
|
//var initialStats = {}; // Used to set scale of memory graph.
|
|
|
|
//
|
|
|
|
//Container.stats({id: $routeParams.id}, function (d) {
|
|
|
|
// var arr = Object.keys(d).map(function (key) {
|
|
|
|
// return d[key];
|
|
|
|
// });
|
|
|
|
// if (arr.join('').indexOf('no such id') !== -1) {
|
|
|
|
// Messages.error('Unable to retrieve stats', 'Is this container running?');
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// initialStats = d;
|
|
|
|
//}, function () {
|
|
|
|
// Messages.error('Unable to retrieve stats', 'Is this container running?');
|
|
|
|
//});
|
2015-08-26 06:28:08 +00:00
|
|
|
|
2015-08-27 06:41:45 +00:00
|
|
|
var cpuLabels = [];
|
|
|
|
var cpuData = [];
|
|
|
|
var memoryLabels = [];
|
|
|
|
var memoryData = [];
|
|
|
|
for (var i = 0; i < 40; i++) {
|
|
|
|
cpuLabels.push('');
|
|
|
|
cpuData.push(0);
|
|
|
|
memoryLabels.push('');
|
|
|
|
memoryData.push(0);
|
2015-08-25 05:55:54 +00:00
|
|
|
}
|
2015-08-27 06:41:45 +00:00
|
|
|
var cpuDataset = { // CPU Usage
|
2015-08-26 06:28:08 +00:00
|
|
|
fillColor: "rgba(151,187,205,0.5)",
|
|
|
|
strokeColor: "rgba(151,187,205,1)",
|
|
|
|
pointColor: "rgba(151,187,205,1)",
|
|
|
|
pointStrokeColor: "#fff",
|
2015-08-27 06:41:45 +00:00
|
|
|
data: cpuData
|
2015-08-26 06:28:08 +00:00
|
|
|
};
|
2015-08-27 06:41:45 +00:00
|
|
|
var memoryDataset = {
|
|
|
|
fillColor: "rgba(151,187,205,0.5)",
|
|
|
|
strokeColor: "rgba(151,187,205,1)",
|
|
|
|
pointColor: "rgba(151,187,205,1)",
|
|
|
|
pointStrokeColor: "#fff",
|
|
|
|
data: memoryData
|
|
|
|
};
|
|
|
|
|
2015-08-26 06:28:08 +00:00
|
|
|
|
2015-08-27 06:41:45 +00:00
|
|
|
var cpuChart = new Chart($('#cpu-stats-chart').get(0).getContext("2d")).Line({
|
|
|
|
labels: cpuLabels,
|
|
|
|
datasets: [cpuDataset]
|
|
|
|
}, {
|
|
|
|
responsive: true
|
|
|
|
});
|
|
|
|
|
|
|
|
var memoryChart = new Chart($('#memory-stats-chart').get(0).getContext('2d')).Line({
|
|
|
|
labels: memoryLabels,
|
|
|
|
datasets: [memoryDataset]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scaleLabel: function (valueObj) {
|
2015-08-29 04:26:39 +00:00
|
|
|
return humansizeFilter(parseInt(valueObj.value, 10));
|
2015-08-27 06:41:45 +00:00
|
|
|
},
|
2015-08-29 04:26:39 +00:00
|
|
|
responsive: true
|
2015-08-27 06:41:45 +00:00
|
|
|
//scaleOverride: true,
|
|
|
|
//scaleSteps: 10,
|
|
|
|
//scaleStepWidth: Math.ceil(initialStats.memory_stats.limit / 10),
|
|
|
|
//scaleStartValue: 0
|
2015-08-26 06:28:08 +00:00
|
|
|
});
|
2015-08-25 05:55:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
function updateStats() {
|
|
|
|
Container.stats({id: $routeParams.id}, function (d) {
|
2015-08-25 05:59:54 +00:00
|
|
|
var arr = Object.keys(d).map(function (key) {
|
|
|
|
return d[key];
|
|
|
|
});
|
2015-08-25 05:55:54 +00:00
|
|
|
if (arr.join('').indexOf('no such id') !== -1) {
|
2015-08-26 06:28:08 +00:00
|
|
|
Messages.error('Unable to retrieve stats', 'Is this container running?');
|
2015-08-25 05:55:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-26 06:28:08 +00:00
|
|
|
|
2015-08-25 05:55:54 +00:00
|
|
|
// Update graph with latest data
|
2015-08-29 04:26:39 +00:00
|
|
|
$scope.data = d;
|
2015-08-26 06:28:08 +00:00
|
|
|
updateChart(d);
|
2015-08-27 06:41:45 +00:00
|
|
|
updateMemoryChart(d);
|
2015-08-29 04:26:39 +00:00
|
|
|
timeout = $timeout(updateStats, 1000);
|
2015-08-25 05:55:54 +00:00
|
|
|
}, function () {
|
2015-08-26 06:28:08 +00:00
|
|
|
Messages.error('Unable to retrieve stats', 'Is this container running?');
|
2015-08-25 05:55:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-29 04:26:39 +00:00
|
|
|
var timeout;
|
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
$timeout.cancel(timeout);
|
|
|
|
});
|
|
|
|
|
2015-08-25 05:55:54 +00:00
|
|
|
updateStats();
|
|
|
|
|
2015-08-26 06:28:08 +00:00
|
|
|
function updateChart(data) {
|
|
|
|
console.log('updateChart', data);
|
2015-08-27 06:41:45 +00:00
|
|
|
cpuChart.addData([calculateCPUPercent(data)], new Date(data.read).toLocaleTimeString());
|
|
|
|
cpuChart.removeData();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateMemoryChart(data) {
|
|
|
|
console.log('updateMemoryChart', data);
|
|
|
|
memoryChart.addData([data.memory_stats.usage], new Date(data.read).toLocaleTimeString());
|
|
|
|
memoryChart.removeData();
|
|
|
|
|
2015-08-26 06:28:08 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 06:41:45 +00:00
|
|
|
function calculateCPUPercent(stats) {
|
2015-08-25 05:55:54 +00:00
|
|
|
// Same algorithm the official client uses: https://github.com/docker/docker/blob/master/api/client/stats.go#L195-L208
|
|
|
|
var prevCpu = stats.precpu_stats;
|
|
|
|
var curCpu = stats.cpu_stats;
|
|
|
|
|
|
|
|
var cpuPercent = 0.0;
|
|
|
|
|
|
|
|
// calculate the change for the cpu usage of the container in between readings
|
|
|
|
var cpuDelta = curCpu.cpu_usage.total_usage - prevCpu.cpu_usage.total_usage;
|
|
|
|
// calculate the change for the entire system between readings
|
|
|
|
var systemDelta = curCpu.system_cpu_usage - prevCpu.system_cpu_usage;
|
|
|
|
|
|
|
|
if (systemDelta > 0.0 && cpuDelta > 0.0) {
|
2015-08-26 06:28:08 +00:00
|
|
|
//console.log('size thing:', curCpu.cpu_usage.percpu_usage);
|
|
|
|
cpuPercent = (cpuDelta / systemDelta) * curCpu.cpu_usage.percpu_usage.length * 100.0;
|
2015-08-25 05:55:54 +00:00
|
|
|
}
|
2015-08-29 04:26:39 +00:00
|
|
|
return cpuPercent;
|
2015-08-27 06:41:45 +00:00
|
|
|
}
|
|
|
|
}])
|
|
|
|
;
|