UNPKG

@wbg-mde/r-factory

Version:

Metadata editor R integration module

96 lines (95 loc) 4.61 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); const repository_1 = require("@wbg-mde/repository"); const path = require("path"); const R = require("@wbg-mde/r-script"); const shared_1 = require("../shared"); const _ = require('lodash'); let ExportDataSetMaster = class ExportDataSetMaster { writeFIle(filepath, variables, csvpath, projectId, version, libraryPath, memoryLimit, callback) { let jsonpath = ''; try { let type = path.extname(filepath).slice(1); filepath = repository_1.Repository_Utility.getAvailableFilePath(filepath); let dataset = this.formatDataset(variables); jsonpath = this.getJSONPath(projectId); repository_1.Repository_Utility.writeFile(jsonpath, JSON.stringify(dataset)); let scriptFile = shared_1.Utility.getRScriptPath(3); this.exportDataSet(scriptFile, csvpath, jsonpath, filepath, version, libraryPath, memoryLimit, (resp) => { callback(resp); repository_1.Repository_Utility.deleteFile(jsonpath); }); } catch (e) { if (jsonpath) { repository_1.Repository_Utility.deleteFile(jsonpath); } ; console.log('Import Master >> writeFIle >> ' + e); callback({ result: 'error', messages: e }); } } exportDataSet(scriptFile, csvpath, jsonpath, filepath, version, libraryPath, memoryLimit, callback) { let type = path.extname(filepath); let workingDirectory = path.dirname(scriptFile); R(scriptFile).data(csvpath, jsonpath, filepath, type.slice(1), version, libraryPath, memoryLimit, workingDirectory) .call((err, resp) => { if (err) { callback({ result: 'error', messages: err.toString() }); } else { callback({ result: 'ok' }); } }); } formatDataset(variables) { let formattedDataset = _.map(variables, (variable) => { let catgry = new Array(); if (variable.catgry) { catgry = _.compact(_.map(variable.catgry, (category) => { if (category.catValu != undefined && category.catValu !== 'NA') { return { catValu: [category.catValu], labl: [category.labl] }; } })); } return { name: [variable.name], internalName: [variable.internalName || variable.name], labl: [variable.labl], val: catgry, dcml: [variable.dcml], width: [(variable.location) ? variable.location.width : 0], type: [(variable.varFormat) ? variable.varFormat.type : "numeric"], intrvl: [variable.intrvl], dataType: [variable.varType] }; }); return formattedDataset; } getJSONPath(projectid) { let outputpath = path.join(repository_1.configuration.userDataPath, repository_1.Repository_Constants.userDataPaths.cur_user, repository_1.Repository_Constants.projectHeader.base_folder, projectid, 'CSV Data'); return path.join(outputpath, 'temp.json'); } getCSVpath(filename, projectid) { let outputpath = path.join(repository_1.configuration.userDataPath, repository_1.Repository_Constants.userDataPaths.cur_user, repository_1.Repository_Constants.projectHeader.base_folder, projectid, 'CSV Data'); return path.join(outputpath, filename.trim() + '.csv'); } }; ExportDataSetMaster = __decorate([ repository_1.Trace({ params: { writeFIle: [1], formatDataset: [0] } }) ], ExportDataSetMaster); exports.ExportDataSetMaster = ExportDataSetMaster;