]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add running jobs property to clients endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Thu, 28 Sep 2023 07:09:23 +0000 (09:09 +0200)
committerEric Bollengier <eric@baculasystems.com>
Tue, 31 Oct 2023 15:00:30 +0000 (16:00 +0100)
gui/baculum/protected/API/Modules/ClientManager.php
gui/baculum/protected/API/Pages/API/Clients.php
gui/baculum/protected/API/openapi_baculum.json

index 305526824be78a4f2d12d8bf938799d3c846e72e..2849db15d11066ecb5495a8ea943dfd30fd2bcb8 100644 (file)
@@ -50,10 +50,11 @@ class ClientManager extends APIModule {
         * @param mixed $limit_val result limit value
         * @param int $offset_val result offset value
         * @param array $criteria SQL criteria to get job list
+        * @param array $jobs jobs used to get job properties per client
         * @param string $mode result mode
         * @return array clients or empty list if no client found
         */
-       public function getClients($limit_val = 0, $offset_val = 0, $criteria = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) {
+       public function getClients($limit_val = 0, $offset_val = 0, $criteria = [], $jobs = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) {
                $limit = '';
                if(is_int($limit_val) && $limit_val > 0) {
                        $limit = ' LIMIT ' . $limit_val;
@@ -62,12 +63,37 @@ class ClientManager extends APIModule {
                if (is_int($offset_val) && $offset_val > 0) {
                        $offset = ' OFFSET ' . $offset_val;
                }
+               $jcriteria = [];
+               if (count($jobs) > 0) {
+                       $jcriteria['Job.Name'] = [];
+                       $jcriteria['Job.Name'][] = [
+                               'operator' => 'IN',
+                               'vals' => $jobs
+                       ];
+               }
+               $jwhere = Database::getWhere($jcriteria, true);
+               if (!empty($jwhere['where'])) {
+                       $jwhere['where'] = ' AND ' . $jwhere['where'];
+               }
                $where = Database::getWhere($criteria);
 
-               $sql = 'SELECT *
-FROM Client 
-' . $where['where'] . $offset . $limit;
-               $statement = Database::runQuery($sql, $where['params']);
+               $sql = '
+SELECT DISTINCT 
+       Client.*,
+       COALESCE(J.running_jobs, 0) AS running_jobs
+FROM
+       Client AS Client
+       LEFT JOIN (
+               SELECT ClientId AS clientid, COUNT(1) AS running_jobs
+               FROM Client
+               JOIN Job USING (ClientId)
+               WHERE Job.JobStatus IN (\'C\', \'R\')
+               ' . $jwhere['where'] . '
+               GROUP BY ClientId
+       ) AS J ON (Client.ClientId = J.ClientId) 
+       ' . $where['where'] . $offset . $limit;
+               $params = array_merge($jwhere['params'], $where['params']);
+               $statement = Database::runQuery($sql, $params);
                $result = $statement->fetchAll(\PDO::FETCH_OBJ);
                $this->setSpecialFields($result);
                if ($mode == self::CLIENT_RESULT_MODE_OVERVIEW) {
index 784847527f9ef7fc5a9adca7bc5aeeaa23a4a232..bec18a5598cd69b6465442e701acf6d22a2129c9 100644 (file)
@@ -98,10 +98,23 @@ class Clients extends BaculumAPIServer {
                                        ];
                                }
                        }
+
+                       $jobs = [];
+                       $result = $this->getModule('bconsole')->bconsoleCommand(
+                               $this->director,
+                               ['.jobs'],
+                               null,
+                               true
+                       );
+                       if ($result->exitcode === 0) {
+                               $jobs = $result->output;
+                       }
+
                        $clients = $this->getModule('client')->getClients(
                                $limit,
                                $offset,
                                $params,
+                               $jobs,
                                $mode
                        );
                        $this->output = $clients;
index f6d751a7119e8d5e58635bd81a33cd5f629a4c76..aa13d30074fbbf5031f61f2c0176a85ceb287ab3 100644 (file)
                                        "description": "Retention time (in seconds) for jobs",
                                        "type": "integer"
                                },
+                               "running_jobs": {
+                                       "description": "Number of currently running jobs on client",
+                                       "type": "integer"
+                               },
                                "os": {
                                        "description": "Operating system information",
                                        "type": "string"