feat(ui): add database stats card on database page

Show table info (entry count, oldest/newest dates, total DB size) in a
new card on the database page, with auto-refresh and i18n support.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
PaulVua
2026-02-16 12:07:58 +01:00
parent e20bb0b8fc
commit 20c6a12251
4 changed files with 150 additions and 4 deletions

View File

@@ -530,6 +530,60 @@ if ($type == "database_size") {
}
if ($type == "db_table_stats") {
$databasePath = '/var/www/nebuleair_pro_4g/sqlite/sensors.db';
if (file_exists($databasePath)) {
try {
$db = new PDO("sqlite:$databasePath");
// Database file size
$fileSizeBytes = filesize($databasePath);
$fileSizeMB = round($fileSizeBytes / (1024 * 1024), 2);
// Sensor data tables to inspect
$tables = ['data_NPM', 'data_NPM_5channels', 'data_BME280', 'data_envea', 'data_WIND', 'data_MPPT', 'data_NOISE'];
$tableStats = [];
foreach ($tables as $tableName) {
// Check if table exists
$check = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='$tableName'");
if ($check->fetch()) {
$countResult = $db->query("SELECT COUNT(*) as cnt FROM $tableName")->fetch();
$count = (int)$countResult['cnt'];
$oldest = null;
$newest = null;
if ($count > 0) {
$oldestResult = $db->query("SELECT MIN(timestamp) as ts FROM $tableName")->fetch();
$newestResult = $db->query("SELECT MAX(timestamp) as ts FROM $tableName")->fetch();
$oldest = $oldestResult['ts'];
$newest = $newestResult['ts'];
}
$tableStats[] = [
'name' => $tableName,
'count' => $count,
'oldest' => $oldest,
'newest' => $newest
];
}
}
echo json_encode([
'success' => true,
'size_mb' => $fileSizeMB,
'size_bytes' => $fileSizeBytes,
'tables' => $tableStats
]);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'error' => 'Database query failed: ' . $e->getMessage()]);
}
} else {
echo json_encode(['success' => false, 'error' => 'Database file not found']);
}
}
if ($type == "linux_disk") {
$command = 'df -h /';
$output = shell_exec($command);