time_series2.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. lines: any;
  29. bars: any;
  30. points: any;
  31. yaxis: any;
  32. zindex: any;
  33. stack: any;
  34. nullPointMode: any;
  35. fillBelowTo: any;
  36. transform: any;
  37. flotpairs: any;
  38. unit: any;
  39. constructor(opts) {
  40. this.datapoints = opts.datapoints;
  41. this.label = opts.alias;
  42. this.id = opts.alias;
  43. this.alias = opts.alias;
  44. this.color = opts.color;
  45. this.valueFormater = kbn.valueFormats.none;
  46. this.stats = {};
  47. this.legend = true;
  48. this.unit = opts.unit;
  49. }
  50. applySeriesOverrides(overrides) {
  51. this.lines = {};
  52. this.points = {};
  53. this.bars = {};
  54. this.yaxis = 1;
  55. this.zindex = 0;
  56. this.nullPointMode = null;
  57. delete this.stack;
  58. for (var i = 0; i < overrides.length; i++) {
  59. var override = overrides[i];
  60. if (!matchSeriesOverride(override.alias, this.alias)) {
  61. continue;
  62. }
  63. if (override.lines !== void 0) { this.lines.show = override.lines; }
  64. if (override.points !== void 0) { this.points.show = override.points; }
  65. if (override.bars !== void 0) { this.bars.show = override.bars; }
  66. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  67. if (override.stack !== void 0) { this.stack = override.stack; }
  68. if (override.linewidth !== void 0) { this.lines.lineWidth = override.linewidth; }
  69. if (override.nullPointMode !== void 0) { this.nullPointMode = override.nullPointMode; }
  70. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  71. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  72. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  73. if (override.fillBelowTo !== void 0) { this.fillBelowTo = override.fillBelowTo; }
  74. if (override.color !== void 0) { this.color = override.color; }
  75. if (override.transform !== void 0) { this.transform = override.transform; }
  76. if (override.legend !== void 0) { this.legend = override.legend; }
  77. if (override.yaxis !== void 0) {
  78. this.yaxis = override.yaxis;
  79. }
  80. }
  81. };
  82. getFlotPairs(fillStyle) {
  83. var result = [];
  84. this.stats.total = 0;
  85. this.stats.max = -Number.MAX_VALUE;
  86. this.stats.min = Number.MAX_VALUE;
  87. this.stats.avg = null;
  88. this.stats.current = null;
  89. this.allIsNull = true;
  90. this.allIsZero = true;
  91. var ignoreNulls = fillStyle === 'connected';
  92. var nullAsZero = fillStyle === 'null as zero';
  93. var currentTime;
  94. var currentValue;
  95. var nonNulls = 0;
  96. for (var i = 0; i < this.datapoints.length; i++) {
  97. currentValue = this.datapoints[i][0];
  98. currentTime = this.datapoints[i][1];
  99. if (currentValue === null) {
  100. if (ignoreNulls) { continue; }
  101. if (nullAsZero) {
  102. currentValue = 0;
  103. }
  104. }
  105. if (currentValue !== null) {
  106. if (_.isNumber(currentValue)) {
  107. this.stats.total += currentValue;
  108. this.allIsNull = false;
  109. nonNulls++;
  110. }
  111. if (currentValue > this.stats.max) {
  112. this.stats.max = currentValue;
  113. }
  114. if (currentValue < this.stats.min) {
  115. this.stats.min = currentValue;
  116. }
  117. }
  118. if (currentValue !== 0) {
  119. this.allIsZero = false;
  120. }
  121. result.push([currentTime, currentValue]);
  122. }
  123. if (this.datapoints.length >= 2) {
  124. this.stats.timeStep = this.datapoints[1][1] - this.datapoints[0][1];
  125. }
  126. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  127. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  128. if (result.length) {
  129. this.stats.avg = (this.stats.total / nonNulls);
  130. this.stats.current = result[result.length-1][1];
  131. if (this.stats.current === null && result.length > 1) {
  132. this.stats.current = result[result.length-2][1];
  133. }
  134. }
  135. this.stats.count = result.length;
  136. return result;
  137. }
  138. updateLegendValues(formater, decimals, scaledDecimals) {
  139. this.valueFormater = formater;
  140. this.decimals = decimals;
  141. this.scaledDecimals = scaledDecimals;
  142. }
  143. formatValue(value) {
  144. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  145. }
  146. isMsResolutionNeeded() {
  147. for (var i = 0; i < this.datapoints.length; i++) {
  148. if (this.datapoints[i][1] !== null) {
  149. var timestamp = this.datapoints[i][1].toString();
  150. if (timestamp.length === 13 && (timestamp % 1000) !== 0) {
  151. return true;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. hideFromLegend(options) {
  158. if (options.hideEmpty && this.allIsNull) {
  159. return true;
  160. }
  161. // ignore series excluded via override
  162. if (!this.legend) {
  163. return true;
  164. }
  165. // ignore zero series
  166. if (options.hideZero && this.allIsZero) {
  167. return true;
  168. }
  169. return false;
  170. }
  171. }