index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /*
  116. Only testing purposes
  117. */
  118. app.get('/runR', (req, res, next) => {
  119. //console.log(console);
  120. console.log('calling R...')
  121. R('./r/selected_data.r').call(
  122. function(err, data) {
  123. console.log('R done');
  124. if (err) {
  125. console.log(err.toString('utf8'));
  126. data = { result: err.toString('utf8') };
  127. }
  128. else {
  129. console.log(data);
  130. data = { result: 'R call succesful' };
  131. }
  132. helpers.formatResponse({ response: data }, req, res);
  133. }
  134. );
  135. });
  136. /*
  137. Calls R script, loads the resulting CSV file and returns it
  138. */
  139. app.get('/clusters', (req, res, next) => {
  140. //console.log(console);
  141. console.log('calling R...')
  142. R('./r/selected_data.r').call(
  143. function(err, data) {
  144. console.log('R done');
  145. if (err) {
  146. console.log(err.toString('utf8'));
  147. data = { result: err.toString('utf8') };
  148. }
  149. else {
  150. console.log(data);
  151. nutsData.loadClusters(_clustersFilePath, function(clusterData) {
  152. data = clusterData;
  153. helpers.formatResponse({ response: data }, req, res);
  154. });
  155. }
  156. }
  157. );
  158. });
  159. // start the service on the port xxxx
  160. app.listen(3000, () => console.log('Rural attractivity WS listening on port 3000...'));
  161. function returnAllScores(req, res) {
  162. var resData = [];
  163. _ruralData.forEach(region => {
  164. //var region = _ruralData[0];
  165. var sumWeight = 0;
  166. var sumValue = 0;
  167. let regionIndexes = { code: region.nuts };
  168. req.body.factors.forEach(f => {
  169. let fi = nutsData.getFactorIndex(region, f);
  170. //console.log("f: " + JSON.stringify(f));
  171. //console.log("fi: " + JSON.stringify(fi));
  172. regionIndexes[f.factor] = fi.index;
  173. sumValue += fi.sumValue * f.weight;
  174. sumWeight += fi.sumWeight;
  175. });
  176. regionIndexes.aggregate = sumValue / sumWeight;
  177. resData.push(regionIndexes);
  178. });
  179. helpers.formatResponse(resData, req, res);
  180. }
  181. function returnRegionScores(nuts, req, res) {
  182. var found = false;
  183. res.header("Content-Type", 'application/json');
  184. _ruralData.forEach(region => {
  185. if (region.nuts == nuts) {
  186. helpers.formatResponse(region, req, res);
  187. found = true;
  188. }
  189. });
  190. if (!found)
  191. // NUTS region not found
  192. res.status(404).send('NUTS region not found.');
  193. }