time_series2.ts 9.3 KB

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