'192.168.0.16', 'database' => 'controls', 'username' => 'lasucaai', 'password' => 'is413#dfslw', ]; // ============================================================================ // Database Connection // ============================================================================ function getAlertLogConnection($config) { $conn = mysqli_connect( $config['host'], $config['username'], $config['password'], $config['database'] ); if ($conn) { mysqli_set_charset($conn, 'utf8mb4'); } return $conn ?: null; } // ============================================================================ // Data Queries // ============================================================================ function getAlertLogs($conn, $filters = []) { $params = []; $types = ''; $sql = "SELECT id, alert_type, alert_tag, alert_value, threshold_value, alert_message, source_page, logged_at FROM alert_log WHERE 1=1"; // Filter by alert type if (!empty($filters['type'])) { $sql .= " AND alert_type = ?"; $params[] = $filters['type']; $types .= 's'; } // Filter by tag if (!empty($filters['tag'])) { $sql .= " AND alert_tag = ?"; $params[] = $filters['tag']; $types .= 's'; } // Filter by date range if (!empty($filters['date_from'])) { $sql .= " AND logged_at >= ?"; $params[] = $filters['date_from'] . ' 00:00:00'; $types .= 's'; } if (!empty($filters['date_to'])) { $sql .= " AND logged_at <= ?"; $params[] = $filters['date_to'] . ' 23:59:59'; $types .= 's'; } $sql .= " ORDER BY logged_at DESC LIMIT 500"; $logs = []; if (!empty($params)) { $stmt = mysqli_prepare($conn, $sql); if ($stmt) { mysqli_stmt_bind_param($stmt, $types, ...$params); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while ($row = mysqli_fetch_assoc($result)) { $logs[] = $row; } mysqli_stmt_close($stmt); } } else { $result = mysqli_query($conn, $sql); if ($result) { while ($row = mysqli_fetch_assoc($result)) { $logs[] = $row; } } } return $logs; } function getAlertStats($conn) { $sql = "SELECT COUNT(*) as total_alerts, COUNT(DISTINCT alert_tag) as unique_tags, MIN(logged_at) as first_alert, MAX(logged_at) as last_alert, SUM(CASE WHEN alert_type = 'danger' THEN 1 ELSE 0 END) as danger_count, SUM(CASE WHEN alert_type = 'warning' THEN 1 ELSE 0 END) as warning_count, SUM(CASE WHEN alert_type = 'info' THEN 1 ELSE 0 END) as info_count, SUM(CASE WHEN logged_at > DATE_SUB(NOW(), INTERVAL 24 HOUR) THEN 1 ELSE 0 END) as last_24h FROM alert_log"; $result = mysqli_query($conn, $sql); return $result ? mysqli_fetch_assoc($result) : null; } function getDistinctTags($conn) { $sql = "SELECT DISTINCT alert_tag FROM alert_log ORDER BY alert_tag"; $result = mysqli_query($conn, $sql); $tags = []; if ($result) { while ($row = mysqli_fetch_assoc($result)) { $tags[] = $row['alert_tag']; } } return $tags; } function getAlertsByHour($conn, $days = 7) { $sql = "SELECT DATE(logged_at) as alert_date, HOUR(logged_at) as alert_hour, COUNT(*) as count FROM alert_log WHERE logged_at > DATE_SUB(NOW(), INTERVAL ? DAY) GROUP BY DATE(logged_at), HOUR(logged_at) ORDER BY alert_date, alert_hour"; $stmt = mysqli_prepare($conn, $sql); $data = []; if ($stmt) { mysqli_stmt_bind_param($stmt, 'i', $days); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } mysqli_stmt_close($stmt); } return $data; } function getTopAlerts($conn, $limit = 10) { $sql = "SELECT alert_tag, alert_type, COUNT(*) as count, MIN(alert_value) as min_value, MAX(alert_value) as max_value, AVG(alert_value) as avg_value, MAX(logged_at) as last_seen FROM alert_log GROUP BY alert_tag, alert_type ORDER BY count DESC LIMIT ?"; $stmt = mysqli_prepare($conn, $sql); $data = []; if ($stmt) { mysqli_stmt_bind_param($stmt, 'i', $limit); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } mysqli_stmt_close($stmt); } return $data; } // ============================================================================ // Main Logic // ============================================================================ $conn = getAlertLogConnection($config); $connectionError = $conn === null; $tableExists = false; $stats = null; $logs = []; $distinctTags = []; $topAlerts = []; $alertsByHour = []; // Get filter values $filterType = isset($_GET['type']) ? trim($_GET['type']) : ''; $filterTag = isset($_GET['tag']) ? trim($_GET['tag']) : ''; $filterDateFrom = isset($_GET['date_from']) ? trim($_GET['date_from']) : ''; $filterDateTo = isset($_GET['date_to']) ? trim($_GET['date_to']) : ''; if (!$connectionError) { // Check if table exists $tableCheck = mysqli_query($conn, "SHOW TABLES LIKE 'alert_log'"); $tableExists = $tableCheck && mysqli_num_rows($tableCheck) > 0; if ($tableExists) { $stats = getAlertStats($conn); $distinctTags = getDistinctTags($conn); $topAlerts = getTopAlerts($conn, 10); $alertsByHour = getAlertsByHour($conn, 7); $logs = getAlertLogs($conn, [ 'type' => $filterType, 'tag' => $filterTag, 'date_from' => $filterDateFrom, 'date_to' => $filterDateTo, ]); } mysqli_close($conn); } // ============================================================================ // Page Layout // ============================================================================ $pageTitle = 'Alert Log Viewer'; $pageSubtitle = 'System Alert History'; $pageDescription = 'View and analyze historical system alerts.'; $layoutWithoutSidebar = true; $layoutReturnUrl = 'overview.php'; $layoutReturnLabel = 'Back to overview'; $assetBasePath = ''; require __DIR__ . '/includes/layout/header.php'; ?>
⚠️ Unable to connect to the database.
📋

No Alert Logs Yet

The alert_log table doesn't exist yet. Alerts will be logged when they first trigger.

Total Alerts
Last 24 Hours
Danger
Warning
Info
Unique Tags

🔥 Most Frequent Alerts

Range: - | Avg:

📜 Alert Log

Clear
📭

No alerts match your filters.

Time Type Tag Value Threshold Message
Showing alerts (max 500)