From: Marcin Haba Date: Mon, 26 Jun 2023 09:00:44 +0000 (+0200) Subject: baculum: Add sorting parameters to volumes overview endpoint X-Git-Tag: Release-13.0.4~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f16c4f64757549eba6ffdcd0bbb8369c4b11fd08;p=thirdparty%2Fbacula.git baculum: Add sorting parameters to volumes overview endpoint --- diff --git a/gui/baculum/protected/API/Modules/VolumeManager.php b/gui/baculum/protected/API/Modules/VolumeManager.php index bc4c29abf..f88e1b4e9 100644 --- a/gui/baculum/protected/API/Modules/VolumeManager.php +++ b/gui/baculum/protected/API/Modules/VolumeManager.php @@ -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); diff --git a/gui/baculum/protected/API/Pages/API/VolumesOverview.php b/gui/baculum/protected/API/Pages/API/VolumesOverview.php index 021110cdd..3d556ac44 100644 --- a/gui/baculum/protected/API/Pages/API/VolumesOverview.php +++ b/gui/baculum/protected/API/Pages/API/VolumesOverview.php @@ -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; } } } diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 938299651..89b4464cd 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -4533,6 +4533,44 @@ }, { "$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"] + } } ] }