From: Marcin Haba Date: Tue, 30 Jan 2024 12:49:04 +0000 (+0100) Subject: baculum: Add volume statistics endpoint X-Git-Tag: Beta-15.0.1~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e5e752a7ad5d9ebc4d852acbffebe4f1cf13586;p=thirdparty%2Fbacula.git baculum: Add volume statistics endpoint --- diff --git a/gui/baculum/protected/API/Modules/VolumeManager.php b/gui/baculum/protected/API/Modules/VolumeManager.php index 6a35e35d4..7af694364 100644 --- a/gui/baculum/protected/API/Modules/VolumeManager.php +++ b/gui/baculum/protected/API/Modules/VolumeManager.php @@ -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 index 000000000..a9588b3eb --- /dev/null +++ b/gui/baculum/protected/API/Pages/API/VolumeStatsByVolType.php @@ -0,0 +1,40 @@ + + * @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; + } +} diff --git a/gui/baculum/protected/API/Pages/API/endpoints.xml b/gui/baculum/protected/API/Pages/API/endpoints.xml index e65599133..867e6e6ca 100644 --- a/gui/baculum/protected/API/Pages/API/endpoints.xml +++ b/gui/baculum/protected/API/Pages/API/endpoints.xml @@ -62,6 +62,7 @@ + diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 65b3bebab..feaaa34bc 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -5519,6 +5519,76 @@ ] } }, + "/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"],