From: Marcin Haba Date: Thu, 27 Apr 2023 09:45:10 +0000 (+0200) Subject: baculum: Change a way of preparing overview with counters in objects endpoint X-Git-Tag: Release-13.0.3~44 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ca37307ba8597d7af14f0a41e2b59c902660986;p=thirdparty%2Fbacula.git baculum: Change a way of preparing overview with counters in objects endpoint --- diff --git a/gui/baculum/protected/API/Modules/Database.php b/gui/baculum/protected/API/Modules/Database.php index 35edac7cf..71b5d91e4 100644 --- a/gui/baculum/protected/API/Modules/Database.php +++ b/gui/baculum/protected/API/Modules/Database.php @@ -232,8 +232,11 @@ class Database extends APIModule { * @param string $group_by column to use as group * @param array $result database results/records (please note - reference) * @param integer $group_limit group limit (zero means no limit) + * @param string|null $overview_by prepare overview (counts) by given object output property + * @param array overview array or empty array if no overview requested */ - public static function groupBy($group_by, &$result, $group_limit = 0) { + public static function groupBy($group_by, &$result, $group_limit = 0, $overview_by = null) { + $overview = []; if (is_string($group_by) && is_array($result)) { // Group results $new_result = []; @@ -250,9 +253,19 @@ class Database extends APIModule { continue; } $new_result[$result[$i]->{$group_by}][] = $result[$i]; + if (!key_exists($result[$i]->{$overview_by}, $overview)) { + $overview[$result[$i]->{$overview_by}] = [ + $overview_by => $result[$i]->{$overview_by}, + 'count' => 0 + ]; + } + if (is_string($overview_by)) { + $overview[$result[$i]->{$overview_by}]['count']++; + } } $result = $new_result; } + return array_values($overview); } /** diff --git a/gui/baculum/protected/API/Modules/ObjectManager.php b/gui/baculum/protected/API/Modules/ObjectManager.php index 40de52990..4b0724571 100644 --- a/gui/baculum/protected/API/Modules/ObjectManager.php +++ b/gui/baculum/protected/API/Modules/ObjectManager.php @@ -118,12 +118,12 @@ LEFT JOIN Client USING (ClientId) ' . $where['where'] . $order . $limit . $offset; $statement = Database::runQuery($sql, $where['params']); $result = $statement->fetchAll(\PDO::FETCH_OBJ); - Database::groupBy($group_by, $result, $group_limit); + $overview = Database::groupBy($group_by, $result, $group_limit, 'objecttype'); if ($mode == self::OBJECT_RESULT_MODE_OVERVIEW) { // Overview mode. $result = [ 'objects' => $result, - 'overview' => $this->getObjectCountByObjectType($criteria) + 'overview' => $overview ]; } return $result;