feat(ui): add full table CSV download on database stats card
Each table row in the stats card now has a download button that exports the entire table as CSV with proper column headers, generated server-side. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -584,6 +584,52 @@ if ($type == "db_table_stats") {
|
||||
}
|
||||
}
|
||||
|
||||
if ($type == "download_full_table") {
|
||||
$databasePath = '/var/www/nebuleair_pro_4g/sqlite/sensors.db';
|
||||
$table = $_GET['table'] ?? '';
|
||||
|
||||
// Whitelist of allowed tables
|
||||
$allowedTables = ['data_NPM', 'data_NPM_5channels', 'data_BME280', 'data_envea', 'data_WIND', 'data_MPPT', 'data_NOISE'];
|
||||
|
||||
if (!in_array($table, $allowedTables)) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['error' => 'Invalid table name']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// CSV headers per table
|
||||
$csvHeaders = [
|
||||
'data_NPM' => 'TimestampUTC,PM1,PM2.5,PM10,Temperature_sensor,Humidity_sensor',
|
||||
'data_NPM_5channels' => 'TimestampUTC,PM_ch1,PM_ch2,PM_ch3,PM_ch4,PM_ch5',
|
||||
'data_BME280' => 'TimestampUTC,Temperature,Humidity,Pressure',
|
||||
'data_envea' => 'TimestampUTC,NO2,H2S,NH3,CO,O3,SO2',
|
||||
'data_WIND' => 'TimestampUTC,Wind_speed_kmh,Wind_direction_V',
|
||||
'data_MPPT' => 'TimestampUTC,Battery_voltage,Battery_current,Solar_voltage,Solar_power,Charger_status',
|
||||
'data_NOISE' => 'TimestampUTC,Current_LEQ,DB_A_value'
|
||||
];
|
||||
|
||||
try {
|
||||
$db = new PDO("sqlite:$databasePath");
|
||||
$rows = $db->query("SELECT * FROM $table ORDER BY timestamp ASC")->fetchAll(PDO::FETCH_NUM);
|
||||
|
||||
header('Content-Type: text/csv; charset=utf-8');
|
||||
header('Content-Disposition: attachment; filename="' . $table . '_full.csv"');
|
||||
|
||||
$output = fopen('php://output', 'w');
|
||||
// Write header
|
||||
fputcsv($output, explode(',', $csvHeaders[$table]));
|
||||
// Write data rows
|
||||
foreach ($rows as $row) {
|
||||
fputcsv($output, $row);
|
||||
}
|
||||
fclose($output);
|
||||
} catch (PDOException $e) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['error' => 'Database query failed: ' . $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($type == "linux_disk") {
|
||||
$command = 'df -h /';
|
||||
$output = shell_exec($command);
|
||||
|
||||
Reference in New Issue
Block a user