-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
263 lines (228 loc) · 7.21 KB
/
index.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const fs = require('fs');
const { Transform } = require('stream');
const readline = require('readline');
const crypto = require('crypto');
const csvStringify = require('csv-stringify');
const glob = require('glob');
const HEROKU_MODE = process.env.HEROKU || false;
const USER_ID = process.env.USER_ID || false;
const filePathTofileNameLines = () =>
new Transform({
objectMode: true,
transform(fileName, encoding, callback) {
const rl = readline.createInterface({
input: fs.createReadStream(fileName),
crlfDelay: Infinity,
});
rl.on('line', line => {
this.push({ fileName, line });
});
rl.on('close', () => callback());
},
});
const filenameLineToLineTimestamp = () =>
new Transform({
objectMode: true,
transform({ line, fileName }, encoding, callback) {
if (!HEROKU_MODE) {
// line on S3:
// Pure content, no prefix.
// file directory structure: 201709/01/05/0308.70025261277880.log
const [, year, month, day, hour, min, second] = fileName.match(
/(\d{4})(\d{2})\/(\d{2})\/(\d{2})\/(\d{2})(\d{2})\.\d+\.log$/
);
this.push({
timestamp: `${year}-${month}-${day}T${hour}:${min}:${second}Z`,
line,
});
return callback();
}
// line on Heroku:
// 112 <190>1 2018-07-16T17:32:16.082803+00:00 app web.1 - - ||LOG||<----------
if (!line.includes('||LOG||')) return callback();
const [, timestamp, content] = line.match(
/.*(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6}\+\d{2}:\d{2}) .+ \|\|LOG\|\|(.*)/
);
this.push({ line: content, timestamp });
callback();
},
});
const lineTimestampToConversationObj = () =>
new Transform({
objectMode: true,
transform({ line, timestamp }, encoding, callback) {
if (line === '<----------') {
if (this._buffer !== '') {
console.info(
`[INFO] Incomplete message detected at ${timestamp}; ignoring`
);
}
this._buffer = '';
return callback();
}
if (line === '---------->') {
let conversationObj;
try {
conversationObj = JSON.parse(this._buffer);
} catch (e) {
console.error(
`[ERROR] Cannot parse message at ${timestamp}; skipping`
);
this._buffer = '';
return callback();
}
const { CONTEXT, INPUT, OUTPUT } = conversationObj;
const issuedAt = new Date(CONTEXT.issuedAt);
try {
this.push({
timestamp: new Date(timestamp).toISOString(),
userId: USER_ID ? INPUT.userId : undefined,
userIdsha256: sha256(INPUT.userId),
'input.message.text': collapseLines(
INPUT.message && INPUT.message.text
),
'context.issuedAt': isNaN(+issuedAt) ? '' : issuedAt.toISOString(),
'context.data.searchedText': collapseLines(
CONTEXT.data && CONTEXT.data.searchedText
),
'context.state': CONTEXT.state,
'context.data.selectedArticleId':
CONTEXT.data && CONTEXT.data.selectedArticleId,
'context.data.selectedReplyId':
CONTEXT.data && CONTEXT.data.selectedReplyId,
'output.context.state': OUTPUT.context.state,
'output.context.data.selectedArticleId':
OUTPUT.context.data.selectedArticleId,
'output.context.data.selectedReplyId':
OUTPUT.context.data.selectedReplyId,
'output.replies': collapseLines(
(OUTPUT.replies || [])
.map(({ text, altText }) => text || altText)
.join('↵')
),
});
} catch (e) {
console.error(`[ERROR] message at ${timestamp} contains error`, e);
}
this._buffer = '';
return callback();
}
this._buffer += line;
callback();
},
});
const conversationObjFilter = () =>
new Transform({
objectMode: true,
transform(conversationObj, encoding, callback) {
// on context.data.searchArticleId change, which means an article is selected
// either automatically or manually.
//
const canOutput =
conversationObj['output.context.data.selectedArticleId'] &&
conversationObj['context.data.selectedArticleId'] !==
conversationObj['output.context.data.selectedArticleId'];
if (canOutput) this.push(conversationObj);
callback();
},
});
/**
*
* @param {string} globFilePath
* @param {(data: object, next?: Function) => {}} callback
* @param {{sequential:boolean}} options
*/
const parseToJson = async (globFilePath, callback, options = {}) => {
const { sequential = true } = options;
const inputFiles = glob.sync(globFilePath);
console.info(`Processing ${inputFiles.length}...`);
const stream = filePathTofileNameLines();
stream
.pipe(filenameLineToLineTimestamp())
.pipe(lineTimestampToConversationObj())
.pipe(
new Transform({
objectMode: true,
transform(conversationObj, encoding, next) {
if (sequential) {
callback(conversationObj, next);
} else {
callback(conversationObj);
next();
}
},
})
);
/**
* @todo this is no guarantee that all pipe has done
*/
const promises = [];
for (let file of inputFiles) {
const p = new Promise(resolve => stream.write(file, resolve));
if (sequential) {
await p;
} else {
promises.push(p);
}
}
return Promise.all(promises);
};
/**
* @param {string} input
* @returns {string} - input's sha256 hash hex string. Empty string if input is falsy.
*/
function sha256(input) {
return input
? crypto
.createHash('sha256')
.update(input, 'utf8')
.digest('hex')
: '';
}
/**
* @param {string} str
* @returns {string} input string with all line breaks being replaced by return symbol
*/
function collapseLines(str) {
return (str || '').replace(/\r|\n/gm, '↵');
}
module.exports = {
parseToJson,
};
if (require.main === module) {
if (!process.argv[3]) {
console.info('Please provide input filename.');
console.info('Usage: node index.js <input file glob> <output file>');
process.exit(1);
}
const inputFiles = glob.sync(process.argv[2]);
const outputFilePath = process.argv[3];
// eslint-disable-next-line no-console
console.log(`Processing ${inputFiles.length} files to ${outputFilePath}...`);
const stream = filePathTofileNameLines();
stream
.pipe(filenameLineToLineTimestamp())
.pipe(lineTimestampToConversationObj())
.pipe(conversationObjFilter())
.pipe(
csvStringify({
header: true,
columns: [
'timestamp',
'userIdsha256',
// 'input.message.text',
// 'context.issuedAt',
// 'context.data.searchedText',
// 'context.state',
'output.context.state',
// 'context.data.selectedArticleId',
'output.context.data.selectedArticleId',
'output.replies',
],
})
)
.pipe(fs.createWriteStream(outputFilePath));
inputFiles.forEach(filePath => {
stream.write(filePath);
});
}