-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
398 lines (327 loc) · 11.2 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#! /usr/bin/env node
const Directory = require('./structures/directory');
const csv = require('./util/csv');
const Classify = require('./util/classification');
const constants = require('./util/constants');
const Logger = require('./util/logger');
Logger.setLog(constants.logger.names.defaultLog, {stdout: true});
const log = Logger.log.bind(Logger, constants.logger.names.defaultLog);
Logger.setLog(constants.logger.names.debugLog, {stdout: false});
const debugLog = Logger.log.bind(Logger, constants.logger.names.debugLog);
// given a top-file, locate valid internal structures and grab the data from them
// This seems to be how things work:
// .cnd - sem_data_version 0
// .mrc - map_raw_condition version 1
// .cnf - sem_data_version 1
function help() {
log('Usage: xes_converter [options] [directory]\n');
log('Options:');
log('-v, --version \tDisplays the version information');
log('-x, --xes \tConverts the xes files into an output file located in the directory given');
log('-q, --qlw \tConverts the qlw files into an output file located in the directory given');
log('-s, --sum \tConverts the xes files into an sum file located in the directory given');
log('-m, --map \tConverts the map directories to csv');
log('-l, --line \tConverts the lin directories to csv');
log('-k, --qmap \tOutputs maps for each qlw directory');
log('-a, --all \tOutputs all data (-xqsmlk)');
log('-j, --loose \tTurns off strict checks on directories and file names');
log('-r, --recover \tAttempts to recover data from potentially corrupted xes files');
log('-d, --debug \tEnabled debugging text');
log('-h, --help \tProvides this text');
log('-o [uri], --output [uri] \tOutput directory uri');
log(`-b [number], --batchsize [number]\tThe number of positions per output file, default: ${constants.batchSize}`);
}
let options = {
batchSize: constants.batchSize,
topDirectoryUri: '',
outputDirectoryUri: '',
xes: false,
qlw: false,
sum: false,
map: false,
line: false,
qmap: false,
recover: false,
help: false,
version: false,
loose: false,
debug: false
};
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i].startsWith('--')) {
switch (process.argv[i]) {
case '--version':
options.version = true;
break;
case '--sum':
options.sum = true;
break;
case '--xes':
options.xes = true;
break;
case '--qlw':
options.qlw = true;
break;
case '--map':
options.map = true;
break;
case '--line':
options.line = true;
break;
case '--all':
options.qlw = true;
options.xes = true;
options.sum = true;
options.map = true;
options.line = true;
options.qmap = true;
break;
case '--qmap':
options.qmap = true;
break;
case '--help':
options.help = true;
break;
case '--output':
options.outputDirectoryUri = process.argv[++i];
break;
case '--batchsize':
options.batchSize = process.argv[++i];
break;
case '--loose':
options.loose = true;
break;
case '--recover':
options.recover = true;
break;
case '--debug':
options.debug = true;
}
} else if (process.argv[i].startsWith('-')) {
switch (process.argv[i]) {
case '-o':
options.outputDirectoryUri = process.argv[++i];
break;
case '-b':
options.batchSize = process.argv[++i];
break;
default:
for (const char of process.argv[i])
switch (char) {
case 'v':
options.version = true;
break;
case 's':
options.sum = true;
break;
case 'x':
options.xes = true;
break;
case 'q':
options.qlw = true;
break;
case 'm':
options.map = true;
break;
case 'l':
options.line = true;
break;
case 'a':
options.qlw = true;
options.xes = true;
options.sum = true;
options.map = true;
options.line = true;
options.qmap = true;
break;
case 'k':
options.qmap = true;
break;
case 'h':
options.help = true;
break;
case 'j':
options.loose = true;
break;
case 'd':
options.debug = true;
break;
case 'r':
options.recover = true;
break;
}
break;
}
} else
options.topDirectoryUri = process.argv[i];
}
if (options.topDirectoryUri === '' && !options.xes && !options.qlw && !options.sum && !options.version)
options.help = true;
if (!options.xes && !options.qlw && !options.sum)
options.xes = true;
if (options.debug) {
process.env.NODE_ENV = 'debug';
Logger.setLog(constants.logger.names.debugLog, {stdout: true});
} else
process.env.NODE_ENV = 'production';
if (options.version)
log(require('./package').version);
if (options.help)
help();
else {
if (!options.topDirectoryUri)
log('Please enter a uri of a directory to process, use with no options or -h for help');
else {
try {
log('Preparing...');
const initialStartTime = Date.now();
debugLog(`Iterating through ${options.topDirectoryUri}`);
const topDirectory = new Directory(options.topDirectoryUri);
const classify = new Classify(options);
debugLog(`Classifying directories`);
classify.exploreDirectory(topDirectory);
const baseFileName = `${options.outputDirectoryUri ? options.outputDirectoryUri : topDirectory.getUri()}/${topDirectory.getName().toLowerCase()}`;
debugLog(`Base file name: ${baseFileName}`);
log(`${topDirectory.totalSubDirectories()} directories traversed and ${classify.totalDirectories()} classified in ${(Date.now() - initialStartTime) / 1000} seconds.`);
log(`${classify.totalQlws()} qlw directories with ${classify.totalQlwPoints()} positions, ${classify.totalMaps()} map directories, ${classify.totalLines()} line directories, `);
debugLog('Starting processing of directories and files');
if (options.xes || options.qlw || options.sum || options.qmap) {
const startTime = Date.now();
log('Processing qlw directories...');
const qlws = classify.getQlws();
const maps = classify.getMaps();
debugLog(`${qlws.size} qlws, ${maps.size} maps`);
let items = [];
let totalLength = 0;
let batchLength = 0;
let failed = 0;
debugLog('Iterating through qlws...');
for (const [uri, qlw] of qlws) {
debugLog(`Processing ${qlw.getDirectory().getUri()}...`);
if (options.qmap)
if (maps.has(uri)) {
debugLog(`Processing as qmap...`);
}
if (options.qlw || options.xes || options.sum) {
debugLog(`Processing as qlw, xes, and/or sum...`);
debugLog(`Reading in map and raw map condition...`);
let qlwData = {
mapCond: qlw.getMapCond(),
mapRawCond: qlw.getMapRawCond(),
positions: []
};
if (options.debug)
debugLog(`Getting positions..`);
const positions = qlw.getPositions();
debugLog(`Iterating through ${positions.size} positions`);
for (const [, position] of positions) {
if (batchLength >= options.batchSize) {
debugLog('Pushing part of data to output array');
items.push(qlwData);
totalLength += batchLength;
if (options.qlw) {
debugLog('Outputting a batch of qlw');
csv.writeQlwToFile(`${baseFileName}_qlw_${totalLength}.csv`, items);
log(`${baseFileName}_qlw_${totalLength}.csv`);
}
if (options.xes) {
debugLog('Outputting a batch of xes');
csv.writeXesToFile(`${baseFileName}_xes_${totalLength}.csv`, items);
log(`${baseFileName}_xes_${totalLength}.csv`);
}
if (options.sum) {
debugLog('Outputting a batch of sum');
csv.writeSumToFile(`${baseFileName}_sum_${totalLength}.csv`, items);
log(`${baseFileName}_sum_${totalLength}.csv`);
}
batchLength = 0;
items = [];
qlwData.positions = [];
}
debugLog('Getting position data condition');
let pos = {
dataCond: position.getDataCond(),
};
try {
debugLog('Attempting to read in qlw, xes, and/or sum');
if (options.qlw)
pos.qlwData = position.getQlwData();
if (options.xes)
pos.xesData = position.getXesData(options);
if (options.sum)
pos.sumData = position.getSumData(pos.xesData);
debugLog(`Pushing position ${position.getDirectory().getUri()} to position array`);
qlwData.positions.push(pos);
batchLength++;
} catch (err) {
if (err.message)
log(err.message);
else
console.error(err);
log(`Skipping ${position.getDirectory().getUri()}\n`);
failed++;
}
}
items.push(qlwData);
}
}
if (batchLength > 0) {
totalLength += batchLength;
if (options.qlw) {
debugLog('Finalizing qlw output');
csv.writeQlwToFile(`${baseFileName}_qlw_${totalLength}.csv`, items);
log(`${baseFileName}_qlw_${totalLength}.csv`);
}
if (options.xes) {
debugLog('Finalizing xes output');
csv.writeXesToFile(`${baseFileName}_xes_${totalLength}.csv`, items);
log(`${baseFileName}_xes_${totalLength}.csv`);
}
if (options.sum) {
debugLog('Finalizing sum output');
csv.writeSumToFile(`${baseFileName}_sum_${totalLength}.csv`, items);
log(`${baseFileName}_sum_${totalLength}.csv`);
}
items = [];
}
const finishTime = (Date.now() - startTime) / 1000;
const batches = Math.ceil(totalLength / constants.batchSize);
log();
log('QLW Output Log');
log(options.recover ? '[recovery] | normal' : ' recovery | [normal]');
log(options.loose ? ' [loose] | strict' : ' loose | [strict]');
log(options.debug ? ' [debug] | normal' : ' debug | [normal]');
log(`Finished processing qlw directories in ${finishTime} seconds`);
log(`Processed ${totalLength} ${totalLength === 1 ? 'position' : 'positions'} in ${batches} ${batches === 1 ? 'batch' : 'batches'}`);
log(`${failed} positions failed to be processed`);
}
} catch (err) {
console.log(err);
debugLog(err.message);
}
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('\n\nDo you want to save this log [y/n]: ', answer => {
if (answer[0] === 'y') {
let log = Logger.getLog(constants.logger.names.defaultLog).log;
rl.question('Do you want to include the debug log [y/n]: ', answer => {
if (answer[0] === 'y')
log = log.concat(Logger.getLog(constants.logger.names.debugLog).log);
log.sort(([timeA], [timeB]) => timeA - timeB);
const fs = require('fs');
// Screw Windows new lines
fs.writeFileSync(`${options.outputDirectoryUri ? options.outputDirectoryUri : options.topDirectoryUri}/xes_converter_log.txt`, log.map(([time, line]) => `[${time}] ${line.replace(/\n/gi, '\r\n')}`).join('\r\n'));
console.log('Log written to file');
rl.close();
process.exit(0);
});
} else {
rl.close();
process.exit(0);
}
});
}
}