index.js 1.32 KB
var express = require('express');
var router = express.Router();
const rscript = require('js-call-r');
const csvtojson=require("csvtojson");
const json2csv = require('json2csv');
var fs = require('fs');
const Iconv = require('iconv').Iconv;

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/getPredict',function(req,res,next){
  let insp = req.body;
  //csv읽어와서 json으로 변환
  csvtojson()
  .fromFile("normal_value_delete_version_utf8.csv")
  .then((json)=>{
    let normal_json = json;
    //정상값에 채움.
    insp.forEach((inspection)=>{
      let insp_name= inspection.name;
      let found = normal_json.find((value)=>{
        return value.name==insp_name;
      })
      if(found){
        found.value=inspection.value;
      }
    })
    //csv포맷에 맞춰 출력
    var iconv = new Iconv('utf-8','euc-kr');
    let csv1 = normal_json.map((val)=>"\""+iconv.convert(val.name).toString()+"\"").join(',');
    let csv2 = normal_json.map((val)=>val.value).join(',');
    let csv = csv1 + '\n' +csv2 +'\n';
    fs.writeFileSync('./inspection.csv',csv);

    //스크립트 수행.
    const result = rscript.callSync('/home/swc/capD/model/model.r', {path : process.cwd()+'/inspection.csv'});
    res.json(result);
  })
});

module.exports = router;