time_series.ts 4.4 KB

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