From faa525ebde6105ec3672cf36658e381ef4630e58 Mon Sep 17 00:00:00 2001 From: "courtmanr@gmail.com" Date: Thu, 5 Jun 2025 11:57:09 +0100 Subject: [PATCH] feat: integrate custom alerts with backend API endpoints - Replace localStorage with real API calls to /api/alerts/rules - Load custom alerts from backend via GET /api/alerts/rules - Create alerts via POST /api/alerts/rules - Update alerts via PUT /api/alerts/rules/:id - Delete alerts via DELETE /api/alerts/rules/:id - Improve alert card display to handle backend data structure - Add proper error handling for all API operations --- src/public/js/ui/alertManagementModal.js | 169 +++++++++++++---------- 1 file changed, 98 insertions(+), 71 deletions(-) diff --git a/src/public/js/ui/alertManagementModal.js b/src/public/js/ui/alertManagementModal.js index b6cce6438..317811441 100644 --- a/src/public/js/ui/alertManagementModal.js +++ b/src/public/js/ui/alertManagementModal.js @@ -458,56 +458,49 @@ PulseApp.ui.alertManagementModal = (() => { systemAlertsContent.innerHTML = systemAlerts.map(alert => createSystemAlertCard(alert)).join(''); } - function loadCustomAlerts() { + async function loadCustomAlerts() { const customAlertsContent = document.getElementById('custom-alerts-content'); if (!customAlertsContent) return; - // Load custom alerts from local storage (temporary solution until backend integration) - const storedAlerts = getCustomAlertsFromStorage(); - - if (storedAlerts.length === 0) { + try { + // Load custom alerts from the backend API + const response = await fetch('/api/alerts/rules'); + if (!response.ok) { + throw new Error(`Failed to fetch alert rules: ${response.status}`); + } + + const data = await response.json(); + const customAlerts = data.rules ? data.rules.filter(rule => + rule.group === 'custom' || rule.type === 'compound_threshold' + ) : []; + + if (customAlerts.length === 0) { + customAlertsContent.innerHTML = ` +
+ + + +

No custom alerts configured

+ +
+ `; + return; + } + + // Display the custom alerts + customAlertsContent.innerHTML = customAlerts.map(alert => createCustomAlertCard(alert)).join(''); + + } catch (error) { + console.error('[AlertManagementModal] Failed to load custom alerts:', error); customAlertsContent.innerHTML = ` -
- - - -

No custom alerts configured

- +
+

Failed to load custom alerts

+

${error.message}

`; - return; - } - - // Display the custom alerts - customAlertsContent.innerHTML = storedAlerts.map(alert => createCustomAlertCard(alert)).join(''); - } - - function getCustomAlertsFromStorage() { - try { - const stored = localStorage.getItem('pulse-custom-alerts'); - return stored ? JSON.parse(stored) : []; - } catch (error) { - console.warn('[AlertManagementModal] Failed to load custom alerts from storage:', error); - return []; - } - } - - function saveCustomAlertToStorage(alertConfig) { - try { - const existingAlerts = getCustomAlertsFromStorage(); - const newAlert = { - ...alertConfig, - id: `custom-${Date.now()}-${Math.random().toString(36).substr(2, 9)}` - }; - existingAlerts.push(newAlert); - localStorage.setItem('pulse-custom-alerts', JSON.stringify(existingAlerts)); - return newAlert; - } catch (error) { - console.error('[AlertManagementModal] Failed to save custom alert to storage:', error); - throw error; } } @@ -518,26 +511,30 @@ PulseApp.ui.alertManagementModal = (() => { ` ).join('') || ''; - const createdDate = new Date(alert.createdAt).toLocaleDateString(); + const createdDate = alert.createdAt ? new Date(alert.createdAt).toLocaleDateString() : 'Unknown'; + const targetDisplay = alert.targetType === 'all' ? 'All VMs/LXCs' : + alert.targetType === 'vm' ? 'VMs only' : + alert.targetType === 'lxc' ? 'LXCs only' : + alert.specificTarget || alert.targetType?.toUpperCase() || 'All'; return `
-
${alert.name}
- - ${alert.enabled ? 'Enabled' : 'Disabled'} +
${alert.name || alert.description || 'Unnamed Alert'}
+ + ${alert.enabled !== false ? 'Enabled' : 'Disabled'}
- Created: ${createdDate} • Target: ${alert.targetType === 'all' ? 'All VMs/LXCs' : alert.targetType.toUpperCase()} + Created: ${createdDate} • Target: ${targetDisplay}
-