createConfiguredGenerator: function()

in generators/csv-generator.ts [47:149]


    createConfiguredGenerator: function (config: any) {
        return {
            generatorTemplate: this,
            makeInstance: (() => (async function*() {
                
                let ranOnce = false;
                let batch: Array<ILogData> = [];
                
                while (config.loop || !ranOnce) {
                    const fileStream = fs.createReadStream(DATA_PATH + config.data);
                    const rl = readline.createInterface({
                        input: fileStream,
                        crlfDelay: Infinity
                    });
                    // Note: we use the crlfDelay option to recognize all instances of CR LF
                    // ('\r\n') in input.txt as a single line break.
                    
                    let skippedHeader = false;
                    let timestamped = true;
                    let parsed: any;
                    for await (const line of rl) {
                        if (!skippedHeader) {
                            skippedHeader = true;
                            if (line === "message") {
                                timestamped = false;
                            }
                            continue;
                        }

                        let cell1: string;
                        let cell2: string;
                        try {

                            if (timestamped) {
                                [cell1, cell2] = line.split(/,(.+)/);
                            }
                            
                            // overwrite cell 2 with cell if there is no timestamp
                            else {
                                cell2 = line;
                            }

                            // overwrite cell 2 with cell if there is no timestamp
                            if (cell2 === undefined) {
                                cell2 = cell1;
                            }

                            // remove quotes if needed
                            if (cell2.charAt(0) === "\"") {
                                cell2 = cell2.slice(1);
                            }
                            if (cell2.charAt(cell2.length - 1) === "\"") {
                                cell2 = cell2.slice(0, -1);
                            }

                            // substitute "" with "
                            try {
                                cell2 = cell2.replace(/\"\"/g, "\"");
                            }
                            catch (e) {
                                console.log(e)
                            }
                            
                            if (config.isJson) {
                                parsed = JSON.parse(cell2); // will throw error
                                if (typeof parsed !== "object") {
                                    continue;
                                }
                            }
                        } catch (e) {
                            continue;
                        }

                        if (config.isJson) {
                            batch.push(parsed);
                        }
                        else {
                            batch.push({
                                [config.logKey]: cell2,
                            });
                        }
                        if (batch.length === config.batchSize) {
                            yield batch;
                            batch = [];
                        }
                        
                        // Each line in input.txt will be successively available here as `line`.
                        // console.log(`Line from file: ${line}`);
                    }

                    rl.close();
                    fileStream.close();
                    ranOnce = true;

                    if (!config.loop) {
                        if (batch.length !== 0) {
                            yield batch;
                        }
                    }
                }
            })()),
        }
    }