time_series.ts 4.6 KB

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