*/
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) {
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) {
*/
use Baculum\Common\Modules\Errors\ObjectError;
+use Baculum\API\Modules\ObjectRecord;
/**
* Objects endpoint.
$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)) {
];
}
- $objects = $this->getModule('object')->getObjects($params, $limit);
+ $objects = $this->getModule('object')->getObjects($params, $limit, $groupby);
$this->output = $objects;
$this->error = ObjectError::ERROR_NO_ERRORS;
}
"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"
+ }
}
]
}