Skip to content

Instantly share code, notes, and snippets.

@awave1
Created November 27, 2019 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awave1/2b013a6c6a6b005b5ed9e4e6939a76b3 to your computer and use it in GitHub Desktop.
Save awave1/2b013a6c6a6b005b5ed9e4e6939a76b3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const fs = require('fs');
/**
* Simple script to generate random data for our mongodb
* example:
* node generate.js <topic_name> <min> <max> <numEntries>
*
* # or
*
* node generate.js <topic_name> <min> <max> <numEntries> true # this will round up the data results
*
* result of the script is written to a file: <topic>.sample.json
*/
// takes in min, max and a bool to round the data
// returns a rand int in the specified range
const rand = (min, max, round = false) =>
round
? Math.floor(Math.random() * (max - min) + min)
: Math.random() * (max - min) + min;
const data = [];
const topic = process.argv[2];
const min = process.argv[3];
const max = process.argv[4];
const numEntries = process.argv[5];
const round = process.argv[6] && process.argv[5] === 'true';
for (let i = 0; i < numEntries; i++) {
data.push({
topic,
data: [rand(min, max)],
});
}
fs.writeFileSync(`${topic}.sample.json`, JSON.stringify(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment