]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add sorting parameters to volumes overview endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Mon, 26 Jun 2023 09:00:44 +0000 (11:00 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Mon, 3 Jul 2023 08:46:57 +0000 (10:46 +0200)
gui/baculum/protected/API/Modules/VolumeManager.php
gui/baculum/protected/API/Pages/API/VolumesOverview.php
gui/baculum/protected/API/openapi_baculum.json

index bc4c29abf7ca28c0d8a663faa3c90b289f4a2280..f88e1b4e905c5a1000d5c07ca00ed64d6e1efdfe 100644 (file)
@@ -193,9 +193,10 @@ LEFT JOIN Storage USING (StorageId)
         * @param array $criteria SQL criteria to get volume overview
         * @param integer $limit_val item limit
         * @param integer $offset_val offset value
+        * @param array $sort order by and order direction in form [[by1, direction1], [by2, direction2]...etc.]
         * @return array volume overview
         */
-       public function getMediaOverview($criteria = [], $limit_val = 0, $offset_val = 0) {
+       public function getMediaOverview($criteria = [], $limit_val = 0, $offset_val = 0, $sort = [['VolStatus', 'ASC'],['LastWritten', 'DESC']]) {
                $limit = '';
                if(is_int($limit_val) && $limit_val > 0) {
                        $limit = " LIMIT $limit_val ";
@@ -205,6 +206,8 @@ LEFT JOIN Storage USING (StorageId)
                        $offset = ' OFFSET ' . $offset_val;
                }
 
+               $order = Database::getOrder($sort);
+
                $where = Database::getWhere($criteria);
 
                // get volume type count
@@ -250,7 +253,7 @@ LEFT JOIN Storage USING (StorageId)
                        JOIN Storage USING (StorageId)
                        JOIN Pool USING (PoolId)
                        ' .  (!empty($where['where']) ? $where['where'] . ' AND ' : ' WHERE ') . ' VolType IN (' . implode(',', $vt_disk) . ')
-                       ORDER BY VolStatus ASC, LastWritten DESC' . $limit . $offset;
+                       ' . $order . $limit . $offset;
 
                $statement = Database::runQuery($sql, $where['params']);
                $voltype_disk = $statement->fetchAll(PDO::FETCH_OBJ);
@@ -276,7 +279,7 @@ LEFT JOIN Storage USING (StorageId)
                        JOIN Storage USING (StorageId)
                        JOIN Pool USING (PoolId)
                        ' .  (!empty($where['where']) ? $where['where'] . ' AND ' : ' WHERE ') . ' VolType IN (' . implode(',', $vt_tape) . ')
-                       ORDER BY VolStatus ASC, LastWritten DESC' . $limit . $offset;
+                       ' . $order . $limit . $offset;
 
                $statement = Database::runQuery($sql, $where['params']);
                $voltype_tape = $statement->fetchAll(PDO::FETCH_OBJ);
@@ -302,7 +305,7 @@ LEFT JOIN Storage USING (StorageId)
                        JOIN Storage USING (StorageId)
                        JOIN Pool USING (PoolId)
                        ' .  (!empty($where['where']) ? $where['where'] . ' AND ' : ' WHERE ') . ' VolType IN (' . implode(',', $vt_cloud) . ')
-                       ORDER BY VolStatus ASC, LastWritten DESC' . $limit . $offset;
+                       ' . $order . $limit . $offset;
 
                $statement = Database::runQuery($sql, $where['params']);
                $voltype_cloud = $statement->fetchAll(PDO::FETCH_OBJ);
index 021110cdd2aa968b3cfb34d1dc53ccae10a382ff..3d556ac440533b40ee7abd1e62fcdf8205c6dc3f 100644 (file)
@@ -22,6 +22,7 @@
 
 use Baculum\API\Modules\BaculumAPIServer;
 use Baculum\Common\Modules\Errors\PoolError;
+use Baculum\Common\Modules\Errors\VolumeError;
 
 /**
  * Media/Volumes overview endpoint.
@@ -35,6 +36,40 @@ class VolumesOverview extends BaculumAPIServer {
                $misc = $this->getModule('misc');
                $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
                $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0;
+               $order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: 'VolStatus';
+               $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction'] : 'ASC';
+               $sec_order_by = $this->Request->contains('sec_order_by') && $misc->isValidColumn($this->Request['sec_order_by']) ? $this->Request['sec_order_by']: 'LastWritten';
+               $sec_order_direction = $this->Request->contains('sec_order_direction') && $misc->isValidOrderDirection($this->Request['sec_order_direction']) ? $this->Request['sec_order_direction'] : 'DESC';
+               $order = [
+                       [
+                               $order_by,
+                               $order_direction
+                       ],
+                       [
+                               $sec_order_by,
+                               $sec_order_direction
+                       ]
+               ];
+               $jr = new \ReflectionClass('Baculum\API\Modules\VolumeRecord');
+               $sort_cols = $jr->getProperties();
+               $columns = [];
+               foreach ($sort_cols as $cols) {
+                       $columns[] = $cols->getName();
+               }
+               $col_err = null;
+               for ($i = 0; $i < count($order); $i++) {
+                       $order_by_lc = strtolower($order[$i][0]);
+                       if (!in_array($order_by_lc, $columns)) {
+                               $col_err = $order[$i][0];
+                               break;
+                       }
+               }
+               if ($col_err) {
+                       $this->output = VolumeError::MSG_ERROR_INVALID_PROPERTY . ' Prop=>' . $col_err;
+                       $this->error = VolumeError::ERROR_INVALID_PROPERTY;
+                       return;
+               }
+
                $pools = $this->getModule('pool')->getPools();
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
@@ -59,17 +94,18 @@ class VolumesOverview extends BaculumAPIServer {
                                $ret = $this->getModule('volume')->getMediaOverview(
                                        $params,
                                        $limit,
-                                       $offset
+                                       $offset,
+                                       $order
                                );
                                $this->output = $ret;
-                               $this->error = PoolError::ERROR_NO_ERRORS;
+                               $this->error = VolumeError::ERROR_NO_ERRORS;
                        } else {
                                $this->output = PoolError::MSG_ERROR_POOL_DOES_NOT_EXISTS;
                                $this->error = PoolError::ERROR_POOL_DOES_NOT_EXISTS;
                        }
                } else {
-                       $this->output = PoolError::MSG_ERROR_WRONG_EXITCODE;
-                       $this->error = PoolError::ERROR_WRONG_EXITCODE . ' Exitcode=> ' . $result->exitcode;
+                       $this->output = PoolError::MSG_ERROR_WRONG_EXITCODE . ' Exitcode=> ' . $result->exitcode;
+                       $this->error = PoolError::ERROR_WRONG_EXITCODE;
                }
        }
 }
index 9382996515063ad8f478dd122413ab5a35bccdc8..89b4464cd564a04f7703e5b08a4bde12635583a2 100644 (file)
                                        },
                                        {
                                                "$ref": "#/components/parameters/Offset"
+                                       },
+                                       {
+                                               "name": "order_by",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Sort by selected volume property (default volstatus). There can be any volume property (mediaid, volumename...etc.)",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "order_direction",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Order direction. It can be 'asc' (ascending order - default) or 'desc' (descending order)",
+                                               "schema": {
+                                                       "type": "string",
+                                                       "enum": ["asc", "desc"]
+                                               }
+                                       },
+                                       {
+                                               "name": "sec_order_by",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Second dimension sort by selected volume property (default lastwritten). There can be any volume property (mediaid, volumename ...etc.)",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "sec_order_direction",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Direction for second dimension of sorting. It can be 'asc' (ascending order) or 'desc' (descending order - default)",
+                                               "schema": {
+                                                       "type": "string",
+                                                       "enum": ["asc", "desc"]
+                                               }
                                        }
                                ]
                        }