fork download
  1. const channelsResponse = [
  2. {
  3. id: 372,
  4. name: 'Channel 01',
  5. lab: 'Fullerton',
  6. tests: [3, 4, 7, 8],
  7. },
  8. {
  9. id: 373,
  10. name: 'Channel 02',
  11. lab: 'Fullerton',
  12. tests: [1, 2, 5, 6],
  13. },
  14. {
  15. id: 374,
  16. name: 'Beta Channel',
  17. lab: 'Fullerton',
  18. tests: [],
  19. },
  20. {
  21. id: 472,
  22. name: 'Channel 01',
  23. lab: 'Queens',
  24. tests: [9, 11, 12, 13],
  25. },
  26. {
  27. id: 473,
  28. name: 'Channel 02',
  29. lab: 'Queens',
  30. tests: [15, 17, 19],
  31. },
  32. {
  33. id: 474,
  34. name: 'Channel 03',
  35. lab: 'Queens',
  36. tests: [21, 22, 24, 25],
  37. },
  38. {
  39. id: 475,
  40. name: 'Channel 04',
  41. lab: 'Queens',
  42. tests: [26, 27, 28, 29, 30],
  43. },
  44. ];
  45.  
  46. const testsResponse = [
  47. {
  48. id: 1,
  49. start: '2019-11-05T11:05:00Z',
  50. end: '2019-11-05T13:05:00Z',
  51. },
  52. {
  53. id: 2,
  54. start: '2019-11-06T11:05:00Z',
  55. end: '2019-11-06T13:05:00Z',
  56. },
  57. {
  58. id: 3,
  59. start: '2019-11-04T11:05:00Z',
  60. end: '2019-11-04T13:09:00Z',
  61. },
  62. {
  63. id: 4,
  64. start: '2019-11-04T17:00:00Z',
  65. end: '2019-11-05T09:32:00Z',
  66. },
  67. {
  68. id: 5,
  69. start: '2019-11-11T11:05:00Z',
  70. end: '2019-11-12T13:05:00Z',
  71. },
  72. {
  73. id: 6,
  74. start: '2019-11-12T14:05:00Z',
  75. end: '2019-11-15T13:05:00Z',
  76. },
  77. {
  78. id: 7,
  79. start: '2019-11-07T11:05:00Z',
  80. end: '2019-11-08T13:05:00Z',
  81. },
  82. {
  83. id: 8,
  84. start: '2019-11-08T15:05:00Z',
  85. end: '2019-11-08T15:35:00Z',
  86. },
  87. {
  88. id: 9,
  89. start: '2019-11-05T09:05:00Z',
  90. end: '2019-11-08T12:05:00Z',
  91. },
  92. {
  93. id: 11,
  94. start: '2019-11-08T12:35:00Z',
  95. end: '2019-11-08T13:35:00Z',
  96. },
  97. {
  98. id: 12,
  99. start: '2019-11-08T17:00:00Z',
  100. end: '2019-11-11T10:00:00Z',
  101. },
  102. {
  103. id: 13,
  104. start: '2019-11-11T12:00:00Z',
  105. end: null,
  106. },
  107. {id: 15},
  108. {
  109. id: 17,
  110. start: '2019-11-05T17:00:00Z',
  111. end: '2019-11-06T10:00:00Z',
  112. },
  113. {
  114. id: 19,
  115. start: '2019-11-06T12:00:00Z',
  116. end: '2019-11-06T13:22:00Z',
  117. },
  118. {
  119. id: 21,
  120. start: '2019-11-05T09:05:00Z',
  121. end: '2019-11-06T12:05:00Z',
  122. },
  123. {
  124. id: 22,
  125. start: '2019-11-08T12:35:00Z',
  126. end: '2019-11-08T13:35:00Z',
  127. },
  128. {
  129. id: 24,
  130. start: '2019-11-11T17:00:00Z',
  131. end: '2019-11-15T10:00:00Z',
  132. },
  133. {
  134. id: 25,
  135. start: '2019-11-15T12:00:00Z',
  136. },
  137. {
  138. id: 26,
  139. start: '2019-11-05T09:05:00Z',
  140. end: '2019-11-06T12:05:00Z',
  141. },
  142. {
  143. id: 27,
  144. start: '2019-11-07T12:35:00Z',
  145. end: '2019-11-07T13:35:00Z',
  146. },
  147. {
  148. id: 28,
  149. start: '2019-11-08T17:00:00Z',
  150. end: '2019-11-11T10:00:00Z',
  151. },
  152. {
  153. id: 29,
  154. start: '2019-11-12T12:00:00Z',
  155. end: '2019-11-12T14:00:00Z',
  156. },
  157. {
  158. id: 30,
  159. start: '2019-11-13T12:00:00Z',
  160. end: '2019-11-13T14:00:00Z',
  161. },
  162. ];
  163.  
  164. const expectedChannelUtilizations = [
  165. {
  166. date: '2019-11-04T00:00:00.000Z',
  167. utilizationPercentage: 14,
  168. },
  169. {
  170. date: '2019-11-05T00:00:00.000Z',
  171. utilizationPercentage: 86,
  172. },
  173. {
  174. date: '2019-11-06T00:00:00.000Z',
  175. utilizationPercentage: 71,
  176. },
  177. {
  178. date: '2019-11-07T00:00:00.000Z',
  179. utilizationPercentage: 43,
  180. },
  181. {
  182. date: '2019-11-08T00:00:00.000Z',
  183. utilizationPercentage: 57,
  184. },
  185. {
  186. date: '2019-11-09T00:00:00.000Z',
  187. utilizationPercentage: 29,
  188. },
  189. {
  190. date: '2019-11-10T00:00:00.000Z',
  191. utilizationPercentage: 29,
  192. },
  193. {
  194. date: '2019-11-11T00:00:00.000Z',
  195. utilizationPercentage: 57,
  196. },
  197. {
  198. date: '2019-11-12T00:00:00.000Z',
  199. utilizationPercentage: 57,
  200. },
  201. {
  202. date: '2019-11-13T00:00:00.000Z',
  203. utilizationPercentage: 57,
  204. },
  205. {
  206. date: '2019-11-14T00:00:00.000Z',
  207. utilizationPercentage: 43,
  208. },
  209. {
  210. date: '2019-11-15T00:00:00.000Z',
  211. utilizationPercentage: 43,
  212. },
  213. ];
  214.  
  215. function cl(data) { console.log(JSON.stringify(data)); }
  216.  
  217. // channelsResponse & testsResponse
  218. const getThem = () => {
  219. let max='';
  220. let min=new Date().toISOString();
  221. testsResponse.forEach(test => {
  222. min=test.start && min.localeCompare(test.start)>0?test.start:min;
  223. max=test.end && max.localeCompare(test.end)<0?test.end:max;
  224. });
  225.  
  226. min=new Date(min.substr(0,10)+'T00:00:00.000Z');
  227. max=new Date(max.substr(0,10)+'T00:00:00.000Z');
  228.  
  229. let results = [];
  230. for(var i=min; i<=max; i.setDate(i.getDate()+1)) {
  231. let p=0;
  232. let d=i.toISOString().substr(0,10);
  233.  
  234. const testIds = testsResponse.filter(t => d.localeCompare((t.start+'').substr(0,10))>=0 && d.localeCompare((t.end+'').substr(0,10))<=0).map(t => t.id);
  235. const channels = channelsResponse.filter(c => c.tests.some(t => testIds.includes(t)));
  236.  
  237. p=Math.round(channels.length*100.0/channelsResponse.length);
  238. results.push({date: i.toISOString(), utilizationPercentage: p})
  239. }
  240. return results;
  241. };
  242.  
  243. cl(getThem());
  244.  
  245.  
Success #stdin #stdout 0.05s 22052KB
stdin
Standard input is empty
stdout
[{"date":"2019-11-04T00:00:00.000Z","utilizationPercentage":14},{"date":"2019-11-05T00:00:00.000Z","utilizationPercentage":86},{"date":"2019-11-06T00:00:00.000Z","utilizationPercentage":71},{"date":"2019-11-07T00:00:00.000Z","utilizationPercentage":43},{"date":"2019-11-08T00:00:00.000Z","utilizationPercentage":57},{"date":"2019-11-09T00:00:00.000Z","utilizationPercentage":29},{"date":"2019-11-10T00:00:00.000Z","utilizationPercentage":29},{"date":"2019-11-11T00:00:00.000Z","utilizationPercentage":57},{"date":"2019-11-12T00:00:00.000Z","utilizationPercentage":57},{"date":"2019-11-13T00:00:00.000Z","utilizationPercentage":57},{"date":"2019-11-14T00:00:00.000Z","utilizationPercentage":43},{"date":"2019-11-15T00:00:00.000Z","utilizationPercentage":43}]