From: Marcin Haba Date: Wed, 5 Apr 2023 11:44:31 +0000 (+0200) Subject: baculum: Add overview parameter to objects endpoint X-Git-Tag: Release-13.0.3~84 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8e193b5339c504028a0335111adaa389cc3325d;p=thirdparty%2Fbacula.git baculum: Add overview parameter to objects endpoint --- diff --git a/gui/baculum/protected/API/Modules/ObjectManager.php b/gui/baculum/protected/API/Modules/ObjectManager.php index 3bfb17b24..895146a64 100644 --- a/gui/baculum/protected/API/Modules/ObjectManager.php +++ b/gui/baculum/protected/API/Modules/ObjectManager.php @@ -49,6 +49,15 @@ class ObjectManager extends APIModule 'ObjectCount' ]; + /** + * Object result modes. + * Modes: + * - normal - record list without any additional data + * - overview - record list with some summary count (files, vSphere, MySQL, PostgreSQL...) + */ + const OBJECT_RESULT_MODE_NORMAL = 'normal'; + const OBJECT_RESULT_MODE_OVERVIEW = 'overview'; + /** * Object result record view. * Views: @@ -71,7 +80,7 @@ class ObjectManager extends APIModule * @param string $view job records view (basic, full) * @return array object list */ - public function getObjects($criteria = array(), $limit_val = null, $offset_val = 0, $sort_col = 'ObjectId', $sort_order = 'DESC', $group_by = null, $group_limit = 0, $view = self::OBJ_RESULT_VIEW_FULL) { + public function getObjects($criteria = array(), $limit_val = null, $offset_val = 0, $sort_col = 'ObjectId', $sort_order = 'DESC', $group_by = null, $group_limit = 0, $view = self::OBJ_RESULT_VIEW_FULL, $mode = self::OBJECT_RESULT_MODE_NORMAL) { $db_params = $this->getModule('api_config')->getConfig('db'); if ($db_params['type'] === Database::PGSQL_TYPE) { $sort_col = strtolower($sort_col); @@ -109,6 +118,13 @@ JOIN Job USING (JobId) ' $statement = Database::runQuery($sql, $where['params']); $result = $statement->fetchAll(\PDO::FETCH_OBJ); Database::groupBy($group_by, $result, $group_limit); + if ($mode == self::OBJECT_RESULT_MODE_OVERVIEW) { + // Overview mode. + $result = [ + 'objects' => $result, + 'overview' => $this->getObjectCountByObjectType($criteria) + ]; + } return $result; } @@ -328,4 +344,23 @@ JOIN Job USING (JobId) ' $sth->execute($where['params']); return $sth->fetchAll(\PDO::FETCH_ASSOC); } + + /** + * Get object count by object type. + * NOTE: It accepts the same criteria as ObjectManager::getObjects(). + * + * @param array $criteria SQL criteria + * @return array object type counts + */ + public function getObjectCountByObjectType($criteria) { + $where = Database::getWhere($criteria); + $sql = 'SELECT DISTINCT ObjectType as objecttype, + COUNT(1) AS count + FROM Object + JOIN Job USING (JobId) + ' . $where['where'] . ' + GROUP BY objecttype'; + $statement = Database::runQuery($sql, $where['params']); + return $statement->fetchAll(\PDO::FETCH_ASSOC); + } } diff --git a/gui/baculum/protected/API/Pages/API/Objects.php b/gui/baculum/protected/API/Pages/API/Objects.php index c893d078d..c870816f4 100644 --- a/gui/baculum/protected/API/Pages/API/Objects.php +++ b/gui/baculum/protected/API/Pages/API/Objects.php @@ -22,6 +22,7 @@ use Baculum\Common\Modules\Errors\ObjectError; use Baculum\API\Modules\ObjectRecord; +use Baculum\API\Modules\ObjectManager; /** * Objects endpoint. @@ -57,6 +58,7 @@ class Objects extends BaculumAPIServer { $age = $this->Request->contains('age') && $misc->isValidInteger($this->Request['age']) ? (int)$this->Request['age'] : null; $order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: 'ObjectId'; $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction']: 'DESC'; + $mode = ($this->Request->contains('overview') && $misc->isValidBooleanTrue($this->Request['overview'])) ? ObjectManager::OBJECT_RESULT_MODE_OVERVIEW : ObjectManager::OBJECT_RESULT_MODE_NORMAL; $or = new \ReflectionClass('Baculum\API\Modules\ObjectRecord'); $prop_cols = $or->getProperties(); @@ -229,7 +231,9 @@ class Objects extends BaculumAPIServer { $order_by_lc, $order_direction, $group_by, - $group_limit + $group_limit, + ObjectManager::OBJ_RESULT_VIEW_FULL, + $mode ); $this->output = $objects; $this->error = ObjectError::ERROR_NO_ERRORS; diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 946f173bf..602a74bc9 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -7607,6 +7607,15 @@ "schema": { "type": "integer" } + }, + { + "name": "overview", + "in": "query", + "required": false, + "description": "If set, it puts objects in 'objects' property and adds the 'overview' property with the object count by object type (PostgreSQL, MySQL, vSphere ...etc.) NOTE: Offset and limit parameters do not apply to overview counts.", + "schema": { + "type": "boolean" + } } ] }