-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport-data.js
51 lines (45 loc) · 1.36 KB
/
import-data.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
49
50
51
'use strict';
/*
* Copyright (c) 2017 Topcoder, Inc. All rights reserved.
*/
/**
* Import data
*/
require('dotenv').config();
const config = require('config');
const co = require('co');
const chalk = require('chalk');
const logger = require('../common/logger');
const datasetService = require('../services/dataset');
const dataService = require('../services/data');
const { syncDB } = require('../models');
co(function* () {
logger.info(`connecting to database: ${config.dbConfig.db_url}`);
yield syncDB();
const files = datasetService.getFilenames(config.downloadPath);
/**
* Read each dataset recursively.
* @param {integer} index - The dataset index
*/
function* readfile(index) {
logger.info(`Processing file: ${files[index]}`);
yield dataService.processFile(yield datasetService.readCSV(`${config.downloadPath}/${files[index]}`));
return index < files.length - 1 ? yield readfile(index + 1) : true;
}
if (files.length === 0) return null;
return yield readfile(0);
})
.then((completed) => {
if (completed) {
logger.info('Operation completed!');
} else {
logger.info('No data available!');
logger.info(`Run ${chalk.cyan('npm run download-data')} to download the datasets.`);
}
process.exit();
})
.catch((error) => {
logger.error(error.message);
logger.error(error);
process.exit();
});