query(" SELECT a.alert_id, a.timestamp, w.name as warehouse_name, s.name as slot_name, n.name as node_name, a.metric_name, a.actual_value, a.threshold_value, a.status FROM alerts a JOIN nodes n ON a.node_id = n.id JOIN slots s ON n.slot_id = s.id JOIN warehouses w ON s.warehouse_id = w.id ORDER BY a.timestamp DESC "); $alerts = $stmt->fetchAll(PDO::FETCH_ASSOC); // 2. Fetch data for overview charts // Alerts per warehouse $stmt = $pdo->query(" SELECT w.name, COUNT(a.alert_id) as alert_count FROM alerts a JOIN nodes n ON a.node_id = n.id JOIN slots s ON n.slot_id = s.id JOIN warehouses w ON s.warehouse_id = w.id GROUP BY w.name "); $alerts_per_warehouse = $stmt->fetchAll(PDO::FETCH_ASSOC); // Alerts over time (last 7 days) $stmt = $pdo->query(" SELECT DATE(timestamp) as alert_date, COUNT(alert_id) as alert_count FROM alerts WHERE timestamp >= CURDATE() - INTERVAL 7 DAY GROUP BY DATE(timestamp) ORDER BY alert_date "); $alerts_over_time = $stmt->fetchAll(PDO::FETCH_ASSOC); // Alert status distribution $stmt = $pdo->query(" SELECT status, COUNT(alert_id) as alert_count FROM alerts GROUP BY status "); $alert_status_distribution = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode([ 'success' => true, 'alerts' => $alerts, 'chart_data' => [ 'per_warehouse' => $alerts_per_warehouse, 'over_time' => $alerts_over_time, 'status_distribution' => $alert_status_distribution ] ]); } catch (Exception $e) { http_response_code(500); echo json_encode(['success' => false, 'error' => $e->getMessage()]); }