diff options
author | Simon Rettberg | 2021-11-30 15:27:59 +0100 |
---|---|---|
committer | Simon Rettberg | 2022-03-09 15:06:54 +0100 |
commit | 41612c4481111969367d1a228ad4875d781558a2 (patch) | |
tree | d59bc50a3a2822faa6b800bd1f74f97c5ca692b3 /modules-available/statistics/hooks | |
parent | [permissionmanager] Fix for PHP 8 (diff) | |
download | slx-admin-41612c4481111969367d1a228ad4875d781558a2.tar.gz slx-admin-41612c4481111969367d1a228ad4875d781558a2.tar.xz slx-admin-41612c4481111969367d1a228ad4875d781558a2.zip |
[locations] Modularize additional column handling
Additional columns are now added through a hook,
moving specialized code where it belongs.
Diffstat (limited to 'modules-available/statistics/hooks')
-rw-r--r-- | modules-available/statistics/hooks/locations-column.inc.php | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/modules-available/statistics/hooks/locations-column.inc.php b/modules-available/statistics/hooks/locations-column.inc.php new file mode 100644 index 00000000..51f280be --- /dev/null +++ b/modules-available/statistics/hooks/locations-column.inc.php @@ -0,0 +1,150 @@ +<?php + +if (!User::hasPermission('.statistics.view.list')) { + return null; +} + +class ClientCountLocationColumn extends AbstractLocationColumn +{ + + private $lookup; + + public function __construct() + { + $this->lookup = StatisticsColumnGetData([]); + } + + public function getColumnHtml(int $locationId): string + { + if (!isset($this->lookup[$locationId])) + return ''; + if ($this->lookup[$locationId]['hasChild'] ?? false) { + $child = <<<EOF + (<a href="?do=Statistics&show=list&filters=location~{$locationId}">↓{$this->lookup[$locationId]['clientCountSum']}</a>) +EOF; + } else { + $child = ''; + } + + return <<<EOF + <div class="pull-right"> + <a href="?do=Statistics&show=list&filters=location={$locationId}"> {$this->lookup[$locationId]['clientCount']} </a> + <span class="text-right" style="display:inline-block;width:6ex">$child</span> + </div> +EOF; + } + + public function getEditUrl(int $locationId): string + { + return ''; + } + + public function header(): string + { + return Dictionary::translateFileModule('statistics', 'module', 'location-column-header-count'); + } + + public function priority(): int + { + return 800; + } + +} + +class ClientLoadLocationColumn extends AbstractLocationColumn +{ + + private $lookup; + + public function __construct() + { + $this->lookup = StatisticsColumnGetData([]); + } + + public function getColumnHtml(int $locationId): string + { + if (!isset($this->lookup[$locationId]) || $this->lookup[$locationId]['clientCount'] === 0) + return ''; + $c =& $this->lookup[$locationId]; + return <<<EOF + <div class="load-col text-right" style="background:linear-gradient(to right, #f97, #f97 {$c['clientLoad']}%, + #6fa {$c['clientLoad']}%, #6fa {$c['clientIdle']}%, #eee {$c['clientIdle']}%)"> + {$c['clientLoad']} % + </div> +EOF; + + } + + public function getEditUrl(int $locationId): string + { + return ''; + } + + public function header(): string + { + return Dictionary::translateFileModule('statistics', 'module', 'location-column-header-load'); + } + + public function priority(): int + { + return 900; + } + +} + +function StatisticsColumnGetData(array $allowedLocationIds): array +{ + static $data = []; + if (!empty($data)) + return $data; + $extra = ''; + if (in_array(0, $allowedLocationIds)) { + $extra = ' OR locationid IS NULL'; + } + $locs = Location::getLocationsAssoc(); + $res = Database::simpleQuery("SELECT m.locationid, Count(*) AS cnt, + Sum(If(m.state = 'OCCUPIED', 1, 0)) AS used, Sum(If(m.state = 'IDLE', 1, 0)) AS idle + FROM machine m WHERE (locationid IN (:allowedLocationIds) $extra) GROUP BY locationid", compact('allowedLocationIds')); + foreach ($res as $row) { + $locId = (int)$row['locationid']; + $data[$locId] = [ + 'clientCount' => $row['cnt'], + 'clientLoad' => round(100 * $row['used'] / $row['cnt']), + 'clientIdle' => round(100 * ($row['used'] + $row['idle']) / $row['cnt']), + ]; + } + foreach ($allowedLocationIds as $locId) { + if (isset($data[$locId])) + continue; + $data[$locId] = [ + 'clientCount' => 0, + 'clientLoad' => 0, + 'clientIdle' => 0, + ]; + } + foreach ($data as $locId => &$loc) { + if (!in_array($locId, $allowedLocationIds)) + continue; + if (!isset($loc['clientCountSum'])) { + $loc['clientCountSum'] = 0; + } + $loc['clientCountSum'] += $loc['clientCount']; + if ($locId !== 0) { + foreach ($locs[$locId]['parents'] as $pid) { + if (!in_array($pid, $allowedLocationIds)) + continue; + $data[$pid]['hasChild'] = true; + if (!isset($data[$pid]['clientCountSum'])) { + $data[$pid]['clientCountSum'] = 0; + } + $data[$pid]['clientCountSum'] += $loc['clientCount']; + } + } + } + unset($loc); + return $data; +} + +StatisticsColumnGetData($allowedLocationIds); + +return [new ClientCountLocationColumn(), new ClientLoadLocationColumn()];
\ No newline at end of file |