]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add groupby parameter to object list endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 15 Nov 2022 08:02:45 +0000 (09:02 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 17 Nov 2022 09:05:10 +0000 (10:05 +0100)
gui/baculum/protected/API/Modules/ObjectManager.php
gui/baculum/protected/API/Pages/API/Objects.php
gui/baculum/protected/API/openapi_baculum.json

index e3ec67e699ed090928d429f01f2b630349b47262..73f15ac2246953af22fa80e52281291eca45dd43 100644 (file)
@@ -31,7 +31,7 @@ namespace Baculum\API\Modules;
  */
 class ObjectManager extends APIModule {
 
-       public function getObjects($criteria = array(), $limit_val = null) {
+       public function getObjects($criteria = array(), $limit_val = null, $groupby = null) {
                $sort_col = 'ObjectId';
                $db_params = $this->getModule('api_config')->getConfig('db');
                if ($db_params['type'] === Database::PGSQL_TYPE) {
@@ -51,7 +51,22 @@ FROM Object
 LEFT JOIN Job USING (JobId) '
 . $where['where'] . $order . $limit;
 
-               return ObjectRecord::finder()->findAllBySql($sql, $where['params']);
+               $result = ObjectRecord::finder()->findAllBySql($sql, $where['params']);
+               if (is_string($groupby) && is_array($result)) {
+                       // Group results
+                       $new_result = [];
+                       for ($i = 0; $i < count($result); $i++) {
+                               if (!property_exists($result[$i], $groupby)) {
+                                       continue;
+                               }
+                               if (!key_exists($result[$i]->{$groupby}, $new_result)) {
+                                       $new_result[$result[$i]->{$groupby}] = [];
+                               }
+                               $new_result[$result[$i]->{$groupby}][] = $result[$i];
+                       }
+                       $result = $new_result;
+               }
+               return $result;
        }
 
        public function getObjectById($objectid) {
index cf3af9979f457913f6838b1dab97e9c55d7b4f4e..736fd2e17f89d28594a130f301549c98b0bf8d4e 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 use Baculum\Common\Modules\Errors\ObjectError;
+use Baculum\API\Modules\ObjectRecord;
 
 /**
  * Objects endpoint.
@@ -42,6 +43,29 @@ class Objects extends BaculumAPIServer {
                $objectstatus = $this->Request->contains('objectstatus') && $misc->isValidState($this->Request['objectstatus']) ? $this->Request['objectstatus'] : null;
                $jobname = $this->Request->contains('jobname') && $misc->isValidName($this->Request['jobname']) ? $this->Request['jobname'] : null;
                $jobids = $this->Request->contains('jobids') && $misc->isValidIdsList($this->Request['jobids']) ? explode(',', $this->Request['jobids']) : [];
+               $groupby = $this->Request->contains('groupby') && $misc->isValidColumn($this->Request['groupby']) ? strtolower($this->Request['groupby']) : null;
+
+               if (is_string($groupby)) {
+                       $or = new \ReflectionClass('Baculum\API\Modules\ObjectRecord');
+                       $group_cols = $or->getProperties();
+
+                       $cols_excl = ['jobname'];
+                       $columns = [];
+                       foreach ($group_cols as $cols) {
+                               $name = $cols->getName();
+                               // skip columns not existing in the catalog
+                               if (in_array($name, $cols_excl)) {
+                                       continue;
+                               }
+                               $columns[] = $name;
+                       }
+
+                       if (!in_array($groupby, $columns)) {
+                               $this->output = ObjectError::MSG_ERROR_INVALID_PROPERTY;
+                               $this->error = ObjectError::ERROR_INVALID_PROPERTY;
+                               return;
+                       }
+               }
 
                $params = [];
                if (!empty($objecttype)) {
@@ -94,7 +118,7 @@ class Objects extends BaculumAPIServer {
                        ];
                }
 
-               $objects = $this->getModule('object')->getObjects($params, $limit);
+               $objects = $this->getModule('object')->getObjects($params, $limit, $groupby);
                $this->output = $objects;
                $this->error = ObjectError::ERROR_NO_ERRORS;
        }
index 6b467c8a34ce2bd22ca24b5d7a3559d581148ca0..e64883c813e5d9f38d6f8b7c5c3e6f2fe92888e3 100644 (file)
                                                                                "error": {
                                                                                        "type": "integer",
                                                                                        "description": "Error code",
-                                                                                       "enum": [0, 1, 2, 3, 6, 7, 1000]
+                                                                                       "enum": [0, 1, 2, 3, 6, 7, 520, 1000]
                                                                                }
                                                                        }
                                                                }
                                                "schema": {
                                                        "type": "string"
                                                }
+                                       },
+                                       {
+                                               "name": "groupby",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Object property to group results. Note: it changes the result output format.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
                                        }
                                ]
                        }