From ae128c579ba1b06ee740ea5f07919cf4f33f340f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 6 Jul 2022 15:56:29 +0200 Subject: [statistics] Fix Chart.js for HDD view --- modules-available/statistics/clientscript.js | 70 ++++++++++++++++++++++ modules-available/statistics/page.inc.php | 21 +++++++ modules-available/statistics/pages/machine.inc.php | 8 ++- .../statistics/templates/clientlist.html | 8 +-- .../statistics/templates/filterbox.html | 68 --------------------- .../statistics/templates/machine-hdds.html | 31 +++------- 6 files changed, 110 insertions(+), 96 deletions(-) create mode 100644 modules-available/statistics/clientscript.js diff --git a/modules-available/statistics/clientscript.js b/modules-available/statistics/clientscript.js new file mode 100644 index 00000000..90be29e9 --- /dev/null +++ b/modules-available/statistics/clientscript.js @@ -0,0 +1,70 @@ +'use strict'; + +// All the pie chars +function makePieChart($parent) { + var data = $parent.data('chart'); + var chartData = { + datasets: [{ + data: data.map(function(x) { return x.value; }), + backgroundColor: data.map(function(x) { return x.color; }) + }] + }; + var $canv = $(''); + $parent.append($canv); + (function() { + var $dest = $parent.data('chart-dest'); + var cur = null; + new Chart($canv[0].getContext('2d'), { + type: 'pie', data: chartData, options: { + animation: false, + onHover: function (_, list) { + if (list.length === 0 || list[0].index !== cur) { + if (cur !== null) { + $($dest + cur).removeClass('slx-bold'); + cur = null; + } + } + if (list.length !== 0 && list[0].index !== cur) { + cur = list[0].index; + $($dest + cur).addClass('slx-bold'); + } + }, + plugins: { + tooltip: {enabled: false}, + legend: {display: false} + } + } + }); + })(); +} + +function popupFilter(field) { + var $row = addFilter(field, null, null); + if ($row !== null) { + $row.find('.arg').focus(); + $row.removeClass('slx-focus') + setTimeout(function() { $row.addClass('slx-focus'); }, 10); + } +} + +function addFilter(field, op, argument) { + if (field === null) + return null; + var $row = $('#filter-' + field); + if ($row.length === 0) + return null; + if (argument !== null) { + $row.find('.op').val(op); + $row.find('.arg').val(argument); + } + // Enable checkbox only if we got a predefined value, or if argument is in a select, as the user might want the preselected item and doesn't notice the checkbox is unchecked + if (argument !== null || $row.find('select.arg').length !== 0) { + $row.find('.filter-enable').prop('checked', true); + } + $row.show(); + return $row; +} + +function refresh() { + $('#query-form').submit(); +} \ No newline at end of file diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index 04d9a515..71c9dea3 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -91,6 +91,8 @@ class Page_Statistics extends Page $this->rebootControl(false); } elseif ($action === 'wol') { $this->wol(); + } elseif ($action === 'benchmark') { + $this->vmstoreBenchmark(); } elseif ($action === 'prepare-exec') { if (Module::isAvailable('rebootcontrol')) { RebootControl::prepareExec(); @@ -169,6 +171,25 @@ class Page_Statistics extends Page } } + /** + * @param bool $reboot true = reboot, false = shutdown + */ + private function vmstoreBenchmark() + { + if (!Module::isAvailable('vmstore')) + return; + $ids = Request::post('uuid', [], 'array'); + $ids = array_values($ids); + if (empty($ids)) { + Message::addError('main.parameter-empty', 'uuid'); + return; + } + $this->getAllowedMachines(".vmstore.benchmark", $ids, $allowedMachines); + if (empty($allowedMachines)) + return; + VmStoreBenchmark::prepareSelectDialog($allowedMachines); + } + private function getAllowedMachines($permission, $ids, &$allowedMachines) { $allowedLocations = User::getAllowedLocations($permission); diff --git a/modules-available/statistics/pages/machine.inc.php b/modules-available/statistics/pages/machine.inc.php index 5bf5ec97..df6a561e 100644 --- a/modules-available/statistics/pages/machine.inc.php +++ b/modules-available/statistics/pages/machine.inc.php @@ -548,6 +548,8 @@ class SubPage private static function mangleHdd(array &$hdd) { + static $hddidx = 0; + $hdd['hddidx'] = $hddidx++; $hours = $hdd['power_on_time//hours'] ?? $hdd['attr_9']['raw'] ?? $hdd['power_on_hours'] ?? $hdd['power_on_time']['hours'] ?? null; if ($hours !== null) { @@ -575,14 +577,16 @@ class SubPage $json = []; $lastEnd = 0; $minDisplaySize = $hdd['size'] / 150; + $i = 0; foreach ($hdd['partitions'] as &$part) { $dist = $part['start'] - $lastEnd; if ($dist > $minDisplaySize) { - error_log('Dist: ' . Util::readableFileSize($dist)); $json[] = ['value' => $dist, 'color' => '#aaa']; + $i++; } if ($part['size'] > $minDisplaySize) { - $json[] = ['value' => $part['size'], 'color' => self::typeToColor($part), 'label' => $part['id']]; + $json[] = ['value' => $part['size'], 'color' => self::typeToColor($part)]; + $part['idx'] = $i++; } $part['size_s'] = Util::readableFileSize($part['size']); $used += $part['size']; diff --git a/modules-available/statistics/templates/clientlist.html b/modules-available/statistics/templates/clientlist.html index 3df95d92..df271591 100644 --- a/modules-available/statistics/templates/clientlist.html +++ b/modules-available/statistics/templates/clientlist.html @@ -152,7 +152,7 @@ {{lang_reset}} -
+
{{#rebootcontrol}} -
+
{{/canBenchmark}}
@@ -230,7 +230,7 @@
{{/rebootcontrol}} {{#canDelete}} -
+
-
- - +
{{/hdds}} + \ No newline at end of file -- cgit v1.2.3-55-g7522