Skip to content

Commit d15bf73

Browse files
peteharversonjgowdyelastic
authored andcommitted
[ML] Switch file data visualizer to use Papa Parse for CSV parsing (#24329)
1 parent 6a60afd commit d15bf73

File tree

5 files changed

+24
-82
lines changed

5 files changed

+24
-82
lines changed

x-pack/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@
154154
"concat-stream": "1.5.1",
155155
"cookie": "^0.3.1",
156156
"copy-to-clipboard": "^3.0.8",
157-
"csv-parse": "^3.1.2",
158157
"d3": "3.5.6",
159158
"d3-scale": "1.0.6",
160159
"dataloader": "^1.4.0",

x-pack/plugins/ml/public/file_datavisualizer/components/import_view/importer/csv_importer.js

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77

88
import { Importer } from './importer';
9-
import { parse } from './csv_parse';
10-
// import csvParse from 'csv-parse';
9+
import Papa from 'papaparse';
1110

1211
export class CsvImporter extends Importer {
1312
constructor(results) {
@@ -21,57 +20,33 @@ export class CsvImporter extends Importer {
2120
}
2221

2322
async read(csv) {
24-
console.log('read delimited file');
25-
console.time('read delimited file');
26-
2723
try {
28-
const settings = {
29-
skip_empty_lines: true,
24+
const config = {
25+
header: false,
26+
skipEmptyLines: 'greedy',
3027
delimiter: this.delimiter,
31-
quote: this.quote,
32-
columns: this.columnNames,
33-
relax_column_count: true,
28+
quoteChar: this.quote,
3429
};
3530

36-
// commented out code left in for future work to optimise
37-
// csv parsing
38-
39-
// const parser = csvParse(settings);
40-
// parser.write(csv);
41-
// parser.end();
42-
43-
this.data = await parse(csv, settings);
44-
45-
// const output = [];
46-
// parser.on('readable', () => {
47-
// let record;
48-
// while (record = parser.read()) {
49-
// output.push(record);
50-
// }
51-
// });
31+
const parserOutput = Papa.parse(csv, config);
5232

53-
// parser.on('error', (err) => {
54-
// console.error(err.message);
55-
// });
33+
if (parserOutput.errors.length) {
34+
// throw an error with the message of the first error encountered
35+
throw parserOutput.errors[0].message;
36+
}
5637

57-
// parser.on('end', () => {
58-
// console.log('output', output);
59-
// console.timeEnd('read file');
60-
// });
38+
this.data = parserOutput.data;
6139

6240
if (this.hasHeaderRow) {
6341
this.data.shift();
6442
}
6543

66-
this.docArray = this.data;
67-
// this.docArray = formatToJson(this.data, this.columnNames);
68-
console.timeEnd('read delimited file');
44+
this.docArray = formatToJson(this.data, this.columnNames);
6945

7046
return {
7147
success: true,
7248
};
7349
} catch (error) {
74-
console.timeEnd('read delimited file');
7550
return {
7651
success: false,
7752
error,
@@ -80,15 +55,15 @@ export class CsvImporter extends Importer {
8055
}
8156
}
8257

83-
// function formatToJson(data, columnNames) {
84-
// const docArray = [];
85-
// for (let i = 0; i < data.length; i++) {
86-
// const line = {};
87-
// for (let c = 0; c < columnNames.length; c++) {
88-
// const col = columnNames[c];
89-
// line[col] = data[i][c];
90-
// }
91-
// docArray.push(line);
92-
// }
93-
// return docArray;
94-
// }
58+
function formatToJson(data, columnNames) {
59+
const docArray = [];
60+
for (let i = 0; i < data.length; i++) {
61+
const line = {};
62+
for (let c = 0; c < columnNames.length; c++) {
63+
const col = columnNames[c];
64+
line[col] = data[i][c];
65+
}
66+
docArray.push(line);
67+
}
68+
return docArray;
69+
}

x-pack/plugins/ml/public/file_datavisualizer/components/import_view/importer/csv_parse.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

x-pack/yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,11 +2803,6 @@ csstype@^2.2.0:
28032803
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.5.tgz#4125484a3d42189a863943f23b9e4b80fedfa106"
28042804
integrity sha512-EGMjeoiN3aqEX5u/cyH5mSdGBDGdLcCQvcEcBWNGFSPXKd9uOTIeVG91YQ22OxI44DKpvI+4C7VUSmEpsHWJaA==
28052805

2806-
csv-parse@^3.1.2:
2807-
version "3.1.2"
2808-
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-3.1.2.tgz#51bfd2d39b13e66f6da8656360b8ec82ca159494"
2809-
integrity sha512-NtcS4h3rLcwawpJJDRolYeuQW/2RvutpHpzbokbk5xMCNEDSyvAK3SmuCtd6tKOftKiMa2wn4viOU32A/5VDTQ==
2810-
28112806
currently-unhandled@^0.4.1:
28122807
version "0.4.1"
28132808
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,11 +4331,6 @@ csstype@^2.2.0:
43314331
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.2.tgz#4534308476ceede8fbe148b9b99f9baf1c80fa06"
43324332
integrity sha512-epoHrKPywwUhFTXadQNA5PPx4GChtkM03SkBeFZTaFtJcn6QfXpBkX2IAc4J9Oe18nha5NrTOo2OMOge+qH1mw==
43334333

4334-
csv-parse@^3.1.2:
4335-
version "3.1.3"
4336-
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-3.1.3.tgz#b96227e85e308f685133cdb2b79ec9a03fde3c1c"
4337-
integrity sha512-fs+nzn0lU+e7uZHkY/SvEYiRT3PCvb746csfl238iUXh1D+gzzN4crzgZ7gkVDutoPWYwfrFSQaSAulSeynTlg==
4338-
43394334
currently-unhandled@^0.4.1:
43404335
version "0.4.1"
43414336
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"

0 commit comments

Comments
 (0)