time_series2.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. hasMsResolution: boolean;
  29. isOutsideRange: boolean;
  30. lines: any;
  31. bars: any;
  32. points: any;
  33. yaxis: any;
  34. zindex: any;
  35. stack: any;
  36. nullPointMode: any;
  37. fillBelowTo: any;
  38. transform: any;
  39. flotpairs: any;
  40. unit: any;
  41. constructor(opts) {
  42. this.datapoints = opts.datapoints;
  43. this.label = opts.alias;
  44. this.id = opts.alias;
  45. this.alias = opts.alias;
  46. this.color = opts.color;
  47. this.valueFormater = kbn.valueFormats.none;
  48. this.stats = {};
  49. this.legend = true;
  50. this.unit = opts.unit;
  51. this.hasMsResolution = this.isMsResolutionNeeded();
  52. }
  53. applySeriesOverrides(overrides) {
  54. this.lines = {};
  55. this.points = {};
  56. this.bars = {};
  57. this.yaxis = 1;
  58. this.zindex = 0;
  59. this.nullPointMode = null;
  60. delete this.stack;
  61. for (var i = 0; i < overrides.length; i++) {
  62. var override = overrides[i];
  63. if (!matchSeriesOverride(override.alias, this.alias)) {
  64. continue;
  65. }
  66. if (override.lines !== void 0) { this.lines.show = override.lines; }
  67. if (override.points !== void 0) { this.points.show = override.points; }
  68. if (override.bars !== void 0) { this.bars.show = override.bars; }
  69. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  70. if (override.stack !== void 0) { this.stack = override.stack; }
  71. if (override.linewidth !== void 0) { this.lines.lineWidth = override.linewidth; }
  72. if (override.nullPointMode !== void 0) { this.nullPointMode = override.nullPointMode; }
  73. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  74. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  75. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  76. if (override.fillBelowTo !== void 0) { this.fillBelowTo = override.fillBelowTo; }
  77. if (override.color !== void 0) { this.color = override.color; }
  78. if (override.transform !== void 0) { this.transform = override.transform; }
  79. if (override.legend !== void 0) { this.legend = override.legend; }
  80. if (override.yaxis !== void 0) {
  81. this.yaxis = override.yaxis;
  82. }
  83. }
  84. };
  85. getFlotPairs(fillStyle) {
  86. var result = [];
  87. this.stats.total = 0;
  88. this.stats.max = -Number.MAX_VALUE;
  89. this.stats.min = Number.MAX_VALUE;
  90. this.stats.logmin = Number.MAX_VALUE;
  91. this.stats.avg = null;
  92. this.stats.current = null;
  93. this.stats.first = null;
  94. this.stats.delta = 0;
  95. this.stats.diff = null;
  96. this.stats.range = null;
  97. this.stats.timeStep = Number.MAX_VALUE;
  98. this.allIsNull = true;
  99. this.allIsZero = true;
  100. var ignoreNulls = fillStyle === 'connected';
  101. var nullAsZero = fillStyle === 'null as zero';
  102. var currentTime;
  103. var currentValue;
  104. var nonNulls = 0;
  105. var previousTime;
  106. var previousValue = 0;
  107. var previousDeltaUp = true;
  108. for (var i = 0; i < this.datapoints.length; i++) {
  109. currentValue = this.datapoints[i][0];
  110. currentTime = this.datapoints[i][1];
  111. // Due to missing values we could have different timeStep all along the series
  112. // so we have to find the minimum one (could occur with aggregators such as ZimSum)
  113. if (previousTime !== undefined) {
  114. let timeStep = currentTime - previousTime;
  115. if (timeStep < this.stats.timeStep) {
  116. this.stats.timeStep = timeStep;
  117. }
  118. }
  119. previousTime = currentTime;
  120. if (currentValue === null) {
  121. if (ignoreNulls) { continue; }
  122. if (nullAsZero) {
  123. currentValue = 0;
  124. }
  125. }
  126. if (currentValue !== null) {
  127. if (_.isNumber(currentValue)) {
  128. this.stats.total += currentValue;
  129. this.allIsNull = false;
  130. nonNulls++;
  131. }
  132. if (currentValue > this.stats.max) {
  133. this.stats.max = currentValue;
  134. }
  135. if (currentValue < this.stats.min) {
  136. this.stats.min = currentValue;
  137. }
  138. if (this.stats.first === null){
  139. this.stats.first = currentValue;
  140. }else{
  141. if (previousValue > currentValue) { // counter reset
  142. previousDeltaUp = false;
  143. if (i === this.datapoints.length-1) { // reset on last
  144. this.stats.delta += currentValue;
  145. }
  146. }else{
  147. if (previousDeltaUp) {
  148. this.stats.delta += currentValue - previousValue; // normal increment
  149. } else {
  150. this.stats.delta += currentValue; // account for counter reset
  151. }
  152. previousDeltaUp = true;
  153. }
  154. }
  155. previousValue = currentValue;
  156. if (currentValue < this.stats.logmin && currentValue > 0) {
  157. this.stats.logmin = currentValue;
  158. }
  159. }
  160. if (currentValue !== 0) {
  161. this.allIsZero = false;
  162. }
  163. result.push([currentTime, currentValue]);
  164. }
  165. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  166. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  167. if (result.length) {
  168. this.stats.avg = (this.stats.total / nonNulls);
  169. this.stats.current = result[result.length-1][1];
  170. if (this.stats.current === null && result.length > 1) {
  171. this.stats.current = result[result.length-2][1];
  172. }
  173. }
  174. if (this.stats.max !== null && this.stats.min !== null) {
  175. this.stats.range = this.stats.max - this.stats.min;
  176. }
  177. if (this.stats.current !== null && this.stats.first !== null) {
  178. this.stats.diff = this.stats.current - this.stats.first;
  179. }
  180. this.stats.count = result.length;
  181. return result;
  182. }
  183. updateLegendValues(formater, decimals, scaledDecimals) {
  184. this.valueFormater = formater;
  185. this.decimals = decimals;
  186. this.scaledDecimals = scaledDecimals;
  187. }
  188. formatValue(value) {
  189. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  190. }
  191. isMsResolutionNeeded() {
  192. for (var i = 0; i < this.datapoints.length; i++) {
  193. if (this.datapoints[i][1] !== null) {
  194. var timestamp = this.datapoints[i][1].toString();
  195. if (timestamp.length === 13 && (timestamp % 1000) !== 0) {
  196. return true;
  197. }
  198. }
  199. }
  200. return false;
  201. }
  202. hideFromLegend(options) {
  203. if (options.hideEmpty && this.allIsNull) {
  204. return true;
  205. }
  206. // ignore series excluded via override
  207. if (!this.legend) {
  208. return true;
  209. }
  210. // ignore zero series
  211. if (options.hideZero && this.allIsZero) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. }