fzadrazil 5 tahun lalu
induk
melakukan
07c714df5a
4 mengubah file dengan 107 tambahan dan 17 penghapusan
  1. 30 0
      README.md
  2. 35 4
      index.js
  3. 29 0
      nuts-data.js
  4. 13 13
      package-lock.json

+ 30 - 0
README.md

@@ -0,0 +1,30 @@
+# Rural Attractivness back-end service
+
+After the update of source data, refresh the service cache
+
+GET https://publish.lesprojekt.cz/nodejs/refresh
+
+
+List of all the datasets
+
+GET https://publish.lesprojekt.cz/nodejs/datasets
+
+
+Attractivity data for the region with ID equal to the 'nuts' parameter
+
+GET https://publish.lesprojekt.cz/nodejs/scores/{nuts}/
+
+
+Attractivity data for all the regions in source CSV data
+
+GET https://publish.lesprojekt.cz/nodejs/scores
+
+
+Computes and returns attractivity data for all the NUTS regions based on the incomming datasets and factor weights
+
+POST https://publish.lesprojekt.cz/nodejs/scores
+
+
+
+## Start the system service
+sudo systemctl start fz-node-rural_attractiveness

+ 35 - 4
index.js

@@ -8,6 +8,7 @@ const app = express();
 
 const _datasetsFilePath = 'data/datasets.csv';
 const _dataFilePath = 'data/data.csv';
+const _clustersFilePath = 'data/clustering/out_file.csv';
 var _datasets = undefined;
 var _ruralData = undefined;
 
@@ -125,22 +126,52 @@ app.post('/scores', (req, res, next) => {
     }
 });
 
+/*
+    Only testing purposes
+*/
 app.get('/runR', (req, res, next) => {
     //console.log(console);
     console.log('calling R...')
     R('./r/selected_data.r').call(
         function(err, data) {
             console.log('R done');
-            if (err) console.log(err.toString('utf8'))
+            if (err) {
+                console.log(err.toString('utf8'));
+                data = { result: err.toString('utf8') };
+            }
             else {
-							console.log(data);
-              data = {result: 'R call succesful'};
-						}
+                console.log(data);
+                data = { result: 'R call succesful' };
+            }
             helpers.formatResponse({ response: data }, req, res);
         }
     );
 });
 
+/*
+    Calls R script, loads the resulting CSV file and returns it
+*/
+app.get('/clusters', (req, res, next) => {
+    //console.log(console);
+    console.log('calling R...')
+    R('./r/selected_data.r').call(
+        function(err, data) {
+            console.log('R done');
+            if (err) {
+                console.log(err.toString('utf8'));
+                data = { result: err.toString('utf8') };
+            }
+            else {
+                console.log(data);
+                nutsData.loadClusters(_clustersFilePath, function(clusterData) {
+                    data = clusterData;
+                    helpers.formatResponse({ response: data }, req, res);
+                });
+            }
+        }
+    );
+});
+
 // start the service on the port xxxx
 app.listen(3000, () => console.log('Rural attractivity WS listening on port 3000...'));
 

+ 29 - 0
nuts-data.js

@@ -75,6 +75,35 @@ module.exports.loadRuralData = function (filePath, datasets, dataLoadedCallback)
         });
 }
 
+/* Reads the out_file.csv created by R script and saves it into an object
+*/
+module.exports.loadClusters = function (filePath, dataLoadedCallback) {
+    console.log('Reading clustering data file processing started.');
+    let clusters = [];
+
+    let columns = undefined;
+
+    fs.createReadStream(filePath)
+        .pipe(csv())
+        .on('data', (row) => {
+            if (!columns) {
+                columns = row;
+            }
+            else {
+                let item = {};
+                for (let i = 0; i < columns.length; i++) {
+                    const colName = columns[i].length > 0 ? columns[i].toLowerCase() : 'nuts_id';
+                    item[colName] = row[i];
+                }
+                clusters.push(item);
+            }
+        })
+        .on('end', () => {
+            //console.log('Rural data file processing finished.');
+            dataLoadedCallback(clusters);
+        });
+}
+
 module.exports.getFactorIndex = function (region, factor) {
     //console.log('getFactorIndex');
     //console.log('region: ' + region.nuts);

+ 13 - 13
package-lock.json

@@ -3334,6 +3334,14 @@
       "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
       "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
     },
+    "r-script": {
+      "version": "0.0.4",
+      "resolved": "https://registry.npmjs.org/r-script/-/r-script-0.0.4.tgz",
+      "integrity": "sha512-PNUS0iQGHLSzKCaTI8HZs5pw20GSJVaIhuBEPY3QYoUmf1RXUAnTcof0G6uMp8jGr9Ke2n/rhXVC6B1m/iiWCA==",
+      "requires": {
+        "underscore": "^1.8.3"
+      }
+    },
     "randomatic": {
       "version": "3.1.1",
       "resolved": "https://registry.npmjs.org/randomatic/-/randomatic-3.1.1.tgz",
@@ -3362,14 +3370,6 @@
         }
       }
     },
-    "r-script": {
-      "version": "0.0.4",
-      "resolved": "https://registry.npmjs.org/r-script/-/r-script-0.0.4.tgz",
-      "integrity": "sha512-PNUS0iQGHLSzKCaTI8HZs5pw20GSJVaIhuBEPY3QYoUmf1RXUAnTcof0G6uMp8jGr9Ke2n/rhXVC6B1m/iiWCA==",
-      "requires": {
-        "underscore": "^1.8.3"
-      }
-    },
     "range-parser": {
       "version": "1.2.1",
       "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@@ -4361,6 +4361,11 @@
         "debug": "^2.2.0"
       }
     },
+    "underscore": {
+      "version": "1.10.2",
+      "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.10.2.tgz",
+      "integrity": "sha512-N4P+Q/BuyuEKFJ43B9gYuOj4TQUHXX+j2FqguVOpjkssLUUrnJofCcBccJSCoeturDoZU6GorDTHSvUDlSQbTg=="
+    },
     "union-value": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz",
@@ -4383,11 +4388,6 @@
         "crypto-random-string": "^1.0.0"
       }
     },
-    "underscore": {
-      "version": "1.10.2",
-      "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.10.2.tgz",
-      "integrity": "sha512-N4P+Q/BuyuEKFJ43B9gYuOj4TQUHXX+j2FqguVOpjkssLUUrnJofCcBccJSCoeturDoZU6GorDTHSvUDlSQbTg=="
-    },
     "unpipe": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",