]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add order_by and order_direction params to jobs objects endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Mon, 3 Apr 2023 14:50:53 +0000 (16:50 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 20 Apr 2023 10:00:26 +0000 (12:00 +0200)
gui/baculum/protected/API/Modules/JobManager.php
gui/baculum/protected/API/Pages/API/JobsObjects.php
gui/baculum/protected/API/openapi_baculum.json

index eb025a663702121ea4adb5d10fe830f9151fe5ce..f31dce949d50cfea83dfa78946833b8a05c103f3 100644 (file)
@@ -191,7 +191,7 @@ LEFT JOIN FileSet USING (FilesetId)'
         * @param string $view job records view (basic, full)
         * @return array job record list with objects or empty list if no job found
         */
-       public function getJobsObjectsOverview($criteria = array(), $limit_val = null, $offset_val = 0, $view = self::JOB_RESULT_VIEW_FULL) {
+       public function getJobsObjectsOverview($criteria = array(), $limit_val = null, $offset_val = 0, $sort_col = 'Job.EndTime', $sort_order = 'DESC', $view = self::JOB_RESULT_VIEW_FULL) {
 
                $connection = JobRecord::finder()->getDbConnection();
                $connection->setActive(true);
@@ -202,6 +202,13 @@ LEFT JOIN FileSet USING (FilesetId)'
                        // start transaction
                        $pdo->beginTransaction();
 
+                       // Prepare order_by and order_direction values
+                       $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 . ' ' . strtoupper($sort_order);
+
                        // create temporary table
                        $jobid_jobstatus_tname = 'jobid_jobstatus_' . getmypid();
                        $db_params = $this->getModule('api_config')->getConfig('db');
@@ -273,7 +280,7 @@ LEFT JOIN FileSet USING (FilesetId)'
                        $sql = 'SELECT ' . $job_record . ' 
                                FROM ' . $jobid_jobstatus_tname . '
                                JOIN Job USING (JobId) 
-                               WHERE ' . $jobid_jobstatus_tname . '.JobStatus IN (\'' . implode('\',\'', $this->js_running) . '\') ' . $jlimit . $offset;
+                               WHERE ' . $jobid_jobstatus_tname . '.JobStatus IN (\'' . implode('\',\'', $this->js_running) . '\') ' . $order . $jlimit . $offset;
 
                        $statement = Database::runQuery($sql);
                        $running_jobs = $statement->fetchAll(PDO::FETCH_ASSOC);
@@ -283,7 +290,7 @@ LEFT JOIN FileSet USING (FilesetId)'
                                FROM ' . $jobid_jobstatus_tname . '
                                JOIN Job USING (JobId) 
                                WHERE ' . $jobid_jobstatus_tname . '.JobStatus IN (\'' . implode('\',\'', $this->js_unsuccessful) . '\')
-                               ORDER BY Job.EndTime DESC ' . $jlimit . $offset;
+                               ' . $order . $jlimit . $offset;
 
                        $statement = Database::runQuery($sql);
                        $unsuccessful_jobs = $statement->fetchAll(PDO::FETCH_ASSOC);
@@ -293,7 +300,7 @@ LEFT JOIN FileSet USING (FilesetId)'
                                FROM ' . $jobid_jobstatus_tname .'
                                JOIN Job USING (JobId) 
                                WHERE ' . $jobid_jobstatus_tname . '.JobStatus IN (\'' . implode('\',\'', $this->js_successful) . '\') AND ' . $jobid_jobstatus_tname . '.JobErrors > 0
-                               ORDER BY Job.EndTime DESC ' . $jlimit . $offset;
+                               ' . $order . $jlimit . $offset;
 
                        $statement = Database::runQuery($sql);
                        $warning_jobs = $statement->fetchAll(PDO::FETCH_ASSOC);
@@ -303,7 +310,7 @@ LEFT JOIN FileSet USING (FilesetId)'
                                FROM ' . $jobid_jobstatus_tname .'
                                JOIN Job USING (JobId) 
                                WHERE ' . $jobid_jobstatus_tname . '.JobStatus IN (\'' . implode('\',\'', $this->js_successful) . '\') AND ' . $jobid_jobstatus_tname . '.JobErrors = 0
-                               ORDER BY Job.EndTime DESC ' . $jlimit . $offset;
+                               ' . $order . $jlimit . $offset;
 
                        $statement = Database::runQuery($sql);
                        $successful_jobs = $statement->fetchAll(PDO::FETCH_ASSOC);
@@ -313,7 +320,7 @@ LEFT JOIN FileSet USING (FilesetId)'
                                FROM ' . $jobid_jobstatus_tname . '
                                JOIN Job USING (JobId) 
                                WHERE ' . $jobid_jobstatus_tname . '.JobStatus NOT IN (\'' . implode('\',\'', $this->js_running) . '\')
-                               ORDER BY Job.StartTime DESC' . $jlimit . $offset;
+                               ' . $order . $jlimit . $offset;
 
                        $statement = Database::runQuery($sql);
                        $all_jobs = $statement->fetchAll(PDO::FETCH_ASSOC);
index ac9af22b341b71945d948d90101a40292d5f0cd6..a5a8280f6385ce89eca00c4bc54727b192bfb158 100644 (file)
@@ -23,6 +23,7 @@
 use Baculum\API\Modules\ConsoleOutputPage;
 use Baculum\API\Modules\ConsoleOutputQueryPage;
 use Baculum\API\Modules\JobManager;
+use Baculum\API\Modules\JobRecord;
 use Baculum\Common\Modules\Logging;
 use Baculum\Common\Modules\Errors\JobError;
 
@@ -57,8 +58,24 @@ class JobsObjects extends BaculumAPIServer {
                $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;
                $age = $this->Request->contains('age') && $misc->isValidInteger($this->Request['age']) ? (int)$this->Request['age'] : null;
+               $order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: 'Job.EndTime';
+               $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction']: 'DESC';
                $view = ($this->Request->contains('view') && $misc->isValidResultView($this->Request['view'])) ? $this->Request['view'] : JobManager::JOB_RESULT_VIEW_FULL;
 
+               $jr = new \ReflectionClass('Baculum\API\Modules\JobRecord');
+               $sort_cols = $jr->getProperties();
+               $order_by_lc = strtolower($order_by);
+               $columns = [];
+               foreach ($sort_cols as $cols) {
+                       $name = $cols->getName();
+                       $columns[] = $name;
+               }
+               if (!in_array($order_by_lc, $columns)) {
+                       $this->output = JobError::MSG_ERROR_INVALID_PROPERTY;
+                       $this->error = JobError::ERROR_INVALID_PROPERTY;
+                       return;
+               }
+
                if (!empty($jobids)) {
                        /**
                         * If jobids parameter provided, all other parameters are not used.
@@ -72,6 +89,8 @@ class JobsObjects extends BaculumAPIServer {
                                $params,
                                null,
                                0,
+                               $order_by,
+                               $order_direction,
                                $view
                        );
                        $this->output = $result;
@@ -282,6 +301,8 @@ class JobsObjects extends BaculumAPIServer {
                                        $params,
                                        $limit,
                                        $offset,
+                                       $order_by,
+                                       $order_direction,
                                        $view
                                );
                                $this->output = $result;
index 3101901be92eb3cc97e7b77e21ec4905b1c4a8f6..87cb6e8107831d98ab4da7dcd5e5987936e9d1b4 100644 (file)
                                                        "type": "integer"
                                                }
                                        },
+                                       {
+                                               "name": "order_by",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Sort by selected job property (default endtime). There can be any job property (jobid, job, clientid ...etc.)",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "order_direction",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "Order direction. It can be 'asc' (ascending order) or 'desc' (descending order - default)",
+                                               "schema": {
+                                                       "type": "string",
+                                                       "enum": ["asc", "desc"]
+                                               }
+                                       },
                                        {
                                                "name": "view",
                                                "in": "query",