time_series2.ts 9.2 KB

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