This commit is contained in:
PaulVua
2025-02-05 15:09:15 +01:00
parent 268a0586b8
commit 49be391eb3
9 changed files with 306 additions and 9 deletions

View File

@@ -13,8 +13,8 @@ if ($type == "get_npm_sqlite_data") {
$db = new PDO("sqlite:$database_path");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Fetch the last 10 records
$stmt = $db->query("SELECT timestamp, PM1, PM25, PM10 FROM data ORDER BY timestamp DESC LIMIT 10");
// Fetch the last 30 records
$stmt = $db->query("SELECT timestamp, PM1, PM25, PM10 FROM data ORDER BY timestamp DESC LIMIT 30");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
@@ -92,6 +92,57 @@ if ($type == "clear_loopLogs") {
echo $output;
}
if ($type == "database_size") {
// Path to the SQLite database file
$databasePath = '/var/www/nebuleair_pro_4g/sqlite/sensors.db';
// Check if the file exists
if (file_exists($databasePath)) {
try {
// Connect to the SQLite database
$db = new PDO("sqlite:$databasePath");
// Get the file size in bytes
$fileSizeBytes = filesize($databasePath);
// Convert the file size to human-readable formats
$fileSizeKilobytes = $fileSizeBytes / 1024; // KB
$fileSizeMegabytes = $fileSizeKilobytes / 1024; // MB
// Query the number of records in the `data` table
$query = "SELECT COUNT(*) AS total_records FROM data";
$result = $db->query($query);
$recordCount = $result ? $result->fetch(PDO::FETCH_ASSOC)['total_records'] : 0;
// Prepare the JSON response
$data = [
'path' => $databasePath,
'size_bytes' => $fileSizeBytes,
'size_kilobytes' => round($fileSizeKilobytes, 2),
'size_megabytes' => round($fileSizeMegabytes, 2),
'data_table_records' => $recordCount
];
// Output the JSON response
echo json_encode($data, JSON_PRETTY_PRINT);
} catch (PDOException $e) {
// Handle database connection errors
echo json_encode([
'error' => 'Database query failed: ' . $e->getMessage()
]);
}
} else {
// Handle error if the file doesn't exist
echo json_encode([
'error' => 'Database file not found',
'path' => $databasePath
]);
}
}
if ($type == "linux_disk") {
$command = 'df -h /';
$output = shell_exec($command);