Commit e84cf590 authored by Marcin Haba's avatar Marcin Haba
Browse files

baculum: Add groupby parameter to object list endpoint

parent 1560de3f
......@@ -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) {
......
......@@ -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;
}
......
......@@ -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"
}
}
]
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment