Skip to content

Commit 81ba614

Browse files
uaArsenstas-fomenko
authored andcommitted
Update sailor version, build type, loggger and fix path to read action (#53)
* Update sailor version, build type, logger and fix path to read action * Update readme
1 parent a17fd46 commit 81ba614

File tree

9 files changed

+171
-142
lines changed

9 files changed

+171
-142
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.0.2 (December 24, 2019)
2+
3+
* Update sailor version to 2.5.4
4+
* Update component to use logger
5+
* Update buildType to docker
6+
* Fixed bug with invalid path to read action
7+
18
## 2.0.1 (October 10, 2019)
29

310
* Hotfix Action path

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ attachment. It can also write a CSV file from the incoming events.
1313
## Requirements
1414

1515
## Environment variables
16-
17-
1. `EIO_REQUIRED_RAM_MB` - recommended value of allocated memory is `512` MB
18-
2. `REQUEST_TIMEOUT` - HTTP request timeout in milliseconds, default value 10000
19-
3. `REQUEST_RETRY_DELAY` - delay between retry attempts in milliseconds, default value 7000
20-
4. `REQUEST_MAX_RETRY` - number of HTTP request retry attempts, default value 7
21-
5. `REQUEST_MAX_CONTENT_LENGTH` - max size of http request in bytes, default value: 10485760
22-
6. `TIMEOUT_BETWEEN_EVENTS` - number of milliseconds write action wait before creating separate attachments, default value: 10000
16+
Name|Mandatory|Description|Values|
17+
|----|---------|-----------|------|
18+
|EIO_REQUIRED_RAM_MB| false | Value of allocated memory to component | Recommended: 512 |
19+
|REQUEST_TIMEOUT| false | HTTP request timeout in milliseconds | Default value: 10000 |
20+
|REQUEST_RETRY_DELAY| false | Delay between retry attempts in milliseconds | Default value: 7000 |
21+
|REQUEST_MAX_RETRY| false | Number of HTTP request retry attempts | Default value: 7 |
22+
|REQUEST_MAX_CONTENT_LENGTH| false | Max size of http request in bytes | Default value: 10485760 |
23+
|TIMEOUT_BETWEEN_EVENTS| false | Number of milliseconds write action wait before creating separate attachments | Default value: 10000 |
24+
|LOG_LEVEL| false | Level of logger verbosity | trace, debug, info, warning, error Default: info |
2325

2426
## Credentials
2527

component.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"title": "CSV",
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",
5+
"buildType" : "docker",
56
"triggers": {
67
"read": {
78
"main": "./lib/triggers/read.js",
@@ -26,7 +27,7 @@
2627
},
2728
"actions" : {
2829
"read_action": {
29-
"main": "./lib/actions/write.js",
30+
"main": "./lib/triggers/read.js",
3031
"title": "Read CSV attachment",
3132
"fields": {
3233
"emitAll": {

lib/actions/write.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const csv = require('csv');
33
const _ = require('lodash');
44
const { messages } = require('elasticio-node');
55
const client = require('elasticio-rest-node')();
6+
const logger = require('@elastic.io/component-logger')();
67

78
const util = require('../util/util');
89

@@ -21,10 +22,10 @@ let putUrl;
2122

2223
let readyFlag = false;
2324

24-
exports.init = async function init(cfg) {
25+
async function init(cfg) {
2526
const delimiter = cfg.writer.separator || ',';
2627
const header = cfg.includeHeaders !== 'No';
27-
console.log('Using delimiter: \'%s\'', delimiter);
28+
logger.trace('Using delimiter: \'%s\'', delimiter);
2829
const options = {
2930
header,
3031
delimiter,
@@ -33,20 +34,19 @@ exports.init = async function init(cfg) {
3334
if (cfg.writer.columns) {
3435
const columns = Object.keys(_.keyBy(cfg.writer.columns, 'property'));
3536

36-
console.log('Configured column names:', columns);
37+
logger.trace('Configured column names:', columns);
3738
options.columns = columns;
3839
}
3940

4041
stringifier = csv.stringify(options);
4142
signedUrl = await client.resources.storage.createSignedUrl();
4243
putUrl = signedUrl.put_url;
43-
console.log('CSV file to be uploaded file to uri=%s', putUrl);
44+
logger.trace('CSV file to be uploaded file to uri=%s', putUrl);
4445
ax = axios.create();
4546
util.addRetryCountInterceptorToAxios(ax);
4647
readyFlag = true;
47-
};
48-
49-
exports.process = async function ProcessAction(msg, cfg) {
48+
}
49+
async function ProcessAction(msg, cfg) {
5050
// eslint-disable-next-line consistent-this
5151
const self = this;
5252

@@ -62,10 +62,10 @@ exports.process = async function ProcessAction(msg, cfg) {
6262
timeout = setTimeout(async () => {
6363
readyFlag = false;
6464

65-
console.log('Closing the stream due to inactivity');
65+
self.logger.info('Closing the stream due to inactivity');
6666

6767
const finalRowCount = rowCount;
68-
console.log('The resulting CSV file contains %s rows', finalRowCount);
68+
self.logger.info('The resulting CSV file contains %s rows', finalRowCount);
6969
ax.put(putUrl, stringifier, {
7070
method: 'PUT',
7171
timeout: REQUEST_TIMEOUT,
@@ -85,19 +85,22 @@ exports.process = async function ProcessAction(msg, cfg) {
8585
};
8686
signedUrl = null;
8787
rowCount = 0;
88-
console.log('Emitting message %j', messageToEmit);
88+
self.logger.trace('Emitting message %j', messageToEmit);
8989
await self.emit('data', messageToEmit);
9090
}, TIMEOUT_BETWEEN_EVENTS);
9191

9292
let row = msg.body.writer;
93-
console.log(`Incoming data: ${JSON.stringify(row)}`);
93+
self.logger.trace(`Incoming data: ${JSON.stringify(row)}`);
9494
if (cfg.writer.columns) {
9595
const columns = Object.keys(_.keyBy(cfg.writer.columns, 'property'));
9696
row = _.pick(row, columns);
9797
}
98-
console.log(`Writing Row: ${JSON.stringify(row)}`);
98+
self.logger.trace(`Writing Row: ${JSON.stringify(row)}`);
9999
stringifier.write(row);
100100
rowCount += 1;
101101

102102
await self.emit('end');
103-
};
103+
}
104+
105+
exports.process = ProcessAction;
106+
exports.init = init;

lib/triggers/read.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ const _ = require('underscore');
55
const csv = require('csv');
66
const { messages } = require('elasticio-node');
77
const moment = require('moment');
8-
const debug = require('debug')('csv');
98
const axios = require('axios');
109
const { Writable } = require('stream');
1110
const util = require('../util/util');
1211

1312
const REQUEST_TIMEOUT = process.env.REQUEST_TIMEOUT || 10000; // ms
1413
const REQUEST_MAX_RETRY = process.env.REQUEST_MAX_RETRY || 7;
1514
const REQUEST_RETRY_DELAY = process.env.REQUEST_RETRY_DELAY || 7000; // ms
16-
1715
const formatters = {
1816
date: (value, col) => moment(value, col.format).toDate(),
1917
number: (value, col) => {
@@ -64,16 +62,15 @@ async function ProcessRead(msg, cfg) {
6462
let index = 0;
6563
const separator = cfg.reader ? cfg.reader.separator || ',' : ',';
6664
const startRow = cfg.reader ? cfg.reader.startRow || 0 : 0;
67-
68-
console.log('Incoming message is %j', msg);
65+
that.logger.trace('Incoming message is %j', msg);
6966
if (!csvURL || csvURL.length === 0) {
7067
// Now let's check for the attachment
7168
if (msg && msg.attachments && Object.keys(msg.attachments).length > 0) {
7269
const key = Object.keys(msg.attachments)[0];
73-
console.log('Found attachment key=%s attachment=%j', key, msg.attachments[key]);
70+
that.logger.trace('Found attachment key=%s attachment=%j', key, msg.attachments[key]);
7471
csvURL = msg.attachments[key].url;
7572
} else {
76-
console.error('URL of the CSV is missing');
73+
that.logger.error('URL of the CSV is missing');
7774
that.emit('error', 'URL of the CSV is missing');
7875
return that.emit('end');
7976
}
@@ -91,18 +88,18 @@ async function ProcessRead(msg, cfg) {
9188
class CsvWriter extends Writable {
9289
async write(chunk, encoding, callback) {
9390
parser.pause();
94-
debug('Processing %d row...', index);
95-
debug('Memory usage: %d Mb', process.memoryUsage().heapUsed / 1024 / 1024);
91+
this.logger.debug('Processing %d row...', index);
92+
this.logger.debug('Memory usage: %d Mb', process.memoryUsage().heapUsed / 1024 / 1024);
9693
if (index >= startRow) {
9794
const msg = createRowMessage(chunk, cfg.reader.columns);
9895
if (cfg.emitAll) {
99-
debug('Row #%s added to result array', index);
96+
this.logger.debug('Row #%s added to result array', index);
10097
outputMsg.result.push(msg.body);
10198
} else {
10299
await that.emit('data', msg);
103100
}
104101
} else {
105-
debug('Row #%s is skipped based on configuration', index);
102+
this.logger.debug('Row #%s is skipped based on configuration', index);
106103
}
107104
index += 1;
108105
parser.resume();
@@ -112,18 +109,19 @@ async function ProcessRead(msg, cfg) {
112109
if (cfg.emitAll) {
113110
await that.emit('data', messages.newMessageWithBody(outputMsg));
114111
}
115-
debug('Processing csv writer end event...');
116-
debug('Memory usage: %d Mb', process.memoryUsage().heapUsed / 1024 / 1024);
112+
this.logger.debug('Processing csv writer end event...');
113+
this.logger.debug('Memory usage: %d Mb', process.memoryUsage().heapUsed / 1024 / 1024);
117114

118-
debug(`Number of lines: ${index}`);
115+
this.logger.debug(`Number of lines: ${index}`);
119116
await that.emit('end');
120117
ended = true;
121118
}
122119
}
123120

124121
const writer = new CsvWriter();
122+
writer.logger = that.logger;
125123

126-
debug('Sending GET request to url=%s', csvURL);
124+
that.logger.debug('Sending GET request to url=%s', csvURL);
127125
const ax = axios.create();
128126
util.addRetryCountInterceptorToAxios(ax);
129127
const response = await ax({
@@ -134,7 +132,7 @@ async function ProcessRead(msg, cfg) {
134132
retry: REQUEST_MAX_RETRY,
135133
delay: REQUEST_RETRY_DELAY,
136134
});
137-
debug('Have got response status=%s headers=%j', response.status, response.headers);
135+
that.logger.debug('Have got response status=%s headers=%j', response.status, response.headers);
138136
if (response.status !== 200) {
139137
await that.emit('error', `Unexpected response code code=${response.status}`);
140138
ended = true;

0 commit comments

Comments
 (0)