From: Marcin Haba Date: Thu, 28 Sep 2023 07:09:23 +0000 (+0200) Subject: baculum: Add running jobs property to clients endpoint X-Git-Tag: Beta-15.0.0~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=96ff5917a30539142a2e4543919769769f468fbc;p=thirdparty%2Fbacula.git baculum: Add running jobs property to clients endpoint --- diff --git a/gui/baculum/protected/API/Modules/ClientManager.php b/gui/baculum/protected/API/Modules/ClientManager.php index 305526824..2849db15d 100644 --- a/gui/baculum/protected/API/Modules/ClientManager.php +++ b/gui/baculum/protected/API/Modules/ClientManager.php @@ -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) { diff --git a/gui/baculum/protected/API/Pages/API/Clients.php b/gui/baculum/protected/API/Pages/API/Clients.php index 784847527..bec18a559 100644 --- a/gui/baculum/protected/API/Pages/API/Clients.php +++ b/gui/baculum/protected/API/Pages/API/Clients.php @@ -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; diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index f6d751a71..aa13d3007 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -10989,6 +10989,10 @@ "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"