-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgenerate_sample_data_previews.js
48 lines (41 loc) · 1.33 KB
/
generate_sample_data_previews.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
const fs = require("fs");
const { parse } = require("csv-parse");
const { stringify } = require("csv-stringify");
/**
*
* For each file in the sample data folder,
* generate a fresh "preview" version that is the first 20 records of that dataset.
* This allows us to serve these previews on Console without needing to load the
* entire sample dataset.
*
*/
const PREVIEW_SIZE = 21; // 20 data rows + header
const READ_DIR = "sample_data";
const PREVIEW_DIR = "sample_data_previews";
function createPreviewForFile(fileName) {
const readStream = fs.createReadStream(`${READ_DIR}/${fileName}`);
const parser = parse({ to: PREVIEW_SIZE });
const stringifier = stringify();
const writeStream = fs.createWriteStream(`${PREVIEW_DIR}/${fileName}`);
readStream.pipe(parser).pipe(stringifier).pipe(writeStream);
}
function generateSampleDataPreviews() {
fs.readdir(READ_DIR, (err, files) => {
if (err) {
console.log("ERROR reading sample data files", err);
}
files.forEach((fileName) => {
console.log("processing file", fileName);
createPreviewForFile(fileName);
});
});
}
function createPreviewDir() {
// this method prevents an error if the folder already exists
fs.mkdirSync(PREVIEW_DIR, { recursive: true });
}
function execute() {
createPreviewDir();
generateSampleDataPreviews();
}
execute();