Skip to content

Commit 3ab39d2

Browse files
committed
update
1 parent 3976f7b commit 3ab39d2

File tree

6 files changed

+118
-5885
lines changed

6 files changed

+118
-5885
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.1.0 (February 25, 2022)
2+
* Added `Emit Batch` behavior for `Read CSV attachment` action
3+
14
## 3.0.0 (July 9, 2021)
25
* Deleted trigger:
36
- `Read CSV attachment`

component.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form",
44
"docsUrl": "https://github.com/elasticio/csv-component",
55
"buildType": "docker",
6-
"version": "3.0.0",
6+
"version": "3.1.0",
77
"actions": {
88
"read_action": {
99
"main": "./lib/actions/read.js",
@@ -13,21 +13,19 @@
1313
"link": "/components/csv/actions#read-csv-attachment"
1414
},
1515
"fields": {
16-
"emitAll": {
16+
"emitBehavior": {
1717
"label": "Emit Behavior",
1818
"required": true,
1919
"viewClass": "SelectView",
2020
"model": {
21-
"true": "Fetch All",
22-
"false": "Emit Individually"
21+
"fetchAll": "Fetch All",
22+
"emitIndividually": "Emit Individually",
23+
"emitBatch": "Emit Batch"
2324
},
2425
"prompt": "Select Emit Behavior"
2526
}
2627
},
27-
"metadata": {
28-
"in": "./lib/schemas/read.in.json",
29-
"out": {}
30-
}
28+
"dynamicMetadata": true
3129
},
3230
"write_from_stream": {
3331
"main": "./lib/actions/writeStream.js",
@@ -88,4 +86,4 @@
8886
"dynamicMetadata": true
8987
}
9088
}
91-
}
89+
}

lib/actions/read.js

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ async function errHelper(text) {
2626
await this.emit('end')
2727
}
2828

29+
function sliceIntoChunks(arr, chunkSize) {
30+
const res = [];
31+
for (let i = 0; i < arr.length; i += chunkSize) {
32+
const chunk = arr.slice(i, i + chunkSize);
33+
res.push(chunk);
34+
}
35+
return res;
36+
}
37+
2938
async function readCSV(msg, cfg) {
3039
const that = this
31-
const emitAll = cfg.emitAll === true || cfg.emitAll === 'true'
32-
const { body } = msg
40+
// const emitAll = cfg.emitAll === true || cfg.emitAll === 'true'
41+
const { emitBehavior } = cfg;
42+
const { body } = msg;
3343

3444
// check if url provided in msg
3545
if (body.url && body.url.length > 0) {
@@ -60,9 +70,7 @@ async function readCSV(msg, cfg) {
6070
}
6171

6272
// if set "Fetch All" create object with results
63-
const outputMsg = {
64-
result: [],
65-
}
73+
const result = [];
6674

6775
let dataStream
6876
const parseStream = papa.parse(papa.NODE_STREAM_INPUT, parseOptions)
@@ -85,12 +93,12 @@ async function readCSV(msg, cfg) {
8593
} else {
8694
data = arrayToObj(chunk)
8795
}
88-
if (emitAll) {
89-
outputMsg.result.push(data)
90-
} else {
96+
if (emitBehavior === 'emitIndividually') {
9197
parseStream.pause()
9298
await that.emit('data', messages.newMessageWithBody(data))
9399
parseStream.resume()
100+
} else {
101+
result.push(data)
94102
}
95103
}
96104
}
@@ -111,10 +119,56 @@ async function readCSV(msg, cfg) {
111119
return
112120
}
113121

114-
if (emitAll) {
115-
await this.emit('data', messages.newMessageWithBody(outputMsg))
122+
if (emitBehavior === 'fetchAll') {
123+
await this.emit('data', messages.newMessageWithBody({ result }))
124+
} else if (emitBehavior === 'emitBatch') {
125+
const chunks = sliceIntoChunks(result, body.batchSize);
126+
// eslint-disable-next-line no-plusplus
127+
for (let i = 0; i < chunks.length; i++) {
128+
// eslint-disable-next-line no-await-in-loop
129+
await this.emit('data', messages.newMessageWithBody({ result: chunks[i] }))
130+
}
116131
}
117132
this.logger.info(`Complete, memory used: ${process.memoryUsage().heapUsed / 1024 / 1024} Mb`)
118133
}
119134

120-
module.exports.process = readCSV
135+
module.exports.process = readCSV;
136+
module.exports.getMetaModel = async function (cfg) {
137+
const meta = {
138+
in: {
139+
type: 'object',
140+
properties: {
141+
url: {
142+
type: 'string',
143+
required: true,
144+
title: 'URL'
145+
},
146+
header: {
147+
type: 'boolean',
148+
required: false,
149+
title: 'Contains headers'
150+
},
151+
delimiter: {
152+
type: 'string',
153+
required: false,
154+
title: 'Delimiter'
155+
},
156+
dynamicTyping: {
157+
type: 'boolean',
158+
required: false,
159+
title: 'Convert Data types'
160+
}
161+
}
162+
},
163+
out: {}
164+
};
165+
166+
if (cfg.emitBehavior === 'emitBatch') {
167+
meta.in.properties.batchSize = {
168+
title: 'Batch Size',
169+
type: 'number',
170+
required: true
171+
}
172+
}
173+
return meta;
174+
}

lib/schemas/read.in.json

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

0 commit comments

Comments
 (0)