Skip to content

Instantly share code, notes, and snippets.

@mr-pascal
Created April 5, 2021 06:48
Show Gist options
  • Select an option

  • Save mr-pascal/0966f787e80ea254aab4f550c09bce79 to your computer and use it in GitHub Desktop.

Select an option

Save mr-pascal/0966f787e80ea254aab4f550c09bce79 to your computer and use it in GitHub Desktop.
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const moment = require('moment');
/**
* Converts a string date to a DATETIME string that can be used
* in BigQuery tables
* @param {string} date Date in string Format 'YYYY-MM-DD'
*/
const format = (date) => {
return `${moment(new Date(date)).format('YYYY-MM-DD hh:mm:ss')}.000000`;
}
/**
* Returns some random date in the range of 2021-01-01 to 2021-01-31
*/
const randomDate = () => {
const max = 31;
const min = 1;
let day = Math.floor(Math.random() * max) + min;
// We want some leading '0' for our day section.
if (day.toString().length === 1) {
day = `0${day}`;
}
return `2021-01-${day}`;
}
/**
* Returns some random browser
*/
const randombrowser = () => {
// Some browsers are mentioned multiple times.
// The reason for this is to introduce some weighting.
// => e.g. 'Chrome' will be returned more often than 'Safari'
const browsers = ['Chrome', 'Chrome', 'Chrome', 'Edge', 'Edge', 'Firefox', 'Safari'];
const randomIdx = Math.floor(Math.random() * browsers.length);
const browser = browsers[randomIdx];
return browser;
}
// -- Set these variables to your needs
// The total entries of the resulting CSV file
const totalEntries = 50000;
// ---------------------------
const data = [];
const csvWriter = createCsvWriter({
path: 'dump.csv',
header: [
{ id: 'date', title: 'date' },
{ id: 'browser', title: 'browser' },
]
});
for (let i = 0; i < totalEntries; i++) {
data.push({
date: format(randomDate()),
browser: randombrowser()
})
}
csvWriter
.writeRecords(data)
.then(() => console.log('The CSV file was written successfully'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment