]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add sorting parameters to clients endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Mon, 2 Oct 2023 11:39:39 +0000 (13:39 +0200)
committerEric Bollengier <eric@baculasystems.com>
Tue, 31 Oct 2023 15:00:30 +0000 (16:00 +0100)
gui/baculum/protected/API/Modules/ClientManager.php
gui/baculum/protected/API/Modules/ClientRecord.php
gui/baculum/protected/API/Pages/API/Clients.php
gui/baculum/protected/API/openapi_baculum.json

index 2849db15d11066ecb5495a8ea943dfd30fd2bcb8..bd5554527c7e194e34a2809026db51c9f8660ce2 100644 (file)
@@ -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;
        }
index df6052d28215e5471a7947dfc90cb372fda25d8d..9f26bb38cb74ad44fcf2708a7205bb4cc6e59af0 100644 (file)
@@ -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);
index bec18a5598cd69b6465442e701acf6d22a2129c9..59fa0c37b299943eb1dc3f0eb863e974ba8a831b 100644 (file)
@@ -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
index aa13d30074fbbf5031f61f2c0176a85ceb287ab3..1843948e232cda0dc1ba2ac1060623f78fa5b15e 100644 (file)
                                                "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"]
+                                               }
                                        }
                                ]
                        }