time_series2.ts 7.6 KB

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