Files
cloudron-box/src/views/system.js

232 lines
8.6 KiB
JavaScript
Raw Normal View History

2018-11-15 19:59:24 +01:00
'use strict';
2020-06-03 23:17:06 +02:00
/* global angular */
/* global $ */
/* global Chart */
2020-02-25 14:54:29 -08:00
angular.module('Application').controller('SystemController', ['$scope', '$location', '$timeout', 'Client', function ($scope, $location, $timeout, Client) {
2020-02-24 12:56:13 +01:00
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
2018-11-15 19:59:24 +01:00
2018-11-20 16:53:42 +01:00
$scope.config = Client.getConfig();
2020-01-28 09:37:25 -08:00
$scope.memory = null;
$scope.volumesById = {};
2018-11-15 19:59:24 +01:00
2020-03-05 18:26:58 -08:00
// http://stackoverflow.com/questions/1484506/random-color-generator-in-javascript
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
var colorIndex = 0;
var colors = [ '#2196F3', '#3995b1', '#f0ad4e', '#ff4c4c' ];
function getNextColor() {
if (colors[colorIndex+1]) return colors[colorIndex++];
return getRandomColor();
}
$scope.disks = {
busy: true,
disks: [],
2020-03-05 18:26:58 -08:00
refresh: function () {
$scope.disks.busy = true;
2022-09-15 14:43:00 +02:00
var result;
$scope.disks.disks = result.disks; // [ { filesystem, type, size, used, available, capacity, mountpoint }]
2022-09-15 14:43:00 +02:00
$scope.disks.disks.forEach(function (disk) {
2022-09-16 17:09:54 +02:00
var usageOther = disk.occupied;
2022-09-15 14:43:00 +02:00
2022-09-16 17:09:54 +02:00
colorIndex = 0;
disk.contains.forEach(function (content) {
content.color = getNextColor();
2022-09-15 14:43:00 +02:00
2022-09-16 17:09:54 +02:00
if (content.type === 'app') content.app = Client.getInstalledAppsByAppId()[content.id];
if (content.type === 'volume') content.volume = $scope.volumesById(content.id);
2020-04-18 22:48:09 -07:00
2022-09-16 17:09:54 +02:00
usageOther -= content.usage;
2020-03-05 18:26:58 -08:00
});
2022-09-16 17:09:54 +02:00
disk.contains.sort(function (x, y) { return y.usage - x.usage; }); // sort by usage
if ($scope.disks.disks[0] === disk) { // the root mount point is the first disk. keep this 'contains' in the end
2022-09-16 17:09:54 +02:00
disk.contains.push({
type: 'standard',
label: 'Everything else (Ubuntu, Swap, etc)',
id: 'other',
color: '#27CE65',
usage: usageOther
});
} else {
disk.contains.push({
type: 'standard',
label: 'Used',
id: 'other',
color: '#27CE65',
usage: usageOther
});
}
});
2020-05-13 23:34:14 +02:00
$scope.disks.busy = false;
}
};
$scope.graphs = {
busy: false,
period: 6,
memoryChart: null,
diskChart: null,
2022-10-11 21:22:56 +02:00
setPeriod: function (hours) {
$scope.graphs.period = hours;
$scope.graphs.refresh();
},
2022-10-11 21:22:56 +02:00
refresh: function () {
$scope.graphs.busy = true;
2022-10-11 21:22:56 +02:00
Client.getSystemGraphs($scope.graphs.period * 60, function (error, result) {
if (error) return console.error('Failed to fetch system graphs:', error);
2020-05-13 23:34:14 +02:00
// in minutes
var timePeriod = $scope.graphs.period * 60;
2022-10-11 21:22:56 +02:00
// keep in sync with graphs.js
var timeBucketSizeMinutes = timePeriod > (24 * 60) ? (6*60) : 5;
var steps = Math.floor(timePeriod/timeBucketSizeMinutes);
2022-10-11 21:22:56 +02:00
var labels = new Array(steps).fill(0);
labels = labels.map(function (v, index) {
var dateTime = new Date(Date.now() - ((timePeriod - (index * timeBucketSizeMinutes)) * 60 * 1000));
2022-10-11 21:22:56 +02:00
if ($scope.graphs.period > 24) {
return dateTime.toLocaleDateString();
} else {
return dateTime.toLocaleTimeString();
}
});
2022-10-11 21:22:56 +02:00
var colors = [ '#2196F3', '#FF6384' ];
function fillGraph(canvasId, contents, chartPropertyName, divisor, max) {
if (!contents || !contents[0]) return; // no data available yet
var datasets = [];
contents.forEach(function (content, index) {
// fill holes with previous value
var cur = 0;
content.data.datapoints.forEach(function (d) {
if (d[0] === null) d[0] = cur;
else cur = d[0];
});
var datapoints = Array(steps).map(function () { return '0'; });
// walk backwards and fill up the datapoints
content.data.datapoints.reverse().forEach(function (d, index) {
datapoints[datapoints.length-1-index] = (d[0] / divisor).toFixed(2);
// return parseInt((d[0] / divisor).toFixed(2));
});
var color = index > 2 ? getRandomColor() : colors[index];
datasets.push({
label: content.label,
backgroundColor: color + '4F',
borderColor: color, // FIXME give real distinct colors
borderWidth: 1,
radius: 0,
data: datapoints,
cubicInterpolationMode: 'monotone',
tension: 0.4
});
2022-10-11 21:22:56 +02:00
});
2022-09-15 12:12:32 +02:00
var graphData = {
labels: labels,
datasets: datasets
};
var options = {
responsive: true,
maintainAspectRatio: true,
aspectRatio: 2.5,
animation: false,
plugins: {
legend: {
display: false
}
},
interaction: {
intersect: false,
mode: 'index',
2022-10-11 21:22:56 +02:00
},
scales: {
x: {
ticks: { autoSkipPadding: 50, maxRotation: 0 }
},
y: {
min: 0,
beginAtZero: true
}
2022-10-11 21:22:56 +02:00
}
};
if (max) options.scales.y.max = max;
2022-10-11 21:22:56 +02:00
var ctx = $(canvasId).get(0).getContext('2d');
if ($scope.graphs[chartPropertyName]) $scope.graphs[chartPropertyName].destroy();
$scope.graphs[chartPropertyName] = new Chart(ctx, { type: 'line', data: graphData, options: options });
}
// var threshold = 1024 * 1024;
// var appsWithHighMemory = Object.keys(result.apps).map(function (appId) {
// result.apps[appId].id = appId;
// return result.apps[appId];
// }).filter(function (app) {
// if (!app.memory) return false; // not sure why we get empty objects
// return app.memory.datapoints.some(function (d) { return d[0] > threshold; });
// }).map(function (app) {
// return { data: app.memory, label: app.id };
// });
var appsWithHighMemory = [];
fillGraph('#graphsCPUChart', [{ data: result.cpu, label: 'CPU' }], 'cpuChart', 100, 1);
fillGraph('#graphsSystemMemoryChart', [{ data: result.memory, label: 'Memory' }].concat(appsWithHighMemory), 'memoryChart', 1024 * 1024, Number.parseInt($scope.memory.memory / 1024 / 1024));
$scope.graphs.busy = false;
});
}
2022-09-16 17:09:54 +02:00
};
2018-11-18 20:01:53 +01:00
Client.onReady(function () {
2020-03-05 18:26:58 -08:00
Client.memory(function (error, memory) {
if (error) console.error(error);
2018-11-18 20:01:53 +01:00
2020-03-05 18:26:58 -08:00
$scope.memory = memory;
2022-09-16 17:09:54 +02:00
Client.getVolumes(function (error, volumes) {
if (error) return console.error(error);
$scope.volumesById = {};
volumes.forEach(function (v) { $scope.volumesById[v.id] = v; });
$scope.graphs.refresh();
// $scope.disks.refresh();
});
2018-11-18 20:01:53 +01:00
});
});
2020-03-17 22:09:34 -07:00
Client.onReconnect(function () {
$scope.reboot.busy = false;
});
2018-11-15 19:59:24 +01:00
}]);