diff options
author | Simon Rettberg | 2021-12-07 14:56:43 +0100 |
---|---|---|
committer | Simon Rettberg | 2021-12-07 14:56:43 +0100 |
commit | da29d71fb2ae51a3389352f100e297606477b62b (patch) | |
tree | 29f0105302980bd659dec5c4638aa85e390b3caf | |
parent | [eventlog] install: Fix SQL (diff) | |
download | slx-admin-da29d71fb2ae51a3389352f100e297606477b62b.tar.gz slx-admin-da29d71fb2ae51a3389352f100e297606477b62b.tar.xz slx-admin-da29d71fb2ae51a3389352f100e297606477b62b.zip |
[statistics/passthrough] Consider group<->location mapping for KCL
6 files changed, 101 insertions, 3 deletions
diff --git a/modules-available/passthrough/hooks/locations-column.inc.php b/modules-available/passthrough/hooks/locations-column.inc.php new file mode 100644 index 00000000..9757ac50 --- /dev/null +++ b/modules-available/passthrough/hooks/locations-column.inc.php @@ -0,0 +1,48 @@ +<?php + +if (!User::hasPermission('.passthrough.view')) { + return null; +} + +class PassthroughLocationColumn extends AbstractLocationColumn +{ + + private $lookup; + + public function __construct(array $allowedLocationIds) + { + $this->lookup = Database::queryKeyValueList("SELECT gxl.locationid, GROUP_CONCAT(gxl.groupid SEPARATOR ', ') AS grps + FROM passthrough_group_x_location gxl + WHERE locationid IN (:allowedLocationIds) GROUP BY locationid", compact('allowedLocationIds')); + } + + public function getColumnHtml(int $locationId): string + { + return htmlspecialchars($this->lookup[$locationId] ?? ''); + } + + public function getEditUrl(int $locationId): string + { + if (!User::hasPermission('.passthrough.assign', $locationId)) + return ''; + return '?do=passthrough&show=assignlocation&locationid=' . $locationId; + } + + public function header(): string + { + return Dictionary::translateFileModule('passthrough', 'module', 'location-column-header'); + } + + public function priority(): int + { + return 4000; + } + + public function propagateColumn(): bool + { + return true; + } + +} + +return new PassthroughLocationColumn($allowedLocationIds);
\ No newline at end of file diff --git a/modules-available/passthrough/install.inc.php b/modules-available/passthrough/install.inc.php index e08be38b..01d3edbb 100644 --- a/modules-available/passthrough/install.inc.php +++ b/modules-available/passthrough/install.inc.php @@ -6,4 +6,18 @@ $result[] = tableCreate('passthrough_group', " PRIMARY KEY (`groupid`) "); +$result[] = tableCreate('passthrough_group_x_location', " + `groupid` varchar(32) CHARACTER SET ascii DEFAULT NULL, + `locationid` INT(11) NOT NULL, + PRIMARY KEY (`groupid`, `locationid`) +"); + +$result[] = tableAddConstraint('passthrough_group_x_location', 'groupid', + 'passthrough_group', 'groupid', + 'ON DELETE CASCADE ON UPDATE CASCADE'); + +$result[] = tableAddConstraint('passthrough_group_x_location', 'locationid', + 'location', 'locationid', + 'ON DELETE CASCADE ON UPDATE CASCADE'); + responseFromArray($result);
\ No newline at end of file diff --git a/modules-available/passthrough/lang/de/module.json b/modules-available/passthrough/lang/de/module.json new file mode 100644 index 00000000..c8657ccd --- /dev/null +++ b/modules-available/passthrough/lang/de/module.json @@ -0,0 +1,3 @@ +{ + "location-column-header": "Passthrough" +}
\ No newline at end of file diff --git a/modules-available/passthrough/lang/en/module.json b/modules-available/passthrough/lang/en/module.json new file mode 100644 index 00000000..c8657ccd --- /dev/null +++ b/modules-available/passthrough/lang/en/module.json @@ -0,0 +1,3 @@ +{ + "location-column-header": "Passthrough" +}
\ No newline at end of file diff --git a/modules-available/statistics/inc/hardwareinfo.inc.php b/modules-available/statistics/inc/hardwareinfo.inc.php index ed47c153..df33a5f6 100644 --- a/modules-available/statistics/inc/hardwareinfo.inc.php +++ b/modules-available/statistics/inc/hardwareinfo.inc.php @@ -29,7 +29,7 @@ class HardwareInfo $mac = Request::get('mac', '', 'string'); $mac = str_replace(':', '-', $mac); } - $res = Database::simpleQuery("SELECT machineuuid, lastseen, cpumodel FROM machine + $res = Database::simpleQuery("SELECT machineuuid, lastseen, cpumodel, locationid FROM machine WHERE machineuuid = :uuid OR macaddr = :mac", ['uuid' => $uuid, 'mac' => $mac]); $best = null; foreach ($res as $row) { @@ -37,11 +37,15 @@ class HardwareInfo $best = $row; } } - if ($best === null) + if ($best === null || ((int)$best['locationid']) === 0) + return ''; + $locations = Location::getLocationRootChain($best['locationid']); + if (empty($locations)) return ''; $hw = new HardwareQuery(self::PCI_DEVICE, $best['machineuuid'], true); // TODO: Get list of enabled pass through groups for this client's location - $hw->addWhere(true, '@PASSTHROUGH', 'IN', ['GPU', 'GVT']); + $hw->addJoin(true, '@PASSTHROUGH', 'passthrough_group_x_location', 'groupid', + 'locationid', $locations); $hw->addGlobalColumn('vendor'); $hw->addGlobalColumn('device'); $hw->addLocalColumn('slot'); diff --git a/modules-available/statistics/inc/hardwarequery.inc.php b/modules-available/statistics/inc/hardwarequery.inc.php index 6b5662d4..2cded089 100644 --- a/modules-available/statistics/inc/hardwarequery.inc.php +++ b/modules-available/statistics/inc/hardwarequery.inc.php @@ -68,6 +68,32 @@ class HardwareQuery $this->columns[$prop] = "$tid.`value` AS `$prop`"; } + public function addJoin(bool $global, string $prop, string $jTable, string $jColumn, string $condColumn = '', $condVal = null) + { + if (isset($this->columns[$jTable])) + return; + if (!isset($this->columns[$prop])) { + $this->fillTableVars($global, $srcTable, $table, $column); + $tid = $this->id(); + $pid = $this->id(); + $this->joins[$prop] = "INNER JOIN $table $tid ON ($srcTable.$column = $tid.$column + AND $tid.prop = :$pid)"; + $this->args[$pid] = $prop; + } + $jtid = $this->id(); + $cond = ''; + if (!empty($condColumn)) { + $vid = $this->id(); + if (is_array($condVal)) { + $cond = " AND $jtid.`$condColumn` IN (:vid)"; + } else { + $cond = " AND $jtid.`$condColumn` = :vid"; + } + $this->args[$vid] = $condVal; + } + $this->joins[$jTable] = "INNER JOIN $jTable $jtid ON ($jtid.$jColumn = $tid.`value` $cond)"; + } + public function addMachineWhere(string $column, string $op, $value) { if (isset($this->columns[$column])) |