time_series2.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. constructor(opts) {
  39. this.datapoints = opts.datapoints;
  40. this.label = opts.alias;
  41. this.id = opts.alias;
  42. this.alias = opts.alias;
  43. this.color = opts.color;
  44. this.valueFormater = kbn.valueFormats.none;
  45. this.stats = {};
  46. this.legend = true;
  47. }
  48. applySeriesOverrides(overrides) {
  49. this.lines = {};
  50. this.points = {};
  51. this.bars = {};
  52. this.yaxis = 1;
  53. this.zindex = 0;
  54. this.nullPointMode = null;
  55. delete this.stack;
  56. for (var i = 0; i < overrides.length; i++) {
  57. var override = overrides[i];
  58. if (!matchSeriesOverride(override.alias, this.alias)) {
  59. continue;
  60. }
  61. if (override.lines !== void 0) { this.lines.show = override.lines; }
  62. if (override.points !== void 0) { this.points.show = override.points; }
  63. if (override.bars !== void 0) { this.bars.show = override.bars; }
  64. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  65. if (override.stack !== void 0) { this.stack = override.stack; }
  66. if (override.linewidth !== void 0) { this.lines.lineWidth = override.linewidth; }
  67. if (override.nullPointMode !== void 0) { this.nullPointMode = override.nullPointMode; }
  68. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  69. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  70. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  71. if (override.fillBelowTo !== void 0) { this.fillBelowTo = override.fillBelowTo; }
  72. if (override.color !== void 0) { this.color = override.color; }
  73. if (override.transform !== void 0) { this.transform = override.transform; }
  74. if (override.legend !== void 0) { this.legend = override.legend; }
  75. if (override.yaxis !== void 0) {
  76. this.yaxis = override.yaxis;
  77. }
  78. }
  79. };
  80. getFlotPairs(fillStyle) {
  81. var result = [];
  82. this.stats.total = 0;
  83. this.stats.max = -Number.MAX_VALUE;
  84. this.stats.min = Number.MAX_VALUE;
  85. this.stats.avg = null;
  86. this.stats.current = null;
  87. this.allIsNull = true;
  88. this.allIsZero = true;
  89. var ignoreNulls = fillStyle === 'connected';
  90. var nullAsZero = fillStyle === 'null as zero';
  91. var currentTime;
  92. var currentValue;
  93. var nonNulls = 0;
  94. for (var i = 0; i < this.datapoints.length; i++) {
  95. currentValue = this.datapoints[i][0];
  96. currentTime = this.datapoints[i][1];
  97. if (currentValue === null) {
  98. if (ignoreNulls) { continue; }
  99. if (nullAsZero) {
  100. currentValue = 0;
  101. }
  102. }
  103. if (currentValue !== null) {
  104. if (_.isNumber(currentValue)) {
  105. this.stats.total += currentValue;
  106. this.allIsNull = false;
  107. nonNulls++;
  108. }
  109. if (currentValue > this.stats.max) {
  110. this.stats.max = currentValue;
  111. }
  112. if (currentValue < this.stats.min) {
  113. this.stats.min = currentValue;
  114. }
  115. }
  116. if (currentValue !== 0) {
  117. this.allIsZero = false;
  118. }
  119. result.push([currentTime, currentValue]);
  120. }
  121. if (this.datapoints.length >= 2) {
  122. this.stats.timeStep = this.datapoints[1][1] - this.datapoints[0][1];
  123. }
  124. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  125. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  126. if (result.length) {
  127. this.stats.avg = (this.stats.total / nonNulls);
  128. this.stats.current = result[result.length-1][1];
  129. if (this.stats.current === null && result.length > 1) {
  130. this.stats.current = result[result.length-2][1];
  131. }
  132. }
  133. this.stats.count = result.length;
  134. return result;
  135. }
  136. updateLegendValues(formater, decimals, scaledDecimals) {
  137. this.valueFormater = formater;
  138. this.decimals = decimals;
  139. this.scaledDecimals = scaledDecimals;
  140. }
  141. formatValue(value) {
  142. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  143. }
  144. }