From 5ffca3891a1a1dc1e2b4f9fa084ae7d58a7d7b88 Mon Sep 17 00:00:00 2001 From: Daniel Tiringer Date: Mon, 4 Oct 2021 11:56:15 +0200 Subject: [PATCH 1/6] Create static array for active connections --- system/Debug/Toolbar/Collectors/Database.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 6fd5b263f4a3..391032661ca4 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -61,6 +61,14 @@ class Database extends BaseCollector */ protected static $queries = []; + /** + * Array of connections used in a collected set + * of queries. + * + * @var array + */ + protected static $activeConnections = []; + /** * Constructor */ From 0e89b019b1617736bf79de9d6ca57046c3823780 Mon Sep 17 00:00:00 2001 From: Daniel Tiringer Date: Mon, 4 Oct 2021 11:57:15 +0200 Subject: [PATCH 2/6] Add unique query connections to array --- system/Debug/Toolbar/Collectors/Database.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 391032661ca4..6b9c7adb0981 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -91,6 +91,12 @@ public static function collect(Query $query) $max = $config->maxQueries ?: 100; if (count(static::$queries) < $max) { + $connection = $query->db->getDatabase(); + + if (! in_array($connection, self::$activeConnections, true)) { + self::$activeConnections[] = $connection; + } + static::$queries[] = $query; } } From 7072f4393a2cc88d316e4ed3deee27695f03499c Mon Sep 17 00:00:00 2001 From: Daniel Tiringer Date: Mon, 4 Oct 2021 11:57:58 +0200 Subject: [PATCH 3/6] Display the count of active connections --- system/Debug/Toolbar/Collectors/Database.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 6b9c7adb0981..eca887626416 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -162,7 +162,8 @@ public function getBadgeValue(): int */ public function getTitleDetails(): string { - return '(' . count(static::$queries) . ' Queries across ' . ($countConnection = count($this->connections)) . ' Connection' . + return '(' . count(static::$queries) . ' Queries across ' . + ($countConnection = count(self::$activeConnections)) . ' Connection' . ($countConnection > 1 ? 's' : '') . ')'; } From dbd154f4f7e2f42d3dfed31a2d523b576b6eb76f Mon Sep 17 00:00:00 2001 From: Daniel Tiringer <53534182+danielTiringer@users.noreply.github.com> Date: Mon, 4 Oct 2021 11:57:24 +0000 Subject: [PATCH 4/6] Format returned string Co-authored-by: John Paul E. Balandan, CPA <51850998+paulbalandan@users.noreply.github.com> --- system/Debug/Toolbar/Collectors/Database.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index eca887626416..513eed59bd94 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -162,9 +162,14 @@ public function getBadgeValue(): int */ public function getTitleDetails(): string { - return '(' . count(static::$queries) . ' Queries across ' . - ($countConnection = count(self::$activeConnections)) . ' Connection' . - ($countConnection > 1 ? 's' : '') . ')'; + $connectionCount = count(static::$activeConnections); + + return sprintf( + '(%d Queries across %d Connection%s)', + count(static::$queries), + $connectionCount, + $connectionCount > 1 ? 's' : '' + ); } /** From 1c866d3cab6f8699d30dca7de3eab97fc7847482 Mon Sep 17 00:00:00 2001 From: Daniel Tiringer Date: Mon, 4 Oct 2021 14:01:20 +0200 Subject: [PATCH 5/6] Properly pluralize the word query --- system/Debug/Toolbar/Collectors/Database.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 513eed59bd94..429e9d63fcc7 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -162,11 +162,13 @@ public function getBadgeValue(): int */ public function getTitleDetails(): string { + $queryCount = count(static::$queries); $connectionCount = count(static::$activeConnections); return sprintf( - '(%d Queries across %d Connection%s)', - count(static::$queries), + '(%d Quer%s across %d Connection%s)', + $queryCount, + $queryCount > 1 ? 'ies' : 'y', $connectionCount, $connectionCount > 1 ? 's' : '' ); From 0c1a6a08db88d264aa123bedf30979a5527586aa Mon Sep 17 00:00:00 2001 From: Daniel Tiringer Date: Tue, 5 Oct 2021 07:40:38 +0200 Subject: [PATCH 6/6] Refresh connections instead of using a new variable --- system/Debug/Toolbar/Collectors/Database.php | 28 +++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 429e9d63fcc7..3686dd1dea76 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -61,20 +61,12 @@ class Database extends BaseCollector */ protected static $queries = []; - /** - * Array of connections used in a collected set - * of queries. - * - * @var array - */ - protected static $activeConnections = []; - /** * Constructor */ public function __construct() { - $this->connections = \Config\Database::getConnections(); + $this->getConnections(); } /** @@ -91,12 +83,6 @@ public static function collect(Query $query) $max = $config->maxQueries ?: 100; if (count(static::$queries) < $max) { - $connection = $query->db->getDatabase(); - - if (! in_array($connection, self::$activeConnections, true)) { - self::$activeConnections[] = $connection; - } - static::$queries[] = $query; } } @@ -162,8 +148,10 @@ public function getBadgeValue(): int */ public function getTitleDetails(): string { + $this->getConnections(); + $queryCount = count(static::$queries); - $connectionCount = count(static::$activeConnections); + $connectionCount = count($this->connections); return sprintf( '(%d Quer%s across %d Connection%s)', @@ -191,4 +179,12 @@ public function icon(): string { return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADMSURBVEhLY6A3YExLSwsA4nIycQDIDIhRWEBqamo/UNF/SjDQjF6ocZgAKPkRiFeEhoYyQ4WIBiA9QAuWAPEHqBAmgLqgHcolGQD1V4DMgHIxwbCxYD+QBqcKINseKo6eWrBioPrtQBq/BcgY5ht0cUIYbBg2AJKkRxCNWkDQgtFUNJwtABr+F6igE8olGQD114HMgHIxAVDyAhA/AlpSA8RYUwoeXAPVex5qHCbIyMgwBCkAuQJIY00huDBUz/mUlBQDqHGjgBjAwAAACexpph6oHSQAAAAASUVORK5CYII='; } + + /** + * Gets the connections from the database config + */ + private function getConnections() + { + $this->connections = \Config\Database::getConnections(); + } }