timeSeries.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. define([
  2. 'lodash',
  3. 'kbn'
  4. ],
  5. function (_, kbn) {
  6. 'use strict';
  7. function TimeSeries(opts) {
  8. this.datapoints = opts.datapoints;
  9. this.info = opts.info;
  10. this.label = opts.info.alias;
  11. this.valueFormater = kbn.valueFormats.none;
  12. this.stats = {};
  13. }
  14. function matchSeriesOverride(aliasOrRegex, seriesAlias) {
  15. if (!aliasOrRegex) { return false; }
  16. if (aliasOrRegex[0] === '/') {
  17. var regex = kbn.stringToJsRegex(aliasOrRegex);
  18. return seriesAlias.match(regex) != null;
  19. }
  20. return aliasOrRegex === seriesAlias;
  21. }
  22. function translateFillOption(fill) {
  23. return fill === 0 ? 0.001 : fill/10;
  24. }
  25. TimeSeries.prototype.applySeriesOverrides = function(overrides) {
  26. this.lines = {};
  27. this.points = {};
  28. this.bars = {};
  29. this.info.yaxis = 1;
  30. this.zindex = 0;
  31. delete this.stack;
  32. for (var i = 0; i < overrides.length; i++) {
  33. var override = overrides[i];
  34. if (!matchSeriesOverride(override.alias, this.info.alias)) {
  35. continue;
  36. }
  37. if (override.lines !== void 0) { this.lines.show = override.lines; }
  38. if (override.points !== void 0) { this.points.show = override.points; }
  39. if (override.bars !== void 0) { this.bars.show = override.bars; }
  40. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  41. if (override.stack !== void 0) { this.stack = override.stack; }
  42. if (override.linewidth !== void 0) { this.lines.lineWidth = override.linewidth; }
  43. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  44. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  45. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  46. if (override.yaxis !== void 0) {
  47. this.info.yaxis = override.yaxis;
  48. }
  49. }
  50. };
  51. TimeSeries.prototype.getFlotPairs = function (fillStyle) {
  52. var result = [];
  53. this.color = this.info.color;
  54. this.yaxis = this.info.yaxis;
  55. this.stats.total = 0;
  56. this.stats.max = -212312321312;
  57. this.stats.min = 212312321312;
  58. var ignoreNulls = fillStyle === 'connected';
  59. var nullAsZero = fillStyle === 'null as zero';
  60. var currentTime;
  61. var currentValue;
  62. for (var i = 0; i < this.datapoints.length; i++) {
  63. currentValue = this.datapoints[i][0];
  64. currentTime = this.datapoints[i][1];
  65. if (currentValue === null) {
  66. if (ignoreNulls) { continue; }
  67. if (nullAsZero) {
  68. currentValue = 0;
  69. }
  70. }
  71. if (_.isNumber(currentValue)) {
  72. this.stats.total += currentValue;
  73. }
  74. if (currentValue > this.stats.max) {
  75. this.stats.max = currentValue;
  76. }
  77. if (currentValue < this.stats.min) {
  78. this.stats.min = currentValue;
  79. }
  80. result.push([currentTime * 1000, currentValue]);
  81. }
  82. if (result.length > 2) {
  83. this.stats.timeStep = result[1][0] - result[0][0];
  84. }
  85. if (result.length) {
  86. this.stats.avg = (this.stats.total / result.length);
  87. this.stats.current = result[result.length-1][1];
  88. }
  89. return result;
  90. };
  91. TimeSeries.prototype.updateLegendValues = function(formater, decimals, scaledDecimals) {
  92. this.valueFormater = formater;
  93. this.decimals = decimals;
  94. this.scaledDecimals = scaledDecimals;
  95. this.info.avg = this.formatValue(this.stats.avg);
  96. this.info.current = this.formatValue(this.stats.current);
  97. this.info.min = this.formatValue(this.stats.min);
  98. this.info.max = this.formatValue(this.stats.max);
  99. this.info.total = this.formatValue(this.stats.total);
  100. };
  101. TimeSeries.prototype.formatValue = function(value) {
  102. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  103. };
  104. return TimeSeries;
  105. });