diff --git a/src/Controller/ToolsController.php b/src/Controller/ToolsController.php index 18b86cbf..d3bb0423 100644 --- a/src/Controller/ToolsController.php +++ b/src/Controller/ToolsController.php @@ -86,6 +86,7 @@ class ToolsController extends AbstractController //DB section 'db_type' => $DBInfoHelper->getDatabaseType() ?? 'Unknown', 'db_version' => $DBInfoHelper->getDatabaseVersion() ?? 'Unknown', + 'db_size' => $DBInfoHelper->getDatabaseSize(), ]); } diff --git a/src/Services/Misc/DBInfoHelper.php b/src/Services/Misc/DBInfoHelper.php index 92312fce..5f377997 100644 --- a/src/Services/Misc/DBInfoHelper.php +++ b/src/Services/Misc/DBInfoHelper.php @@ -73,4 +73,32 @@ class DBInfoHelper return null; } + + /** + * Returns the database size in bytes. + * @return int|null The database size in bytes or null if unknown + * @throws \Doctrine\DBAL\Exception + */ + public function getDatabaseSize(): ?int + { + if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { + try { + return $this->connection->fetchOne('SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = DATABASE()'); + } catch (\Doctrine\DBAL\Exception $e) { + return null; + } + } + + if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { + try { + return $this->connection->fetchOne('SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();'); + } catch (\Doctrine\DBAL\Exception $e) { + return null; + } + } + + return null; + } + + } \ No newline at end of file diff --git a/src/Twig/FormatExtension.php b/src/Twig/FormatExtension.php index 6d251267..87c906b1 100644 --- a/src/Twig/FormatExtension.php +++ b/src/Twig/FormatExtension.php @@ -86,6 +86,8 @@ final class FormatExtension extends AbstractExtension new TwigFilter('format_si', [$this, 'siFormat']), /** Format the given amount using the given MeasurementUnit */ new TwigFilter('format_amount', [$this, 'amountFormat']), + /** Format the given number of bytes as human readable number */ + new TwigFilter('format_bytes', [$this, 'formatBytes']), ]; } @@ -107,4 +109,16 @@ final class FormatExtension extends AbstractExtension { return $this->amountFormatter->format($value, $unit, $options); } + + /** + * @param $bytes + * @param int $precision + * @return string + */ + public function formatBytes(int $bytes, int $precision = 2): string + { + $size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; + $factor = floor((strlen((string) $bytes) - 1) / 3); + return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor]; + } } diff --git a/templates/Tools/ServerInfos/_db.html.twig b/templates/Tools/ServerInfos/_db.html.twig index 7b7f1d99..d146b129 100644 --- a/templates/Tools/ServerInfos/_db.html.twig +++ b/templates/Tools/ServerInfos/_db.html.twig @@ -9,5 +9,9 @@ Database Server Version {{ db_version }} + + Database Size + {{ db_size != null ? db_size | format_bytes : 'Unknown' }} + \ No newline at end of file