index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const express = require('express');
  2. const helpers = require('./helpers.js');
  3. const nutsData = require('./nuts-data.js');
  4. const cors = require('cors');
  5. const R = require('r-script');
  6. const app = express();
  7. const _datasetsFilePath = 'data/datasets.csv';
  8. const _dataFilePath = 'data/data.csv';
  9. const _clustersFilePath = 'data/clustering/out_file.csv';
  10. var _datasets = undefined;
  11. var _ruralData = undefined;
  12. // parse incoming POST requests body to JSON
  13. app.use(express.json());
  14. // handle CORS
  15. app.use(cors())
  16. /* Dummy web service call without the method specified */
  17. app.get('/', (req, res) => {
  18. res.send('Rural attractivness web service');
  19. });
  20. /* Makes refresh of the data loaded to the server objects.
  21. Must be called after the CSV data has changed */
  22. app.get('/refresh', (req, res, next) => {
  23. nutsData.loadDatasets(_datasetsFilePath, function (ds) {
  24. //console.log('Datasets loaded succesfully');
  25. _datasets = ds;
  26. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  27. //console.log('Rural data loaded succesfully');
  28. _ruralData = rd;
  29. });
  30. });
  31. });
  32. /* Returns JSON array with the list of all the datasets */
  33. app.get('/datasets', (req, res, next) => {
  34. if (_datasets) {
  35. helpers.formatResponse(_datasets, req, res);
  36. }
  37. else {
  38. nutsData.loadDatasets(_datasetsFilePath, function (ds) {
  39. //console.log('Datasets loaded callback');
  40. _datasets = ds;
  41. helpers.formatResponse(_datasets, req, res);
  42. });
  43. }
  44. });
  45. /* Returns attractivity data for the region with ID equal to the 'nuts' parameter */
  46. app.get('/scores/:nuts', (req, res, next) => {
  47. if (_ruralData) {
  48. returnRegionScores(req.params.nuts, req, res);
  49. }
  50. else {
  51. if (_datasets) { // datasets must be loaded prior to data loading
  52. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  53. _ruralData = rd;
  54. returnRegionScores(req.params.nuts, req, res);
  55. });
  56. }
  57. else {
  58. nutsData.loadDatasets(_datasetsFilePath, function (ds) {
  59. _datasets = ds;
  60. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  61. _ruralData = rd;
  62. returnRegionScores(req.params.nuts, req, res);
  63. });
  64. });
  65. }
  66. }
  67. });
  68. /* Returns attractivity data for all the regions in source CSV data. */
  69. app.get('/scores', (req, res, next) => {
  70. if (_ruralData)
  71. helpers.formatResponse(_ruralData, req, res);
  72. else {
  73. if (_datasets) { // datasets must be loaded prior to data loading
  74. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  75. _ruralData = rd;
  76. helpers.formatResponse(_ruralData, req, res);
  77. });
  78. }
  79. else {
  80. nutsData.loadDatasets(_datasetsFilePath, function (ds) {
  81. _datasets = ds;
  82. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  83. _ruralData = rd;
  84. helpers.formatResponse(_ruralData, req, res);
  85. });
  86. });
  87. }
  88. }
  89. });
  90. /* Computes and returns attractivity data for all the NUTS regions based on the
  91. incomming datasets and factor weights. */
  92. app.post('/scores', (req, res, next) => {
  93. //console.log("query: " + JSON.stringify(req.body.factors, null, 4));
  94. if (_ruralData) {
  95. returnAllScores(req, res);
  96. }
  97. else {
  98. if (_datasets) { // datasets must be loaded prior to data loading
  99. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  100. _ruralData = rd;
  101. returnAllScores(req, res);
  102. });
  103. }
  104. else {
  105. nutsData.loadDatasets(_datasetsFilePath, function (ds) {
  106. _datasets = ds;
  107. nutsData.loadRuralData(_dataFilePath, _datasets, function (rd) {
  108. _ruralData = rd;
  109. returnAllScores(req, res);
  110. });
  111. });
  112. }
  113. }
  114. });
  115. app.get('/runR', (req, res, next) => {
  116. //console.log(console);
  117. console.log('calling R...')
  118. R('./r/selected_data.r').call(
  119. function(err, data) {
  120. console.log('R done');
  121. if (err) {
  122. console.log(err.toString('utf8'));
  123. data = { result: err.toString('utf8') };
  124. }
  125. else {
  126. console.log(data);
  127. data = { result: 'R call succesful' };
  128. }
  129. helpers.formatResponse({ response: data }, req, res);
  130. }
  131. );
  132. });
  133. app.get('/clusters', (req, res, next) => {
  134. //console.log(console);
  135. console.log('calling R...')
  136. R('./r/selected_data.r').call(
  137. function(err, data) {
  138. console.log('R done');
  139. if (err) {
  140. console.log(err.toString('utf8'));
  141. data = { result: err.toString('utf8') };
  142. }
  143. else {
  144. console.log(data);
  145. nutsData.loadClusters(_clustersFilePath, function(clusterData) {
  146. data = clusterData;
  147. helpers.formatResponse({ response: data }, req, res);
  148. });
  149. }
  150. }
  151. );
  152. });
  153. // start the service on the port xxxx
  154. app.listen(3000, () => console.log('Rural attractivity WS listening on port 3000...'));
  155. function returnAllScores(req, res) {
  156. var resData = [];
  157. _ruralData.forEach(region => {
  158. //var region = _ruralData[0];
  159. var sumWeight = 0;
  160. var sumValue = 0;
  161. let regionIndexes = { code: region.nuts };
  162. req.body.factors.forEach(f => {
  163. let fi = nutsData.getFactorIndex(region, f);
  164. //console.log("f: " + JSON.stringify(f));
  165. //console.log("fi: " + JSON.stringify(fi));
  166. regionIndexes[f.factor] = fi.index;
  167. sumValue += fi.sumValue * f.weight;
  168. sumWeight += fi.sumWeight;
  169. });
  170. regionIndexes.aggregate = sumValue / sumWeight;
  171. resData.push(regionIndexes);
  172. });
  173. helpers.formatResponse(resData, req, res);
  174. }
  175. function returnRegionScores(nuts, req, res) {
  176. var found = false;
  177. res.header("Content-Type", 'application/json');
  178. _ruralData.forEach(region => {
  179. if (region.nuts == nuts) {
  180. helpers.formatResponse(region, req, res);
  181. found = true;
  182. }
  183. });
  184. if (!found)
  185. // NUTS region not found
  186. res.status(404).send('NUTS region not found.');
  187. }