From: Marcin Haba Date: Wed, 21 Jun 2023 08:36:46 +0000 (+0200) Subject: baculum: Add offset and limit parameters to director status endpoint X-Git-Tag: Release-13.0.4~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca62aa1e83941ad9344169dca8f100682a2d14da;p=thirdparty%2Fbacula.git baculum: Add offset and limit parameters to director status endpoint --- diff --git a/gui/baculum/protected/API/Modules/StatusDirector.php b/gui/baculum/protected/API/Modules/StatusDirector.php index c24ebad76..1c0ee9223 100644 --- a/gui/baculum/protected/API/Modules/StatusDirector.php +++ b/gui/baculum/protected/API/Modules/StatusDirector.php @@ -46,9 +46,11 @@ class StatusDirector extends ComponentStatusModule { * @param string $director director name * @param string $component_name component name * @param string $type output type (e.g. header, running, terminated ...etc.) + * @param integer $limit item limit + * @param integer $offset item offset * @return array ready array parsed component status output */ - public function getStatus($director, $component_name = null, $type = null) { + public function getStatus($director, $component_name = null, $type = null, $limit = 0, $offset = 0) { $ret = array('output' => array(), 'error' => 0); $result = $this->getModule('bconsole')->bconsoleCommand( $director, @@ -57,13 +59,36 @@ class StatusDirector extends ComponentStatusModule { ); if ($result->exitcode === 0) { $ret['output'] = $this->parseStatus($result->output, $type); - if (is_string($type) && key_exists($type, $ret['output'])) { + $is_type = (is_string($type) && key_exists($type, $ret['output'])); + if ($is_type) { if ($type === self::OUTPUT_TYPE_HEADER) { $ret['output'] = array_pop($ret['output'][$type]); } else { $ret['output'] = $ret['output'][$type]; } } + if ($limit > 0 || $offset > 0 && (!$type || $type != self::OUTPUT_TYPE_HEADER)) { + $output = $ret['output']; + if ($is_type) { + $output = [$type => $output]; + } + $item_types = [ + self::OUTPUT_TYPE_SCHEDULED, + self::OUTPUT_TYPE_RUNNING, + self::OUTPUT_TYPE_TERMINATED + ]; + foreach ($output as $type => $values) { + if (!in_array($type, $item_types)) { + continue; + } + $output[$type] = array_slice($values, $offset, $limit); + } + if ($is_type) { + $ret['output'] = $output[$type]; + } else { + $ret['output'] = $output; + } + } } else { $ret['output'] = $result->output; } @@ -94,9 +119,13 @@ class StatusDirector extends ComponentStatusModule { ); $opts = array(); for($i = 0; $i < count($output); $i++) { + if (preg_match('/^(error|errmsg)=/', $output[$i]) === 1) { + // skip error type items + continue; + } if (in_array($output[$i], $types)) { // check if type $type = rtrim($output[$i], ':'); - } elseif ($type === self::OUTPUT_TYPE_HEADER && count($opts) == 0 && $output[$i] === '') { + } elseif ($type === self::OUTPUT_TYPE_HEADER && count($opts) == 1 && strpos($output[$i], 'level') === 0) { /** * special treating 'scheduled' type because this type * is missing in the api status dir output. diff --git a/gui/baculum/protected/API/Pages/API/DirectorStatus.php b/gui/baculum/protected/API/Pages/API/DirectorStatus.php index d055f45af..5d203b6ce 100644 --- a/gui/baculum/protected/API/Pages/API/DirectorStatus.php +++ b/gui/baculum/protected/API/Pages/API/DirectorStatus.php @@ -34,10 +34,13 @@ use Baculum\Common\Modules\Errors\GenericError; class DirectorStatus extends ConsoleOutputPage { public function get() { + $misc = $this->getModule('misc'); $status = $this->getModule('status_dir'); $director = $this->Request->contains('name') && $this->getModule('misc')->isValidName($this->Request['name']) ? $this->Request['name'] : null; $type = $this->Request->contains('type') && $status->isValidOutputType($this->Request['type']) ? $this->Request['type'] : null; $out_format = $this->Request->contains('output') && $this->isOutputFormatValid($this->Request['output']) ? $this->Request['output'] : parent::OUTPUT_FORMAT_RAW; + $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0; + $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0; $dirs = []; $result = $this->getModule('bconsole')->getDirectors(); @@ -58,7 +61,9 @@ class DirectorStatus extends ConsoleOutputPage { } elseif ($out_format === parent::OUTPUT_FORMAT_JSON) { $out = $this->getJSONOutput([ 'director' => $director, - 'type' => $type + 'type' => $type, + 'limit' => $limit, + 'offset' => $offset ]); } $this->output = $out['output']; @@ -88,7 +93,9 @@ class DirectorStatus extends ConsoleOutputPage { return $status->getStatus( $params['director'], null, - $params['type'] + $params['type'], + $params['limit'], + $params['offset'] ); } } diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 3bdbee747..5aa8eb8f5 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -7314,6 +7314,24 @@ "type": "string", "enum": ["header", "scheduled", "running", "terminated"] } + }, + { + "name": "limit", + "in": "query", + "description": "Item limit. It works only for JSON type output.", + "required": false, + "schema": { + "type": "integer" + } + }, + { + "name": "offset", + "in": "query", + "description": "Items offset. It works only for JSON type output.", + "required": false, + "schema": { + "type": "integer" + } } ] }