time_series2.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.bars = { fillColor: opts.color };
  91. this.valueFormater = kbn.valueFormats.none;
  92. this.stats = {};
  93. this.legend = true;
  94. this.unit = opts.unit;
  95. this.hasMsResolution = this.isMsResolutionNeeded();
  96. }
  97. applySeriesOverrides(overrides) {
  98. this.lines = {};
  99. this.dashes = {
  100. dashLength: [],
  101. };
  102. this.points = {};
  103. this.yaxis = 1;
  104. this.zindex = 0;
  105. this.nullPointMode = null;
  106. delete this.stack;
  107. delete this.bars.show;
  108. for (var i = 0; i < overrides.length; i++) {
  109. var override = overrides[i];
  110. if (!matchSeriesOverride(override.alias, this.alias)) {
  111. continue;
  112. }
  113. if (override.lines !== void 0) {
  114. this.lines.show = override.lines;
  115. }
  116. if (override.dashes !== void 0) {
  117. this.dashes.show = override.dashes;
  118. this.lines.lineWidth = 0;
  119. }
  120. if (override.points !== void 0) {
  121. this.points.show = override.points;
  122. }
  123. if (override.bars !== void 0) {
  124. this.bars.show = override.bars;
  125. }
  126. if (override.fill !== void 0) {
  127. this.lines.fill = translateFillOption(override.fill);
  128. }
  129. if (override.stack !== void 0) {
  130. this.stack = override.stack;
  131. }
  132. if (override.linewidth !== void 0) {
  133. this.lines.lineWidth = this.dashes.show ? 0 : override.linewidth;
  134. this.dashes.lineWidth = override.linewidth;
  135. }
  136. if (override.dashLength !== void 0) {
  137. this.dashes.dashLength[0] = override.dashLength;
  138. }
  139. if (override.spaceLength !== void 0) {
  140. this.dashes.dashLength[1] = override.spaceLength;
  141. }
  142. if (override.nullPointMode !== void 0) {
  143. this.nullPointMode = override.nullPointMode;
  144. }
  145. if (override.pointradius !== void 0) {
  146. this.points.radius = override.pointradius;
  147. }
  148. if (override.steppedLine !== void 0) {
  149. this.lines.steps = override.steppedLine;
  150. }
  151. if (override.zindex !== void 0) {
  152. this.zindex = override.zindex;
  153. }
  154. if (override.fillBelowTo !== void 0) {
  155. this.fillBelowTo = override.fillBelowTo;
  156. }
  157. if (override.color !== void 0) {
  158. this.setColor(override.color);
  159. }
  160. if (override.transform !== void 0) {
  161. this.transform = override.transform;
  162. }
  163. if (override.legend !== void 0) {
  164. this.legend = override.legend;
  165. }
  166. if (override.yaxis !== void 0) {
  167. this.yaxis = override.yaxis;
  168. }
  169. }
  170. }
  171. getFlotPairs(fillStyle) {
  172. var result = [];
  173. this.stats.total = 0;
  174. this.stats.max = -Number.MAX_VALUE;
  175. this.stats.min = Number.MAX_VALUE;
  176. this.stats.logmin = Number.MAX_VALUE;
  177. this.stats.avg = null;
  178. this.stats.current = null;
  179. this.stats.first = null;
  180. this.stats.delta = 0;
  181. this.stats.diff = null;
  182. this.stats.range = null;
  183. this.stats.timeStep = Number.MAX_VALUE;
  184. this.allIsNull = true;
  185. this.allIsZero = true;
  186. var ignoreNulls = fillStyle === 'connected';
  187. var nullAsZero = fillStyle === 'null as zero';
  188. var currentTime;
  189. var currentValue;
  190. var nonNulls = 0;
  191. var previousTime;
  192. var previousValue = 0;
  193. var previousDeltaUp = true;
  194. for (var i = 0; i < this.datapoints.length; i++) {
  195. currentValue = this.datapoints[i][0];
  196. currentTime = this.datapoints[i][1];
  197. // Due to missing values we could have different timeStep all along the series
  198. // so we have to find the minimum one (could occur with aggregators such as ZimSum)
  199. if (previousTime !== undefined) {
  200. let timeStep = currentTime - previousTime;
  201. if (timeStep < this.stats.timeStep) {
  202. this.stats.timeStep = timeStep;
  203. }
  204. }
  205. previousTime = currentTime;
  206. if (currentValue === null) {
  207. if (ignoreNulls) {
  208. continue;
  209. }
  210. if (nullAsZero) {
  211. currentValue = 0;
  212. }
  213. }
  214. if (currentValue !== null) {
  215. if (_.isNumber(currentValue)) {
  216. this.stats.total += currentValue;
  217. this.allIsNull = false;
  218. nonNulls++;
  219. }
  220. if (currentValue > this.stats.max) {
  221. this.stats.max = currentValue;
  222. }
  223. if (currentValue < this.stats.min) {
  224. this.stats.min = currentValue;
  225. }
  226. if (this.stats.first === null) {
  227. this.stats.first = currentValue;
  228. } else {
  229. if (previousValue > currentValue) {
  230. // counter reset
  231. previousDeltaUp = false;
  232. if (i === this.datapoints.length - 1) {
  233. // reset on last
  234. this.stats.delta += currentValue;
  235. }
  236. } else {
  237. if (previousDeltaUp) {
  238. this.stats.delta += currentValue - previousValue; // normal increment
  239. } else {
  240. this.stats.delta += currentValue; // account for counter reset
  241. }
  242. previousDeltaUp = true;
  243. }
  244. }
  245. previousValue = currentValue;
  246. if (currentValue < this.stats.logmin && currentValue > 0) {
  247. this.stats.logmin = currentValue;
  248. }
  249. if (currentValue !== 0) {
  250. this.allIsZero = false;
  251. }
  252. }
  253. result.push([currentTime, currentValue]);
  254. }
  255. if (this.stats.max === -Number.MAX_VALUE) {
  256. this.stats.max = null;
  257. }
  258. if (this.stats.min === Number.MAX_VALUE) {
  259. this.stats.min = null;
  260. }
  261. if (result.length && !this.allIsNull) {
  262. this.stats.avg = this.stats.total / nonNulls;
  263. this.stats.current = result[result.length - 1][1];
  264. if (this.stats.current === null && result.length > 1) {
  265. this.stats.current = result[result.length - 2][1];
  266. }
  267. }
  268. if (this.stats.max !== null && this.stats.min !== null) {
  269. this.stats.range = this.stats.max - this.stats.min;
  270. }
  271. if (this.stats.current !== null && this.stats.first !== null) {
  272. this.stats.diff = this.stats.current - this.stats.first;
  273. }
  274. this.stats.count = result.length;
  275. return result;
  276. }
  277. updateLegendValues(formater, decimals, scaledDecimals) {
  278. this.valueFormater = formater;
  279. this.decimals = decimals;
  280. this.scaledDecimals = scaledDecimals;
  281. }
  282. formatValue(value) {
  283. if (!_.isFinite(value)) {
  284. value = null; // Prevent NaN formatting
  285. }
  286. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  287. }
  288. isMsResolutionNeeded() {
  289. for (var i = 0; i < this.datapoints.length; i++) {
  290. if (this.datapoints[i][1] !== null) {
  291. var timestamp = this.datapoints[i][1].toString();
  292. if (timestamp.length === 13 && timestamp % 1000 !== 0) {
  293. return true;
  294. }
  295. }
  296. }
  297. return false;
  298. }
  299. hideFromLegend(options) {
  300. if (options.hideEmpty && this.allIsNull) {
  301. return true;
  302. }
  303. // ignore series excluded via override
  304. if (!this.legend) {
  305. return true;
  306. }
  307. // ignore zero series
  308. if (options.hideZero && this.allIsZero) {
  309. return true;
  310. }
  311. return false;
  312. }
  313. setColor(color) {
  314. this.color = color;
  315. this.bars.fillColor = color;
  316. }
  317. }