Skip to content

Instantly share code, notes, and snippets.

@aguegu
Created December 13, 2018 08:21
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 aguegu/39b918cb2782ebe4c694572c960ba4bf to your computer and use it in GitHub Desktop.
Save aguegu/39b918cb2782ebe4c694572c960ba4bf to your computer and use it in GitHub Desktop.
csv.tar.gz generator
/* eslint-env mocha */
/* eslint no-console: 'off' */
import chai from 'chai';
import chaiHttp from 'chai-http';
import 'chai/register-should';
import { createObjectCsvWriter } from 'csv-writer';
import config from 'config';
import _ from 'lodash';
import moment from 'moment';
import fs from 'fs-extra';
import path from 'path';
import tar from 'tar';
import os from 'os';
chai.use(chaiHttp);
describe.only('feeder', () => {
const sampleFile = path.join(process.cwd(), config.get('csv'), 'sample.tar.gz');
before(async () => {
const rowCount = 10000;
const fileCount = 10;
const t = moment().subtract(1, 'y');
const gen = (j) => {
const csvWriter = createObjectCsvWriter({
path: path.join(os.tmpdir(), `${_.padStart(j, 2, '0')}.csv`),
header: [
{ id: 'ts', title: '' },
{ id: 'id', title: 'ID' },
],
});
const records = _.chain(_.range(rowCount)).map((i) => {
const ts = t.format('YYYY-MM-DD HH:mm:ss');
t.add(1, 's');
return {
id: rowCount * j + i,
ts,
};
}).value();
return csvWriter.writeRecords(records);
};
const ids = _.range(fileCount);
const files = ids.map(i => `${_.padStart(i, 2, '0')}.csv`);
await Promise.all(ids.map(j => gen(j)));
await tar.c({
gzip: true,
file: sampleFile,
cwd: os.tmpdir(),
}, files);
await Promise.all(files.map(f => fs.remove(path.join(os.tmpdir(), f))));
});
it(`should get ${sampleFile}`, () => {
fs.statSync(sampleFile);
});
after(async () => fs.remove(sampleFile));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment