timeSeries.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Libraries
  2. import _ from 'lodash';
  3. // Utils
  4. import colors from 'app/core/utils/colors';
  5. // Types
  6. import { TimeSeries, TimeSeriesVMs, NullValueMode } from 'app/types';
  7. interface Options {
  8. timeSeries: TimeSeries[];
  9. nullValueMode: NullValueMode;
  10. }
  11. export function getTimeSeriesVMs({ timeSeries, nullValueMode }: Options): TimeSeriesVMs {
  12. const vmSeries = timeSeries.map((item, index) => {
  13. const colorIndex = index % colors.length;
  14. const label = item.target;
  15. const result = [];
  16. // stat defaults
  17. let total = 0;
  18. let max = -Number.MAX_VALUE;
  19. let min = Number.MAX_VALUE;
  20. let logmin = Number.MAX_VALUE;
  21. let avg = null;
  22. let current = null;
  23. let first = null;
  24. let delta = 0;
  25. let diff = null;
  26. let range = null;
  27. let timeStep = Number.MAX_VALUE;
  28. let allIsNull = true;
  29. let allIsZero = true;
  30. const ignoreNulls = nullValueMode === NullValueMode.Ignore;
  31. const nullAsZero = nullValueMode === NullValueMode.AsZero;
  32. let currentTime;
  33. let currentValue;
  34. let nonNulls = 0;
  35. let previousTime;
  36. let previousValue = 0;
  37. let previousDeltaUp = true;
  38. for (let i = 0; i < item.datapoints.length; i++) {
  39. currentValue = item.datapoints[i][0];
  40. currentTime = item.datapoints[i][1];
  41. // Due to missing values we could have different timeStep all along the series
  42. // so we have to find the minimum one (could occur with aggregators such as ZimSum)
  43. if (previousTime !== undefined) {
  44. const currentStep = currentTime - previousTime;
  45. if (currentStep < timeStep) {
  46. timeStep = currentStep;
  47. }
  48. }
  49. previousTime = currentTime;
  50. if (currentValue === null) {
  51. if (ignoreNulls) {
  52. continue;
  53. }
  54. if (nullAsZero) {
  55. currentValue = 0;
  56. }
  57. }
  58. if (currentValue !== null) {
  59. if (_.isNumber(currentValue)) {
  60. total += currentValue;
  61. allIsNull = false;
  62. nonNulls++;
  63. }
  64. if (currentValue > max) {
  65. max = currentValue;
  66. }
  67. if (currentValue < min) {
  68. min = currentValue;
  69. }
  70. if (first === null) {
  71. first = currentValue;
  72. } else {
  73. if (previousValue > currentValue) {
  74. // counter reset
  75. previousDeltaUp = false;
  76. if (i === item.datapoints.length - 1) {
  77. // reset on last
  78. delta += currentValue;
  79. }
  80. } else {
  81. if (previousDeltaUp) {
  82. delta += currentValue - previousValue; // normal increment
  83. } else {
  84. delta += currentValue; // account for counter reset
  85. }
  86. previousDeltaUp = true;
  87. }
  88. }
  89. previousValue = currentValue;
  90. if (currentValue < logmin && currentValue > 0) {
  91. logmin = currentValue;
  92. }
  93. if (currentValue !== 0) {
  94. allIsZero = false;
  95. }
  96. }
  97. result.push([currentTime, currentValue]);
  98. }
  99. if (max === -Number.MAX_VALUE) {
  100. max = null;
  101. }
  102. if (min === Number.MAX_VALUE) {
  103. min = null;
  104. }
  105. if (result.length && !allIsNull) {
  106. avg = total / nonNulls;
  107. current = result[result.length - 1][1];
  108. if (current === null && result.length > 1) {
  109. current = result[result.length - 2][1];
  110. }
  111. }
  112. if (max !== null && min !== null) {
  113. range = max - min;
  114. }
  115. if (current !== null && first !== null) {
  116. diff = current - first;
  117. }
  118. const count = result.length;
  119. return {
  120. data: result,
  121. label: label,
  122. color: colors[colorIndex],
  123. stats: {
  124. total,
  125. min,
  126. max,
  127. current,
  128. logmin,
  129. avg,
  130. diff,
  131. delta,
  132. timeStep,
  133. range,
  134. count,
  135. first,
  136. allIsZero,
  137. allIsNull,
  138. },
  139. };
  140. });
  141. return vmSeries;
  142. }