Show database size in system info tool

This commit is contained in:
Jan Böhmer 2023-01-16 00:06:14 +01:00
parent 706253ce74
commit 6eb40c6a41
4 changed files with 47 additions and 0 deletions

View file

@ -86,6 +86,7 @@ class ToolsController extends AbstractController
//DB section
'db_type' => $DBInfoHelper->getDatabaseType() ?? 'Unknown',
'db_version' => $DBInfoHelper->getDatabaseVersion() ?? 'Unknown',
'db_size' => $DBInfoHelper->getDatabaseSize(),
]);
}

View file

@ -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;
}
}

View file

@ -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];
}
}

View file

@ -9,5 +9,9 @@
<td>Database Server Version</td>
<td>{{ db_version }}</td>
</tr>
<tr>
<td>Database Size</td>
<td>{{ db_size != null ? db_size | format_bytes : 'Unknown' }}</td>
</tr>
</tbody>
</table>