github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/static/js/all-alerts.js (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 'use strict'; 18 19 let alertGridDiv = null; 20 let alertRowData = []; 21 22 let mapIndexToAlertState=new Map([ 23 [0,"Normal"], 24 [1,"Pending"], 25 [2,"Firing"], 26 ]); 27 28 $(document).ready(function () { 29 if (Cookies.get('theme')) { 30 theme = Cookies.get('theme'); 31 $('body').attr('data-theme', theme); 32 } 33 $('.theme-btn').on('click', themePickerHandler); 34 getAllAlerts(); 35 36 $('#new-alert-rule').on('click',function(){ 37 window.location.href = "../alert.html"; 38 }); 39 }); 40 41 //get all alerts 42 function getAllAlerts(){ 43 $.ajax({ 44 method: "get", 45 url: "api/allalerts", 46 headers: { 47 'Content-Type': 'application/json; charset=utf-8', 48 'Accept': '*/*' 49 }, 50 dataType: 'json', 51 crossDomain: true, 52 }).then(function (res) { 53 displayAllAlerts(res.alerts); 54 }) 55 } 56 class btnRenderer { 57 init(params) { 58 this.eGui = document.createElement('span'); 59 this.eGui.innerHTML = `<div id="alert-grid-btn"> 60 <button class='btn' id="editbutton" title="Edit Alert Rule"></button> 61 <button class="btn-simple" id="delbutton" title="Delete Alert Rule"></button> 62 </div>`; 63 this.eButton = this.eGui.querySelector('#editbutton'); 64 this.dButton = this.eGui.querySelector('.btn-simple'); 65 66 function editAlert(event){ 67 var queryString = "?id=" + params.data.alertId; 68 window.location.href = "../alert.html" + queryString; 69 event.stopPropagation(); 70 } 71 72 function deleteAlert() { 73 $.ajax({ 74 method: 'delete', 75 url: 'api/alerts/delete', 76 headers: { 77 'Content-Type': 'application/json; charset=utf-8', 78 Accept: '*/*', 79 }, 80 data: JSON.stringify({ 81 alert_id: params.data.alertId 82 }), 83 crossDomain: true, 84 }).then(function (res) { 85 let deletedRowID = params.data.rowId; 86 alertGridOptions.api.applyTransaction({ 87 remove: [{ rowId: deletedRowID }], 88 }); 89 showToast(res.message) 90 }); 91 } 92 93 function showPrompt(event) { 94 event.stopPropagation(); 95 const alertRuleName = params.data.alertName; 96 const confirmationMessage = `Are you sure you want to delete the "<strong>${alertRuleName}</strong>" alert?`; 97 98 $('.popupOverlay, .popupContent').addClass('active'); 99 $('#delete-alert-name').html(confirmationMessage); 100 101 $('#cancel-btn, .popupOverlay, #delete-btn').click(function () { 102 $('.popupOverlay, .popupContent').removeClass('active'); 103 }); 104 $('#delete-btn').click(deleteAlert) 105 } 106 107 108 this.eButton.addEventListener('click', editAlert); 109 this.dButton.addEventListener('click', showPrompt); 110 } 111 112 getGui() { 113 return this.eGui; 114 } 115 refresh(params) { 116 return false; 117 } 118 } 119 120 let alertColumnDefs = [ 121 { 122 field: "rowId", 123 hide: true 124 }, 125 { 126 field: "alertId", 127 hide: true 128 }, 129 { 130 headerName: "State", 131 field: "alertState", 132 width:50, 133 }, 134 { 135 headerName: "Alert Name", 136 field: "alertName", 137 width: 100, 138 }, 139 { 140 headerName: "Labels", 141 field: "labels", 142 width:100, 143 }, 144 { 145 headerName: "Actions", 146 cellRenderer: btnRenderer, 147 width:50, 148 }, 149 ]; 150 151 const alertGridOptions = { 152 columnDefs: alertColumnDefs, 153 rowData: alertRowData, 154 animateRows: true, 155 rowHeight: 44, 156 headerHeight:32, 157 defaultColDef: { 158 icons: { 159 sortAscending: '<i class="fa fa-sort-alpha-up"/>', 160 sortDescending: '<i class="fa fa-sort-alpha-down"/>', 161 }, 162 cellClass: 'align-center-grid', 163 resizable: true, 164 sortable: true, 165 }, 166 enableCellTextSelection: true, 167 suppressScrollOnNewData: true, 168 suppressAnimationFrame: true, 169 getRowId: (params) => params.data.rowId, 170 onGridReady(params) { 171 this.gridApi = params.api; 172 }, 173 onRowClicked: onRowClicked, 174 }; 175 176 function displayAllAlerts(res){ 177 if (alertGridDiv === null) { 178 alertGridDiv = document.querySelector('#ag-grid'); 179 new agGrid.Grid(alertGridDiv, alertGridOptions); 180 } 181 alertGridOptions.api.setColumnDefs(alertColumnDefs); 182 let newRow = new Map() 183 $.each(res, function (key, value) { 184 newRow.set("rowId", key); 185 newRow.set("alertId", value.alert_id); 186 newRow.set("alertName", value.alert_name); 187 let labels= []; 188 value.labels.forEach(function (label) { 189 labels.push(label.label_name + '=' + label.label_value); 190 }); 191 let allLabels = labels.join(', '); 192 193 newRow.set("labels", allLabels); 194 newRow.set("alertState", mapIndexToAlertState.get(value.state)); 195 alertRowData = _.concat(alertRowData, Object.fromEntries(newRow)); 196 }) 197 alertGridOptions.api.setRowData(alertRowData); 198 alertGridOptions.api.sizeColumnsToFit(); 199 } 200 201 function showToast(msg) { 202 let toast = 203 `<div class="div-toast" id="save-db-modal"> 204 ${msg} 205 <button type="button" aria-label="Close" class="toast-close">✖</button> 206 <div>` 207 $('body').prepend(toast); 208 $('.toast-close').on('click', removeToast) 209 setTimeout(removeToast, 2000); 210 } 211 212 function removeToast() { 213 $('.div-toast').remove(); 214 } 215 216 217 function onRowClicked(event) { 218 var queryString = "?id=" + event.data.alertId; 219 window.location.href = "../alert-details.html" + queryString; 220 event.stopPropagation(); 221 }