Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active June 12, 2019 12:58
Show Gist options
  • Save Ratstail91/48b39b64d03e6a8f6496e84e5f35d6bc to your computer and use it in GitHub Desktop.
Save Ratstail91/48b39b64d03e6a8f6496e84e5f35d6bc to your computer and use it in GitHub Desktop.
//save data sequentially
let counter = 0;
let fpath = path.join(__dirname, 'sampledata');
fs.writeFileSync(path.resolve(fpath, `${counter}.json`), 'utf8');
//get the filenames for concatenation as array
let filenames = fs.readdirSync(fpath);
filenames.forEach(fname => {
//concatenate the files
});
//let's do this
let rate = 30;
let timestampGroup = 0;
let data = [];
let result = []; //the 'output' structure
//iterate over each file saved from the previous stage
while (filenames.length > 0) {
//grab the next chunk if we're out of data
if (data.length === 0) {
data = JSON.parse(fs.readFileSync(filenames.shift(), 'utf8'));
}
//ready to move to the next chunk?
if (data[0].timestamp - timestampGroup > rate) {
//save the newly created structure
fs.writeFileSync(path.resolve('outputdata', `${timestampGroup}.json'), JSON.stringify(result), 'utf8');
result = []; //clear
timestampGroup += rate;
}
//move from the front of the 'stream' to the result
result.push(data.shift());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment