Pārlūkot izejas kodu

Unify API methods

jmacura 4 gadi atpakaļ
vecāks
revīzija
92a532b3f0
1 mainītis faili ar 41 papildinājumiem un 94 dzēšanām
  1. 41 94
      index.js

+ 41 - 94
index.js

@@ -41,8 +41,10 @@ app.get('/', (req, res) => {
 app.get('/refresh', async (req, res, next) => {
   try {
     _datasets = await nutsData.loadDatasets(_datasetsFilePath)
+    _datasetsCZ = await nutsData.loadDatasets(_datasetsCzFilePath)
     //console.log('Datasets loaded succesfully');
     _ruralData = await nutsData.loadRuralData(_dataFilePath, _datasets)
+    _ruralDataCZ = await nutsData.loadRuralData(_datasetsCzFilePath, _datasetsCZ)
     //console.log('Rural data loaded succesfully');
   } catch (e) {
     res.send(e.toString() || e)
@@ -51,29 +53,19 @@ app.get('/refresh', async (req, res, next) => {
 });
 
 /* Returns JSON array with the list of all the datasets */
-app.get('/datasets', async (req, res, next) => {
-  if (!_datasets) {
+app.get('/datasets/:aoi?', async (req, res, next) => {
+  console.log('received datasets/ GET request')
+  const aoi = req.params.aoi || 'eu'
+  let datasets = aoi == 'cz' ? _datasetsCZ : _datasets
+  if (!datasets) {
+    const datasetsFilePath = aoi == 'cz' ? _datasetsCzFilePath : _datasetsFilePath
     try {
-      _datasets = await nutsData.loadDatasets(_datasetsFilePath)
+      datasets = await nutsData.loadDatasets(datasetsFilePath)
     } catch (e) {
       res.send(e.toString() || e)
     }
   }
-  helpers.formatResponse(_datasets, req, res)
-});
-
-/* Returns JSON array with the list of all the datasets */
-app.get('/datasets/cz', async (req, res, next) => {
-  console.log('received datasets/cz GET request')
-  if (!_datasetsCZ) {
-    try {
-      _datasetsCZ = await nutsData.loadDatasets(_datasetsCzFilePath)
-    } catch (e) {
-      res.send(e.toString() || e)
-    }
-  }
-  //console.log('Datasets loaded callback');
-  helpers.formatResponse(_datasetsCZ, req, res)
+  helpers.formatResponse(datasets, req, res)
 });
 
 /* Returns attractivity data for the region with ID equal to the 'nuts' parameter */
@@ -132,7 +124,7 @@ app.post('/scores', async (req, res, next) => {
       res.send(e.toString() || e)
     }
   }
-  returnAllScores(req, res)
+  returnAllScores(req, res, 'eu')
 });
 
 /* Returns attractivity data for all the regions in source CSV data. */
@@ -174,17 +166,17 @@ app.post('/scores/cz', async (req, res, next) => {
       res.send(e.toString() || e)
     }
   }
-    returnAllScoresCz(req, res)
+    returnAllScores(req, res, 'cz')
 });
 
 /*
   Only testing purposes
 */
-app.get('/runR/:version?', (req, res, next) => {
+app.get('/runR/:aoi?', (req, res, next) => {
   //console.log(console);
-  let v = req.params.version;
-  const filename = v == 'cz' ? './r/CZ_clusters.r' :'./r/selected_data.r';
-  console.log('calling R... for ' + v);
+  let aoi = req.params.aoi;
+  const filename = aoi == 'cz' ? './r/CZ_clusters.r' :'./r/selected_data.r';
+  console.log('calling R... for ' + aoi);
   //console.log(req)
   R(filename).data(9).call(
     function (err, data) {
@@ -205,7 +197,7 @@ app.get('/runR/:version?', (req, res, next) => {
 /*
     Just informative response. POST with JSON data is required.
 */
-app.get(['/clusters', '/clusters/cz'], (req, res, next) => {
+app.get('/clusters/:aoi?', (req, res, next) => {
   const data = { response: '/clusters methods are only available under POST' }
   helpers.formatResponse(data, req, res)
 });
@@ -213,10 +205,16 @@ app.get(['/clusters', '/clusters/cz'], (req, res, next) => {
 /*
     Modifies input CSV file, calls R script, loads the resulting CSV file and returns it
 */
-app.post('/clusters', async (req, res, next) => {
-  if (!_datasets) { // datasets must be loaded prior to data loading
+app.post('/clusters/:aoi?', async (req, res, next) => {
+  const aoi = req.params.aoi || 'eu'
+  let datasets = aoi == 'cz' ? _datasetsCZ : _datasets
+  const dataFilePath = aoi == 'cz' ? _dataCzFilePath : _dataFilePath
+  const clusteringModifiedFilePath = aoi == 'cz' ? _clusteringCzModifiedFilePath : _clusteringModifiedFilePath
+  const idString = aoi == 'cz' ? 'LAU2' : 'NUTS_ID'
+  if (!datasets) { // datasets must be loaded prior to data loading
     try {
-      _datasets = await nutsData.loadDatasets(_datasetsFilePath)
+      const datasetsFilePath = aoi == 'cz' ? _datasetsCzFilePath : _datasetsFilePath
+      datasets = await nutsData.loadDatasets(datasetsFilePath)
     } catch (e) {
       res.send(e.toString() || e)
     }
@@ -224,48 +222,21 @@ app.post('/clusters', async (req, res, next) => {
   try {
     //console.log(req.body);
     const clusteringData = await nutsData.loadClusteringInput(
-      _dataFilePath
+      dataFilePath
     );
     await nutsData.modifyClusteringData({
-      datasets: _datasets,
+      datasets: datasets,
       data: clusteringData,
       params: req.body,
-      idString: 'NUTS_ID',
-      outputFileName: _clusteringModifiedFilePath
+      idString: idString,
+      outputFileName: clusteringModifiedFilePath
     });
-    handleRCall(req, res, 'eu')
+    handleRCall(req, res, aoi)
   } catch (error) { // Catch errors in async functions
     next(error.toString())
   }
 });
 
-/*
-    Modifies input CSV file, calls R script, loads the resulting CSV file and returns it
-*/
-app.post('/clusters/cz', async (req, res, next) => {
-  if (!_datasetsCZ) {
-    _datasetsCZ = await nutsData.loadDatasets(_datasetsCzFilePath)
-    //console.log('Datasets loaded succesfully');
-  }
-  try {
-    //console.log(req.body);
-    const clusteringData = await nutsData.loadClusteringInput(
-      _clusteringCzInputFilePath
-    );
-    await nutsData.modifyClusteringData({
-      region: 'cz',
-      datasets: _datasetsCZ,
-      data: clusteringData,
-      params: req.body,
-      idString: 'LAU2',
-      outputFileName: _clusteringCzModifiedFilePath
-    })
-    handleRCall(req, res, 'cz');
-  } catch (error) { // Catch errors in async functions
-    next(error.toString());
-  }
-});
-
 app.get('/georeport/:nuts', async (req, res, next) => {
   if (!_datasets) { // datasets must be loaded prior to data loading
     try {
@@ -314,12 +285,13 @@ function renderPugReport(nuts, req, res) {
     res.render('err', {});
 }
 
-function returnAllScores(req, res) {
-  var resData = [];
-  _ruralData.forEach(region => {
+function returnAllScores(req, res, aoi = 'eu') {
+  let resData = []
+  const ruralData = aoi == 'cz' ? _ruralDataCZ : _ruralData
+  ruralData.forEach(region => {
     //var region = _ruralData[0];
-    var sumWeight = 0;
-    var sumValue = 0;
+    let sumWeight = 0;
+    let sumValue = 0;
     let regionIndexes = { code: region.nuts };
 
     req.body.factors.forEach(f => {
@@ -339,31 +311,6 @@ function returnAllScores(req, res) {
   helpers.formatResponse(resData, req, res);
 }
 
-function returnAllScoresCz(req, res) {
-  var resData = [];
-  _ruralDataCZ.forEach(region => {
-    //var region = _ruralData[0];
-    var sumWeight = 0;
-    var sumValue = 0;
-    let regionIndexes = { code: region.lau2 };
-
-    req.body.factors.forEach(f => {
-      let fi = nutsData.getFactorIndex(region, f);
-      //console.log("f: " + JSON.stringify(f));
-      //console.log("fi: " + JSON.stringify(fi));
-
-      regionIndexes[f.factor] = fi.index;
-      sumValue += fi.sumValue * f.weight;
-      sumWeight += fi.sumWeight;
-    });
-
-    regionIndexes.aggregate = sumValue / sumWeight;
-    resData.push(regionIndexes);
-  });
-
-  helpers.formatResponse(resData, req, res);
-}
-
 function returnRegionScores(nuts, req, res) {
   var found = false;
   res.header("Content-Type", 'application/json');
@@ -380,12 +327,12 @@ function returnRegionScores(nuts, req, res) {
     res.status(404).send('NUTS region not found.');
 }
 
-function handleRCall(req, res, region = 'eu') {
+function handleRCall(req, res, aoi = 'eu') {
   const numberOfClusters = req.body.numberOfClusters || 12;
   //console.log('calling R...', numberOfClusters)
-  const rScriptInputFileName = region == 'cz' ? './r/CZ_clusters.r' :'./r/selected_data.r';
-  const rScriptResultsFileName = region == 'cz' ? _clustersCzFilePath : _clustersFilePath;
-  const idString = region == 'cz' ? 'lau2' : 'nuts_id';
+  const rScriptInputFileName = aoi == 'cz' ? './r/CZ_clusters.r' :'./r/selected_data.r';
+  const rScriptResultsFileName = aoi == 'cz' ? _clustersCzFilePath : _clustersFilePath;
+  const idString = aoi == 'cz' ? 'lau2' : 'nuts_id';
   R(rScriptInputFileName).data(numberOfClusters).call(
     function (err, data) {
       //console.log('R done');