time_series2.ts 9.6 KB

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