From: Marcin Haba Date: Mon, 2 Oct 2023 11:39:39 +0000 (+0200) Subject: baculum: Add sorting parameters to clients endpoint X-Git-Tag: Beta-15.0.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7adaa0c4b3ad9f2fc05f92bf359c4ff79d590b44;p=thirdparty%2Fbacula.git baculum: Add sorting parameters to clients endpoint --- diff --git a/gui/baculum/protected/API/Modules/ClientManager.php b/gui/baculum/protected/API/Modules/ClientManager.php index 2849db15d..bd5554527 100644 --- a/gui/baculum/protected/API/Modules/ClientManager.php +++ b/gui/baculum/protected/API/Modules/ClientManager.php @@ -49,12 +49,18 @@ class ClientManager extends APIModule { * * @param mixed $limit_val result limit value * @param int $offset_val result offset value + * @param array $sort order by and order direction in form [[by1, direction1]] * @param array $criteria SQL criteria to get job list * @param array $jobs jobs used to get job properties per client * @param string $mode result mode * @return array clients or empty list if no client found */ - public function getClients($limit_val = 0, $offset_val = 0, $criteria = [], $jobs = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) { + public function getClients($limit_val = 0, $offset_val = 0, $sort = [['Name', 'ASC']], $criteria = [], $jobs = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) { + $extra_cols = ['os', 'version']; + $order = ''; + if (count($sort) == 1 && !in_array($sort[0][0], $extra_cols)) { + $order = Database::getOrder($sort); + } $limit = ''; if(is_int($limit_val) && $limit_val > 0) { $limit = ' LIMIT ' . $limit_val; @@ -91,11 +97,22 @@ FROM ' . $jwhere['where'] . ' GROUP BY ClientId ) AS J ON (Client.ClientId = J.ClientId) - ' . $where['where'] . $offset . $limit; + ' . $where['where'] . $order . $offset . $limit; $params = array_merge($jwhere['params'], $where['params']); $statement = Database::runQuery($sql, $params); - $result = $statement->fetchAll(\PDO::FETCH_OBJ); + $result = $statement->fetchAll(\PDO::FETCH_ASSOC); $this->setSpecialFields($result); + if (count($sort) == 1 && in_array($sort[0][0], $extra_cols)) { + $misc = $this->getModule('misc'); + // Order by os or version + $misc::sortResultsByField( + $result, + $sort[0][0], + $sort[0][1], + 'name', + $misc::ORDER_DIRECTION_ASC + ); + } if ($mode == self::CLIENT_RESULT_MODE_OVERVIEW) { $sql = 'SELECT COUNT(1) AS count FROM Client ' . $where['where']; $statement = Database::runQuery($sql, $where['params']); @@ -119,11 +136,11 @@ FROM private function setSpecialFields(&$result) { for ($i = 0; $i < count($result); $i++) { // Add operating system info and client version - $result[$i]->os = ''; - $result[$i]->version = ''; - if (preg_match(self::CLIENT_UNAME_PATTERN, $result[$i]->uname, $match) === 1) { - $result[$i]->os = $match['os']; - $result[$i]->version = $match['version']; + $result[$i]['os'] = ''; + $result[$i]['version'] = ''; + if (preg_match(self::CLIENT_UNAME_PATTERN, $result[$i]['uname'], $match) === 1) { + $result[$i]['os'] = $match['os']; + $result[$i]['version'] = $match['version']; } } } @@ -131,9 +148,9 @@ FROM public function getClientByName($name) { $result = ClientRecord::finder()->findByName($name); if (is_object($result)) { - $result = [$result]; + $result = [(array)$result]; $this->setSpecialFields($result); - $result = array_shift($result); + $result = (object)array_shift($result); } return $result; } @@ -141,9 +158,9 @@ FROM public function getClientById($id) { $result = ClientRecord::finder()->findByclientid($id); if (is_object($result)) { - $result = [$result]; + $result = [(array)$result]; $this->setSpecialFields($result); - $result = array_shift($result); + $result = (object)array_shift($result); } return $result; } diff --git a/gui/baculum/protected/API/Modules/ClientRecord.php b/gui/baculum/protected/API/Modules/ClientRecord.php index df6052d28..9f26bb38c 100644 --- a/gui/baculum/protected/API/Modules/ClientRecord.php +++ b/gui/baculum/protected/API/Modules/ClientRecord.php @@ -42,6 +42,7 @@ class ClientRecord extends APIDbModule { public $os; public $version; + public $running_jobs; public static function finder($className = __CLASS__) { return parent::finder($className); diff --git a/gui/baculum/protected/API/Pages/API/Clients.php b/gui/baculum/protected/API/Pages/API/Clients.php index bec18a559..59fa0c37b 100644 --- a/gui/baculum/protected/API/Pages/API/Clients.php +++ b/gui/baculum/protected/API/Pages/API/Clients.php @@ -43,6 +43,8 @@ class Clients extends BaculumAPIServer { $os = $this->Request->contains('os') && $misc->isValidNameExt($this->Request['os']) ? $this->Request['os'] : ''; $version = $this->Request->contains('version') && $misc->isValidColumn($this->Request['version']) ? $this->Request['version'] : ''; $mode = $this->Request->contains('overview') && $misc->isValidBooleanTrue($this->Request['overview']) ? ClientManager::CLIENT_RESULT_MODE_OVERVIEW : ClientManager::CLIENT_RESULT_MODE_NORMAL; + $order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: null; + $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction']: null; $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client')); if ($result->exitcode === 0) { $params = []; @@ -110,9 +112,30 @@ class Clients extends BaculumAPIServer { $jobs = $result->output; } + $sort = []; + if (!is_null($order_by)) { + if (is_null($order_direction)) { + $order_direction = 'ASC'; + } + $cr = new \ReflectionClass('Baculum\API\Modules\ClientRecord'); + $sort_cols = $cr->getProperties(); + $order_by_lc = strtolower($order_by); + $columns = []; + foreach ($sort_cols as $cols) { + $columns[] = $cols->getName(); + } + if (!in_array($order_by_lc, $columns)) { + $this->output = ClientError::MSG_ERROR_INVALID_PROPERTY; + $this->error = ClientError::ERROR_INVALID_PROPERTY; + return; + } + $sort = [[$order_by_lc, $order_direction]]; + } + $clients = $this->getModule('client')->getClients( $limit, $offset, + $sort, $params, $jobs, $mode diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index aa13d3007..1843948e2 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -515,6 +515,25 @@ "schema": { "type": "string" } + }, + { + "name": "order_by", + "in": "query", + "required": false, + "description": "Sort by selected client property (default by name). There can be any client property.", + "schema": { + "type": "string" + } + }, + { + "name": "order_direction", + "in": "query", + "required": false, + "description": "Order direction. It can be 'asc' (ascending order) or 'desc' (descending order - default)", + "schema": { + "type": "string", + "enum": ["asc", "desc"] + } } ] }