Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. async getLatestData(versionId: number) {
  2. const models = await this.modelRepository.find({ cache: true });
  3. const predictions = await this.predictions.getPredictions(versionId);
  4. const factsList = await Promise.all(
  5. [...models.map(m => this.getFactData(m.modelId)),
  6. ].map(p => p.catch(() => null)),
  7. );
  8.  
  9. const results: any[] = [];
  10. models.forEach((model: Model, i: number) => {
  11. const predictValues = predictions
  12. .filter(p => p.model.modelId === model.modelId)
  13. .map(p => ({
  14. timestamp: p.predictDt.formatToNumber(),
  15. value: p.predictValue,
  16. }));
  17. const factsValues = factsList[i] ? factsList[i].values.map((f => ({
  18. ...f,
  19. timestamp: new AppDate(f.timestamp).formatToNumber(),
  20. }))) : null;
  21.  
  22. const firstPredict = predictValues[0] || null;
  23. const firstPredictDt = firstPredict ? moment(firstPredict.timestamp) : null;
  24. const lastPredict = predictValues[predictValues.length - 1] || null;
  25. const lastPredictDt = lastPredict ? moment(lastPredict.timestamp) : null;
  26.  
  27. let maxDate = factsValues && factsValues.length ? moment(factsValues[factsValues.length - 1].timestamp) : null;
  28. let minDate = factsValues && factsValues.length ? moment(factsValues[0].timestamp) : null;
  29.  
  30. if (firstPredictDt && firstPredictDt.isAfter(minDate) && firstPredictDt.isSameOrBefore(minDate)) {
  31. minDate = firstPredictDt;
  32. }
  33.  
  34. if (lastPredictDt && lastPredictDt.isBefore(maxDate) && lastPredictDt.isSameOrAfter(minDate)) {
  35. maxDate = lastPredictDt;
  36. }
  37.  
  38. const predicts = maxDate && minDate && predictValues ? predictValues.filter(p => {
  39. const dt = +new Date(p.timestamp);
  40. return dt >= minDate.valueOf() && dt <= +maxDate.valueOf();
  41. }) : [];
  42.  
  43. const facts = maxDate && minDate && factsValues ? factsValues.filter(f => {
  44. const dt = +new Date(f.timestamp);
  45. return dt >= minDate.valueOf() && dt <= maxDate.valueOf();
  46. }) : [];
  47.  
  48. const lastPredictValue = predicts[predicts.length - 1];
  49. const lastFactValue = facts[facts.length - 1];
  50.  
  51. results.push({
  52. model: {
  53. id: model.modelId,
  54. name: model.modelName,
  55. unit: model.modelUnit,
  56. },
  57. fact: lastFactValue,
  58. prediction: lastPredictValue,
  59. factAverage: lastFactValue
  60. ? getAverageValueByPeriod(
  61. facts,
  62. moment(lastFactValue.timestamp)
  63. .subtract(1, 'days')
  64. .valueOf(),
  65. )
  66. : null,
  67. predictionAverage: lastPredictValue
  68. ? getAverageValueByPeriod(
  69. predicts,
  70. moment(lastPredictValue.timestamp)
  71. .subtract(1, 'days')
  72. .valueOf(),
  73. )
  74. : null,
  75. });
  76. });
  77. return results;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement