time_series2.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import kbn from 'app/core/utils/kbn';
  2. import {getFlotTickDecimals} from 'app/core/utils/ticks';
  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. /**
  16. * Calculate decimals for legend and update values for each series.
  17. * @param data series data
  18. * @param panel
  19. */
  20. export function updateLegendValues(data: TimeSeries[], panel) {
  21. for (let i = 0; i < data.length; i++) {
  22. let series = data[i];
  23. let yaxes = panel.yaxes;
  24. let axis = yaxes[series.yaxis - 1];
  25. let {tickDecimals, scaledDecimals} = getFlotTickDecimals(data, axis);
  26. let formater = kbn.valueFormats[panel.yaxes[series.yaxis - 1].format];
  27. // decimal override
  28. if (_.isNumber(panel.decimals)) {
  29. series.updateLegendValues(formater, panel.decimals, null);
  30. } else {
  31. // auto decimals
  32. // legend and tooltip gets one more decimal precision
  33. // than graph legend ticks
  34. tickDecimals = (tickDecimals || -1) + 1;
  35. series.updateLegendValues(formater, tickDecimals, scaledDecimals + 2);
  36. }
  37. }
  38. }
  39. export function getDataMinMax(data: TimeSeries[]) {
  40. let datamin = null;
  41. let datamax = null;
  42. for (let series of data) {
  43. if (datamax === null || datamax < series.stats.max) {
  44. datamax = series.stats.max;
  45. }
  46. if (datamin === null || datamin > series.stats.min) {
  47. datamin = series.stats.min;
  48. }
  49. }
  50. return {datamin, datamax};
  51. }
  52. export default class TimeSeries {
  53. datapoints: any;
  54. id: string;
  55. label: string;
  56. alias: string;
  57. aliasEscaped: string;
  58. color: string;
  59. valueFormater: any;
  60. stats: any;
  61. legend: boolean;
  62. allIsNull: boolean;
  63. allIsZero: boolean;
  64. decimals: number;
  65. scaledDecimals: number;
  66. hasMsResolution: boolean;
  67. isOutsideRange: boolean;
  68. lines: any;
  69. dashes: any;
  70. bars: any;
  71. points: any;
  72. yaxis: any;
  73. zindex: any;
  74. stack: any;
  75. nullPointMode: any;
  76. fillBelowTo: any;
  77. transform: any;
  78. flotpairs: any;
  79. unit: any;
  80. constructor(opts) {
  81. this.datapoints = opts.datapoints;
  82. this.label = opts.alias;
  83. this.id = opts.alias;
  84. this.alias = opts.alias;
  85. this.aliasEscaped = _.escape(opts.alias);
  86. this.color = opts.color;
  87. this.valueFormater = kbn.valueFormats.none;
  88. this.stats = {};
  89. this.legend = true;
  90. this.unit = opts.unit;
  91. this.hasMsResolution = this.isMsResolutionNeeded();
  92. }
  93. applySeriesOverrides(overrides) {
  94. this.lines = {};
  95. this.dashes = {
  96. dashLength: []
  97. };
  98. this.points = {};
  99. this.bars = {};
  100. this.yaxis = 1;
  101. this.zindex = 0;
  102. this.nullPointMode = null;
  103. delete this.stack;
  104. for (var i = 0; i < overrides.length; i++) {
  105. var override = overrides[i];
  106. if (!matchSeriesOverride(override.alias, this.alias)) {
  107. continue;
  108. }
  109. if (override.lines !== void 0) { this.lines.show = override.lines; }
  110. if (override.dashes !== void 0) {
  111. this.dashes.show = override.dashes;
  112. this.lines.lineWidth = 0;
  113. }
  114. if (override.points !== void 0) { this.points.show = override.points; }
  115. if (override.bars !== void 0) { this.bars.show = override.bars; }
  116. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  117. if (override.stack !== void 0) { this.stack = override.stack; }
  118. if (override.linewidth !== void 0) {
  119. this.lines.lineWidth = this.dashes.show ? 0: override.linewidth;
  120. this.dashes.lineWidth = override.linewidth;
  121. }
  122. if (override.dashLength !== void 0) { this.dashes.dashLength[0] = override.dashLength; }
  123. if (override.spaceLength !== void 0) { this.dashes.dashLength[1] = override.spaceLength; }
  124. if (override.nullPointMode !== void 0) { this.nullPointMode = override.nullPointMode; }
  125. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  126. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  127. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  128. if (override.fillBelowTo !== void 0) { this.fillBelowTo = override.fillBelowTo; }
  129. if (override.color !== void 0) { this.color = override.color; }
  130. if (override.transform !== void 0) { this.transform = override.transform; }
  131. if (override.legend !== void 0) { this.legend = override.legend; }
  132. if (override.yaxis !== void 0) {
  133. this.yaxis = override.yaxis;
  134. }
  135. }
  136. }
  137. getFlotPairs(fillStyle) {
  138. var result = [];
  139. this.stats.total = 0;
  140. this.stats.max = -Number.MAX_VALUE;
  141. this.stats.min = Number.MAX_VALUE;
  142. this.stats.logmin = Number.MAX_VALUE;
  143. this.stats.avg = null;
  144. this.stats.current = null;
  145. this.stats.first = null;
  146. this.stats.delta = 0;
  147. this.stats.diff = null;
  148. this.stats.range = null;
  149. this.stats.timeStep = Number.MAX_VALUE;
  150. this.allIsNull = true;
  151. this.allIsZero = true;
  152. var ignoreNulls = fillStyle === 'connected';
  153. var nullAsZero = fillStyle === 'null as zero';
  154. var currentTime;
  155. var currentValue;
  156. var nonNulls = 0;
  157. var previousTime;
  158. var previousValue = 0;
  159. var previousDeltaUp = true;
  160. for (var i = 0; i < this.datapoints.length; i++) {
  161. currentValue = this.datapoints[i][0];
  162. currentTime = this.datapoints[i][1];
  163. // Due to missing values we could have different timeStep all along the series
  164. // so we have to find the minimum one (could occur with aggregators such as ZimSum)
  165. if (previousTime !== undefined) {
  166. let timeStep = currentTime - previousTime;
  167. if (timeStep < this.stats.timeStep) {
  168. this.stats.timeStep = timeStep;
  169. }
  170. }
  171. previousTime = currentTime;
  172. if (currentValue === null) {
  173. if (ignoreNulls) { continue; }
  174. if (nullAsZero) {
  175. currentValue = 0;
  176. }
  177. }
  178. if (currentValue !== null) {
  179. if (_.isNumber(currentValue)) {
  180. this.stats.total += currentValue;
  181. this.allIsNull = false;
  182. nonNulls++;
  183. }
  184. if (currentValue > this.stats.max) {
  185. this.stats.max = currentValue;
  186. }
  187. if (currentValue < this.stats.min) {
  188. this.stats.min = currentValue;
  189. }
  190. if (this.stats.first === null) {
  191. this.stats.first = currentValue;
  192. } else {
  193. if (previousValue > currentValue) { // counter reset
  194. previousDeltaUp = false;
  195. if (i === this.datapoints.length-1) { // reset on last
  196. this.stats.delta += currentValue;
  197. }
  198. } else {
  199. if (previousDeltaUp) {
  200. this.stats.delta += currentValue - previousValue; // normal increment
  201. } else {
  202. this.stats.delta += currentValue; // account for counter reset
  203. }
  204. previousDeltaUp = true;
  205. }
  206. }
  207. previousValue = currentValue;
  208. if (currentValue < this.stats.logmin && currentValue > 0) {
  209. this.stats.logmin = currentValue;
  210. }
  211. if (currentValue !== 0) {
  212. this.allIsZero = false;
  213. }
  214. }
  215. result.push([currentTime, currentValue]);
  216. }
  217. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  218. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  219. if (result.length && !this.allIsNull) {
  220. this.stats.avg = (this.stats.total / nonNulls);
  221. this.stats.current = result[result.length-1][1];
  222. if (this.stats.current === null && result.length > 1) {
  223. this.stats.current = result[result.length-2][1];
  224. }
  225. }
  226. if (this.stats.max !== null && this.stats.min !== null) {
  227. this.stats.range = this.stats.max - this.stats.min;
  228. }
  229. if (this.stats.current !== null && this.stats.first !== null) {
  230. this.stats.diff = this.stats.current - this.stats.first;
  231. }
  232. this.stats.count = result.length;
  233. return result;
  234. }
  235. updateLegendValues(formater, decimals, scaledDecimals) {
  236. this.valueFormater = formater;
  237. this.decimals = decimals;
  238. this.scaledDecimals = scaledDecimals;
  239. }
  240. formatValue(value) {
  241. if (!_.isFinite(value)) {
  242. value = null; // Prevent NaN formatting
  243. }
  244. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  245. }
  246. isMsResolutionNeeded() {
  247. for (var i = 0; i < this.datapoints.length; i++) {
  248. if (this.datapoints[i][1] !== null) {
  249. var timestamp = this.datapoints[i][1].toString();
  250. if (timestamp.length === 13 && (timestamp % 1000) !== 0) {
  251. return true;
  252. }
  253. }
  254. }
  255. return false;
  256. }
  257. hideFromLegend(options) {
  258. if (options.hideEmpty && this.allIsNull) {
  259. return true;
  260. }
  261. // ignore series excluded via override
  262. if (!this.legend) {
  263. return true;
  264. }
  265. // ignore zero series
  266. if (options.hideZero && this.allIsZero) {
  267. return true;
  268. }
  269. return false;
  270. }
  271. }