index.js 6.5 KB

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