From 999e724c42a832df37638eee609f594d88ea40c2 Mon Sep 17 00:00:00 2001 From: Dobricean Ioan Dorian <50819975+dobri1408@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:23:40 +0300 Subject: [PATCH 1/4] fix download when no data source is present --- src/Utils/Download.jsx | 4 ++ src/Utils/utils.js | 58 +++++++++++++++-- src/helpers/csvString.js | 136 +++++++++++++++++++++++++-------------- 3 files changed, 143 insertions(+), 55 deletions(-) diff --git a/src/Utils/Download.jsx b/src/Utils/Download.jsx index 1ef8501d..e9b77d4d 100644 --- a/src/Utils/Download.jsx +++ b/src/Utils/Download.jsx @@ -79,6 +79,8 @@ function Download(props) { provider_metadata, url_source, core_metadata, + chartData, + reactChartEditorLib, ); zip.file(`${title}.csv`, completeCSVData); @@ -96,6 +98,8 @@ function Download(props) { provider_metadata, url_source, core_metadata, + chartData, + reactChartEditorLib, ); exportCSVFile(csvData, title); }; diff --git a/src/Utils/utils.js b/src/Utils/utils.js index 82d11d34..35b00d57 100644 --- a/src/Utils/utils.js +++ b/src/Utils/utils.js @@ -51,16 +51,62 @@ export const generateOriginalCSV = ( provider_metadata, url_source, core_metadata, + chartData, + reactChartEditorLib, ) => { let array = []; let readme = provider_metadata?.readme ? [provider_metadata?.readme] : []; - - Object.entries(dataSources).forEach(([key, items]) => { - items.forEach((item, index) => { - if (!array[index]) array[index] = {}; - array[index][key] = item; + const hasDataSources = + dataSources && + Object.keys(dataSources).some( + (key) => Array.isArray(dataSources[key]) && dataSources[key].length > 0, + ); + console.log({ hasDataSources }, chartData?.data); + if (hasDataSources) { + Object.entries(dataSources).forEach(([key, items]) => { + items.forEach((item, index) => { + if (!array[index]) array[index] = {}; + array[index][key] = item; + }); }); - }); + } else if (chartData?.data && Array.isArray(chartData.data)) { + // Extract data from traces when dataSources is empty + const allTraceData = []; + for (const trace of chartData.data) { + const traceData = processTraceData( + trace, + dataSources || {}, + reactChartEditorLib, + ); + allTraceData.push(traceData); + } + console.log({ allTraceData }); + // Process all trace data + if ( + allTraceData.length > 0 && + !allTraceData.every((data) => data.length === 0) + ) { + const maxLength = Math.max(...allTraceData.map((data) => data.length)); + + for (let i = 0; i < maxLength; i++) { + if (!array[i]) array[i] = {}; + + allTraceData.forEach((traceData) => { + if (traceData[i]) { + Object.keys(traceData[i]).forEach((key) => { + if ( + traceData[i][key] !== null && + traceData[i][key] !== undefined + ) { + array[i][key] = traceData[i][key]; + } + }); + } + }); + } + } + console.log({ array }); + } const metadataFlags = getMetadataFlags(core_metadata); const metadataArrays = processMetadataArrays(core_metadata, metadataFlags); diff --git a/src/helpers/csvString.js b/src/helpers/csvString.js index 8da36880..46287a5f 100644 --- a/src/helpers/csvString.js +++ b/src/helpers/csvString.js @@ -240,67 +240,105 @@ function processTraceData(trace, dataSources, reactChartEditorLib) { const usedColumns = new Set(); const { getAttrsPath, constants, getSrcAttr } = reactChartEditorLib; + const hasDataSources = + dataSources && + Object.keys(dataSources).some( + (key) => Array.isArray(dataSources[key]) && dataSources[key].length > 0, + ); + if (hasDataSources) { + // Get all data attributes from constants.TRACE_SRC_ATTRIBUTES + const traceDataAttrs = getAttrsPath(trace, constants.TRACE_SRC_ATTRIBUTES); + + // For each data attribute, get the corresponding src attribute using getSrcAttr + Object.entries(traceDataAttrs).forEach(([dataAttrPath, dataValue]) => { + const srcAttr = getSrcAttr(trace, dataAttrPath); + + if (srcAttr && srcAttr.value && dataSources[srcAttr.value]) { + usedColumns.add(srcAttr.value); + } + }); - // Get all data attributes from constants.TRACE_SRC_ATTRIBUTES - const traceDataAttrs = getAttrsPath(trace, constants.TRACE_SRC_ATTRIBUTES); - - // For each data attribute, get the corresponding src attribute using getSrcAttr - Object.entries(traceDataAttrs).forEach(([dataAttrPath, dataValue]) => { - const srcAttr = getSrcAttr(trace, dataAttrPath); + // Add columns from transforms + if (trace.transforms && Array.isArray(trace.transforms)) { + trace.transforms.forEach((transform) => { + if (transform.targetsrc && dataSources[transform.targetsrc]) { + usedColumns.add(transform.targetsrc); + } + }); + } - if (srcAttr && srcAttr.value && dataSources[srcAttr.value]) { - usedColumns.add(srcAttr.value); + // If no specific columns found, use all available data sources + if (usedColumns.size === 0) { + Object.keys(dataSources).forEach((key) => { + if (Array.isArray(dataSources[key])) { + usedColumns.add(key); + } + }); } - }); - // Add columns from transforms - if (trace.transforms && Array.isArray(trace.transforms)) { - trace.transforms.forEach((transform) => { - if (transform.targetsrc && dataSources[transform.targetsrc]) { - usedColumns.add(transform.targetsrc); - } - }); - } + const maxLength = Math.max( + ...Array.from(usedColumns).map((col) => + dataSources[col] ? dataSources[col].length : 0, + ), + ); + + for (let i = 0; i < maxLength; i++) { + const row = {}; + usedColumns.forEach((col) => { + if (dataSources[col] && dataSources[col][i] !== undefined) { + row[col] = dataSources[col][i]; + } + }); + processedData.push(row); + } - // If no specific columns found, use all available data sources - if (usedColumns.size === 0) { - Object.keys(dataSources).forEach((key) => { - if (Array.isArray(dataSources[key])) { - usedColumns.add(key); - } + // Ensure all rows have the same columns (fill missing columns with empty values) + const allColumns = new Set(); + processedData.forEach((row) => { + Object.keys(row).forEach((col) => allColumns.add(col)); }); - } - const maxLength = Math.max( - ...Array.from(usedColumns).map((col) => - dataSources[col] ? dataSources[col].length : 0, - ), - ); - - for (let i = 0; i < maxLength; i++) { - const row = {}; - usedColumns.forEach((col) => { - if (dataSources[col] && dataSources[col][i] !== undefined) { - row[col] = dataSources[col][i]; - } + processedData.forEach((row) => { + allColumns.forEach((col) => { + if (!(col in row)) { + row[col] = ''; + } + }); }); - processedData.push(row); - } + } else { + const traceDataAttrs = getAttrsPath(trace, constants.TRACE_SRC_ATTRIBUTES); - // Ensure all rows have the same columns (fill missing columns with empty values) - const allColumns = new Set(); - processedData.forEach((row) => { - Object.keys(row).forEach((col) => allColumns.add(col)); - }); + // Extract data directly from trace properties + const dataColumns = new Set(); + const traceDataValues = {}; - processedData.forEach((row) => { - allColumns.forEach((col) => { - if (!(col in row)) { - row[col] = ''; + Object.entries(traceDataAttrs).forEach(([dataAttrPath, dataValue]) => { + if (dataValue && Array.isArray(dataValue)) { + const columnName = dataAttrPath.replace(/\./g, '_'); + dataColumns.add(columnName); + traceDataValues[columnName] = dataValue; } }); - }); - + if (dataColumns.size > 0) { + const maxLength = Math.max( + ...Array.from(dataColumns).map((col) => + traceDataValues[col] ? traceDataValues[col].length : 0, + ), + ); + + for (let i = 0; i < maxLength; i++) { + const row = {}; + dataColumns.forEach((col) => { + if (traceDataValues[col] && traceDataValues[col][i] !== undefined) { + row[col] = traceDataValues[col][i]; + } + }); + if (Object.keys(row).length > 0) { + processedData.push(row); + } + } + } + } // Apply transforms if they exist if (trace.transforms && Array.isArray(trace.transforms)) { trace.transforms.forEach((transform) => { From e163c3bd3ab7682d638d55a26edfd175842962cc Mon Sep 17 00:00:00 2001 From: dobri1408 <50819975+dobri1408@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:30:50 +0300 Subject: [PATCH 2/4] Update src/Utils/utils.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- src/Utils/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils/utils.js b/src/Utils/utils.js index 35b00d57..903d2019 100644 --- a/src/Utils/utils.js +++ b/src/Utils/utils.js @@ -86,7 +86,7 @@ export const generateOriginalCSV = ( allTraceData.length > 0 && !allTraceData.every((data) => data.length === 0) ) { - const maxLength = Math.max(...allTraceData.map((data) => data.length)); + const maxLength = allTraceData.length > 0 ? Math.max(...allTraceData.map((data) => data.length)) : 0; for (let i = 0; i < maxLength; i++) { if (!array[i]) array[i] = {}; From 1037e5a3cb30a7ae07d97aaf2116825cd6c356f8 Mon Sep 17 00:00:00 2001 From: Dobricean Ioan Dorian <50819975+dobri1408@users.noreply.github.com> Date: Tue, 16 Sep 2025 08:31:20 +0300 Subject: [PATCH 3/4] clean --- src/Utils/utils.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Utils/utils.js b/src/Utils/utils.js index 35b00d57..db2c3d97 100644 --- a/src/Utils/utils.js +++ b/src/Utils/utils.js @@ -61,7 +61,7 @@ export const generateOriginalCSV = ( Object.keys(dataSources).some( (key) => Array.isArray(dataSources[key]) && dataSources[key].length > 0, ); - console.log({ hasDataSources }, chartData?.data); + if (hasDataSources) { Object.entries(dataSources).forEach(([key, items]) => { items.forEach((item, index) => { @@ -80,7 +80,6 @@ export const generateOriginalCSV = ( ); allTraceData.push(traceData); } - console.log({ allTraceData }); // Process all trace data if ( allTraceData.length > 0 && @@ -105,7 +104,6 @@ export const generateOriginalCSV = ( }); } } - console.log({ array }); } const metadataFlags = getMetadataFlags(core_metadata); From d4bd4654b94fa0d12aaa074c89951bdcddad4884 Mon Sep 17 00:00:00 2001 From: eea-jenkins Date: Tue, 16 Sep 2025 07:35:19 +0200 Subject: [PATCH 4/4] style: Automated code fix --- src/Utils/utils.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Utils/utils.js b/src/Utils/utils.js index 867bb600..d8ec4a46 100644 --- a/src/Utils/utils.js +++ b/src/Utils/utils.js @@ -85,7 +85,10 @@ export const generateOriginalCSV = ( allTraceData.length > 0 && !allTraceData.every((data) => data.length === 0) ) { - const maxLength = allTraceData.length > 0 ? Math.max(...allTraceData.map((data) => data.length)) : 0; + const maxLength = + allTraceData.length > 0 + ? Math.max(...allTraceData.map((data) => data.length)) + : 0; for (let i = 0; i < maxLength; i++) { if (!array[i]) array[i] = {};