|
|
@@ -8,6 +8,8 @@ const app = express();
|
|
|
|
|
|
const _datasetsFilePath = 'data/datasets.csv';
|
|
|
const _dataFilePath = 'data/data.csv';
|
|
|
+const _clusteringInputFilePath = 'data/clustering/input_all.csv';
|
|
|
+const _clusteringModifiedFilePath = 'data/clustering/input_modified.csv';
|
|
|
const _clustersFilePath = 'data/clustering/out_file.csv';
|
|
|
var _datasets = undefined;
|
|
|
var _ruralData = undefined;
|
|
|
@@ -31,6 +33,7 @@ app.get('/refresh', (req, res, next) => {
|
|
|
nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
|
|
|
//console.log('Rural data loaded succesfully');
|
|
|
_ruralData = rd;
|
|
|
+ res.send('Data refreshed');
|
|
|
});
|
|
|
});
|
|
|
});
|
|
|
@@ -132,6 +135,7 @@ app.post('/scores', (req, res, next) => {
|
|
|
app.get('/runR', (req, res, next) => {
|
|
|
//console.log(console);
|
|
|
console.log('calling R...')
|
|
|
+ console.log(req)
|
|
|
R('./r/selected_data.r').call(
|
|
|
function(err, data) {
|
|
|
console.log('R done');
|
|
|
@@ -149,27 +153,39 @@ app.get('/runR', (req, res, next) => {
|
|
|
});
|
|
|
|
|
|
/*
|
|
|
- Calls R script, loads the resulting CSV file and returns it
|
|
|
+ Just informative response. POST with JSON data is required.
|
|
|
*/
|
|
|
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);
|
|
|
- });
|
|
|
- }
|
|
|
+ const data = { response: '/clusters method is only available under POST' }
|
|
|
+ helpers.formatResponse(data, req, res);
|
|
|
+});
|
|
|
+
|
|
|
+/*
|
|
|
+ Modifies input CSV file, calls R script, loads the resulting CSV file and returns it
|
|
|
+*/
|
|
|
+app.post('/clusters', async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ if (!_datasets) {
|
|
|
+ //TODO: promisify all functions to avoid callback hell and make this work properly
|
|
|
+ await nutsData.loadDatasets(_datasetsFilePath, function (ds) {
|
|
|
+ //console.log('Datasets loaded succesfully');
|
|
|
+ _datasets = ds;
|
|
|
+ })
|
|
|
}
|
|
|
- );
|
|
|
+ //console.log(req.body);
|
|
|
+ const clusteringData = await nutsData.loadClusteringInput(
|
|
|
+ _clusteringInputFilePath
|
|
|
+ );
|
|
|
+ await nutsData.modifyClusteringData({
|
|
|
+ datasets: _datasets,
|
|
|
+ data: clusteringData,
|
|
|
+ params: req.body,
|
|
|
+ outputFileName: _clusteringModifiedFilePath
|
|
|
+ });
|
|
|
+ handleRCall(req, res);
|
|
|
+ } catch (error) { // Catch errors in async functions
|
|
|
+ next(error);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
// start the service on the port xxxx
|
|
|
@@ -216,3 +232,23 @@ function returnRegionScores(nuts, req, res) {
|
|
|
// NUTS region not found
|
|
|
res.status(404).send('NUTS region not found.');
|
|
|
}
|
|
|
+
|
|
|
+function handleRCall(req, res) {
|
|
|
+ //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);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+}
|