Commit 14cd5a65 authored by Marcin Haba's avatar Marcin Haba
Browse files

baculum: Add to jobs endpoint parameters to sort property and sort order

parent ea4363a0
......@@ -34,13 +34,12 @@ use Prado\Data\ActiveRecord\TActiveRecordCriteria;
*/
class JobManager extends APIModule {
public function getJobs($criteria = array(), $limit_val = null) {
$sort_col = 'JobId';
public function getJobs($criteria = array(), $limit_val = null, $sort_col = 'JobId', $sort_order = 'ASC') {
$db_params = $this->getModule('api_config')->getConfig('db');
if ($db_params['type'] === Database::PGSQL_TYPE) {
$sort_col = strtolower($sort_col);
}
$order = ' ORDER BY ' . $sort_col . ' DESC';
$order = ' ORDER BY ' . $sort_col . ' ' . strtoupper($sort_order);
$limit = '';
if(is_int($limit_val) && $limit_val > 0) {
$limit = ' LIMIT ' . $limit_val;
......
......@@ -21,6 +21,7 @@
*/
use Baculum\API\Modules\BaculumAPIServer;
use Baculum\API\Modules\JobRecord;
use Baculum\Common\Modules\Errors\JobError;
/**
......@@ -31,6 +32,7 @@ use Baculum\Common\Modules\Errors\JobError;
* @package Baculum API
*/
class Jobs extends BaculumAPIServer {
public function get() {
$misc = $this->getModule('misc');
$limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
......@@ -47,6 +49,8 @@ class Jobs extends BaculumAPIServer {
$endtime_to = $this->Request->contains('endtime_to') && $misc->isValidInteger($this->Request['endtime_to']) ? (int)$this->Request['endtime_to'] : null;
$realendtime_from = $this->Request->contains('realendtime_from') && $misc->isValidInteger($this->Request['realendtime_from']) ? (int)$this->Request['realendtime_from'] : null;
$realendtime_to = $this->Request->contains('realendtime_to') && $misc->isValidInteger($this->Request['realendtime_to']) ? (int)$this->Request['realendtime_to'] : null;
$order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: 'JobId';
$order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction']: 'DESC';
if (!empty($clientid) && !$misc->isValidId($clientid)) {
$this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
......@@ -60,6 +64,25 @@ class Jobs extends BaculumAPIServer {
$this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
return;
}
$jr = new \ReflectionClass('Baculum\API\Modules\JobRecord');
$sort_cols = $jr->getProperties();
$order_by_lc = strtolower($order_by);
$cols_excl = ['client', 'fileset', 'pool'];
$columns = [];
foreach ($sort_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($order_by_lc, $columns)) {
$this->output = JobError::MSG_ERROR_INVALID_PROPERTY;
$this->error = JobError::ERROR_INVALID_PROPERTY;
return;
}
$params = [];
$jobstatuses = array_keys($misc->getJobState());
......@@ -215,7 +238,7 @@ class Jobs extends BaculumAPIServer {
}
if ($error === false) {
$jobs = $this->getModule('job')->getJobs($params, $limit);
$jobs = $this->getModule('job')->getJobs($params, $limit, $order_by, $order_direction);
$this->output = $jobs;
$this->error = JobError::ERROR_NO_ERRORS;
}
......
......@@ -35,10 +35,12 @@ class GenericError {
const ERROR_INTERNAL_ERROR = 1000;
const ERROR_INVALID_PATH = 8;
const ERROR_WRONG_EXITCODE = 9;
const ERROR_INVALID_PROPERTY = 520;
const MSG_ERROR_NO_ERRORS = '';
const MSG_ERROR_INVALID_COMMAND = 'Invalid command.';
const MSG_ERROR_INTERNAL_ERROR = 'Internal error.';
const MSG_ERROR_INVALID_PATH = 'Invalid path.';
const MSG_ERROR_WRONG_EXITCODE = 'Wrong exitcode.';
const MSG_ERROR_INVALID_PROPERTY = 'Invalid property.';
}
......@@ -302,6 +302,13 @@ class Miscellaneous extends TModule {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
public function isValidColumn($column) {
return (preg_match('/^[\w+.]+$/i', $column) === 1);
}
public function isValidOrderDirection($order) {
return (preg_match('/^(asc|desc)$/i', $order) === 1);
}
public function escapeCharsToConsole($path) {
return preg_replace('/([$])/', '\\\${1}', $path);
}
......
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