time_series2.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.allIsNull = true;
  93. this.allIsZero = true;
  94. var ignoreNulls = fillStyle === 'connected';
  95. var nullAsZero = fillStyle === 'null as zero';
  96. var currentTime;
  97. var currentValue;
  98. var nonNulls = 0;
  99. for (var i = 0; i < this.datapoints.length; i++) {
  100. currentValue = this.datapoints[i][0];
  101. currentTime = this.datapoints[i][1];
  102. if (currentValue === null) {
  103. if (ignoreNulls) { continue; }
  104. if (nullAsZero) {
  105. currentValue = 0;
  106. }
  107. }
  108. if (currentValue !== null) {
  109. if (_.isNumber(currentValue)) {
  110. this.stats.total += currentValue;
  111. this.allIsNull = false;
  112. nonNulls++;
  113. }
  114. if (currentValue > this.stats.max) {
  115. this.stats.max = currentValue;
  116. }
  117. if (currentValue < this.stats.min) {
  118. this.stats.min = currentValue;
  119. }
  120. }
  121. if (currentValue !== 0) {
  122. this.allIsZero = false;
  123. }
  124. result.push([currentTime, currentValue]);
  125. }
  126. if (this.datapoints.length >= 2) {
  127. this.stats.timeStep = this.datapoints[1][1] - this.datapoints[0][1];
  128. }
  129. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  130. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  131. if (result.length) {
  132. this.stats.avg = (this.stats.total / nonNulls);
  133. this.stats.current = result[result.length-1][1];
  134. if (this.stats.current === null && result.length > 1) {
  135. this.stats.current = result[result.length-2][1];
  136. }
  137. }
  138. this.stats.count = result.length;
  139. return result;
  140. }
  141. updateLegendValues(formater, decimals, scaledDecimals) {
  142. this.valueFormater = formater;
  143. this.decimals = decimals;
  144. this.scaledDecimals = scaledDecimals;
  145. }
  146. formatValue(value) {
  147. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  148. }
  149. isMsResolutionNeeded() {
  150. for (var i = 0; i < this.datapoints.length; i++) {
  151. if (this.datapoints[i][1] !== null) {
  152. var timestamp = this.datapoints[i][1].toString();
  153. if (timestamp.length === 13 && (timestamp % 1000) !== 0) {
  154. return true;
  155. }
  156. }
  157. }
  158. return false;
  159. }
  160. hideFromLegend(options) {
  161. if (options.hideEmpty && this.allIsNull) {
  162. return true;
  163. }
  164. // ignore series excluded via override
  165. if (!this.legend) {
  166. return true;
  167. }
  168. // ignore zero series
  169. if (options.hideZero && this.allIsZero) {
  170. return true;
  171. }
  172. return false;
  173. }
  174. }