time_series2.ts 7.7 KB

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