kbn_specs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. define([
  2. 'app/core/utils/kbn',
  3. 'app/core/utils/datemath',
  4. 'moment'
  5. ], function(kbn, dateMath, moment) {
  6. 'use strict';
  7. describe('unit format menu', function() {
  8. var menu = kbn.getUnitFormats();
  9. menu.map(function(submenu) {
  10. describe('submenu ' + submenu.text, function() {
  11. it('should have a title', function() { expect(submenu.text).to.be.a('string'); });
  12. it('should have a submenu', function() { expect(submenu.submenu).to.be.an('array'); });
  13. submenu.submenu.map(function(entry) {
  14. describe('entry ' + entry.text, function() {
  15. it('should have a title', function() { expect(entry.text).to.be.a('string'); });
  16. it('should have a format', function() { expect(entry.value).to.be.a('string'); });
  17. it('should have a valid format', function() {
  18. expect(kbn.valueFormats[entry.value]).to.be.a('function');
  19. });
  20. });
  21. });
  22. });
  23. });
  24. });
  25. function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
  26. describe('value format: ' + desc, function() {
  27. it('should translate ' + value + ' as ' + result, function() {
  28. var scaledDecimals = tickDecimals - Math.floor(Math.log(tickSize) / Math.LN10);
  29. var str = kbn.valueFormats[desc](value, tickDecimals, scaledDecimals);
  30. expect(str).to.be(result);
  31. });
  32. });
  33. }
  34. describeValueFormat('ms', 0.0024, 0.0005, 4, '0.0024 ms');
  35. describeValueFormat('ms', 100, 1, 0, '100 ms');
  36. describeValueFormat('ms', 1250, 10, 0, '1.25 s');
  37. describeValueFormat('ms', 1250, 300, 0, '1.3 s');
  38. describeValueFormat('ms', 65150, 10000, 0, '1.1 min');
  39. describeValueFormat('ms', 6515000, 1500000, 0, '1.8 hour');
  40. describeValueFormat('ms', 651500000, 150000000, 0, '8 day');
  41. describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
  42. describeValueFormat('none', 0, 0, 2, '0');
  43. describeValueFormat('dB', 10, 1000, 2, '10.00 dB');
  44. describeValueFormat('percent', 0, 0, 0, '0%');
  45. describeValueFormat('percent', 53, 0, 1, '53.0%');
  46. describeValueFormat('percentunit', 0.0, 0, 0, '0%');
  47. describeValueFormat('percentunit', 0.278, 0, 1, '27.8%');
  48. describeValueFormat('percentunit', 1.0, 0, 0, '100%');
  49. describeValueFormat('currencyUSD', 7.42, 10000, 2, '$7.42');
  50. describeValueFormat('currencyUSD', 1532.82, 1000, 1, '$1.53K');
  51. describeValueFormat('currencyUSD', 18520408.7, 10000000, 0, '$19M');
  52. describeValueFormat('bytes', -1.57e+308, -1.57e+308, 2, 'NA');
  53. describeValueFormat('ns', 25, 1, 0, '25 ns');
  54. describeValueFormat('ns', 2558, 50, 0, '2.56 µs');
  55. describeValueFormat('ops', 123, 1, 0, '123 ops');
  56. describeValueFormat('rps', 456000, 1000, -1, '456K rps');
  57. describeValueFormat('rps', 123456789, 1000000, 2, '123.457M rps');
  58. describeValueFormat('wps', 789000000, 1000000, -1, '789M wps');
  59. describeValueFormat('iops', 11000000000, 1000000000, -1, '11B iops');
  60. describeValueFormat('s', 1.23456789e-7, 1e-10, 8, '123.5 ns');
  61. describeValueFormat('s', 1.23456789e-4, 1e-7, 5, '123.5 µs');
  62. describeValueFormat('s', 1.23456789e-3, 1e-6, 4, '1.235 ms');
  63. describeValueFormat('s', 1.23456789e-2, 1e-5, 3, '12.35 ms');
  64. describeValueFormat('s', 1.23456789e-1, 1e-4, 2, '123.5 ms');
  65. describeValueFormat('s', 24, 1, 0, '24 s');
  66. describeValueFormat('s', 246, 1, 0, '4.1 min');
  67. describeValueFormat('s', 24567, 100, 0, '6.82 hour');
  68. describeValueFormat('s', 24567890, 10000, 0, '40.62 week');
  69. describeValueFormat('s', 24567890000, 1000000, 0, '778.53 year');
  70. describeValueFormat('m', 24, 1, 0, '24 min');
  71. describeValueFormat('m', 246, 10, 0, '4.1 hour');
  72. describeValueFormat('m', 6545, 10, 0, '4.55 day');
  73. describeValueFormat('m', 24567, 100, 0, '2.44 week');
  74. describeValueFormat('m', 24567892, 10000, 0, '46.7 year');
  75. describeValueFormat('h', 21, 1, 0, '21 hour');
  76. describeValueFormat('h', 145, 1, 0, '6.04 day');
  77. describeValueFormat('h', 1234, 100, 0, '7.3 week');
  78. describeValueFormat('h', 9458, 1000, 0, '1.08 year');
  79. describeValueFormat('d', 3, 1, 0, '3 day');
  80. describeValueFormat('d', 245, 100, 0, '35 week');
  81. describeValueFormat('d', 2456, 10, 0, '6.73 year');
  82. describe('date time formats', function() {
  83. it('should format as iso date', function() {
  84. var str = kbn.valueFormats.dateTimeAsIso(1505634997920, 1);
  85. expect(str).to.be(moment(1505634997920).format('YYYY-MM-DD HH:mm:ss'));
  86. });
  87. it('should format as iso date and skip date when today', function() {
  88. var now = moment();
  89. var str = kbn.valueFormats.dateTimeAsIso(now.valueOf(), 1);
  90. expect(str).to.be(now.format("HH:mm:ss"));
  91. });
  92. it('should format as US date', function() {
  93. var str = kbn.valueFormats.dateTimeAsUS(1505634997920, 1);
  94. expect(str).to.be(moment(1505634997920).format('MM/DD/YYYY H:mm:ss a'));
  95. });
  96. it('should format as US date and skip date when today', function() {
  97. var now = moment();
  98. var str = kbn.valueFormats.dateTimeAsUS(now.valueOf(), 1);
  99. expect(str).to.be(now.format("h:mm:ss a"));
  100. });
  101. it('should format as from now with days', function() {
  102. var daysAgo = moment().add(-7, 'd');
  103. var str = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), 1);
  104. expect(str).to.be('7 days ago');
  105. });
  106. it('should format as from now with minutes', function() {
  107. var daysAgo = moment().add(-2, 'm');
  108. var str = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), 1);
  109. expect(str).to.be('2 minutes ago');
  110. });
  111. });
  112. describe('kbn.toFixed and negative decimals', function() {
  113. it('should treat as zero decimals', function() {
  114. var str = kbn.toFixed(186.123, -2);
  115. expect(str).to.be('186');
  116. });
  117. });
  118. describe('kbn ms format when scaled decimals is null do not use it', function() {
  119. it('should use specified decimals', function() {
  120. var str = kbn.valueFormats['ms'](10000086.123, 1, null);
  121. expect(str).to.be('2.8 hour');
  122. });
  123. });
  124. describe('kbn kbytes format when scaled decimals is null do not use it', function() {
  125. it('should use specified decimals', function() {
  126. var str = kbn.valueFormats['kbytes'](10000000, 3, null);
  127. expect(str).to.be('9.537 GiB');
  128. });
  129. });
  130. describe('kbn deckbytes format when scaled decimals is null do not use it', function() {
  131. it('should use specified decimals', function() {
  132. var str = kbn.valueFormats['deckbytes'](10000000, 3, null);
  133. expect(str).to.be('10.000 GB');
  134. });
  135. });
  136. describe('kbn roundValue', function() {
  137. it('should should handle null value', function() {
  138. var str = kbn.roundValue(null, 2);
  139. expect(str).to.be(null);
  140. });
  141. });
  142. describe('calculateInterval', function() {
  143. it('1h 100 resultion', function() {
  144. var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
  145. var res = kbn.calculateInterval(range, 100, null);
  146. expect(res.interval).to.be('30s');
  147. });
  148. it('10m 1600 resolution', function() {
  149. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  150. var res = kbn.calculateInterval(range, 1600, null);
  151. expect(res.interval).to.be('500ms');
  152. expect(res.intervalMs).to.be(500);
  153. });
  154. it('fixed user min interval', function() {
  155. var range = {from: dateMath.parse('now-10m'), to: dateMath.parse('now')};
  156. var res = kbn.calculateInterval(range, 1600, '10s');
  157. expect(res.interval).to.be('10s');
  158. expect(res.intervalMs).to.be(10000);
  159. });
  160. it('short time range and user low limit', function() {
  161. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  162. var res = kbn.calculateInterval(range, 1600, '>10s');
  163. expect(res.interval).to.be('10s');
  164. });
  165. it('large time range and user low limit', function() {
  166. var range = {from: dateMath.parse('now-14d'), to: dateMath.parse('now')};
  167. var res = kbn.calculateInterval(range, 1000, '>10s');
  168. expect(res.interval).to.be('20m');
  169. });
  170. it('10s 900 resolution and user low limit in ms', function() {
  171. var range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') };
  172. var res = kbn.calculateInterval(range, 900, '>15ms');
  173. expect(res.interval).to.be('15ms');
  174. });
  175. it('1d 1 resolution', function() {
  176. var range = { from: dateMath.parse('now-1d'), to: dateMath.parse('now') };
  177. var res = kbn.calculateInterval(range, 1, null);
  178. expect(res.interval).to.be('1d');
  179. expect(res.intervalMs).to.be(86400000);
  180. });
  181. it('86399s 1 resolution', function() {
  182. var range = { from: dateMath.parse('now-86390s'), to: dateMath.parse('now') };
  183. var res = kbn.calculateInterval(range, 1, null);
  184. expect(res.interval).to.be('12h');
  185. expect(res.intervalMs).to.be(43200000);
  186. });
  187. });
  188. describe('hex', function() {
  189. it('positive integer', function() {
  190. var str = kbn.valueFormats.hex(100, 0);
  191. expect(str).to.be('64');
  192. });
  193. it('negative integer', function() {
  194. var str = kbn.valueFormats.hex(-100, 0);
  195. expect(str).to.be('-64');
  196. });
  197. it('null', function() {
  198. var str = kbn.valueFormats.hex(null, 0);
  199. expect(str).to.be('');
  200. });
  201. it('positive float', function() {
  202. var str = kbn.valueFormats.hex(50.52, 1);
  203. expect(str).to.be('32.8');
  204. });
  205. it('negative float', function() {
  206. var str = kbn.valueFormats.hex(-50.333, 2);
  207. expect(str).to.be('-32.547AE147AE14');
  208. });
  209. });
  210. describe('hex 0x', function() {
  211. it('positive integeter', function() {
  212. var str = kbn.valueFormats.hex0x(7999,0);
  213. expect(str).to.be('0x1F3F');
  214. });
  215. it('negative integer', function() {
  216. var str = kbn.valueFormats.hex0x(-584,0);
  217. expect(str).to.be('-0x248');
  218. });
  219. it('null', function() {
  220. var str = kbn.valueFormats.hex0x(null, 0);
  221. expect(str).to.be('');
  222. });
  223. it('positive float', function() {
  224. var str = kbn.valueFormats.hex0x(74.443, 3);
  225. expect(str).to.be('0x4A.716872B020C4');
  226. });
  227. it('negative float', function() {
  228. var str = kbn.valueFormats.hex0x(-65.458, 1);
  229. expect(str).to.be('-0x41.8');
  230. });
  231. });
  232. describe('duration', function() {
  233. it('null', function() {
  234. var str = kbn.toDuration(null, 0, "millisecond");
  235. expect(str).to.be('');
  236. });
  237. it('0 milliseconds', function() {
  238. var str = kbn.toDuration(0, 0, "millisecond");
  239. expect(str).to.be('0 milliseconds');
  240. });
  241. it('1 millisecond', function() {
  242. var str = kbn.toDuration(1, 0, "millisecond");
  243. expect(str).to.be('1 millisecond');
  244. });
  245. it('-1 millisecond', function() {
  246. var str = kbn.toDuration(-1, 0, "millisecond");
  247. expect(str).to.be('1 millisecond ago');
  248. });
  249. it('seconds', function() {
  250. var str = kbn.toDuration(1, 0, "second");
  251. expect(str).to.be('1 second');
  252. });
  253. it('minutes', function() {
  254. var str = kbn.toDuration(1, 0, "minute");
  255. expect(str).to.be('1 minute');
  256. });
  257. it('hours', function() {
  258. var str = kbn.toDuration(1, 0, "hour");
  259. expect(str).to.be('1 hour');
  260. });
  261. it('days', function() {
  262. var str = kbn.toDuration(1, 0, "day");
  263. expect(str).to.be('1 day');
  264. });
  265. it('weeks', function() {
  266. var str = kbn.toDuration(1, 0, "week");
  267. expect(str).to.be('1 week');
  268. });
  269. it('months', function() {
  270. var str = kbn.toDuration(1, 0, "month");
  271. expect(str).to.be('1 month');
  272. });
  273. it('years', function() {
  274. var str = kbn.toDuration(1, 0, "year");
  275. expect(str).to.be('1 year');
  276. });
  277. it('decimal days', function() {
  278. var str = kbn.toDuration(1.5, 2, "day");
  279. expect(str).to.be('1 day, 12 hours, 0 minutes');
  280. });
  281. it('decimal months', function() {
  282. var str = kbn.toDuration(1.5, 3, "month");
  283. expect(str).to.be('1 month, 2 weeks, 1 day, 0 hours');
  284. });
  285. it('no decimals', function() {
  286. var str = kbn.toDuration(38898367008, 0, "millisecond");
  287. expect(str).to.be('1 year');
  288. });
  289. it('1 decimal', function() {
  290. var str = kbn.toDuration(38898367008, 1, "millisecond");
  291. expect(str).to.be('1 year, 2 months');
  292. });
  293. it('too many decimals', function() {
  294. var str = kbn.toDuration(38898367008, 20, "millisecond");
  295. expect(str).to.be('1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds, 8 milliseconds');
  296. });
  297. it('floating point error', function() {
  298. var str = kbn.toDuration(36993906007, 8, "millisecond");
  299. expect(str).to.be('1 year, 2 months, 0 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds');
  300. });
  301. });
  302. });