@wbg-mde/r-factory
Version:
Metadata editor R integration module
68 lines (67 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const test_config_1 = require("./test-config");
const fs = require("fs");
const path = require("path");
const categories_from_statistics_1 = require("../modules/convert/categories-from-statistics");
const import_test_1 = require("./import.test");
class CreateCategoriesTest {
constructor() {
this.fileIndex = 0;
this.freqLimit = 50;
this.catStats = new categories_from_statistics_1.CategoriesFromStatistics();
this.tstImport = new import_test_1.ImportAPITest();
}
execute(files, listOfVariables) {
try {
this.fileIndex = 0;
files.reduce((p, x) => p.then(_ => this.executeFile(x, listOfVariables, this.freqLimit)), Promise.resolve());
}
catch (e) {
console.log('execute error >> ' + e);
}
}
executeFile(filepath, listOfVariables, freqLimit) {
this.fileIndex += 1;
const inputDatasetPath = path.join(test_config_1.TestConfig.inputPath.dataset, filepath);
const inputCSVPath = path.join(test_config_1.TestConfig.inputPath.csv, 'F' + this.fileIndex + '.csv');
const inputJSONPath = path.join(test_config_1.TestConfig.inputPath.json, 'F' + this.fileIndex + '.json');
const outputJSONPath = path.join(test_config_1.TestConfig.outputPath.json, 'F' + this.fileIndex + '.json');
return Promise.all([
this.tstImport.readDataFIle(inputDatasetPath, this.fileIndex, inputJSONPath),
this.tstImport.writeCSV(inputDatasetPath, this.fileIndex, inputCSVPath)
]).then(resp => this.updateVarStatus(resp, outputJSONPath, listOfVariables, freqLimit))
.then((resp) => {
if ((resp && resp.result === 'ok')) {
console.log(`file ${filepath} completed successfully!!`);
}
else {
console.log(`file ${filepath} completed with errors!!`);
}
}).catch((e) => {
console.log(`error while executing the file ${filepath}`);
console.log(e);
});
}
updateVarStatus(resp, outPath, listOfVariables, freqLimit) {
if (resp.length === 2 &&
(resp[0].data && resp[0].data.result === 'ok') &&
(resp[1].data && resp[1].data.result === 'ok')) {
const csvFilepath = resp[1].data.file;
return this.catStats.create({
"variables": listOfVariables,
"csvPath": csvFilepath,
"limit": freqLimit
}).then((resp) => {
if (resp.result === 'ok') {
fs.writeFileSync(outPath, JSON.stringify(resp.data, null, 4));
return resp;
}
else {
return resp;
}
});
}
}
}
exports.CreateCategoriesTest = CreateCategoriesTest;