]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add volume statistics endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 30 Jan 2024 12:49:04 +0000 (13:49 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Wed, 7 Feb 2024 16:00:06 +0000 (17:00 +0100)
gui/baculum/protected/API/Modules/VolumeManager.php
gui/baculum/protected/API/Pages/API/VolumeStatsByVolType.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/openapi_baculum.json

index 6a35e35d49ca746d4a132bf6ea87d411aece092c..7af694364ed0d1c8f1ec84630d7bc9c7029f684d 100644 (file)
@@ -103,6 +103,36 @@ class VolumeManager extends APIModule {
                ];
        }
 
+       /**
+        * Check if volume type is disk type.
+        *
+        * @return boolean true if it is disk type, otherwise false
+        */
+       private function isDiskVolType($voltype) {
+               $disk_vt = $this->getDiskVolTypes();
+               return in_array($voltype, $disk_vt);
+       }
+
+       /**
+        * Check if volume type is cloud type.
+        *
+        * @return boolean true if it is cloud type, otherwise false
+        */
+       private function isCloudVolType($voltype) {
+               $cloud_vt = $this->getCloudVolTypes();
+               return in_array($voltype, $cloud_vt);
+       }
+
+       /**
+        * Check if volume type is tape type.
+        *
+        * @return boolean true if it is tape type, otherwise false
+        */
+       private function isTapeVolType($voltype) {
+               $tape_vt = $this->getTapeVolTypes();
+               return in_array($voltype, $tape_vt);
+       }
+
        public function getVolumes($criteria = array(), $props = [], $limit_val = 0, $offset_val = 0, $order_by = null, $order_direction = 'DESC') {
                $order_pool_id = 'PoolId';
                $order_volume = 'VolumeName';
@@ -488,5 +518,35 @@ LEFT JOIN Storage USING (StorageId)
                $volumes = array_keys($result);
                return $volumes;
        }
+
+       /**
+        * Get volume statistics per volume type (disk, cloud, tape...)
+        *
+        * @return array statistics or empty array if no volume record found
+        */
+       public function getVolumeStatsByType() {
+               $sql = 'SELECT VolType AS voltype, 
+                              SUM(VolBytes) AS total_size, 
+                              COUNT(1) AS count
+                       FROM Media
+                       GROUP BY voltype';
+               $statement = Database::runQuery($sql);
+               $result = $statement->fetchAll(\PDO::FETCH_GROUP);
+               $stats = [];
+               foreach ($result as $voltype => $item) {
+                       $res = [
+                               'total_size' => (int)$item[0]['total_size'],
+                               'count' => $item[0]['count']
+                       ];
+                       if ($this->isDiskVolType($voltype)) {
+                               $stats[self::VOLTYPE_GROUP_DISK] = $res;
+                       } elseif ($this->isCloudVolType($voltype)) {
+                               $stats[self::VOLTYPE_GROUP_CLOUD] = $res;
+                       } elseif ($this->isTapeVolType($voltype)) {
+                               $stats[self::VOLTYPE_GROUP_TAPE] = $res;
+                       }
+               }
+               return $stats;
+       }
 }
 ?>
diff --git a/gui/baculum/protected/API/Pages/API/VolumeStatsByVolType.php b/gui/baculum/protected/API/Pages/API/VolumeStatsByVolType.php
new file mode 100644 (file)
index 0000000..a9588b3
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2024 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+use Baculum\API\Modules\BaculumAPIServer;
+use Baculum\Common\Modules\Errors\VolumeError;
+
+/**
+ * Volumes endpoint.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class VolumeStatsByVolType extends BaculumAPIServer {
+
+       public function get() {
+               $result = $this->getModule('volume')->getVolumeStatsByType();
+               $this->output = $result;
+               $this->error = VolumeError::ERROR_NO_ERRORS;
+       }
+}
index e655991330cf86d2d10c75459869dee89d789ad8..867e6e6ca087fcac086bcc3267eb982ed35378e9 100644 (file)
@@ -62,6 +62,7 @@
        <url ServiceParameter="VolumeLabelBarcodes" pattern="api/v2/volumes/label/barcodes/" />
        <url ServiceParameter="SlotsUpdate" pattern="api/v2/volumes/update/" />
        <url ServiceParameter="SlotsUpdate" pattern="api/v2/volumes/update/{barcodes}/" parameters.barcodes="barcodes" />
+       <url ServiceParameter="VolumeStatsByVolType" pattern="api/v2/volumes/stats/" />
        <url ServiceParameter="VolumeNames" pattern="api/v2/volumes/names/" />
        <!-- pools endpoints -->
        <url ServiceParameter="Pools" pattern="api/v2/pools/" />
index 65b3bebab8b1f5c6be0219fb61c8badb9f997633..feaaa34bcef00703a2625dec8643f730974a641a 100644 (file)
                                ]
                        }
                },
+               "/api/v2/volumes/stats": {
+                       "get": {
+                               "tags": ["volumes"],
+                               "summary": "Volume statistics.",
+                               "description": "Get volume statistics.",
+                               "responses": {
+                                       "200": {
+                                               "description": "Volume statistics per volume type.",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "object",
+                                                                                       "properties": {
+                                                                                               "disk": {
+                                                                                                       "type": "object",
+                                                                                                       "properties": {
+                                                                                                               "total_size": {
+                                                                                                                       "type": "integer",
+                                                                                                                       "description": "Total size of disk volumes"
+                                                                                                               },
+                                                                                                               "count": {
+                                                                                                                       "type": "integer",
+                                                                                                                       "description": "Disk volume count"
+                                                                                                               }
+                                                                                                       }
+                                                                                               },
+                                                                                               "cloud": {
+                                                                                                       "type": "object",
+                                                                                                       "properties": {
+                                                                                                               "total_size": {
+                                                                                                                       "type": "integer",
+                                                                                                                       "description": "Total size of cloud volumes"
+                                                                                                               },
+                                                                                                               "count": {
+                                                                                                                       "type": "integer",
+                                                                                                                       "description": "Cloud volume count"
+                                                                                                               }
+                                                                                                       }
+                                                                                               },
+                                                                                               "tape": {
+                                                                                                       "type": "object",
+                                                                                                       "properties": {
+                                                                                                               "total_size": {
+                                                                                                                       "type": "integer",
+                                                                                                                       "description": "Total size of tape volumes"
+                                                                                                               },
+                                                                                                               "count": {
+                                                                                                                       "type": "integer",
+                                                                                                                       "description": "Tape volume count"
+                                                                                                               }
+                                                                                                       }
+                                                                                               }
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1, 2, 3, 5, 6, 7, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               },
                "/api/v2/volumes/names": {
                        "get": {
                                "tags": ["volumes"],