-
Notifications
You must be signed in to change notification settings - Fork 0
/
leitor.js
97 lines (82 loc) · 2.9 KB
/
leitor.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
import { dirname, join } from "path";
import { promisify } from "util";
import { readdir, createReadStream, createWriteStream, statSync, ReadStream } from "fs";
import { pipeline, Transform } from "stream";
import { randomUUID } from "crypto";
import csvtojson from "csvtojson";
import StreamConcat from "stream-concat";
const pipelineAsync = promisify(pipeline);
const readDirAsync = promisify(readdir);
const { pathname: currentFile } = new URL(import.meta.url);
const currentWorkDir = dirname(currentFile);
const datasetWorkDir = `${currentWorkDir}/data`.replace("C:/", "");
const outputDataFile = `${currentWorkDir}/finalFile.csv`.replace("C:/", "");
const informationFilesToReading = [];
const filesToReading = (await readDirAsync(datasetWorkDir)).filter(
(file) => !!~file.indexOf(".csv")
);
let timeInProcessingDataInSegs = 0;
let currentSizeFinalFile = 0;
setInterval(() => {
timeInProcessingDataInSegs++;
const messages = [
`============================================================`,
`==> Arquivos : ${filesToReading}`,
`==> Processando ${ByteToMB_Rounded(
currentSizeFinalFile
)}MB : ${progressBar(timeInProcessingDataInSegs)}`,
`============================================================`,
];
applyInterface(messages);
}, 1000).unref();
const streams = filesToReading.map((document) => {
const pathFile = join(datasetWorkDir, document);
const infoFile = statSync(pathFile);
informationFilesToReading.push({
path: join(datasetWorkDir, document),
size: infoFile.size,
});
return createReadStream(pathFile);
});
const mergedStreams = new StreamConcat(streams);
const createOutFile = createWriteStream(outputDataFile).on("finish", () => {
const messages = [
`=============== PROCESSO FINALIZADO ========================`,
`==> Arquivos : ${filesToReading}`,
`==> Processado: ${ByteToMB_Rounded(currentSizeFinalFile)} MB `,
`==> Duração : ${timeInProcessingDataInSegs}s`,
`==> Localização : ${outputDataFile}`,
`============================================================`,
];
applyInterface(messages);
});
const handlerFile = new Transform({
transform: (chunk, enc, cb) => {
let item = JSON.parse(chunk);
item = JSON.stringify({
id: randomUUID(),
...item,
});
currentSizeFinalFile += Buffer.byteLength(item);
return cb(null, item);
},
});
pipelineAsync(mergedStreams, csvtojson(), handlerFile, createOutFile).catch(
(e) => console.log("DEU RUIM ! " + e.message)
);
function progressBar(progress) {
let bar = "";
for (let ps = 0; ps <= progress; ps++) {
bar += ".";
}
return bar;
}
function ByteToMB_Rounded(byte) {
return Math.round(byte / 1000000);
}
function applyInterface(messages) {
console.clear();
for (const message of messages) {
process.stdout.write(message + "\n");
}
}