kbn.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. import kbn from '../utils/kbn';
  2. import * as dateMath from '../utils/datemath';
  3. import moment from 'moment';
  4. describe('unit format menu', () => {
  5. const menu = kbn.getUnitFormats();
  6. menu.map(submenu => {
  7. describe('submenu ' + submenu.text, () => {
  8. it('should have a title', () => {
  9. expect(typeof submenu.text).toBe('string');
  10. });
  11. it('should have a submenu', () => {
  12. expect(Array.isArray(submenu.submenu)).toBe(true);
  13. });
  14. submenu.submenu.map(entry => {
  15. describe('entry ' + entry.text, () => {
  16. it('should have a title', () => {
  17. expect(typeof entry.text).toBe('string');
  18. });
  19. it('should have a format', () => {
  20. expect(typeof entry.value).toBe('string');
  21. });
  22. it('should have a valid format', () => {
  23. expect(typeof kbn.valueFormats[entry.value]).toBe('function');
  24. });
  25. });
  26. });
  27. });
  28. });
  29. });
  30. function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
  31. describe('value format: ' + desc, () => {
  32. it('should translate ' + value + ' as ' + result, () => {
  33. const scaledDecimals = tickDecimals - Math.floor(Math.log(tickSize) / Math.LN10);
  34. const str = kbn.valueFormats[desc](value, tickDecimals, scaledDecimals);
  35. expect(str).toBe(result);
  36. });
  37. });
  38. }
  39. describeValueFormat('ms', 0.0024, 0.0005, 4, '0.0024 ms');
  40. describeValueFormat('ms', 100, 1, 0, '100 ms');
  41. describeValueFormat('ms', 1250, 10, 0, '1.25 s');
  42. describeValueFormat('ms', 1250, 300, 0, '1.3 s');
  43. describeValueFormat('ms', 65150, 10000, 0, '1.1 min');
  44. describeValueFormat('ms', 6515000, 1500000, 0, '1.8 hour');
  45. describeValueFormat('ms', 651500000, 150000000, 0, '8 day');
  46. describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
  47. describeValueFormat('none', 0, 0, 2, '0');
  48. describeValueFormat('dB', 10, 1000, 2, '10.00 dB');
  49. describeValueFormat('percent', 0, 0, 0, '0%');
  50. describeValueFormat('percent', 53, 0, 1, '53.0%');
  51. describeValueFormat('percentunit', 0.0, 0, 0, '0%');
  52. describeValueFormat('percentunit', 0.278, 0, 1, '27.8%');
  53. describeValueFormat('percentunit', 1.0, 0, 0, '100%');
  54. describeValueFormat('currencyUSD', 7.42, 10000, 2, '$7.42');
  55. describeValueFormat('currencyUSD', 1532.82, 1000, 1, '$1.53K');
  56. describeValueFormat('currencyUSD', 18520408.7, 10000000, 0, '$19M');
  57. describeValueFormat('bytes', -1.57e308, -1.57e308, 2, 'NA');
  58. describeValueFormat('ns', 25, 1, 0, '25 ns');
  59. describeValueFormat('ns', 2558, 50, 0, '2.56 µs');
  60. describeValueFormat('ops', 123, 1, 0, '123 ops');
  61. describeValueFormat('rps', 456000, 1000, -1, '456K rps');
  62. describeValueFormat('rps', 123456789, 1000000, 2, '123.457M rps');
  63. describeValueFormat('wps', 789000000, 1000000, -1, '789M wps');
  64. describeValueFormat('iops', 11000000000, 1000000000, -1, '11B iops');
  65. describeValueFormat('s', 1.23456789e-7, 1e-10, 8, '123.5 ns');
  66. describeValueFormat('s', 1.23456789e-4, 1e-7, 5, '123.5 µs');
  67. describeValueFormat('s', 1.23456789e-3, 1e-6, 4, '1.235 ms');
  68. describeValueFormat('s', 1.23456789e-2, 1e-5, 3, '12.35 ms');
  69. describeValueFormat('s', 1.23456789e-1, 1e-4, 2, '123.5 ms');
  70. describeValueFormat('s', 24, 1, 0, '24 s');
  71. describeValueFormat('s', 246, 1, 0, '4.1 min');
  72. describeValueFormat('s', 24567, 100, 0, '6.82 hour');
  73. describeValueFormat('s', 24567890, 10000, 0, '40.62 week');
  74. describeValueFormat('s', 24567890000, 1000000, 0, '778.53 year');
  75. describeValueFormat('m', 24, 1, 0, '24 min');
  76. describeValueFormat('m', 246, 10, 0, '4.1 hour');
  77. describeValueFormat('m', 6545, 10, 0, '4.55 day');
  78. describeValueFormat('m', 24567, 100, 0, '2.44 week');
  79. describeValueFormat('m', 24567892, 10000, 0, '46.7 year');
  80. describeValueFormat('h', 21, 1, 0, '21 hour');
  81. describeValueFormat('h', 145, 1, 0, '6.04 day');
  82. describeValueFormat('h', 1234, 100, 0, '7.3 week');
  83. describeValueFormat('h', 9458, 1000, 0, '1.08 year');
  84. describeValueFormat('d', 3, 1, 0, '3 day');
  85. describeValueFormat('d', 245, 100, 0, '35 week');
  86. describeValueFormat('d', 2456, 10, 0, '6.73 year');
  87. describe('date time formats', () => {
  88. const epoch = 1505634997920;
  89. const utcTime = moment.utc(epoch);
  90. const browserTime = moment(epoch);
  91. it('should format as iso date', () => {
  92. const expected = browserTime.format('YYYY-MM-DD HH:mm:ss');
  93. const actual = kbn.valueFormats.dateTimeAsIso(epoch);
  94. expect(actual).toBe(expected);
  95. });
  96. it('should format as iso date (in UTC)', () => {
  97. const expected = utcTime.format('YYYY-MM-DD HH:mm:ss');
  98. const actual = kbn.valueFormats.dateTimeAsIso(epoch, true);
  99. expect(actual).toBe(expected);
  100. });
  101. it('should format as iso date and skip date when today', () => {
  102. const now = moment();
  103. const expected = now.format('HH:mm:ss');
  104. const actual = kbn.valueFormats.dateTimeAsIso(now.valueOf(), false);
  105. expect(actual).toBe(expected);
  106. });
  107. it('should format as iso date (in UTC) and skip date when today', () => {
  108. const now = moment.utc();
  109. const expected = now.format('HH:mm:ss');
  110. const actual = kbn.valueFormats.dateTimeAsIso(now.valueOf(), true);
  111. expect(actual).toBe(expected);
  112. });
  113. it('should format as US date', () => {
  114. const expected = browserTime.format('MM/DD/YYYY h:mm:ss a');
  115. const actual = kbn.valueFormats.dateTimeAsUS(epoch, false);
  116. expect(actual).toBe(expected);
  117. });
  118. it('should format as US date (in UTC)', () => {
  119. const expected = utcTime.format('MM/DD/YYYY h:mm:ss a');
  120. const actual = kbn.valueFormats.dateTimeAsUS(epoch, true);
  121. expect(actual).toBe(expected);
  122. });
  123. it('should format as US date and skip date when today', () => {
  124. const now = moment();
  125. const expected = now.format('h:mm:ss a');
  126. const actual = kbn.valueFormats.dateTimeAsUS(now.valueOf(), false);
  127. expect(actual).toBe(expected);
  128. });
  129. it('should format as US date (in UTC) and skip date when today', () => {
  130. const now = moment.utc();
  131. const expected = now.format('h:mm:ss a');
  132. const actual = kbn.valueFormats.dateTimeAsUS(now.valueOf(), true);
  133. expect(actual).toBe(expected);
  134. });
  135. it('should format as from now with days', () => {
  136. const daysAgo = moment().add(-7, 'd');
  137. const expected = '7 days ago';
  138. const actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), false);
  139. expect(actual).toBe(expected);
  140. });
  141. it('should format as from now with days (in UTC)', () => {
  142. const daysAgo = moment.utc().add(-7, 'd');
  143. const expected = '7 days ago';
  144. const actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), true);
  145. expect(actual).toBe(expected);
  146. });
  147. it('should format as from now with minutes', () => {
  148. const daysAgo = moment().add(-2, 'm');
  149. const expected = '2 minutes ago';
  150. const actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), false);
  151. expect(actual).toBe(expected);
  152. });
  153. it('should format as from now with minutes (in UTC)', () => {
  154. const daysAgo = moment.utc().add(-2, 'm');
  155. const expected = '2 minutes ago';
  156. const actual = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), true);
  157. expect(actual).toBe(expected);
  158. });
  159. });
  160. describe('kbn.toFixed and negative decimals', () => {
  161. it('should treat as zero decimals', () => {
  162. const str = kbn.toFixed(186.123, -2);
  163. expect(str).toBe('186');
  164. });
  165. });
  166. describe('kbn ms format when scaled decimals is null do not use it', () => {
  167. it('should use specified decimals', () => {
  168. const str = kbn.valueFormats['ms'](10000086.123, 1, null);
  169. expect(str).toBe('2.8 hour');
  170. });
  171. });
  172. describe('kbn kbytes format when scaled decimals is null do not use it', () => {
  173. it('should use specified decimals', () => {
  174. const str = kbn.valueFormats['kbytes'](10000000, 3, null);
  175. expect(str).toBe('9.537 GiB');
  176. });
  177. });
  178. describe('kbn deckbytes format when scaled decimals is null do not use it', () => {
  179. it('should use specified decimals', () => {
  180. const str = kbn.valueFormats['deckbytes'](10000000, 3, null);
  181. expect(str).toBe('10.000 GB');
  182. });
  183. });
  184. describe('kbn roundValue', () => {
  185. it('should should handle null value', () => {
  186. const str = kbn.roundValue(null, 2);
  187. expect(str).toBe(null);
  188. });
  189. it('should round value', () => {
  190. const str = kbn.roundValue(200.877, 2);
  191. expect(str).toBe(200.88);
  192. });
  193. });
  194. describe('calculateInterval', () => {
  195. it('1h 100 resultion', () => {
  196. const range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
  197. const res = kbn.calculateInterval(range, 100, null);
  198. expect(res.interval).toBe('30s');
  199. });
  200. it('10m 1600 resolution', () => {
  201. const range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  202. const res = kbn.calculateInterval(range, 1600, null);
  203. expect(res.interval).toBe('500ms');
  204. expect(res.intervalMs).toBe(500);
  205. });
  206. it('fixed user min interval', () => {
  207. const range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  208. const res = kbn.calculateInterval(range, 1600, '10s');
  209. expect(res.interval).toBe('10s');
  210. expect(res.intervalMs).toBe(10000);
  211. });
  212. it('short time range and user low limit', () => {
  213. const range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  214. const res = kbn.calculateInterval(range, 1600, '>10s');
  215. expect(res.interval).toBe('10s');
  216. });
  217. it('large time range and user low limit', () => {
  218. const range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') };
  219. const res = kbn.calculateInterval(range, 1000, '>10s');
  220. expect(res.interval).toBe('20m');
  221. });
  222. it('10s 900 resolution and user low limit in ms', () => {
  223. const range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') };
  224. const res = kbn.calculateInterval(range, 900, '>15ms');
  225. expect(res.interval).toBe('15ms');
  226. });
  227. it('1d 1 resolution', () => {
  228. const range = { from: dateMath.parse('now-1d'), to: dateMath.parse('now') };
  229. const res = kbn.calculateInterval(range, 1, null);
  230. expect(res.interval).toBe('1d');
  231. expect(res.intervalMs).toBe(86400000);
  232. });
  233. it('86399s 1 resolution', () => {
  234. const range = {
  235. from: dateMath.parse('now-86390s'),
  236. to: dateMath.parse('now'),
  237. };
  238. const res = kbn.calculateInterval(range, 1, null);
  239. expect(res.interval).toBe('12h');
  240. expect(res.intervalMs).toBe(43200000);
  241. });
  242. });
  243. describe('hex', () => {
  244. it('positive integer', () => {
  245. const str = kbn.valueFormats.hex(100, 0);
  246. expect(str).toBe('64');
  247. });
  248. it('negative integer', () => {
  249. const str = kbn.valueFormats.hex(-100, 0);
  250. expect(str).toBe('-64');
  251. });
  252. it('null', () => {
  253. const str = kbn.valueFormats.hex(null, 0);
  254. expect(str).toBe('');
  255. });
  256. it('positive float', () => {
  257. const str = kbn.valueFormats.hex(50.52, 1);
  258. expect(str).toBe('32.8');
  259. });
  260. it('negative float', () => {
  261. const str = kbn.valueFormats.hex(-50.333, 2);
  262. expect(str).toBe('-32.547AE147AE14');
  263. });
  264. });
  265. describe('hex 0x', () => {
  266. it('positive integeter', () => {
  267. const str = kbn.valueFormats.hex0x(7999, 0);
  268. expect(str).toBe('0x1F3F');
  269. });
  270. it('negative integer', () => {
  271. const str = kbn.valueFormats.hex0x(-584, 0);
  272. expect(str).toBe('-0x248');
  273. });
  274. it('null', () => {
  275. const str = kbn.valueFormats.hex0x(null, 0);
  276. expect(str).toBe('');
  277. });
  278. it('positive float', () => {
  279. const str = kbn.valueFormats.hex0x(74.443, 3);
  280. expect(str).toBe('0x4A.716872B020C4');
  281. });
  282. it('negative float', () => {
  283. const str = kbn.valueFormats.hex0x(-65.458, 1);
  284. expect(str).toBe('-0x41.8');
  285. });
  286. });
  287. describe('duration', () => {
  288. it('null', () => {
  289. const str = kbn.toDuration(null, 0, 'millisecond');
  290. expect(str).toBe('');
  291. });
  292. it('0 milliseconds', () => {
  293. const str = kbn.toDuration(0, 0, 'millisecond');
  294. expect(str).toBe('0 milliseconds');
  295. });
  296. it('1 millisecond', () => {
  297. const str = kbn.toDuration(1, 0, 'millisecond');
  298. expect(str).toBe('1 millisecond');
  299. });
  300. it('-1 millisecond', () => {
  301. const str = kbn.toDuration(-1, 0, 'millisecond');
  302. expect(str).toBe('1 millisecond ago');
  303. });
  304. it('seconds', () => {
  305. const str = kbn.toDuration(1, 0, 'second');
  306. expect(str).toBe('1 second');
  307. });
  308. it('minutes', () => {
  309. const str = kbn.toDuration(1, 0, 'minute');
  310. expect(str).toBe('1 minute');
  311. });
  312. it('hours', () => {
  313. const str = kbn.toDuration(1, 0, 'hour');
  314. expect(str).toBe('1 hour');
  315. });
  316. it('days', () => {
  317. const str = kbn.toDuration(1, 0, 'day');
  318. expect(str).toBe('1 day');
  319. });
  320. it('weeks', () => {
  321. const str = kbn.toDuration(1, 0, 'week');
  322. expect(str).toBe('1 week');
  323. });
  324. it('months', () => {
  325. const str = kbn.toDuration(1, 0, 'month');
  326. expect(str).toBe('1 month');
  327. });
  328. it('years', () => {
  329. const str = kbn.toDuration(1, 0, 'year');
  330. expect(str).toBe('1 year');
  331. });
  332. it('decimal days', () => {
  333. const str = kbn.toDuration(1.5, 2, 'day');
  334. expect(str).toBe('1 day, 12 hours, 0 minutes');
  335. });
  336. it('decimal months', () => {
  337. const str = kbn.toDuration(1.5, 3, 'month');
  338. expect(str).toBe('1 month, 2 weeks, 1 day, 0 hours');
  339. });
  340. it('no decimals', () => {
  341. const str = kbn.toDuration(38898367008, 0, 'millisecond');
  342. expect(str).toBe('1 year');
  343. });
  344. it('1 decimal', () => {
  345. const str = kbn.toDuration(38898367008, 1, 'millisecond');
  346. expect(str).toBe('1 year, 2 months');
  347. });
  348. it('too many decimals', () => {
  349. const str = kbn.toDuration(38898367008, 20, 'millisecond');
  350. expect(str).toBe('1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds, 8 milliseconds');
  351. });
  352. it('floating point error', () => {
  353. const str = kbn.toDuration(36993906007, 8, 'millisecond');
  354. expect(str).toBe('1 year, 2 months, 0 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds');
  355. });
  356. });
  357. describe('clock', () => {
  358. it('null', () => {
  359. const str = kbn.toClock(null, 0);
  360. expect(str).toBe('');
  361. });
  362. it('size less than 1 second', () => {
  363. const str = kbn.toClock(999, 0);
  364. expect(str).toBe('999ms');
  365. });
  366. describe('size less than 1 minute', () => {
  367. it('default', () => {
  368. const str = kbn.toClock(59999);
  369. expect(str).toBe('59s:999ms');
  370. });
  371. it('decimals equals 0', () => {
  372. const str = kbn.toClock(59999, 0);
  373. expect(str).toBe('59s');
  374. });
  375. });
  376. describe('size less than 1 hour', () => {
  377. it('default', () => {
  378. const str = kbn.toClock(3599999);
  379. expect(str).toBe('59m:59s:999ms');
  380. });
  381. it('decimals equals 0', () => {
  382. const str = kbn.toClock(3599999, 0);
  383. expect(str).toBe('59m');
  384. });
  385. it('decimals equals 1', () => {
  386. const str = kbn.toClock(3599999, 1);
  387. expect(str).toBe('59m:59s');
  388. });
  389. });
  390. describe('size greater than or equal 1 hour', () => {
  391. it('default', () => {
  392. const str = kbn.toClock(7199999);
  393. expect(str).toBe('01h:59m:59s:999ms');
  394. });
  395. it('decimals equals 0', () => {
  396. const str = kbn.toClock(7199999, 0);
  397. expect(str).toBe('01h');
  398. });
  399. it('decimals equals 1', () => {
  400. const str = kbn.toClock(7199999, 1);
  401. expect(str).toBe('01h:59m');
  402. });
  403. it('decimals equals 2', () => {
  404. const str = kbn.toClock(7199999, 2);
  405. expect(str).toBe('01h:59m:59s');
  406. });
  407. });
  408. describe('size greater than or equal 1 day', () => {
  409. it('default', () => {
  410. const str = kbn.toClock(89999999);
  411. expect(str).toBe('24h:59m:59s:999ms');
  412. });
  413. it('decimals equals 0', () => {
  414. const str = kbn.toClock(89999999, 0);
  415. expect(str).toBe('24h');
  416. });
  417. it('decimals equals 1', () => {
  418. const str = kbn.toClock(89999999, 1);
  419. expect(str).toBe('24h:59m');
  420. });
  421. it('decimals equals 2', () => {
  422. const str = kbn.toClock(89999999, 2);
  423. expect(str).toBe('24h:59m:59s');
  424. });
  425. });
  426. });
  427. describe('volume', () => {
  428. it('1000m3', () => {
  429. const str = kbn.valueFormats['m3'](1000, 1, null);
  430. expect(str).toBe('1000.0 m³');
  431. });
  432. });
  433. describe('hh:mm:ss', () => {
  434. it('00:04:06', () => {
  435. const str = kbn.valueFormats['dthms'](246, 1);
  436. expect(str).toBe('00:04:06');
  437. });
  438. it('24:00:00', () => {
  439. const str = kbn.valueFormats['dthms'](86400, 1);
  440. expect(str).toBe('24:00:00');
  441. });
  442. it('6824413:53:20', () => {
  443. const str = kbn.valueFormats['dthms'](24567890000, 1);
  444. expect(str).toBe('6824413:53:20');
  445. });
  446. });