-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·105 lines (91 loc) · 2.73 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
const fs = require('fs');
const path = require('path');
const jsonexport = require('jsonexport');
const split = require('split2');
const { SingleBar, Presets } = require('cli-progress');
const { Transform } = require('stream');
const {
NUMBER_OF_ROWS, MIN_LATITUDE, MAX_LATITUDE, MIN_LONGITUDE, MAX_LONGITUDE, MIN_ALTITUDE, MAX_ALTITUDE, YEARS,
} = require('./config');
const { getMeasurement } = require('./lib/random');
const { info, error } = console;
const NOW = Date.now();
const JSON_OUTPUT = path.resolve(`${__dirname}/output`, `dl-measurements_${NOW}.ndjson`);
const CSV_OUTPUT = path.resolve(`${__dirname}/output`, `dl-measurements_${NOW}.csv`);
async function writeJSON() {
return new Promise((resolve, reject) => {
let index = 0;
const progress = new SingleBar({}, Presets.shades_classic);
const jsonWriter = fs.createWriteStream(JSON_OUTPUT, { flags: 'a' });
jsonWriter.on('error', reject);
jsonWriter.on('close', () => {
progress.stop();
info(`\nCorrectly wrote ${NUMBER_OF_ROWS} measurement in ndjson format\n`);
resolve();
});
function write(data, next) {
const wrote = jsonWriter.write(data, 'utf-8');
if (!wrote) {
jsonWriter.once('drain', next);
} else {
process.nextTick(next);
}
}
function run() {
if (index < NUMBER_OF_ROWS) {
const measurement = getMeasurement(
MIN_LATITUDE,
MAX_LATITUDE,
MIN_LONGITUDE,
MAX_LONGITUDE,
MIN_ALTITUDE,
MAX_ALTITUDE,
YEARS,
);
write((`${JSON.stringify(measurement)}\n`), run);
progress.increment();
index += 1;
} else {
jsonWriter.end();
}
}
info(`Writing ${JSON_OUTPUT} file\n`);
progress.start(NUMBER_OF_ROWS, 0);
run();
});
}
async function writeCSV() {
return new Promise((resolve, reject) => {
const progress = new SingleBar({}, Presets.shades_classic);
const jsonReader = fs.createReadStream(JSON_OUTPUT);
const csvWriter = fs.createWriteStream(CSV_OUTPUT);
jsonReader.on('error', reject);
csvWriter.on('error', reject);
csvWriter.on('close', () => {
progress.stop();
info(`\nCorrectly wrote ${NUMBER_OF_ROWS} measurement in csv format\n`);
resolve();
});
info(`Writing ${CSV_OUTPUT} file\n`);
progress.start(NUMBER_OF_ROWS, 0);
jsonReader
.pipe(split())
.pipe(new Transform({
transform(chunk, _, callback) {
this.push(chunk);
progress.increment();
callback();
},
}))
.pipe(jsonexport())
.pipe(csvWriter);
});
}
(async () => {
try {
await writeJSON();
await writeCSV();
} catch (err) {
error({ err });
}
})();