(data, chartData, timezone = "")
| 42 | |
| 43 | class TableView { |
| 44 | getTableData(data, chartData, timezone = "") { |
| 45 | const rawData = data.configuration; |
| 46 | const tabularData = {}; |
| 47 | const datasetConfigs = chartData.chart?.ChartDatasetConfigs; |
| 48 | |
| 49 | Object.keys(rawData).forEach((key, datasetIndex) => { |
| 50 | const tab = { columns: [], data: [] }; |
| 51 | const dataset = rawData[key]; |
| 52 | |
| 53 | // Pre-compute dataset config and excluded fields |
| 54 | const datasetConfig = datasetConfigs?.[datasetIndex]; |
| 55 | const baseExcludedFields = chartData.datasets[datasetIndex].options.excludedFields || []; |
| 56 | const excludedFields = datasetConfig?.excludedFields?.length > 0 |
| 57 | ? [...baseExcludedFields, ...datasetConfig.excludedFields] |
| 58 | : baseExcludedFields; |
| 59 | |
| 60 | const excludedSet = new Set(excludedFields); |
| 61 | const columnMap = new Map(); // Header -> index |
| 62 | const nestedColumnMap = new Set(); // `${k}?${n}` |
| 63 | const columnsFormatting = datasetConfig?.configuration?.columnsFormatting; |
| 64 | |
| 65 | // Process items in chunks to avoid blocking the event loop |
| 66 | const chunkSize = 1000; |
| 67 | for (let i = 0; i < dataset.length; i += chunkSize) { |
| 68 | const chunk = dataset.slice(i, i + chunkSize); |
| 69 | |
| 70 | chunk.forEach((item) => { |
| 71 | const dataItem = {}; |
| 72 | |
| 73 | Object.entries(item).forEach(([k, val]) => { |
| 74 | if (excludedSet.has(k)) return; |
| 75 | |
| 76 | const type = determineType(val); |
| 77 | |
| 78 | if (type === "object" && val && !Array.isArray(val)) { |
| 79 | Object.entries(val).forEach(([n, nestedVal]) => { |
| 80 | const headerKey = `${k}?${n}`; |
| 81 | if (excludedSet.has(headerKey)) return; |
| 82 | |
| 83 | const nestedType = determineType(nestedVal); |
| 84 | |
| 85 | if (!columnMap.has(k)) { |
| 86 | columnMap.set(k, tab.columns.length); |
| 87 | tab.columns.push({ Header: k, accessor: k, columns: [] }); |
| 88 | } |
| 89 | |
| 90 | const colIndex = columnMap.get(k); |
| 91 | if (!nestedColumnMap.has(headerKey)) { |
| 92 | nestedColumnMap.add(headerKey); |
| 93 | if (!tab.columns[colIndex].columns) tab.columns[colIndex].columns = []; |
| 94 | tab.columns[colIndex].columns.push({ Header: n, accessor: headerKey }); |
| 95 | } |
| 96 | |
| 97 | const columnConfig = columnsFormatting?.[headerKey]; |
| 98 | if (nestedType === "object") { |
| 99 | dataItem[headerKey] = `__cb_object${JSON.stringify(nestedVal)}`; |
| 100 | } else if (nestedType === "array") { |
| 101 | dataItem[headerKey] = `__cb_array${JSON.stringify(nestedVal)}`; |
no test coverage detected