adjuster.service.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // import attractivity from '../Attractivity.json';
  2. import nuts from '../nuts.js';
  3. export default function (HsCore, HsUtilsService, $rootScope, $http) {
  4. 'ngInject';
  5. const me = {
  6. factors: [
  7. {
  8. name: 'Natural',
  9. column: 'N_index',
  10. weight: 1,
  11. },
  12. {
  13. name: 'Social & Human',
  14. column: 'S_index',
  15. weight: 1,
  16. },
  17. {
  18. name: 'Anthropic',
  19. column: 'A_index',
  20. weight: 1,
  21. },
  22. {
  23. name: 'Economical',
  24. column: 'E_index',
  25. weight: 1,
  26. },
  27. {
  28. name: 'Cultural',
  29. column: 'C_index',
  30. weight: 1,
  31. },
  32. {
  33. name: 'Institutional',
  34. column: 'I_index',
  35. weight: 1,
  36. },
  37. ],
  38. apply() {
  39. HsUtilsService.debounce(function () {
  40. $http({
  41. method: 'post',
  42. url: 'https://publish.lesprojekt.cz/nodejs/scores',
  43. data: {
  44. factors: me.factors.map((f) => {
  45. return {
  46. factor: f.factor,
  47. weight: f.weight,
  48. datasets: f.datasets
  49. .filter((ds) => ds.included)
  50. .map((ds) => ds.name),
  51. };
  52. }),
  53. },
  54. }).then((response) => {
  55. me.attractivity = response.data;
  56. let max = 0;
  57. me.attractivity.forEach((a) => {
  58. if (a.aggregate > max) {
  59. max = a.aggregate;
  60. }
  61. });
  62. const normalizer = 1 / max;
  63. me.attractivity.forEach((a) => {
  64. a.aggregate *= normalizer;
  65. });
  66. me.attractivity.forEach((a) => {
  67. me.nutsCodeRecordRelations[a.code] = a;
  68. });
  69. nuts.nuts3Source.forEachFeature((feature) => {
  70. feature.set(
  71. 'total',
  72. me.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate
  73. );
  74. feature.set(
  75. 'totalForHumans',
  76. (
  77. me.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate *
  78. 100
  79. ).toFixed(2)
  80. );
  81. me.factors.forEach((factor) => {
  82. feature.set(
  83. factor.factor,
  84. (
  85. me.nutsCodeRecordRelations[feature.get('NUTS_ID')][
  86. factor.factor
  87. ] * 100
  88. ).toFixed(2)
  89. );
  90. });
  91. });
  92. });
  93. /*
  94. */
  95. }, 300)();
  96. },
  97. };
  98. me.nutsCodeRecordRelations = {};
  99. $http({url: 'https://publish.lesprojekt.cz/nodejs/datasets'}).then(
  100. (response) => {
  101. me.factors = response.data.map((dataset) => {
  102. return {factor: dataset.Factor, weight: 1, datasets: []};
  103. });
  104. me.factors = HsUtilsService.removeDuplicates(me.factors, 'factor');
  105. me.factors.forEach((factor) => {
  106. factor.datasets = response.data
  107. .filter((ds) => ds.Factor === factor.factor)
  108. .map((ds) => {
  109. return {
  110. name: ds.Name,
  111. included: true,
  112. };
  113. });
  114. });
  115. me.apply();
  116. }
  117. );
  118. return me;
  119. }