]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Change a way of preparing overview with counters in objects endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Thu, 27 Apr 2023 09:45:10 +0000 (11:45 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Mon, 1 May 2023 11:18:25 +0000 (13:18 +0200)
gui/baculum/protected/API/Modules/Database.php
gui/baculum/protected/API/Modules/ObjectManager.php

index 35edac7cf0b0dd249fd011938019242be6ddfbd4..71b5d91e402d4ac55b5927b9beadc65b540f72d8 100644 (file)
@@ -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);
        }
 
        /**
index 40de52990040ec3fb7b3e3280f764fe687f4d372..4b0724571ba13a5e6a9b95ed6887c9cf615c02e4 100644 (file)
@@ -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;