diff --git a/src/public/js/ui/alertManagementModal.js b/src/public/js/ui/alertManagementModal.js index 068097a92..9394a0d2a 100644 --- a/src/public/js/ui/alertManagementModal.js +++ b/src/public/js/ui/alertManagementModal.js @@ -670,6 +670,10 @@ PulseApp.ui.alertManagementModal = (() => {
+
@@ -1068,11 +1082,11 @@ PulseApp.ui.alertManagementModal = (() => { // Handle save if (saveBtn) { - saveBtn.addEventListener('click', saveCustomAlert); + saveBtn.addEventListener('click', () => saveCustomAlert(existingAlert)); } } - async function saveCustomAlert() { + async function saveCustomAlert(existingAlert = null) { const form = document.getElementById('custom-alert-form'); const formData = new FormData(form); @@ -1116,16 +1130,24 @@ PulseApp.ui.alertManagementModal = (() => { thresholds: thresholds, // Use array of thresholds instead of single values sendEmail: formData.has('sendEmail'), sendWebhook: formData.has('sendWebhook'), - enabled: true, - createdAt: Date.now() + enabled: existingAlert ? existingAlert.enabled : true, + createdAt: existingAlert ? existingAlert.createdAt : Date.now() }; - console.log('Creating custom alert:', alertConfig); + // If editing, preserve the existing alert ID + if (existingAlert) { + alertConfig.id = existingAlert.id; + } + + console.log(existingAlert ? 'Updating custom alert:' : 'Creating custom alert:', alertConfig); try { // Save to backend via API - const response = await fetch('/api/alerts/rules', { - method: 'POST', + const url = existingAlert ? `/api/alerts/rules/${existingAlert.id}` : '/api/alerts/rules'; + const method = existingAlert ? 'PUT' : 'POST'; + + const response = await fetch(url, { + method: method, headers: { 'Content-Type': 'application/json', }, @@ -1138,10 +1160,10 @@ PulseApp.ui.alertManagementModal = (() => { } const result = await response.json(); - console.log('Alert rule created successfully:', result); + console.log(`Alert rule ${existingAlert ? 'updated' : 'created'} successfully:`, result); const thresholdSummary = thresholds.map(t => `${t.type.toUpperCase()}: ${t.value}${['cpu', 'memory', 'disk'].includes(t.type) ? '%' : ''}`).join(', '); - alert(`Custom alert created successfully!\n\nAlert: ${alertConfig.name}\nThresholds: ${thresholdSummary}\n\nRule ID: ${result.rule?.id || 'Generated'}`); + alert(`Custom alert ${existingAlert ? 'updated' : 'created'} successfully!\n\nAlert: ${alertConfig.name}\nThresholds: ${thresholdSummary}\n\nRule ID: ${result.rule?.id || existingAlert?.id || 'Generated'}`); // Close modal document.getElementById('custom-alert-modal').remove(); @@ -1380,6 +1402,30 @@ PulseApp.ui.alertManagementModal = (() => { } } + async function editCustomAlert(alertId) { + try { + // First, fetch the current alert data + 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 alert = data.rules ? data.rules.find(rule => rule.id === alertId) : null; + + if (!alert) { + throw new Error('Alert not found'); + } + + // Open the custom alert modal with pre-filled data + openCustomAlertModal(alert.thresholds || [], alert); + + } catch (error) { + console.error('Failed to load alert for editing:', error); + alert(`Failed to load alert for editing: ${error.message}`); + } + } + async function deleteCustomAlert(alertId) { if (!confirm('Are you sure you want to delete this custom alert? This action cannot be undone.')) { return; @@ -1418,6 +1464,7 @@ PulseApp.ui.alertManagementModal = (() => { closeModal, switchTab: switchTab, openCustomAlertModal, + editCustomAlert, toggleCustomAlert, deleteCustomAlert, loadCurrentAlerts: () => {