time_series2.ts 10.0 KB

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