time_series2.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ///<reference path="../headers/common.d.ts" />
  2. import kbn from 'app/core/utils/kbn';
  3. import _ from 'lodash';
  4. function matchSeriesOverride(aliasOrRegex, seriesAlias) {
  5. if (!aliasOrRegex) { return false; }
  6. if (aliasOrRegex[0] === '/') {
  7. var regex = kbn.stringToJsRegex(aliasOrRegex);
  8. return seriesAlias.match(regex) != null;
  9. }
  10. return aliasOrRegex === seriesAlias;
  11. }
  12. function translateFillOption(fill) {
  13. return fill === 0 ? 0.001 : fill/10;
  14. }
  15. export default class TimeSeries {
  16. datapoints: any;
  17. id: string;
  18. label: string;
  19. alias: string;
  20. color: string;
  21. valueFormater: any;
  22. stats: any;
  23. legend: boolean;
  24. allIsNull: boolean;
  25. allIsZero: boolean;
  26. decimals: number;
  27. scaledDecimals: number;
  28. hasMsResolution: boolean;
  29. isOutsideRange: boolean;
  30. lines: any;
  31. bars: any;
  32. points: any;
  33. yaxis: any;
  34. zindex: any;
  35. stack: any;
  36. nullPointMode: any;
  37. fillBelowTo: any;
  38. transform: any;
  39. flotpairs: any;
  40. unit: any;
  41. constructor(opts) {
  42. this.datapoints = opts.datapoints;
  43. this.label = opts.alias;
  44. this.id = opts.alias;
  45. this.alias = opts.alias;
  46. this.color = opts.color;
  47. this.valueFormater = kbn.valueFormats.none;
  48. this.stats = {};
  49. this.legend = true;
  50. this.unit = opts.unit;
  51. this.hasMsResolution = this.isMsResolutionNeeded();
  52. }
  53. applySeriesOverrides(overrides) {
  54. this.lines = {};
  55. this.points = {};
  56. this.bars = {};
  57. this.yaxis = 1;
  58. this.zindex = 0;
  59. this.nullPointMode = null;
  60. delete this.stack;
  61. for (var i = 0; i < overrides.length; i++) {
  62. var override = overrides[i];
  63. if (!matchSeriesOverride(override.alias, this.alias)) {
  64. continue;
  65. }
  66. if (override.lines !== void 0) { this.lines.show = override.lines; }
  67. if (override.points !== void 0) { this.points.show = override.points; }
  68. if (override.bars !== void 0) { this.bars.show = override.bars; }
  69. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  70. if (override.stack !== void 0) { this.stack = override.stack; }
  71. if (override.linewidth !== void 0) { this.lines.lineWidth = override.linewidth; }
  72. if (override.nullPointMode !== void 0) { this.nullPointMode = override.nullPointMode; }
  73. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  74. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  75. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  76. if (override.fillBelowTo !== void 0) { this.fillBelowTo = override.fillBelowTo; }
  77. if (override.color !== void 0) { this.color = override.color; }
  78. if (override.transform !== void 0) { this.transform = override.transform; }
  79. if (override.legend !== void 0) { this.legend = override.legend; }
  80. if (override.yaxis !== void 0) {
  81. this.yaxis = override.yaxis;
  82. }
  83. }
  84. };
  85. getFlotPairs(fillStyle) {
  86. var result = [];
  87. this.stats.total = 0;
  88. this.stats.max = -Number.MAX_VALUE;
  89. this.stats.min = Number.MAX_VALUE;
  90. this.stats.avg = null;
  91. this.stats.current = null;
  92. this.stats.first = null;
  93. this.stats.delta = 0;
  94. this.stats.range = null;
  95. this.stats.timeStep = Number.MAX_VALUE;
  96. this.allIsNull = true;
  97. this.allIsZero = true;
  98. var ignoreNulls = fillStyle === 'connected';
  99. var nullAsZero = fillStyle === 'null as zero';
  100. var currentTime;
  101. var currentValue;
  102. var nonNulls = 0;
  103. var previousTime;
  104. var previousValue = 0;
  105. var previousDeltaUp = true;
  106. for (var i = 0; i < this.datapoints.length; i++) {
  107. currentValue = this.datapoints[i][0];
  108. currentTime = this.datapoints[i][1];
  109. // Due to missing values we could have different timeStep all along the series
  110. // so we have to find the minimum one (could occur with aggregators such as ZimSum)
  111. if (previousTime !== undefined) {
  112. let timeStep = currentTime - previousTime;
  113. if (timeStep < this.stats.timeStep) {
  114. this.stats.timeStep = timeStep;
  115. }
  116. }
  117. previousTime = currentTime;
  118. if (currentValue === null) {
  119. if (ignoreNulls) { continue; }
  120. if (nullAsZero) {
  121. currentValue = 0;
  122. }
  123. }
  124. if (currentValue !== null) {
  125. if (_.isNumber(currentValue)) {
  126. this.stats.total += currentValue;
  127. this.allIsNull = false;
  128. nonNulls++;
  129. }
  130. if (currentValue > this.stats.max) {
  131. this.stats.max = currentValue;
  132. }
  133. if (currentValue < this.stats.min) {
  134. this.stats.min = currentValue;
  135. }
  136. if (this.stats.first === null){
  137. this.stats.first = currentValue;
  138. }else{
  139. if (previousValue > currentValue) { // counter reset
  140. previousDeltaUp = false;
  141. if (i === this.datapoints.length-1) { // reset on last
  142. this.stats.delta += currentValue;
  143. }
  144. }else{
  145. if (previousDeltaUp) {
  146. this.stats.delta += currentValue - previousValue; // normal increment
  147. } else {
  148. this.stats.delta += currentValue; // account for counter reset
  149. }
  150. previousDeltaUp = true;
  151. }
  152. }
  153. previousValue = currentValue;
  154. }
  155. if (currentValue !== 0) {
  156. this.allIsZero = false;
  157. }
  158. result.push([currentTime, currentValue]);
  159. }
  160. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  161. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  162. if (result.length) {
  163. this.stats.avg = (this.stats.total / nonNulls);
  164. this.stats.current = result[result.length-1][1];
  165. if (this.stats.current === null && result.length > 1) {
  166. this.stats.current = result[result.length-2][1];
  167. }
  168. }
  169. if (this.stats.max !== null && this.stats.min !== null) {
  170. this.stats.range = this.stats.max - this.stats.min;
  171. }
  172. this.stats.count = result.length;
  173. return result;
  174. }
  175. updateLegendValues(formater, decimals, scaledDecimals) {
  176. this.valueFormater = formater;
  177. this.decimals = decimals;
  178. this.scaledDecimals = scaledDecimals;
  179. }
  180. formatValue(value) {
  181. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  182. }
  183. isMsResolutionNeeded() {
  184. for (var i = 0; i < this.datapoints.length; i++) {
  185. if (this.datapoints[i][1] !== null) {
  186. var timestamp = this.datapoints[i][1].toString();
  187. if (timestamp.length === 13 && (timestamp % 1000) !== 0) {
  188. return true;
  189. }
  190. }
  191. }
  192. return false;
  193. }
  194. hideFromLegend(options) {
  195. if (options.hideEmpty && this.allIsNull) {
  196. return true;
  197. }
  198. // ignore series excluded via override
  199. if (!this.legend) {
  200. return true;
  201. }
  202. // ignore zero series
  203. if (options.hideZero && this.allIsZero) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. }