From e84cf590925ea19e338146fa52d49dd16cc2f37f Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Tue, 15 Nov 2022 09:02:45 +0100 Subject: [PATCH] baculum: Add groupby parameter to object list endpoint --- .../protected/API/Modules/ObjectManager.php | 19 ++++++++++++-- .../protected/API/Pages/API/Objects.php | 26 ++++++++++++++++++- .../protected/API/openapi_baculum.json | 11 +++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/gui/baculum/protected/API/Modules/ObjectManager.php b/gui/baculum/protected/API/Modules/ObjectManager.php index e3ec67e69..73f15ac22 100644 --- a/gui/baculum/protected/API/Modules/ObjectManager.php +++ b/gui/baculum/protected/API/Modules/ObjectManager.php @@ -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) { diff --git a/gui/baculum/protected/API/Pages/API/Objects.php b/gui/baculum/protected/API/Pages/API/Objects.php index cf3af9979..736fd2e17 100644 --- a/gui/baculum/protected/API/Pages/API/Objects.php +++ b/gui/baculum/protected/API/Pages/API/Objects.php @@ -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; } diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 6b467c8a3..e64883c81 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -6283,7 +6283,7 @@ "error": { "type": "integer", "description": "Error code", - "enum": [0, 1, 2, 3, 6, 7, 1000] + "enum": [0, 1, 2, 3, 6, 7, 520, 1000] } } } @@ -6366,6 +6366,15 @@ "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" + } } ] } -- 2.47.3