time_series2.ts 9.8 KB

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