-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·131 lines (120 loc) · 3.94 KB
/
index.js
File metadata and controls
executable file
·131 lines (120 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
const { argv } = require('yargs');
const csv = require('csv-parser');
const through2 = require('through2');
const process = require('process');
const fs = require('fs-extra');
const path = require('path');
const filterDebugger = require('debug')('Filter');
const writerDebugger = require('debug')('Filter');
const csvFile = argv.infile;
const outFile = argv.outfile;
const outFileMaxEntries = argv.maxlen || null;
if (!csvFile) {
console.error(`Usage: ${argv.$0} --infile=<filename>`);
process.exit(1);
}
const emails = new Set();
let processedRowCount = 0;
const responseFilter = {
field: 'attributes.interaction.response.0.value',
value: 'Yes',
};
console.info(`Opening file ${csvFile}`);
const recordCounter = through2.obj((row, enc, cb) => {
processedRowCount += 1;
cb(null, row);
});
/**
* @param {Object} filter
* @param {String} filter.field
* @param {Number|String} filter.name
* @returns {DestroyableTransform}
*/
const recordFilter = (filter) => {
let firstLine = true;
filterDebugger(`Filtering records on ${filter.field} === ${filter.value}`);
return through2.obj(function (appcuesEvent, enc, cb) { // eslint-disable-line func-names
if (!emails.has(appcuesEvent.user_id)
&& appcuesEvent[filter.field] === filter.value
&& appcuesEvent.name === 'appcues:form_submitted') {
this.push(`${firstLine ? '' : ','}${appcuesEvent.user_id}`);
emails.add(appcuesEvent.user_id);
firstLine = false;
}
cb();
});
};
/**
* @param {String} outFileName
* @param {Number|null} maxBatchLen
* @returns {DestroyableTransform}
*/
const batchedWriter = (outFileName, maxBatchLen = null) => {
if (!maxBatchLen) {
return fs.createWriteStream(outFileName);
}
writerDebugger(`Creating batches of ${maxBatchLen} files`);
const fileStreams = [];
let currFileLen = 0;
let currFileIndex = 0;
const genFileName = i => outFileName.replace(path.extname(outFileName), `.${i}${path.extname(outFileName)}`);
let ws = fs.createWriteStream(genFileName(currFileIndex));
fileStreams.push(ws);
return through2((chunk, enc, cb) => {
if (currFileLen > maxBatchLen) {
currFileIndex += 1;
currFileLen = 0;
fileStreams.push(ws = fs.createWriteStream(genFileName(currFileIndex)));
}
ws.write(currFileLen === 0 ? chunk.toString().replace(',', '') : chunk); // omit leading comma for new files
currFileLen += 1;
cb();
}, (onFlush) => {
fileStreams.forEach(s => s.end());
writerDebugger(`Created ${fileStreams.length} file(s)`);
onFlush();
});
};
/**
* @typedef {Object} parseOptions
* @property {String} parseOptions.outFile
* @property {Number|null} parseOptions.batchLen
*
* @typedef {Object} outputInfo
* @property {Number} outputInfo.total
* @property {Number} outputInfo.filtered
*
* @param {String} input
* @param {parseOptions} opts
* @returns {Promise.<outputInfo>}
*/
const parse = (input, opts = {}) => {
const runtimeOptions = Object.assign({}, {
outFile: 'out.txt',
batchLen: null,
}, opts);
const outputStream = runtimeOptions.outFile ?
batchedWriter(runtimeOptions.outFile, runtimeOptions.batchLen) : through2((c, e, cb) => { cb(null, c); });
return new Promise((resolve, reject) => {
fs.access(input).then(() => {
fs.createReadStream(csvFile)
.pipe(csv())
.pipe(recordCounter)
.pipe(recordFilter(responseFilter))
.pipe(outputStream)
.on('finish', () => resolve({ total: processedRowCount, filtered: emails.size }));
}).catch(() => reject(new Error(`File '${input}' does not exist`)));
});
};
(async () => {
try {
const { total, filtered } = await parse(csvFile, { outfile: outFile, batchLen: outFileMaxEntries });
console.info(`Processed a total of ${total} entries`);
console.info(`${filtered} unique email addresses indicated as Suppliers`);
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();