processTimeSeries.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Libraries
  2. import isNumber from 'lodash/isNumber';
  3. import { colors } from './colors';
  4. // Types
  5. import { TimeSeriesVMs, NullValueMode, TimeSeriesValue, TableData } from '../types';
  6. interface Options {
  7. data: TableData[];
  8. xColumn?: number; // Time (or null to guess)
  9. yColumn?: number; // Value (or null to guess)
  10. nullValueMode: NullValueMode;
  11. }
  12. // NOTE: this should move to processTableData.ts
  13. // I left it as is so the merge changes are more clear.
  14. export function processTimeSeries({ data, xColumn, yColumn, nullValueMode }: Options): TimeSeriesVMs {
  15. const vmSeries = data.map((item, index) => {
  16. if (!isNumber(xColumn)) {
  17. xColumn = 1; // Default timeseries colum. TODO, find first time field!
  18. }
  19. if (!isNumber(yColumn)) {
  20. yColumn = 0; // TODO, find first non-time field
  21. }
  22. // TODO? either % or throw error?
  23. if (xColumn >= item.columns.length) {
  24. throw new Error('invalid colum: ' + xColumn);
  25. }
  26. if (yColumn >= item.columns.length) {
  27. throw new Error('invalid colum: ' + yColumn);
  28. }
  29. const colorIndex = index % colors.length;
  30. const label = item.columns[yColumn].text;
  31. const result = [];
  32. // stat defaults
  33. let total = 0;
  34. let max: TimeSeriesValue = -Number.MAX_VALUE;
  35. let min: TimeSeriesValue = Number.MAX_VALUE;
  36. let logmin = Number.MAX_VALUE;
  37. let avg: TimeSeriesValue = null;
  38. let current: TimeSeriesValue = null;
  39. let first: TimeSeriesValue = null;
  40. let delta: TimeSeriesValue = 0;
  41. let diff: TimeSeriesValue = null;
  42. let range: TimeSeriesValue = null;
  43. let timeStep = Number.MAX_VALUE;
  44. let allIsNull = true;
  45. let allIsZero = true;
  46. const ignoreNulls = nullValueMode === NullValueMode.Ignore;
  47. const nullAsZero = nullValueMode === NullValueMode.AsZero;
  48. let currentTime: TimeSeriesValue = null;
  49. let currentValue: TimeSeriesValue = null;
  50. let nonNulls = 0;
  51. let previousTime: TimeSeriesValue = null;
  52. let previousValue = 0;
  53. let previousDeltaUp = true;
  54. for (let i = 0; i < item.rows.length; i++) {
  55. currentValue = item.rows[i][yColumn];
  56. currentTime = item.rows[i][xColumn];
  57. if (typeof currentTime !== 'number') {
  58. continue;
  59. }
  60. if (currentValue !== null && typeof currentValue !== 'number') {
  61. throw { message: 'Time series contains non number values' };
  62. }
  63. // Due to missing values we could have different timeStep all along the series
  64. // so we have to find the minimum one (could occur with aggregators such as ZimSum)
  65. if (previousTime !== null && currentTime !== null) {
  66. const currentStep = currentTime - previousTime;
  67. if (currentStep < timeStep) {
  68. timeStep = currentStep;
  69. }
  70. }
  71. previousTime = currentTime;
  72. if (currentValue === null) {
  73. if (ignoreNulls) {
  74. continue;
  75. }
  76. if (nullAsZero) {
  77. currentValue = 0;
  78. }
  79. }
  80. if (currentValue !== null) {
  81. if (isNumber(currentValue)) {
  82. total += currentValue;
  83. allIsNull = false;
  84. nonNulls++;
  85. }
  86. if (currentValue > max) {
  87. max = currentValue;
  88. }
  89. if (currentValue < min) {
  90. min = currentValue;
  91. }
  92. if (first === null) {
  93. first = currentValue;
  94. } else {
  95. if (previousValue > currentValue) {
  96. // counter reset
  97. previousDeltaUp = false;
  98. if (i === item.rows.length - 1) {
  99. // reset on last
  100. delta += currentValue;
  101. }
  102. } else {
  103. if (previousDeltaUp) {
  104. delta += currentValue - previousValue; // normal increment
  105. } else {
  106. delta += currentValue; // account for counter reset
  107. }
  108. previousDeltaUp = true;
  109. }
  110. }
  111. previousValue = currentValue;
  112. if (currentValue < logmin && currentValue > 0) {
  113. logmin = currentValue;
  114. }
  115. if (currentValue !== 0) {
  116. allIsZero = false;
  117. }
  118. }
  119. result.push([currentTime, currentValue]);
  120. }
  121. if (max === -Number.MAX_VALUE) {
  122. max = null;
  123. }
  124. if (min === Number.MAX_VALUE) {
  125. min = null;
  126. }
  127. if (result.length && !allIsNull) {
  128. avg = total / nonNulls;
  129. current = result[result.length - 1][1];
  130. if (current === null && result.length > 1) {
  131. current = result[result.length - 2][1];
  132. }
  133. }
  134. if (max !== null && min !== null) {
  135. range = max - min;
  136. }
  137. if (current !== null && first !== null) {
  138. diff = current - first;
  139. }
  140. const count = result.length;
  141. return {
  142. data: result,
  143. label: label,
  144. color: colors[colorIndex],
  145. allIsZero,
  146. allIsNull,
  147. stats: {
  148. total,
  149. min,
  150. max,
  151. current,
  152. logmin,
  153. avg,
  154. diff,
  155. delta,
  156. timeStep,
  157. range,
  158. count,
  159. first,
  160. },
  161. };
  162. });
  163. return vmSeries;
  164. }