time_series_specs.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {describe, beforeEach, it, expect} from 'test/lib/common';
  2. import TimeSeries from 'app/core/time_series2';
  3. describe("TimeSeries", function() {
  4. var points, series;
  5. var yAxisFormats = ['short', 'ms'];
  6. var testData;
  7. beforeEach(function() {
  8. testData = {
  9. alias: 'test',
  10. datapoints: [
  11. [1,2],[null,3],[10,4],[8,5]
  12. ]
  13. };
  14. });
  15. describe('when getting flot pairs', function() {
  16. it('with connected style, should ignore nulls', function() {
  17. series = new TimeSeries(testData);
  18. points = series.getFlotPairs('connected', yAxisFormats);
  19. expect(points.length).to.be(3);
  20. });
  21. it('with null as zero style, should replace nulls with zero', function() {
  22. series = new TimeSeries(testData);
  23. points = series.getFlotPairs('null as zero', yAxisFormats);
  24. expect(points.length).to.be(4);
  25. expect(points[1][1]).to.be(0);
  26. });
  27. it('if last is null current should pick next to last', function() {
  28. series = new TimeSeries({
  29. datapoints: [[10,1], [null, 2]]
  30. });
  31. series.getFlotPairs('null', yAxisFormats);
  32. expect(series.stats.current).to.be(10);
  33. });
  34. it('max value should work for negative values', function() {
  35. series = new TimeSeries({
  36. datapoints: [[-10,1], [-4, 2]]
  37. });
  38. series.getFlotPairs('null', yAxisFormats);
  39. expect(series.stats.max).to.be(-4);
  40. });
  41. it('average value should ignore nulls', function() {
  42. series = new TimeSeries(testData);
  43. series.getFlotPairs('null', yAxisFormats);
  44. expect(series.stats.avg).to.be(6.333333333333333);
  45. });
  46. it('the delta value should account for nulls', function() {
  47. series = new TimeSeries({
  48. datapoints: [[1,2],[3,3],[null,4],[10,5],[15,6]]
  49. });
  50. series.getFlotPairs('null', yAxisFormats);
  51. expect(series.stats.delta).to.be(14);
  52. });
  53. it('the delta value should account for nulls on first', function() {
  54. series = new TimeSeries({
  55. datapoints: [[null,2],[1,3],[10,4],[15,5]]
  56. });
  57. series.getFlotPairs('null', yAxisFormats);
  58. expect(series.stats.delta).to.be(14);
  59. });
  60. it('the delta value should account for nulls on last', function() {
  61. series = new TimeSeries({
  62. datapoints: [[1,2],[5,3],[10,4],[null,5]]
  63. });
  64. series.getFlotPairs('null', yAxisFormats);
  65. expect(series.stats.delta).to.be(9);
  66. });
  67. it('the delta value should account for resets', function() {
  68. series = new TimeSeries({
  69. datapoints: [[1,2],[5,3],[10,4],[0,5],[10,6]]
  70. });
  71. series.getFlotPairs('null', yAxisFormats);
  72. expect(series.stats.delta).to.be(19);
  73. });
  74. it('the delta value should account for resets on last', function() {
  75. series = new TimeSeries({
  76. datapoints: [[1,2],[2,3],[10,4],[8,5]]
  77. });
  78. series.getFlotPairs('null', yAxisFormats);
  79. expect(series.stats.delta).to.be(17);
  80. });
  81. it('the range value should be max - min', function() {
  82. series = new TimeSeries(testData);
  83. series.getFlotPairs('null', yAxisFormats);
  84. expect(series.stats.range).to.be(9);
  85. });
  86. it('first value should ingone nulls', function() {
  87. series = new TimeSeries(testData);
  88. series.getFlotPairs('null', yAxisFormats);
  89. expect(series.stats.first).to.be(1);
  90. series = new TimeSeries({
  91. datapoints: [[null,2],[1,3],[10,4],[8,5]]
  92. });
  93. series.getFlotPairs('null', yAxisFormats);
  94. expect(series.stats.first).to.be(1);
  95. });
  96. it('with null as zero style, average value should treat nulls as 0', function() {
  97. series = new TimeSeries(testData);
  98. series.getFlotPairs('null as zero', yAxisFormats);
  99. expect(series.stats.avg).to.be(4.75);
  100. });
  101. it('average value should be null if all values is null', function() {
  102. series = new TimeSeries({
  103. datapoints: [[null,2],[null,3],[null,4],[null,5]]
  104. });
  105. series.getFlotPairs('null');
  106. expect(series.stats.avg).to.be(null);
  107. });
  108. });
  109. describe('When checking if ms resolution is needed', function() {
  110. describe('msResolution with second resolution timestamps', function() {
  111. beforeEach(function() {
  112. series = new TimeSeries({datapoints: [[45, 1234567890], [60, 1234567899]]});
  113. });
  114. it('should set hasMsResolution to false', function() {
  115. expect(series.hasMsResolution).to.be(false);
  116. });
  117. });
  118. describe('msResolution with millisecond resolution timestamps', function() {
  119. beforeEach(function() {
  120. series = new TimeSeries({datapoints: [[55, 1236547890001], [90, 1234456709000]]});
  121. });
  122. it('should show millisecond resolution tooltip', function() {
  123. expect(series.hasMsResolution).to.be(true);
  124. });
  125. });
  126. describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() {
  127. beforeEach(function() {
  128. series = new TimeSeries({datapoints: [[45, 1234567890000], [60, 1234567899000]]});
  129. });
  130. it('should not show millisecond resolution tooltip', function() {
  131. expect(series.hasMsResolution).to.be(false);
  132. });
  133. });
  134. });
  135. describe('can detect if series contains ms precision', function() {
  136. var fakedata;
  137. beforeEach(function() {
  138. fakedata = testData;
  139. });
  140. it('missing datapoint with ms precision', function() {
  141. fakedata.datapoints[0] = [1337, 1234567890000];
  142. series = new TimeSeries(fakedata);
  143. expect(series.isMsResolutionNeeded()).to.be(false);
  144. });
  145. it('contains datapoint with ms precision', function() {
  146. fakedata.datapoints[0] = [1337, 1236547890001];
  147. series = new TimeSeries(fakedata);
  148. expect(series.isMsResolutionNeeded()).to.be(true);
  149. });
  150. });
  151. describe('series overrides', function() {
  152. var series;
  153. beforeEach(function() {
  154. series = new TimeSeries(testData);
  155. });
  156. describe('fill & points', function() {
  157. beforeEach(function() {
  158. series.alias = 'test';
  159. series.applySeriesOverrides([{ alias: 'test', fill: 0, points: true }]);
  160. });
  161. it('should set fill zero, and enable points', function() {
  162. expect(series.lines.fill).to.be(0.001);
  163. expect(series.points.show).to.be(true);
  164. });
  165. });
  166. describe('series option overrides, bars, true & lines false', function() {
  167. beforeEach(function() {
  168. series.alias = 'test';
  169. series.applySeriesOverrides([{ alias: 'test', bars: true, lines: false }]);
  170. });
  171. it('should disable lines, and enable bars', function() {
  172. expect(series.lines.show).to.be(false);
  173. expect(series.bars.show).to.be(true);
  174. });
  175. });
  176. describe('series option overrides, linewidth, stack', function() {
  177. beforeEach(function() {
  178. series.alias = 'test';
  179. series.applySeriesOverrides([{ alias: 'test', linewidth: 5, stack: false }]);
  180. });
  181. it('should disable stack, and set lineWidth', function() {
  182. expect(series.stack).to.be(false);
  183. expect(series.lines.lineWidth).to.be(5);
  184. });
  185. });
  186. describe('series option overrides, dashes and lineWidth', function() {
  187. beforeEach(function() {
  188. series.alias = 'test';
  189. series.applySeriesOverrides([{ alias: 'test', linewidth: 5, dashes: true }]);
  190. });
  191. it('should enable dashes, set dashes lineWidth to 5 and lines lineWidth to 0', function() {
  192. expect(series.dashes.show).to.be(true);
  193. expect(series.dashes.lineWidth).to.be(5);
  194. expect(series.lines.lineWidth).to.be(0);
  195. });
  196. });
  197. describe('series option overrides, fill below to', function() {
  198. beforeEach(function() {
  199. series.alias = 'test';
  200. series.applySeriesOverrides([{ alias: 'test', fillBelowTo: 'min' }]);
  201. });
  202. it('should disable line fill and add fillBelowTo', function() {
  203. expect(series.fillBelowTo).to.be('min');
  204. });
  205. });
  206. describe('series option overrides, pointradius, steppedLine', function() {
  207. beforeEach(function() {
  208. series.alias = 'test';
  209. series.applySeriesOverrides([{ alias: 'test', pointradius: 5, steppedLine: true }]);
  210. });
  211. it('should set pointradius, and set steppedLine', function() {
  212. expect(series.points.radius).to.be(5);
  213. expect(series.lines.steps).to.be(true);
  214. });
  215. });
  216. describe('override match on regex', function() {
  217. beforeEach(function() {
  218. series.alias = 'test_01';
  219. series.applySeriesOverrides([{ alias: '/.*01/', lines: false }]);
  220. });
  221. it('should match second series', function() {
  222. expect(series.lines.show).to.be(false);
  223. });
  224. });
  225. describe('override series y-axis, and z-index', function() {
  226. beforeEach(function() {
  227. series.alias = 'test';
  228. series.applySeriesOverrides([{ alias: 'test', yaxis: 2, zindex: 2 }]);
  229. });
  230. it('should set yaxis', function() {
  231. expect(series.yaxis).to.be(2);
  232. });
  233. it('should set zindex', function() {
  234. expect(series.zindex).to.be(2);
  235. });
  236. });
  237. });
  238. describe('value formatter', function() {
  239. var series;
  240. beforeEach(function() {
  241. series = new TimeSeries(testData);
  242. });
  243. it('should format non-numeric values as empty string', function() {
  244. expect(series.formatValue(null)).to.be("");
  245. expect(series.formatValue(undefined)).to.be("");
  246. expect(series.formatValue(NaN)).to.be("");
  247. expect(series.formatValue(Infinity)).to.be("");
  248. expect(series.formatValue(-Infinity)).to.be("");
  249. });
  250. });
  251. });