]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add overview parameter to objects endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Wed, 5 Apr 2023 11:44:31 +0000 (13:44 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 20 Apr 2023 10:00:26 +0000 (12:00 +0200)
gui/baculum/protected/API/Modules/ObjectManager.php
gui/baculum/protected/API/Pages/API/Objects.php
gui/baculum/protected/API/openapi_baculum.json

index 3bfb17b243f2de73f4fb141d67f9d8eb9f488a70..895146a64f01297aae9e3a4be5932333fc078ba6 100644 (file)
@@ -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);
+       }
 }
index c893d078d02ad6fb034088b32878449b4d70228f..c870816f4fba2cf1c0d4744063efa738827a77f1 100644 (file)
@@ -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;
index 946f173bf0569693177d0167035b4b4f2035198f..602a74bc9e0d1d0e81cd78e4d70d7fbc0545a75b 100644 (file)
                                                "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"
+                                               }
                                        }
                                ]
                        }