* Function fetches log data based on params specified by user and download data in specified fileType * @param {*} api_endpoint : API endpoint URL * @param {*} column_list : List of Columns for the selected event type * @param {*} api_params : GET parameters for the API call * @param {*} fileTy
(api_endpoint, column_list, api_params, fileType)
| 256 | * @param {*} fileType : File Type ( Json, Excel, Csv) |
| 257 | */ |
| 258 | function downloadLogData(api_endpoint, column_list, api_params, fileType) { |
| 259 | $.ajax({ |
| 260 | type: "GET", |
| 261 | url: api_endpoint, |
| 262 | data: { |
| 263 | ...api_params, |
| 264 | limit: 9999 |
| 265 | }, |
| 266 | success: function (result, status, xhr) { |
| 267 | const data = result.data |
| 268 | switch (fileType) { |
| 269 | case "JSON": { |
| 270 | const filename = get_export_fileName('json'); |
| 271 | const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(data, undefined, 2)); |
| 272 | const downloadAnchorNode = document.createElement('a'); |
| 273 | downloadAnchorNode.setAttribute("href", dataStr); |
| 274 | downloadAnchorNode.setAttribute("download", filename); |
| 275 | document.body.appendChild(downloadAnchorNode); |
| 276 | downloadAnchorNode.click(); |
| 277 | break; |
| 278 | } |
| 279 | case "CSV": { |
| 280 | let csvFileData = ""; |
| 281 | for (let column in column_list) { |
| 282 | csvFileData += `"${column_list[column].data}"` + ","; |
| 283 | } |
| 284 | csvFileData += "\n" |
| 285 | for (let index = 0; index < data.length; index++) { |
| 286 | for (let column in column_list) { |
| 287 | csvFileData += `"${data[index][column_list[column].data]}"` + ","; |
| 288 | } |
| 289 | csvFileData += "\n"; |
| 290 | } |
| 291 | const filename = get_export_fileName('csv'); |
| 292 | const dataStr = "data:text/csv;charset=utf-8," + encodeURIComponent(csvFileData); |
| 293 | const downloadAnchorNode = document.createElement('a'); |
| 294 | downloadAnchorNode.setAttribute("href", dataStr); |
| 295 | downloadAnchorNode.setAttribute("download", filename); |
| 296 | document.body.appendChild(downloadAnchorNode); |
| 297 | downloadAnchorNode.click(); |
| 298 | break; |
| 299 | } |
| 300 | case "EXCEL": { |
| 301 | const filename = get_export_fileName('xlsx'); |
| 302 | let ws = XLSX.utils.json_to_sheet(data); |
| 303 | let wb = XLSX.utils.book_new(); |
| 304 | XLSX.utils.book_append_sheet(wb, ws, api_params['event_type'] + "_data"); |
| 305 | XLSX.writeFile(wb, filename); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | }, |
| 310 | error: function (jqXHR, textStatus, errorThrown) { |
| 311 | console.log(jqXHR.responseText) |
| 312 | } |
| 313 | }); |
| 314 | } |
| 315 |
no test coverage detected