github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/static/js/ag-grid-seg-stats.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 /** 18 * siglens 19 * (c) 2022 - All rights reserved. 20 */ 21 22 'use strict'; 23 24 let eGridDiv = null; 25 26 function renderMeasuresGrid(columnOrder, hits) { 27 if (eGridDiv === null) { 28 eGridDiv = document.querySelector('#measureAggGrid'); 29 new agGrid.Grid(eGridDiv, aggGridOptions); 30 } 31 // set the column headers from the data 32 let colDefs = aggGridOptions.api.getColumnDefs(); 33 colDefs.length = 0; 34 colDefs = columnOrder.map((colName, index) => { 35 let title = colName; 36 let resize = index + 1 == columnOrder.length ? false : true; 37 let maxWidth = Math.max(displayTextWidth(colName, "italic 19pt DINpro "), 200) //200 is approx width of 1trillion number 38 return { 39 field: title, 40 headerName: title, 41 resizable: resize, 42 minWidth: maxWidth, 43 }; 44 }); 45 aggsColumnDefs = _.chain(aggsColumnDefs).concat(colDefs).uniqBy('field').value(); 46 aggGridOptions.api.setColumnDefs(aggsColumnDefs); 47 let newRow = new Map() 48 $.each(hits, function (key, resMap) { 49 newRow.set("id", 0) 50 columnOrder.map((colName, index) => { 51 if (resMap.GroupByValues!=null && resMap.GroupByValues[index]!="*" && index< (resMap.GroupByValues).length){ 52 newRow.set(colName, resMap.GroupByValues[index]) 53 }else{ 54 // Check if MeasureVal is undefined or null and set it to 0 55 if (resMap.MeasureVal[colName] === undefined || resMap.MeasureVal[colName] === null) { 56 newRow.set(colName, "0"); 57 } else { 58 newRow.set(colName, resMap.MeasureVal[colName]); 59 } 60 } 61 }) 62 segStatsRowData = _.concat(segStatsRowData, Object.fromEntries(newRow)); 63 }) 64 aggGridOptions.api.setRowData(segStatsRowData); 65 66 67 } 68 69 function displayTextWidth(text, font) { 70 let canvas = displayTextWidth.canvas || (displayTextWidth.canvas = document.createElement("canvas")); 71 let context = canvas.getContext("2d"); 72 context.font = font; 73 let metrics = context.measureText(text); 74 return metrics.width; 75 }