time_series2.ts 9.8 KB

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