github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/static/js/dashboard-from-logs.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 const newDashboardBtn = $(".new-dashboard-btn"); 18 const existingDashboardBtn = $(".existing-dashboard-btn"); 19 const newDashboard = $(".new-dashboard"); 20 const existingDashboard = $(".existing-dashboard"); 21 let newDashboardFlag = true; 22 let dashboardID; 23 24 $(document).ready(function () { 25 existingDashboard.hide(); 26 $("#create-panel").hide(); 27 $("#create-db").show(); 28 newDashboardBtn.on("click", showNewDashboard); 29 existingDashboardBtn.on("click", showExistingDashboard); 30 31 $("#add-logs-to-db-btn").on("click", openPopup); 32 $("#cancel-dbbtn, .popupOverlay").on("click", closePopup); 33 $("#selected-dashboard").on("click", displayExistingDashboards); 34 35 let data = getInitialSearchFilter(false, false); 36 }); 37 38 function showNewDashboard() { 39 newDashboardFlag = true; 40 newDashboardBtn.addClass("active"); 41 existingDashboardBtn.removeClass("active"); 42 newDashboard.show(); 43 existingDashboard.hide(); 44 $("#create-panel").hide(); 45 $("#create-db").show(); 46 } 47 48 function showExistingDashboard() { 49 newDashboardFlag = false; 50 existingDashboardBtn.addClass("active"); 51 newDashboardBtn.removeClass("active"); 52 existingDashboard.show(); 53 newDashboard.hide(); 54 $("#create-panel").show(); 55 $("#create-db").hide(); 56 } 57 58 function openPopup() { 59 $(".popupOverlay, .popupContent").addClass("active"); 60 } 61 62 function closePopup() { 63 $("#db-name").val(""); 64 $("#db-description").val(""); 65 $(".popupOverlay, .popupContent").removeClass("active"); 66 $(".error-tip").removeClass("active"); 67 $(".dashboard").removeClass("active"); 68 $("#selected-dashboard span").html("Choose Dashboard"); 69 newDashboardBtn.addClass("active"); 70 existingDashboardBtn.removeClass("active"); 71 newDashboard.show(); 72 existingDashboard.hide(); 73 $("#create-db").show(); 74 $("#create-panel").hide(); 75 } 76 77 //Create panel in a new dashboard 78 function createPanelToNewDashboard() { 79 var inputdbname = $("#db-name").val(); 80 var inputdbdescription = $("#db-description").val(); 81 var timeRange = data.startEpoch; 82 var refresh = ""; 83 84 if (!inputdbname) { 85 $(".error-tip").addClass("active"); 86 } else { 87 $("#save-dbbtn").off("click"); 88 $(document).off("keypress"); 89 90 $.ajax({ 91 method: "post", 92 url: "api/dashboards/create", 93 headers: { 94 "Content-Type": "application/json; charset=utf-8", 95 Accept: "*/*", 96 }, 97 data: JSON.stringify(inputdbname), 98 dataType: "json", 99 crossDomain: true, 100 }).then(function (res) { 101 $("#db-name").val(""); 102 $("#db-description").val(""); 103 $(".error-tip").removeClass("active"); 104 $(".popupOverlay, .popupContent").removeClass("active"); 105 let panelCreatedFromLogs = createPanel(0); 106 var dashboard = { 107 id: Object.keys(res)[0], 108 name: Object.values(res)[0], 109 details: { 110 name: Object.values(res)[0], 111 description: inputdbdescription, 112 timeRange: timeRange, 113 refresh: refresh, 114 panels: [panelCreatedFromLogs], 115 }, 116 }; 117 updateDashboard(dashboard); 118 var queryString = "?id=" + Object.keys(res)[0]; 119 window.location.href = "../dashboard.html" + queryString; 120 }).catch(function (updateError) { 121 if (updateError.status === 409) { 122 $('.error-tip').text('Dashboard name already exists!'); 123 $('.error-tip').addClass('active'); 124 } 125 }); 126 } 127 } 128 129 $("#create-db").click(function () { 130 if (newDashboardFlag) createPanelToNewDashboard(); 131 }); 132 133 const existingDashboards = []; 134 135 // Display list of existing dashboards 136 function displayExistingDashboards() { 137 $.ajax({ 138 method: "get", 139 url: "api/dashboards/listall", 140 headers: { 141 "Content-Type": "application/json; charset=utf-8", 142 Accept: "*/*", 143 }, 144 crossDomain: true, 145 dataType: "json", 146 }).then(function (res) { 147 if (res) { 148 let dropdown = $("#dashboard-options"); 149 // Filtering default dashboards 150 let defaultDashboardIds = [ 151 "10329b95-47a8-48df-8b1d-0a0a01ec6c42", 152 "a28f485c-4747-4024-bb6b-d230f101f852", 153 "bd74f11e-26c8-4827-bf65-c0b464e1f2a4", 154 "53cb3dde-fd78-4253-808c-18e4077ef0f1" 155 ]; 156 let additionalDashboards = Object.keys(res).filter(id => !defaultDashboardIds.includes(id) && !existingDashboards.includes(id)); 157 if (additionalDashboards.length === 0 && existingDashboards.length === 0) { 158 // Add empty list item when there are no additional dashboards 159 dropdown.html(`<li class="dashboard"></li>`); 160 } else { 161 $.each(res, function (id, dashboardName) { 162 // exclude default dashboards 163 if (!defaultDashboardIds.includes(id) && !existingDashboards.includes(id)) { 164 dropdown.append(`<li class="dashboard" id="${id}">${dashboardName}</li>`); 165 existingDashboards.push(id); 166 } 167 }); 168 dropdown.off("click", ".dashboard"); 169 dropdown.on("click", ".dashboard", selectDashboardHandler); 170 } 171 } 172 }); 173 } 174 175 // Select a existing dashboard 176 function selectDashboardHandler() { 177 let selectedOption = $(this).html(); 178 $(".dashboard").removeClass("active"); 179 $("#selected-dashboard span").html(selectedOption); 180 $(this).addClass("active"); 181 dashboardID = $(this).attr("id"); 182 let dashboard; 183 184 // Get the selected dashboard details 185 function createPanelToExistingDashboard() { 186 $.ajax({ 187 method: "get", 188 url: "api/dashboards/" + dashboardID, 189 headers: { 190 "Content-Type": "application/json; charset=utf-8", 191 Accept: "*/*", 192 }, 193 crossDomain: true, 194 dataType: "json", 195 }).then(function (res) { 196 let dashboardDetails = res; 197 if (!dashboardDetails.panels) { 198 // If there is no existing Panel 199 dashboardDetails.panels = []; 200 } 201 let panelCreatedFromLogs = createPanel( 202 dashboardDetails.panels.length, 203 dashboardDetails.panels[0]?.queryData?.startEpoch, 204 ); 205 206 dashboardDetails = handlePanelPosition( 207 dashboardDetails, 208 panelCreatedFromLogs, 209 ); 210 dashboard = { 211 id: dashboardID, 212 name: selectedOption, 213 details: dashboardDetails, 214 }; 215 updateDashboard(dashboard); 216 var queryString = "?id=" + dashboardID; 217 window.location.href = "../dashboard.html" + queryString; 218 }); 219 } 220 221 $("#create-panel").click(function () { 222 if (!newDashboardFlag) createPanelToExistingDashboard(); 223 }); 224 } 225 226 function handlePanelPosition(existingDashboard, newPanel) { 227 const maxY = existingDashboard.panels.reduce((max, panel) => { 228 return Math.max(max, panel.gridpos.y + panel.gridpos.h); 229 }, 0); 230 newPanel.gridpos.y = maxY + 20; 231 existingDashboard.panels.push(newPanel); 232 return existingDashboard; 233 } 234 235 function updateDashboard(dashboard) { 236 $.ajax({ 237 method: "post", 238 url: "api/dashboards/update", 239 headers: { 240 "Content-Type": "application/json; charset=utf-8", 241 Accept: "*/*", 242 }, 243 data: JSON.stringify(dashboard), 244 dataType: "json", 245 crossDomain: true, 246 }).then(function (msg) { 247 console.log("done:", msg); 248 }); 249 } 250 251 function createPanel(panelIndex, startEpoch) { 252 let panelId = uuidv4(); 253 254 let panel = { 255 chartType: "Data Table", 256 dataType: "", 257 description: "", 258 gridpos: { 259 h: 250, 260 w: 653, 261 wPercent: 0.49733434881949734, 262 x: 10, 263 y: 20, 264 }, 265 logLinesViewType: "Table view", 266 name: "New-Panel", 267 panelId: panelId, 268 panelIndex: panelIndex, 269 queryData: { 270 endEpoch: data.endEpoch, 271 from: 0, 272 indexName: data.indexName, 273 queryLanguage: data.queryLanguage, 274 searchText: data.searchText, 275 startEpoch: startEpoch ? startEpoch : data.startEpoch, 276 state: data.state, 277 }, 278 queryType: "logs", 279 unit: "", 280 }; 281 return panel; 282 } 283 284 $('#alert-from-logs-btn').click(function() { 285 var queryParams = { 286 "queryLanguage": data.queryLanguage, 287 "searchText": data.searchText, 288 "startEpoch": data.startEpoch, 289 "endEpoch": data.endEpoch, 290 }; 291 var queryString = $.param(queryParams); 292 293 // Open the alert.html in a new tab 294 var newTab = window.open("../alert.html" + "?" + queryString, '_blank'); 295 newTab.focus(); 296 });