processTimeSeries.ts 4.2 KB

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