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

290 lines
12 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 async */
/* 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.activeTab = 0;
$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();
}
2020-04-18 22:48:09 -07:00
$scope.disks = {
busy: true,
errorMessage: '',
disks: [],
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
setError: function (error) {
$scope.disks.errorMessage = 'Error loading disk : ' + error.message;
},
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
update: function () {
$scope.disks.busy = true;
2022-09-15 14:43:00 +02:00
Client.getSystemGraphs($scope.graphs.period * 60, function (error, result) {
2020-04-18 22:48:09 -07:00
if (error) return $scope.disks.setError(error);
2022-09-15 14:43:00 +02:00
console.log(result);
2022-09-15 14:43:00 +02:00
$scope.disks.disks = result.disks; // [ { filesystem, type, size, used, available, capacity, mountpoint }]
// https://graphite.readthedocs.io/en/latest/render_api.html#paths-and-wildcards
// on scaleway, for some reason docker devices are collected as part of collectd
// until we figure why just hardcode popular disk devices - https://www.mjmwired.net/kernel/Documentation/devices.txt
// Client.disks(function (error, result) {
// if (error) return $scope.disks.setError(error);
// // segregate locations into the correct disks based on 'filesystem'
// result.disks.forEach(function (disk, index) {
// disk.id = index;
// disk.contains = [];
// if (disk.filesystem === result.platformDataDisk) disk.contains.push({ type: 'standard', label: 'Platform data', id: 'platformdata', usage: 0 });
// if (disk.filesystem === result.boxDataDisk) disk.contains.push({ type: 'standard', label: 'Box data', id: 'boxdata', usage: 0 });
// if (disk.filesystem === result.dockerDataDisk) disk.contains.push({ type: 'standard', label: 'Docker images', id: 'docker', usage: 0 });
// if (disk.filesystem === result.mailDataDisk) disk.contains.push({ type: 'standard', label: 'Email data', id: 'maildata', usage: 0 });
// if (disk.filesystem === result.backupsDisk) disk.contains.push({ type: 'standard', label: 'Backup data', id: 'cloudron-backup', usage: 0 });
// const apps = Object.keys(result.apps).filter(function (appId) { return result.apps[appId] === disk.filesystem; });
// apps.forEach(function (appId) {
// var app = Client.getCachedAppSync(appId);
// disk.contains.push({ type: 'app', app: app, label: app.label || app.fqdn, id: appId, usage: 0 });
// });
// const volumes = Object.keys(result.volumes).filter(function (volumeId) { return result.volumes[volumeId] === disk.filesystem; });
// volumes.forEach(function (volumeId) {
// var volume = $scope.volumesById[volumeId];
// disk.contains.push({ type: 'volume', volume: volume, label: volume.name, id: volumeId, usage: 0 });
// });
// });
2020-04-18 22:48:09 -07:00
$scope.disks.disks = result.disks; // [ { filesystem, type, size, used, available, capacity, mountpoint }]
// render data of each disk
2020-06-03 23:17:06 +02:00
async.eachSeries(result.disks, function (disk, iteratorCallback) {
2020-04-18 22:48:09 -07:00
// /dev/sda1 -> sda1
// /dev/mapper/foo.com -> mapper_foo_com (see #348)
2022-09-15 14:43:00 +02:00
// var diskName = disk.filesystem.slice(disk.filesystem.indexOf('/', 1) + 1);
// diskName = diskName.replace(/\/|\./g, '_');
// // use collectd instead of df data so the timeframe matches with the du data
// Client.getSystemGraphs([
// 'absolute(collectd.localhost.df-' + diskName + '.df_complex-free)',
// 'absolute(collectd.localhost.df-' + diskName + '.df_complex-reserved)', // reserved for root (default: 5%) tune2fs -l/m
// 'absolute(collectd.localhost.df-' + diskName + '.df_complex-used)'
// ], '-1min', {}, function (error, data) {
// if (error) return iteratorCallback(error);
// disk.size = data[2].datapoints[0][0] + data[1].datapoints[0][0] + data[0].datapoints[0][0];
// disk.free = data[0].datapoints[0][0];
// disk.occupied = data[2].datapoints[0][0];
colorIndex = 0;
disk.contains.forEach(function (content) {
content.color = getNextColor();
});
2020-03-05 18:26:58 -08:00
2022-09-15 14:43:00 +02:00
// // get disk usage data
// var graphiteQueries = disk.contains.map(function (content) {
// return 'absolute(collectd.localhost.du-' + content.id + '.capacity-usage)';
// });
2020-03-05 18:26:58 -08:00
2022-09-15 14:43:00 +02:00
// Client.graphs(graphiteQueries, '-1day', { noNullPoints: true }, function (error, data) {
// if (error) return iteratorCallback(error);
var usageOther = disk.occupied;
// data.forEach(function (d) {
// var content = disk.contains.find(function (content) { return d.target.indexOf(content.id) !== -1; });
// if (!content) return; // didn't match any content
// var tmp = d.datapoints[d.datapoints.length-1][0];
// content.usage = tmp;
// // deduct from overal disk usage to track other
// usageOther -= tmp;
// });
2020-04-18 22:48:09 -07:00
2022-09-15 14:43:00 +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
disk.contains.push({
type: 'standard',
label: 'Everything else (Ubuntu, Swap, etc)',
id: 'other',
color: '#27CE65',
usage: usageOther
2020-04-18 22:48:09 -07:00
});
2022-09-15 14:43:00 +02:00
} else {
disk.contains.push({
type: 'standard',
label: 'Used',
id: 'other',
color: '#27CE65',
usage: usageOther
});
}
iteratorCallback();
// });
// });
2020-04-18 22:48:09 -07:00
}, function iteratorDone(error) {
if (error) $scope.disks.setError(error);
$scope.disks.busy = false;
2020-03-05 18:26:58 -08:00
});
});
2020-04-18 22:48:09 -07:00
}
};
2020-01-28 09:37:25 -08:00
$scope.graphs = {
error: {},
period: 6,
memoryChart: null,
diskChart: null,
setPeriod: function (hours) {
$scope.graphs.period = hours;
$scope.graphs.show();
},
show: function () {
2022-09-15 12:12:32 +02:00
var appsById = Client.getInstalledAppsByAppId();
2020-05-13 23:34:14 +02:00
// both in minutes
var timePeriod = $scope.graphs.period * 60;
var timeBucketSize = $scope.graphs.period > 24 ? (6*60) : 5;
function fillGraph(canvasId, data, appsData, label, chartPropertyName, max, valueDivider) {
// translate the data from bytes to MB
var datapoints = data.datapoints.map(function (d) { return parseInt((d[0] / valueDivider).toFixed(2)); });
var labels = datapoints.map(function (d, index) {
var dateTime = new Date(Date.now() - ((timePeriod - (index * timeBucketSize)) * 60 *1000));
2020-09-02 18:53:46 +02:00
if ($scope.graphs.period > 24) {
return dateTime.toLocaleDateString();
} else {
return dateTime.toLocaleTimeString();
}
});
2020-05-13 23:34:14 +02:00
var datasets = [{
label: label,
backgroundColor: '#82C4F844',
borderColor: '#2196F3',
borderWidth: 1,
radius: 0,
data: datapoints
}];
2022-09-15 12:12:32 +02:00
Object.keys(appsData).forEach(function (appId) {
var data = appsData[appId].memory;
if (!data.datapoints.some(function (p) { return p[0] > 1024*1024*1024; })) return;
datasets.push({
label: appsById[appId].fqdn,
fill: false,
borderColor: getRandomColor(),
borderWidth: 1,
radius: 0,
data: data.datapoints.map(function (d) { return parseInt((d[0] / valueDivider).toFixed(2)); })
2020-05-13 23:34:14 +02:00
});
2022-09-15 12:12:32 +02:00
});
2020-05-13 23:34:14 +02:00
2021-01-27 21:48:16 -08:00
var chartData = {
labels: labels,
2020-05-13 23:34:14 +02:00
datasets: datasets
};
var options = {
maintainAspectRatio: true,
aspectRatio: 2.5,
legend: {
display: false
},
tooltips: {
intersect: false
},
scales: {
2020-09-02 18:53:46 +02:00
xAxes: [{
ticks: {
autoSkipPadding: 20,
}
}],
yAxes: [{
ticks: {
min: 0,
max: max,
beginAtZero: true
}
}]
}
};
var ctx = $(canvasId).get(0).getContext('2d');
if ($scope.graphs[chartPropertyName]) $scope.graphs[chartPropertyName].destroy();
2021-01-27 21:48:16 -08:00
$scope.graphs[chartPropertyName] = new Chart(ctx, { type: 'line', data: chartData, options: options });
}
2022-09-15 12:12:32 +02:00
Client.getSystemGraphs(timePeriod, function (error, result) {
if (error) return console.error(error);
2022-09-15 14:43:00 +02:00
fillGraph('#graphsCPUChart', result.cpu, {}, 'CPU Usage', 'cpuChart', 100, 1);
2022-09-15 12:12:32 +02:00
fillGraph('#graphsSystemMemoryChart', result.memory, result.apps, 'Memory', 'memoryChart', Number.parseInt(($scope.memory.memory + $scope.memory.swap) / 1024 / 1024), 1024 * 1024);
});
}
};
function refreshVolumes(callback) {
Client.getVolumes(function (error, results) {
if (error) return console.error(error);
$scope.volumesById = {};
results.forEach(function (v) { $scope.volumesById[v.id] = v; });
callback();
});
}
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;
refreshVolumes(function (error) {
if (error) console.error(error);
$scope.disks.update();
});
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
}]);