time_series2.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { isNumber, isFinite, escape } from 'lodash';
  2. import { DecimalCount, formattedValueToString, getValueFormat, stringToJsRegex, ValueFormatter } from '@grafana/data';
  3. function matchSeriesOverride(aliasOrRegex: string, seriesAlias: string) {
  4. if (!aliasOrRegex) {
  5. return false;
  6. }
  7. if (aliasOrRegex[0] === '/') {
  8. const regex = stringToJsRegex(aliasOrRegex);
  9. return seriesAlias.match(regex) != null;
  10. }
  11. return aliasOrRegex === seriesAlias;
  12. }
  13. function translateFillOption(fill: number) {
  14. return fill === 0 ? 0.001 : fill / 10;
  15. }
  16. function getFillGradient(amount: number) {
  17. if (!amount) {
  18. return null;
  19. }
  20. return {
  21. colors: [{ opacity: 0.0 }, { opacity: amount / 10 }],
  22. };
  23. }
  24. /**
  25. * Calculate decimals for legend and update values for each series.
  26. * @param data series data
  27. * @param panel
  28. * @param height
  29. */
  30. export function updateLegendValues(data: TimeSeries[], panel: any, height: number) {
  31. for (let i = 0; i < data.length; i++) {
  32. const series = data[i];
  33. const yaxes = panel.yaxes;
  34. const seriesYAxis = series.yaxis || 1;
  35. const axis = yaxes[seriesYAxis - 1];
  36. const formatter = getValueFormat(axis.format);
  37. // decimal override
  38. if (isNumber(panel.decimals)) {
  39. series.updateLegendValues(formatter, panel.decimals);
  40. } else if (isNumber(axis.decimals)) {
  41. series.updateLegendValues(formatter, axis.decimals + 1);
  42. } else {
  43. series.updateLegendValues(formatter, null);
  44. }
  45. }
  46. }
  47. /**
  48. * @deprecated: This class should not be used in new panels
  49. *
  50. * Use DataFrame and helpers instead
  51. */
  52. export default class TimeSeries {
  53. datapoints: any;
  54. id: string;
  55. // Represents index of original data frame in the quey response
  56. dataFrameIndex: number;
  57. // Represents index of field in the data frame
  58. fieldIndex: number;
  59. label: string;
  60. alias: string;
  61. aliasEscaped: string;
  62. color?: string;
  63. valueFormater: any;
  64. stats: any;
  65. legend: boolean;
  66. hideTooltip?: boolean;
  67. allIsNull?: boolean;
  68. allIsZero?: boolean;
  69. decimals: DecimalCount;
  70. hasMsResolution: boolean;
  71. isOutsideRange?: boolean;
  72. lines: any;
  73. hiddenSeries?: boolean;
  74. dashes: any;
  75. bars: any;
  76. points: any;
  77. yaxis: any;
  78. zindex: any;
  79. stack: any;
  80. nullPointMode: any;
  81. fillBelowTo: any;
  82. transform: any;
  83. flotpairs: any;
  84. unit: any;
  85. constructor(opts: any) {
  86. this.datapoints = opts.datapoints;
  87. this.label = opts.alias;
  88. this.id = opts.alias;
  89. this.alias = opts.alias;
  90. this.aliasEscaped = escape(opts.alias);
  91. this.color = opts.color;
  92. this.bars = { fillColor: opts.color };
  93. this.valueFormater = getValueFormat('none');
  94. this.stats = {};
  95. this.legend = true;
  96. this.unit = opts.unit;
  97. this.dataFrameIndex = opts.dataFrameIndex;
  98. this.fieldIndex = opts.fieldIndex;
  99. this.hasMsResolution = this.isMsResolutionNeeded();
  100. }
  101. applySeriesOverrides(overrides: any[]) {
  102. this.lines = {};
  103. this.dashes = {
  104. dashLength: [],
  105. };
  106. this.points = {};
  107. this.yaxis = 1;
  108. this.zindex = 0;
  109. this.nullPointMode = null;
  110. delete this.stack;
  111. delete this.bars.show;
  112. for (let i = 0; i < overrides.length; i++) {
  113. const override = overrides[i];
  114. if (!matchSeriesOverride(override.alias, this.alias)) {
  115. continue;
  116. }
  117. if (override.lines !== void 0) {
  118. this.lines.show = override.lines;
  119. }
  120. if (override.dashes !== void 0) {
  121. this.dashes.show = override.dashes;
  122. this.lines.lineWidth = 0;
  123. }
  124. if (override.points !== void 0) {
  125. this.points.show = override.points;
  126. }
  127. if (override.bars !== void 0) {
  128. this.bars.show = override.bars;
  129. }
  130. if (override.fill !== void 0) {
  131. this.lines.fill = translateFillOption(override.fill);
  132. }
  133. if (override.fillGradient !== void 0) {
  134. this.lines.fillColor = getFillGradient(override.fillGradient);
  135. }
  136. if (override.stack !== void 0) {
  137. this.stack = override.stack;
  138. }
  139. if (override.linewidth !== void 0) {
  140. this.lines.lineWidth = this.dashes.show ? 0 : override.linewidth;
  141. this.dashes.lineWidth = override.linewidth;
  142. }
  143. if (override.dashLength !== void 0) {
  144. this.dashes.dashLength[0] = override.dashLength;
  145. }
  146. if (override.spaceLength !== void 0) {
  147. this.dashes.dashLength[1] = override.spaceLength;
  148. }
  149. if (override.nullPointMode !== void 0) {
  150. this.nullPointMode = override.nullPointMode;
  151. }
  152. if (override.pointradius !== void 0) {
  153. this.points.radius = override.pointradius;
  154. }
  155. if (override.steppedLine !== void 0) {
  156. this.lines.steps = override.steppedLine;
  157. }
  158. if (override.zindex !== void 0) {
  159. this.zindex = override.zindex;
  160. }
  161. if (override.fillBelowTo !== void 0) {
  162. this.fillBelowTo = override.fillBelowTo;
  163. }
  164. if (override.color !== void 0) {
  165. this.setColor(override.color);
  166. }
  167. if (override.transform !== void 0) {
  168. this.transform = override.transform;
  169. }
  170. if (override.legend !== void 0) {
  171. this.legend = override.legend;
  172. }
  173. if (override.hideTooltip !== void 0) {
  174. this.hideTooltip = override.hideTooltip;
  175. }
  176. if (override.yaxis !== void 0) {
  177. this.yaxis = override.yaxis;
  178. }
  179. if (override.hiddenSeries !== void 0) {
  180. this.hiddenSeries = override.hiddenSeries;
  181. }
  182. }
  183. }
  184. getFlotPairs(fillStyle: string) {
  185. const result = [];
  186. this.stats.total = 0;
  187. this.stats.max = -Number.MAX_VALUE;
  188. this.stats.min = Number.MAX_VALUE;
  189. this.stats.logmin = Number.MAX_VALUE;
  190. this.stats.avg = null;
  191. this.stats.current = null;
  192. this.stats.first = null;
  193. this.stats.delta = 0;
  194. this.stats.diff = null;
  195. this.stats.diffperc = 0;
  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. this.stats.diffperc = this.stats.diff / this.stats.first;
  288. }
  289. this.stats.count = result.length;
  290. return result;
  291. }
  292. updateLegendValues(formater: ValueFormatter, decimals: DecimalCount) {
  293. this.valueFormater = formater;
  294. this.decimals = decimals;
  295. }
  296. formatValue(value: number | null) {
  297. if (!isFinite(value)) {
  298. value = null; // Prevent NaN formatting
  299. }
  300. return formattedValueToString(this.valueFormater(value, this.decimals));
  301. }
  302. isMsResolutionNeeded() {
  303. for (let i = 0; i < this.datapoints.length; i++) {
  304. if (this.datapoints[i][1] !== null && this.datapoints[i][1] !== undefined) {
  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: any) {
  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. }