]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add overview parameter to job list endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Mon, 16 Jan 2023 14:43:58 +0000 (15:43 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sun, 5 Mar 2023 06:06:30 +0000 (07:06 +0100)
gui/baculum/protected/API/Modules/JobManager.php
gui/baculum/protected/API/Pages/API/Jobs.php
gui/baculum/protected/API/openapi_baculum.json

index 354d75837303fe0229e9b1402993f718ae2453ac..0a13a0bd1f0073735b01ff320f1afcd81daa7022 100644 (file)
@@ -34,7 +34,7 @@ use Prado\Data\ActiveRecord\TActiveRecordCriteria;
  */
 class JobManager extends APIModule {
 
-       public function getJobs($criteria = array(), $limit_val = null, $offset_val = 0, $sort_col = 'JobId', $sort_order = 'ASC') {
+       public function getJobs($criteria = array(), $limit_val = null, $offset_val = 0, $sort_col = 'JobId', $sort_order = 'ASC', $overview = false) {
                $db_params = $this->getModule('api_config')->getConfig('db');
                if ($db_params['type'] === Database::PGSQL_TYPE) {
                    $sort_col = strtolower($sort_col);
@@ -61,7 +61,37 @@ LEFT JOIN Pool USING (PoolId)
 LEFT JOIN FileSet USING (FilesetId)'
 . $where['where'] . $order . $limit . $offset;
 
-               return JobRecord::finder()->findAllBySql($sql, $where['params']);
+               $result = JobRecord::finder()->findAllBySql($sql, $where['params']);
+               if ($overview) {
+                       $misc = $this->getModule('misc');
+                       $st_ok = array_keys($misc->getJobStatesByType('ok'));
+                       $st_warn = array_keys($misc->getJobStatesByType('warning'));
+                       $st_err = array_keys($misc->getJobStatesByType('error'));
+                       $st_can = array_keys($misc->getJobStatesByType('cancel'));
+                       $st_run = array_keys($misc->getJobStatesByType('running'));
+                       $successful = array_merge($st_ok, $st_warn);
+                       $unsuccessful = array_merge($st_err, $st_can);
+                       $running = $st_run;
+                       $sql = 'SELECT
+                               (SELECT COUNT(1) FROM Job ' . $where['where'] . ' AND Job.JobStatus IN (\'' . implode('\',\'', $successful) . '\')) AS successful,
+                               (SELECT COUNT(1) FROM Job ' . $where['where'] . ' AND Job.JobStatus IN (\'' . implode('\',\'', $unsuccessful) . '\')) AS unsuccessful,
+                               (SELECT COUNT(1) FROM Job ' . $where['where'] . ' AND Job.JobStatus IN (\'' . implode('\',\'', $running) . '\')) AS running,
+                               (SELECT COUNT(1) FROM Job ' . $where['where'] . ') AS all
+                       ';
+
+                       $record = JobRecord::finder();
+                       $connection = $record->getDbConnection();
+                       $tableInfo = $record->getRecordGateway()->getRecordTableInfo($record);
+                       $builder = $tableInfo->createCommandBuilder($connection);
+                       $command = $builder->applyCriterias($sql, $where['params']);
+                       $res = $command->query();
+                       $ov = $res->read();
+                       $result = [
+                               'jobs' => $result,
+                               'overview' => $ov
+                       ];
+               }
+               return $result;
        }
 
        public function getJobById($jobid) {
index 8382aab4b1285c892f4646fc7c6b0156661ccb96..c166eb1dedc083d73f1f4a6c8fe5b75a2182751b 100644 (file)
@@ -55,6 +55,7 @@ class Jobs extends BaculumAPIServer {
                $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']: 'JobId';
                $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction']: 'DESC';
+               $overview = ($this->Request->contains('overview') && $misc->isValidBooleanTrue($this->Request['overview']));
 
                if (!empty($jobids)) {
                        /**
@@ -283,7 +284,8 @@ class Jobs extends BaculumAPIServer {
                                        $limit,
                                        $offset,
                                        $order_by,
-                                       $order_direction
+                                       $order_direction,
+                                       $overview
                                );
                                $this->output = $result;
                                $this->error = JobError::ERROR_NO_ERRORS;
index b0d38f5a45e2b8570977aaaab5c9c3c6055ea76d..d7412067047c242012c4eb6f13bdbd96600e7dff 100644 (file)
                                                        "type": "string",
                                                        "enum": ["asc", "desc"]
                                                }
+                                       },
+                                       {
+                                               "name": "overview",
+                                               "in": "query",
+                                               "required": false,
+                                               "description": "If set, it puts jobs in 'jobs' property and adds the 'overview' property with the job count (successful, unsuccessful, running, all). NOTE: Offset and limit parameters do not apply to overview counts.",
+                                               "schema": {
+                                                       "type": "boolean"
+                                               }
                                        }
                                ]
                        }