]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add offset and limit parameters to director status endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Wed, 21 Jun 2023 08:36:46 +0000 (10:36 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Mon, 3 Jul 2023 08:46:57 +0000 (10:46 +0200)
gui/baculum/protected/API/Modules/StatusDirector.php
gui/baculum/protected/API/Pages/API/DirectorStatus.php
gui/baculum/protected/API/openapi_baculum.json

index c24ebad769f082c8296fe67e78af724e44fcefda..1c0ee9223cbc8ad85f3a3b95153d1d8f18fdb804 100644 (file)
@@ -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.
index d055f45af02f93dac97eed13d54c274db5347593..5d203b6ce7f56bfe909ee42b430530a00da85fca 100644 (file)
@@ -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']
                );
        }
 }
index 3bdbee74733af3df825d7144b5429dd0e9a6b0c2..5aa8eb8f5ad9c1a6f2891fd29e3842e9967970f3 100644 (file)
                                                        "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"
+                                               }
                                        }
                                ]
                        }