From 8d362f29ce5e91a73793c19517274a0c4a2ab342 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 16 Sep 2026 15:05:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8:=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D1=8B=20=D0=B4=D0=BB=D1=8F=20MySQL=20=D1=81?= =?UTF-8?q?=D1=87=D0=B8=D1=82=D0=B0=D1=82=D1=8C=20=D0=BF=D0=BE=20informati?= =?UTF-8?q?on=5Fschema,=20=D0=B0=20=D0=BD=D0=B5=20=D0=BF=D0=BE=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D1=83=20sqlite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/Support/Metrics.php | 30 +++++++++++++++++++++++++++--- tests/MetricsTest.php | 12 ++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/framework/Support/Metrics.php b/framework/Support/Metrics.php index 3ff2db2..7d81201 100644 --- a/framework/Support/Metrics.php +++ b/framework/Support/Metrics.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Rsgrinko\Proton\Support; use Rsgrinko\Proton\Cache\Cache; +use Rsgrinko\Proton\Database\Connection; use Rsgrinko\Proton\Models\AuditEntry; use Rsgrinko\Proton\Models\User; use Rsgrinko\Proton\Models\UserNotification; @@ -193,16 +194,39 @@ final class Metrics */ public static function storage(): array { - $database = (string) Config::get('db.sqlite.path', APP_ROOT . '/var/app.sqlite'); - $free = @disk_free_space(APP_ROOT); + $free = @disk_free_space(APP_ROOT); return [ - 'database_bytes' => is_file($database) ? (int) filesize($database) : 0, + 'database_bytes' => self::databaseBytes(), 'var_bytes' => self::size(APP_ROOT . '/var'), 'free_bytes' => $free === false ? 0 : (int) $free, ]; } + /** + * Размер базы: у SQLite это файл целиком, у MySQL — сумма данных и индексов + * по своей схеме by information_schema (сам файл базы MySQL тут не при чём — + * это общий для всех баз сервера кластер). + */ + private static function databaseBytes(): int + { + $db = Connection::instance(); + + if ($db->isSqlite()) { + $path = (string) Config::get('db.sqlite.path', APP_ROOT . '/var/app.sqlite'); + + return is_file($path) ? (int) filesize($path) : 0; + } + + $row = $db->selectOne( + 'SELECT SUM(data_length + index_length) AS bytes + FROM information_schema.tables + WHERE table_schema = DATABASE()' + ); + + return $row === null || $row['bytes'] === null ? 0 : (int) $row['bytes']; + } + /** * Размер папки. Обходим без рекурсии по всему дереву глубоко: var/ и так * плоская, а считать миллион файлов на каждый запрос незачем. diff --git a/tests/MetricsTest.php b/tests/MetricsTest.php index 5e6ef9a..e38b6a2 100644 --- a/tests/MetricsTest.php +++ b/tests/MetricsTest.php @@ -7,10 +7,22 @@ declare(strict_types=1); */ use Rsgrinko\Proton\Cache\Cache; +use Rsgrinko\Proton\Database\Connection; use Rsgrinko\Proton\Models\Setting; use Rsgrinko\Proton\Support\Metrics; use Rsgrinko\Proton\Support\Monitor; +test('показатели: размер базы считается по драйверу, а не всегда как файл sqlite', function (): void { + $storage = Metrics::storage(); + + assertTrue($storage['database_bytes'] >= 0); + assertTrue($storage['var_bytes'] >= 0); + + // Тестовая база — sqlite, тут это файл; для mysql тот же метод суммирует + // data_length + index_length по information_schema.tables (см. Metrics::databaseBytes) + assertTrue(Connection::instance()->isSqlite(), 'тесты всегда идут на своей sqlite'); +}); + test('показатели: запросы складываются, ошибки и медленные считаются отдельно', function (): void { Cache::flush();