time_series_specs.js 9.1 KB

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