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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user