time_series2.ts 10 KB

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