kbn.ts 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. var kbn: any = {};
  4. kbn.valueFormats = {};
  5. kbn.regexEscape = function(value) {
  6. return value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&');
  7. };
  8. ///// HELPER FUNCTIONS /////
  9. kbn.round_interval = function(interval) {
  10. switch (true) {
  11. // 0.015s
  12. case interval < 15:
  13. return 10; // 0.01s
  14. // 0.035s
  15. case interval < 35:
  16. return 20; // 0.02s
  17. // 0.075s
  18. case interval < 75:
  19. return 50; // 0.05s
  20. // 0.15s
  21. case interval < 150:
  22. return 100; // 0.1s
  23. // 0.35s
  24. case interval < 350:
  25. return 200; // 0.2s
  26. // 0.75s
  27. case interval < 750:
  28. return 500; // 0.5s
  29. // 1.5s
  30. case interval < 1500:
  31. return 1000; // 1s
  32. // 3.5s
  33. case interval < 3500:
  34. return 2000; // 2s
  35. // 7.5s
  36. case interval < 7500:
  37. return 5000; // 5s
  38. // 12.5s
  39. case interval < 12500:
  40. return 10000; // 10s
  41. // 17.5s
  42. case interval < 17500:
  43. return 15000; // 15s
  44. // 25s
  45. case interval < 25000:
  46. return 20000; // 20s
  47. // 45s
  48. case interval < 45000:
  49. return 30000; // 30s
  50. // 1.5m
  51. case interval < 90000:
  52. return 60000; // 1m
  53. // 3.5m
  54. case interval < 210000:
  55. return 120000; // 2m
  56. // 7.5m
  57. case interval < 450000:
  58. return 300000; // 5m
  59. // 12.5m
  60. case interval < 750000:
  61. return 600000; // 10m
  62. // 12.5m
  63. case interval < 1050000:
  64. return 900000; // 15m
  65. // 25m
  66. case interval < 1500000:
  67. return 1200000; // 20m
  68. // 45m
  69. case interval < 2700000:
  70. return 1800000; // 30m
  71. // 1.5h
  72. case interval < 5400000:
  73. return 3600000; // 1h
  74. // 2.5h
  75. case interval < 9000000:
  76. return 7200000; // 2h
  77. // 4.5h
  78. case interval < 16200000:
  79. return 10800000; // 3h
  80. // 9h
  81. case interval < 32400000:
  82. return 21600000; // 6h
  83. // 1d
  84. case interval < 86400000:
  85. return 43200000; // 12h
  86. // 1w
  87. case interval < 604800000:
  88. return 86400000; // 1d
  89. // 3w
  90. case interval < 1814400000:
  91. return 604800000; // 1w
  92. // 6w
  93. case interval < 3628800000:
  94. return 2592000000; // 30d
  95. default:
  96. return 31536000000; // 1y
  97. }
  98. };
  99. kbn.secondsToHms = function(seconds) {
  100. var numyears = Math.floor(seconds / 31536000);
  101. if (numyears) {
  102. return numyears + 'y';
  103. }
  104. var numdays = Math.floor((seconds % 31536000) / 86400);
  105. if (numdays) {
  106. return numdays + 'd';
  107. }
  108. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  109. if (numhours) {
  110. return numhours + 'h';
  111. }
  112. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  113. if (numminutes) {
  114. return numminutes + 'm';
  115. }
  116. var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
  117. if (numseconds) {
  118. return numseconds + 's';
  119. }
  120. var nummilliseconds = Math.floor(seconds * 1000.0);
  121. if (nummilliseconds) {
  122. return nummilliseconds + 'ms';
  123. }
  124. return 'less than a millisecond'; //'just now' //or other string you like;
  125. };
  126. kbn.to_percent = function(nr, outof) {
  127. return Math.floor(nr / outof * 10000) / 100 + '%';
  128. };
  129. kbn.addslashes = function(str) {
  130. str = str.replace(/\\/g, '\\\\');
  131. str = str.replace(/\'/g, "\\'");
  132. str = str.replace(/\"/g, '\\"');
  133. str = str.replace(/\0/g, '\\0');
  134. return str;
  135. };
  136. kbn.interval_regex = /(\d+(?:\.\d+)?)(ms|[Mwdhmsy])/;
  137. // histogram & trends
  138. kbn.intervals_in_seconds = {
  139. y: 31536000,
  140. M: 2592000,
  141. w: 604800,
  142. d: 86400,
  143. h: 3600,
  144. m: 60,
  145. s: 1,
  146. ms: 0.001,
  147. };
  148. kbn.calculateInterval = function(range, resolution, lowLimitInterval) {
  149. var lowLimitMs = 1; // 1 millisecond default low limit
  150. var intervalMs;
  151. if (lowLimitInterval) {
  152. if (lowLimitInterval[0] === '>') {
  153. lowLimitInterval = lowLimitInterval.slice(1);
  154. }
  155. lowLimitMs = kbn.interval_to_ms(lowLimitInterval);
  156. }
  157. intervalMs = kbn.round_interval((range.to.valueOf() - range.from.valueOf()) / resolution);
  158. if (lowLimitMs > intervalMs) {
  159. intervalMs = lowLimitMs;
  160. }
  161. return {
  162. intervalMs: intervalMs,
  163. interval: kbn.secondsToHms(intervalMs / 1000),
  164. };
  165. };
  166. kbn.describe_interval = function(str) {
  167. var matches = str.match(kbn.interval_regex);
  168. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  169. throw new Error('Invalid interval string, expecting a number followed by one of "Mwdhmsy"');
  170. } else {
  171. return {
  172. sec: kbn.intervals_in_seconds[matches[2]],
  173. type: matches[2],
  174. count: parseInt(matches[1], 10),
  175. };
  176. }
  177. };
  178. kbn.interval_to_ms = function(str) {
  179. var info = kbn.describe_interval(str);
  180. return info.sec * 1000 * info.count;
  181. };
  182. kbn.interval_to_seconds = function(str) {
  183. var info = kbn.describe_interval(str);
  184. return info.sec * info.count;
  185. };
  186. kbn.query_color_dot = function(color, diameter) {
  187. return (
  188. '<div class="icon-circle" style="' +
  189. ['display:inline-block', 'color:' + color, 'font-size:' + diameter + 'px'].join(';') +
  190. '"></div>'
  191. );
  192. };
  193. kbn.slugifyForUrl = function(str) {
  194. return str
  195. .toLowerCase()
  196. .replace(/[^\w ]+/g, '')
  197. .replace(/ +/g, '-');
  198. };
  199. kbn.stringToJsRegex = function(str) {
  200. if (str[0] !== '/') {
  201. return new RegExp('^' + str + '$');
  202. }
  203. var match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
  204. return new RegExp(match[1], match[2]);
  205. };
  206. kbn.toFixed = function(value, decimals) {
  207. if (value === null) {
  208. return '';
  209. }
  210. var factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
  211. var formatted = String(Math.round(value * factor) / factor);
  212. // if exponent return directly
  213. if (formatted.indexOf('e') !== -1 || value === 0) {
  214. return formatted;
  215. }
  216. // If tickDecimals was specified, ensure that we have exactly that
  217. // much precision; otherwise default to the value's own precision.
  218. if (decimals != null) {
  219. var decimalPos = formatted.indexOf('.');
  220. var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
  221. if (precision < decimals) {
  222. return (precision ? formatted : formatted + '.') + String(factor).substr(1, decimals - precision);
  223. }
  224. }
  225. return formatted;
  226. };
  227. kbn.toFixedScaled = function(value, decimals, scaledDecimals, additionalDecimals, ext) {
  228. if (scaledDecimals === null) {
  229. return kbn.toFixed(value, decimals) + ext;
  230. } else {
  231. return kbn.toFixed(value, scaledDecimals + additionalDecimals) + ext;
  232. }
  233. };
  234. kbn.roundValue = function(num, decimals) {
  235. if (num === null) {
  236. return null;
  237. }
  238. var n = Math.pow(10, decimals);
  239. var formatted = (n * num).toFixed(decimals);
  240. return Math.round(parseFloat(formatted)) / n;
  241. };
  242. ///// FORMAT FUNCTION CONSTRUCTORS /////
  243. kbn.formatBuilders = {};
  244. // Formatter which always appends a fixed unit string to the value. No
  245. // scaling of the value is performed.
  246. kbn.formatBuilders.fixedUnit = function(unit) {
  247. return function(size, decimals) {
  248. if (size === null) {
  249. return '';
  250. }
  251. return kbn.toFixed(size, decimals) + ' ' + unit;
  252. };
  253. };
  254. // Formatter which scales the unit string geometrically according to the given
  255. // numeric factor. Repeatedly scales the value down by the factor until it is
  256. // less than the factor in magnitude, or the end of the array is reached.
  257. kbn.formatBuilders.scaledUnits = function(factor, extArray) {
  258. return function(size, decimals, scaledDecimals) {
  259. if (size === null) {
  260. return '';
  261. }
  262. var steps = 0;
  263. var limit = extArray.length;
  264. while (Math.abs(size) >= factor) {
  265. steps++;
  266. size /= factor;
  267. if (steps >= limit) {
  268. return 'NA';
  269. }
  270. }
  271. if (steps > 0 && scaledDecimals !== null) {
  272. decimals = scaledDecimals + 3 * steps;
  273. }
  274. return kbn.toFixed(size, decimals) + extArray[steps];
  275. };
  276. };
  277. // Extension of the scaledUnits builder which uses SI decimal prefixes. If an
  278. // offset is given, it adjusts the starting units at the given prefix; a value
  279. // of 0 starts at no scale; -3 drops to nano, +2 starts at mega, etc.
  280. kbn.formatBuilders.decimalSIPrefix = function(unit, offset) {
  281. var prefixes = ['n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
  282. prefixes = prefixes.slice(3 + (offset || 0));
  283. var units = prefixes.map(function(p) {
  284. return ' ' + p + unit;
  285. });
  286. return kbn.formatBuilders.scaledUnits(1000, units);
  287. };
  288. // Extension of the scaledUnits builder which uses SI binary prefixes. If
  289. // offset is given, it starts the units at the given prefix; otherwise, the
  290. // offset defaults to zero and the initial unit is not prefixed.
  291. kbn.formatBuilders.binarySIPrefix = function(unit, offset) {
  292. var prefixes = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'].slice(offset);
  293. var units = prefixes.map(function(p) {
  294. return ' ' + p + unit;
  295. });
  296. return kbn.formatBuilders.scaledUnits(1024, units);
  297. };
  298. // Currency formatter for prefixing a symbol onto a number. Supports scaling
  299. // up to the trillions.
  300. kbn.formatBuilders.currency = function(symbol) {
  301. var units = ['', 'K', 'M', 'B', 'T'];
  302. var scaler = kbn.formatBuilders.scaledUnits(1000, units);
  303. return function(size, decimals, scaledDecimals) {
  304. if (size === null) {
  305. return '';
  306. }
  307. var scaled = scaler(size, decimals, scaledDecimals);
  308. return symbol + scaled;
  309. };
  310. };
  311. kbn.formatBuilders.simpleCountUnit = function(symbol) {
  312. var units = ['', 'K', 'M', 'B', 'T'];
  313. var scaler = kbn.formatBuilders.scaledUnits(1000, units);
  314. return function(size, decimals, scaledDecimals) {
  315. if (size === null) {
  316. return '';
  317. }
  318. var scaled = scaler(size, decimals, scaledDecimals);
  319. return scaled + ' ' + symbol;
  320. };
  321. };
  322. ///// VALUE FORMATS /////
  323. // Dimensionless Units
  324. kbn.valueFormats.none = kbn.toFixed;
  325. kbn.valueFormats.short = kbn.formatBuilders.scaledUnits(1000, [
  326. '',
  327. ' K',
  328. ' Mil',
  329. ' Bil',
  330. ' Tri',
  331. ' Quadr',
  332. ' Quint',
  333. ' Sext',
  334. ' Sept',
  335. ]);
  336. kbn.valueFormats.dB = kbn.formatBuilders.fixedUnit('dB');
  337. kbn.valueFormats.ppm = kbn.formatBuilders.fixedUnit('ppm');
  338. kbn.valueFormats.percent = function(size, decimals) {
  339. if (size === null) {
  340. return '';
  341. }
  342. return kbn.toFixed(size, decimals) + '%';
  343. };
  344. kbn.valueFormats.percentunit = function(size, decimals) {
  345. if (size === null) {
  346. return '';
  347. }
  348. return kbn.toFixed(100 * size, decimals) + '%';
  349. };
  350. /* Formats the value to hex. Uses float if specified decimals are not 0.
  351. * There are two options, one with 0x, and one without */
  352. kbn.valueFormats.hex = function(value, decimals) {
  353. if (value == null) {
  354. return '';
  355. }
  356. return parseFloat(kbn.toFixed(value, decimals))
  357. .toString(16)
  358. .toUpperCase();
  359. };
  360. kbn.valueFormats.hex0x = function(value, decimals) {
  361. if (value == null) {
  362. return '';
  363. }
  364. var hexString = kbn.valueFormats.hex(value, decimals);
  365. if (hexString.substring(0, 1) === '-') {
  366. return '-0x' + hexString.substring(1);
  367. }
  368. return '0x' + hexString;
  369. };
  370. kbn.valueFormats.sci = function(value, decimals) {
  371. return value.toExponential(decimals);
  372. };
  373. kbn.valueFormats.locale = function(value, decimals) {
  374. return value.toLocaleString(undefined, { maximumFractionDigits: decimals });
  375. };
  376. // Currencies
  377. kbn.valueFormats.currencyUSD = kbn.formatBuilders.currency('$');
  378. kbn.valueFormats.currencyGBP = kbn.formatBuilders.currency('£');
  379. kbn.valueFormats.currencyEUR = kbn.formatBuilders.currency('€');
  380. kbn.valueFormats.currencyJPY = kbn.formatBuilders.currency('¥');
  381. kbn.valueFormats.currencyRUB = kbn.formatBuilders.currency('₽');
  382. kbn.valueFormats.currencyUAH = kbn.formatBuilders.currency('₴');
  383. kbn.valueFormats.currencyBRL = kbn.formatBuilders.currency('R$');
  384. kbn.valueFormats.currencyDKK = kbn.formatBuilders.currency('kr');
  385. kbn.valueFormats.currencyISK = kbn.formatBuilders.currency('kr');
  386. kbn.valueFormats.currencyNOK = kbn.formatBuilders.currency('kr');
  387. kbn.valueFormats.currencySEK = kbn.formatBuilders.currency('kr');
  388. // Data (Binary)
  389. kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b');
  390. kbn.valueFormats.bytes = kbn.formatBuilders.binarySIPrefix('B');
  391. kbn.valueFormats.kbytes = kbn.formatBuilders.binarySIPrefix('B', 1);
  392. kbn.valueFormats.mbytes = kbn.formatBuilders.binarySIPrefix('B', 2);
  393. kbn.valueFormats.gbytes = kbn.formatBuilders.binarySIPrefix('B', 3);
  394. // Data (Decimal)
  395. kbn.valueFormats.decbits = kbn.formatBuilders.decimalSIPrefix('b');
  396. kbn.valueFormats.decbytes = kbn.formatBuilders.decimalSIPrefix('B');
  397. kbn.valueFormats.deckbytes = kbn.formatBuilders.decimalSIPrefix('B', 1);
  398. kbn.valueFormats.decmbytes = kbn.formatBuilders.decimalSIPrefix('B', 2);
  399. kbn.valueFormats.decgbytes = kbn.formatBuilders.decimalSIPrefix('B', 3);
  400. // Data Rate
  401. kbn.valueFormats.pps = kbn.formatBuilders.decimalSIPrefix('pps');
  402. kbn.valueFormats.bps = kbn.formatBuilders.decimalSIPrefix('bps');
  403. kbn.valueFormats.Bps = kbn.formatBuilders.decimalSIPrefix('Bps');
  404. kbn.valueFormats.KBs = kbn.formatBuilders.decimalSIPrefix('Bs', 1);
  405. kbn.valueFormats.Kbits = kbn.formatBuilders.decimalSIPrefix('bps', 1);
  406. kbn.valueFormats.MBs = kbn.formatBuilders.decimalSIPrefix('Bs', 2);
  407. kbn.valueFormats.Mbits = kbn.formatBuilders.decimalSIPrefix('bps', 2);
  408. kbn.valueFormats.GBs = kbn.formatBuilders.decimalSIPrefix('Bs', 3);
  409. kbn.valueFormats.Gbits = kbn.formatBuilders.decimalSIPrefix('bps', 3);
  410. // Throughput
  411. kbn.valueFormats.ops = kbn.formatBuilders.simpleCountUnit('ops');
  412. kbn.valueFormats.rps = kbn.formatBuilders.simpleCountUnit('rps');
  413. kbn.valueFormats.wps = kbn.formatBuilders.simpleCountUnit('wps');
  414. kbn.valueFormats.iops = kbn.formatBuilders.simpleCountUnit('iops');
  415. kbn.valueFormats.opm = kbn.formatBuilders.simpleCountUnit('opm');
  416. kbn.valueFormats.rpm = kbn.formatBuilders.simpleCountUnit('rpm');
  417. kbn.valueFormats.wpm = kbn.formatBuilders.simpleCountUnit('wpm');
  418. // Energy
  419. kbn.valueFormats.watt = kbn.formatBuilders.decimalSIPrefix('W');
  420. kbn.valueFormats.kwatt = kbn.formatBuilders.decimalSIPrefix('W', 1);
  421. kbn.valueFormats.mwatt = kbn.formatBuilders.decimalSIPrefix('W', -1);
  422. kbn.valueFormats.kwattm = kbn.formatBuilders.decimalSIPrefix('W/Min', 1);
  423. kbn.valueFormats.voltamp = kbn.formatBuilders.decimalSIPrefix('VA');
  424. kbn.valueFormats.kvoltamp = kbn.formatBuilders.decimalSIPrefix('VA', 1);
  425. kbn.valueFormats.voltampreact = kbn.formatBuilders.decimalSIPrefix('var');
  426. kbn.valueFormats.kvoltampreact = kbn.formatBuilders.decimalSIPrefix('var', 1);
  427. kbn.valueFormats.watth = kbn.formatBuilders.decimalSIPrefix('Wh');
  428. kbn.valueFormats.kwatth = kbn.formatBuilders.decimalSIPrefix('Wh', 1);
  429. kbn.valueFormats.joule = kbn.formatBuilders.decimalSIPrefix('J');
  430. kbn.valueFormats.ev = kbn.formatBuilders.decimalSIPrefix('eV');
  431. kbn.valueFormats.amp = kbn.formatBuilders.decimalSIPrefix('A');
  432. kbn.valueFormats.kamp = kbn.formatBuilders.decimalSIPrefix('A', 1);
  433. kbn.valueFormats.mamp = kbn.formatBuilders.decimalSIPrefix('A', -1);
  434. kbn.valueFormats.volt = kbn.formatBuilders.decimalSIPrefix('V');
  435. kbn.valueFormats.kvolt = kbn.formatBuilders.decimalSIPrefix('V', 1);
  436. kbn.valueFormats.mvolt = kbn.formatBuilders.decimalSIPrefix('V', -1);
  437. kbn.valueFormats.dBm = kbn.formatBuilders.decimalSIPrefix('dBm');
  438. kbn.valueFormats.ohm = kbn.formatBuilders.decimalSIPrefix('Ω');
  439. kbn.valueFormats.lumens = kbn.formatBuilders.decimalSIPrefix('Lm');
  440. // Temperature
  441. kbn.valueFormats.celsius = kbn.formatBuilders.fixedUnit('°C');
  442. kbn.valueFormats.farenheit = kbn.formatBuilders.fixedUnit('°F');
  443. kbn.valueFormats.kelvin = kbn.formatBuilders.fixedUnit('K');
  444. kbn.valueFormats.humidity = kbn.formatBuilders.fixedUnit('%H');
  445. // Pressure
  446. kbn.valueFormats.pressurebar = kbn.formatBuilders.decimalSIPrefix('bar');
  447. kbn.valueFormats.pressurembar = kbn.formatBuilders.decimalSIPrefix('bar', -1);
  448. kbn.valueFormats.pressurekbar = kbn.formatBuilders.decimalSIPrefix('bar', 1);
  449. kbn.valueFormats.pressurehpa = kbn.formatBuilders.fixedUnit('hPa');
  450. kbn.valueFormats.pressurehg = kbn.formatBuilders.fixedUnit('"Hg');
  451. kbn.valueFormats.pressurepsi = kbn.formatBuilders.scaledUnits(1000, [' psi', ' ksi', ' Mpsi']);
  452. // Force
  453. kbn.valueFormats.forceNm = kbn.formatBuilders.decimalSIPrefix('Nm');
  454. kbn.valueFormats.forcekNm = kbn.formatBuilders.decimalSIPrefix('Nm', 1);
  455. kbn.valueFormats.forceN = kbn.formatBuilders.decimalSIPrefix('N');
  456. kbn.valueFormats.forcekN = kbn.formatBuilders.decimalSIPrefix('N', 1);
  457. // Length
  458. kbn.valueFormats.lengthm = kbn.formatBuilders.decimalSIPrefix('m');
  459. kbn.valueFormats.lengthmm = kbn.formatBuilders.decimalSIPrefix('m', -1);
  460. kbn.valueFormats.lengthkm = kbn.formatBuilders.decimalSIPrefix('m', 1);
  461. kbn.valueFormats.lengthmi = kbn.formatBuilders.fixedUnit('mi');
  462. kbn.valueFormats.lengthft = kbn.formatBuilders.fixedUnit('ft');
  463. // Area
  464. kbn.valueFormats.areaM2 = kbn.formatBuilders.fixedUnit('m²');
  465. kbn.valueFormats.areaF2 = kbn.formatBuilders.fixedUnit('ft²');
  466. kbn.valueFormats.areaMI2 = kbn.formatBuilders.fixedUnit('mi²');
  467. // Mass
  468. kbn.valueFormats.massmg = kbn.formatBuilders.decimalSIPrefix('g', -1);
  469. kbn.valueFormats.massg = kbn.formatBuilders.decimalSIPrefix('g');
  470. kbn.valueFormats.masskg = kbn.formatBuilders.decimalSIPrefix('g', 1);
  471. kbn.valueFormats.masst = kbn.formatBuilders.fixedUnit('t');
  472. // Velocity
  473. kbn.valueFormats.velocityms = kbn.formatBuilders.fixedUnit('m/s');
  474. kbn.valueFormats.velocitykmh = kbn.formatBuilders.fixedUnit('km/h');
  475. kbn.valueFormats.velocitymph = kbn.formatBuilders.fixedUnit('mph');
  476. kbn.valueFormats.velocityknot = kbn.formatBuilders.fixedUnit('kn');
  477. // Acceleration
  478. kbn.valueFormats.accMS2 = kbn.formatBuilders.fixedUnit('m/sec²');
  479. kbn.valueFormats.accFS2 = kbn.formatBuilders.fixedUnit('f/sec²');
  480. kbn.valueFormats.accG = kbn.formatBuilders.fixedUnit('g');
  481. // Volume
  482. kbn.valueFormats.litre = kbn.formatBuilders.decimalSIPrefix('L');
  483. kbn.valueFormats.mlitre = kbn.formatBuilders.decimalSIPrefix('L', -1);
  484. kbn.valueFormats.m3 = kbn.formatBuilders.decimalSIPrefix('m3');
  485. kbn.valueFormats.dm3 = kbn.formatBuilders.decimalSIPrefix('dm3');
  486. kbn.valueFormats.gallons = kbn.formatBuilders.fixedUnit('gal');
  487. // Flow
  488. kbn.valueFormats.flowgpm = kbn.formatBuilders.fixedUnit('gpm');
  489. kbn.valueFormats.flowcms = kbn.formatBuilders.fixedUnit('cms');
  490. kbn.valueFormats.flowcfs = kbn.formatBuilders.fixedUnit('cfs');
  491. kbn.valueFormats.flowcfm = kbn.formatBuilders.fixedUnit('cfm');
  492. // Angle
  493. kbn.valueFormats.degree = kbn.formatBuilders.fixedUnit('°');
  494. kbn.valueFormats.radian = kbn.formatBuilders.fixedUnit('rad');
  495. kbn.valueFormats.grad = kbn.formatBuilders.fixedUnit('grad');
  496. // Time
  497. kbn.valueFormats.hertz = kbn.formatBuilders.decimalSIPrefix('Hz');
  498. kbn.valueFormats.ms = function(size, decimals, scaledDecimals) {
  499. if (size === null) {
  500. return '';
  501. }
  502. if (Math.abs(size) < 1000) {
  503. return kbn.toFixed(size, decimals) + ' ms';
  504. } else if (Math.abs(size) < 60000) {
  505. // Less than 1 min
  506. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, ' s');
  507. } else if (Math.abs(size) < 3600000) {
  508. // Less than 1 hour, devide in minutes
  509. return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, ' min');
  510. } else if (Math.abs(size) < 86400000) {
  511. // Less than one day, devide in hours
  512. return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, ' hour');
  513. } else if (Math.abs(size) < 31536000000) {
  514. // Less than one year, devide in days
  515. return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, ' day');
  516. }
  517. return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, ' year');
  518. };
  519. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  520. if (size === null) {
  521. return '';
  522. }
  523. // Less than 1 µs, devide in ns
  524. if (Math.abs(size) < 0.000001) {
  525. return kbn.toFixedScaled(size * 1e9, decimals, scaledDecimals - decimals, -9, ' ns');
  526. }
  527. // Less than 1 ms, devide in µs
  528. if (Math.abs(size) < 0.001) {
  529. return kbn.toFixedScaled(size * 1e6, decimals, scaledDecimals - decimals, -6, ' µs');
  530. }
  531. // Less than 1 second, devide in ms
  532. if (Math.abs(size) < 1) {
  533. return kbn.toFixedScaled(size * 1e3, decimals, scaledDecimals - decimals, -3, ' ms');
  534. }
  535. if (Math.abs(size) < 60) {
  536. return kbn.toFixed(size, decimals) + ' s';
  537. } else if (Math.abs(size) < 3600) {
  538. // Less than 1 hour, devide in minutes
  539. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, ' min');
  540. } else if (Math.abs(size) < 86400) {
  541. // Less than one day, devide in hours
  542. return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, ' hour');
  543. } else if (Math.abs(size) < 604800) {
  544. // Less than one week, devide in days
  545. return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, ' day');
  546. } else if (Math.abs(size) < 31536000) {
  547. // Less than one year, devide in week
  548. return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, ' week');
  549. }
  550. return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, ' year');
  551. };
  552. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  553. if (size === null) {
  554. return '';
  555. }
  556. if (Math.abs(size) < 1000) {
  557. return kbn.toFixed(size, decimals) + ' µs';
  558. } else if (Math.abs(size) < 1000000) {
  559. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, ' ms');
  560. } else {
  561. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, ' s');
  562. }
  563. };
  564. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  565. if (size === null) {
  566. return '';
  567. }
  568. if (Math.abs(size) < 1000) {
  569. return kbn.toFixed(size, decimals) + ' ns';
  570. } else if (Math.abs(size) < 1000000) {
  571. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, ' µs');
  572. } else if (Math.abs(size) < 1000000000) {
  573. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, ' ms');
  574. } else if (Math.abs(size) < 60000000000) {
  575. return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, ' s');
  576. } else {
  577. return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, ' min');
  578. }
  579. };
  580. kbn.valueFormats.m = function(size, decimals, scaledDecimals) {
  581. if (size === null) {
  582. return '';
  583. }
  584. if (Math.abs(size) < 60) {
  585. return kbn.toFixed(size, decimals) + ' min';
  586. } else if (Math.abs(size) < 1440) {
  587. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 2, ' hour');
  588. } else if (Math.abs(size) < 10080) {
  589. return kbn.toFixedScaled(size / 1440, decimals, scaledDecimals, 3, ' day');
  590. } else if (Math.abs(size) < 604800) {
  591. return kbn.toFixedScaled(size / 10080, decimals, scaledDecimals, 4, ' week');
  592. } else {
  593. return kbn.toFixedScaled(size / 5.25948e5, decimals, scaledDecimals, 5, ' year');
  594. }
  595. };
  596. kbn.valueFormats.h = function(size, decimals, scaledDecimals) {
  597. if (size === null) {
  598. return '';
  599. }
  600. if (Math.abs(size) < 24) {
  601. return kbn.toFixed(size, decimals) + ' hour';
  602. } else if (Math.abs(size) < 168) {
  603. return kbn.toFixedScaled(size / 24, decimals, scaledDecimals, 2, ' day');
  604. } else if (Math.abs(size) < 8760) {
  605. return kbn.toFixedScaled(size / 168, decimals, scaledDecimals, 3, ' week');
  606. } else {
  607. return kbn.toFixedScaled(size / 8760, decimals, scaledDecimals, 4, ' year');
  608. }
  609. };
  610. kbn.valueFormats.d = function(size, decimals, scaledDecimals) {
  611. if (size === null) {
  612. return '';
  613. }
  614. if (Math.abs(size) < 7) {
  615. return kbn.toFixed(size, decimals) + ' day';
  616. } else if (Math.abs(size) < 365) {
  617. return kbn.toFixedScaled(size / 7, decimals, scaledDecimals, 2, ' week');
  618. } else {
  619. return kbn.toFixedScaled(size / 365, decimals, scaledDecimals, 3, ' year');
  620. }
  621. };
  622. kbn.toDuration = function(size, decimals, timeScale) {
  623. if (size === null) {
  624. return '';
  625. }
  626. if (size === 0) {
  627. return '0 ' + timeScale + 's';
  628. }
  629. if (size < 0) {
  630. return kbn.toDuration(-size, decimals, timeScale) + ' ago';
  631. }
  632. var units = [
  633. { short: 'y', long: 'year' },
  634. { short: 'M', long: 'month' },
  635. { short: 'w', long: 'week' },
  636. { short: 'd', long: 'day' },
  637. { short: 'h', long: 'hour' },
  638. { short: 'm', long: 'minute' },
  639. { short: 's', long: 'second' },
  640. { short: 'ms', long: 'millisecond' },
  641. ];
  642. // convert $size to milliseconds
  643. // intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors
  644. size *=
  645. kbn.intervals_in_seconds[
  646. units.find(function(e) {
  647. return e.long === timeScale;
  648. }).short
  649. ] * 1000;
  650. var strings = [];
  651. // after first value >= 1 print only $decimals more
  652. var decrementDecimals = false;
  653. for (var i = 0; i < units.length && decimals >= 0; i++) {
  654. var interval = kbn.intervals_in_seconds[units[i].short] * 1000;
  655. var value = size / interval;
  656. if (value >= 1 || decrementDecimals) {
  657. decrementDecimals = true;
  658. var floor = Math.floor(value);
  659. var unit = units[i].long + (floor !== 1 ? 's' : '');
  660. strings.push(floor + ' ' + unit);
  661. size = size % interval;
  662. decimals--;
  663. }
  664. }
  665. return strings.join(', ');
  666. };
  667. kbn.valueFormats.dtdurationms = function(size, decimals) {
  668. return kbn.toDuration(size, decimals, 'millisecond');
  669. };
  670. kbn.valueFormats.dtdurations = function(size, decimals) {
  671. return kbn.toDuration(size, decimals, 'second');
  672. };
  673. kbn.valueFormats.dateTimeAsIso = function(epoch) {
  674. var time = moment(epoch);
  675. if (moment().isSame(epoch, 'day')) {
  676. return time.format('HH:mm:ss');
  677. }
  678. return time.format('YYYY-MM-DD HH:mm:ss');
  679. };
  680. kbn.valueFormats.dateTimeAsUS = function(epoch) {
  681. var time = moment(epoch);
  682. if (moment().isSame(epoch, 'day')) {
  683. return time.format('h:mm:ss a');
  684. }
  685. return time.format('MM/DD/YYYY h:mm:ss a');
  686. };
  687. kbn.valueFormats.dateTimeFromNow = function(epoch) {
  688. return moment(epoch).fromNow();
  689. };
  690. ///// FORMAT MENU /////
  691. kbn.getUnitFormats = function() {
  692. return [
  693. {
  694. text: 'none',
  695. submenu: [
  696. { text: 'none', value: 'none' },
  697. { text: 'short', value: 'short' },
  698. { text: 'percent (0-100)', value: 'percent' },
  699. { text: 'percent (0.0-1.0)', value: 'percentunit' },
  700. { text: 'Humidity (%H)', value: 'humidity' },
  701. { text: 'ppm', value: 'ppm' },
  702. { text: 'decibel', value: 'dB' },
  703. { text: 'hexadecimal (0x)', value: 'hex0x' },
  704. { text: 'hexadecimal', value: 'hex' },
  705. { text: 'scientific notation', value: 'sci' },
  706. { text: 'locale format', value: 'locale' },
  707. ],
  708. },
  709. {
  710. text: 'currency',
  711. submenu: [
  712. { text: 'Dollars ($)', value: 'currencyUSD' },
  713. { text: 'Pounds (£)', value: 'currencyGBP' },
  714. { text: 'Euro (€)', value: 'currencyEUR' },
  715. { text: 'Yen (¥)', value: 'currencyJPY' },
  716. { text: 'Rubles (₽)', value: 'currencyRUB' },
  717. { text: 'Hryvnias (₴)', value: 'currencyUAH' },
  718. { text: 'Real (R$)', value: 'currencyBRL' },
  719. { text: 'Danish Krone (kr)', value: 'currencyDKK' },
  720. { text: 'Icelandic Krone (kr)', value: 'currencyISK' },
  721. { text: 'Norwegian Krone (kr)', value: 'currencyNOK' },
  722. { text: 'Swedish Krone (kr)', value: 'currencySEK' },
  723. ],
  724. },
  725. {
  726. text: 'time',
  727. submenu: [
  728. { text: 'Hertz (1/s)', value: 'hertz' },
  729. { text: 'nanoseconds (ns)', value: 'ns' },
  730. { text: 'microseconds (µs)', value: 'µs' },
  731. { text: 'milliseconds (ms)', value: 'ms' },
  732. { text: 'seconds (s)', value: 's' },
  733. { text: 'minutes (m)', value: 'm' },
  734. { text: 'hours (h)', value: 'h' },
  735. { text: 'days (d)', value: 'd' },
  736. { text: 'duration (ms)', value: 'dtdurationms' },
  737. { text: 'duration (s)', value: 'dtdurations' },
  738. ],
  739. },
  740. {
  741. text: 'date & time',
  742. submenu: [
  743. { text: 'YYYY-MM-DD HH:mm:ss', value: 'dateTimeAsIso' },
  744. { text: 'DD/MM/YYYY h:mm:ss a', value: 'dateTimeAsUS' },
  745. { text: 'From Now', value: 'dateTimeFromNow' },
  746. ],
  747. },
  748. {
  749. text: 'data (IEC)',
  750. submenu: [
  751. { text: 'bits', value: 'bits' },
  752. { text: 'bytes', value: 'bytes' },
  753. { text: 'kibibytes', value: 'kbytes' },
  754. { text: 'mebibytes', value: 'mbytes' },
  755. { text: 'gibibytes', value: 'gbytes' },
  756. ],
  757. },
  758. {
  759. text: 'data (Metric)',
  760. submenu: [
  761. { text: 'bits', value: 'decbits' },
  762. { text: 'bytes', value: 'decbytes' },
  763. { text: 'kilobytes', value: 'deckbytes' },
  764. { text: 'megabytes', value: 'decmbytes' },
  765. { text: 'gigabytes', value: 'decgbytes' },
  766. ],
  767. },
  768. {
  769. text: 'data rate',
  770. submenu: [
  771. { text: 'packets/sec', value: 'pps' },
  772. { text: 'bits/sec', value: 'bps' },
  773. { text: 'bytes/sec', value: 'Bps' },
  774. { text: 'kilobits/sec', value: 'Kbits' },
  775. { text: 'kilobytes/sec', value: 'KBs' },
  776. { text: 'megabits/sec', value: 'Mbits' },
  777. { text: 'megabytes/sec', value: 'MBs' },
  778. { text: 'gigabytes/sec', value: 'GBs' },
  779. { text: 'gigabits/sec', value: 'Gbits' },
  780. ],
  781. },
  782. {
  783. text: 'throughput',
  784. submenu: [
  785. { text: 'ops/sec (ops)', value: 'ops' },
  786. { text: 'reads/sec (rps)', value: 'rps' },
  787. { text: 'writes/sec (wps)', value: 'wps' },
  788. { text: 'I/O ops/sec (iops)', value: 'iops' },
  789. { text: 'ops/min (opm)', value: 'opm' },
  790. { text: 'reads/min (rpm)', value: 'rpm' },
  791. { text: 'writes/min (wpm)', value: 'wpm' },
  792. ],
  793. },
  794. {
  795. text: 'length',
  796. submenu: [
  797. { text: 'millimetre (mm)', value: 'lengthmm' },
  798. { text: 'meter (m)', value: 'lengthm' },
  799. { text: 'feet (ft)', value: 'lengthft' },
  800. { text: 'kilometer (km)', value: 'lengthkm' },
  801. { text: 'mile (mi)', value: 'lengthmi' },
  802. ],
  803. },
  804. {
  805. text: 'area',
  806. submenu: [
  807. { text: 'Square Meters (m²)', value: 'areaM2' },
  808. { text: 'Square Feet (ft²)', value: 'areaF2' },
  809. { text: 'Square Miles (mi²)', value: 'areaMI2' },
  810. ],
  811. },
  812. {
  813. text: 'mass',
  814. submenu: [
  815. { text: 'milligram (mg)', value: 'massmg' },
  816. { text: 'gram (g)', value: 'massg' },
  817. { text: 'kilogram (kg)', value: 'masskg' },
  818. { text: 'metric ton (t)', value: 'masst' },
  819. ],
  820. },
  821. {
  822. text: 'velocity',
  823. submenu: [
  824. { text: 'm/s', value: 'velocityms' },
  825. { text: 'km/h', value: 'velocitykmh' },
  826. { text: 'mph', value: 'velocitymph' },
  827. { text: 'knot (kn)', value: 'velocityknot' },
  828. ],
  829. },
  830. {
  831. text: 'volume',
  832. submenu: [
  833. { text: 'millilitre', value: 'mlitre' },
  834. { text: 'litre', value: 'litre' },
  835. { text: 'cubic metre', value: 'm3' },
  836. { text: 'cubic decimetre', value: 'dm3' },
  837. { text: 'gallons', value: 'gallons' },
  838. ],
  839. },
  840. {
  841. text: 'energy',
  842. submenu: [
  843. { text: 'Watt (W)', value: 'watt' },
  844. { text: 'Kilowatt (kW)', value: 'kwatt' },
  845. { text: 'Milliwatt (mW)', value: 'mwatt' },
  846. { text: 'Volt-ampere (VA)', value: 'voltamp' },
  847. { text: 'Kilovolt-ampere (kVA)', value: 'kvoltamp' },
  848. { text: 'Volt-ampere reactive (var)', value: 'voltampreact' },
  849. { text: 'Kilovolt-ampere reactive (kvar)', value: 'kvoltampreact' },
  850. { text: 'Watt-hour (Wh)', value: 'watth' },
  851. { text: 'Kilowatt-hour (kWh)', value: 'kwatth' },
  852. { text: 'Kilowatt-min (kWm)', value: 'kwattm' },
  853. { text: 'Joule (J)', value: 'joule' },
  854. { text: 'Electron volt (eV)', value: 'ev' },
  855. { text: 'Ampere (A)', value: 'amp' },
  856. { text: 'Kiloampere (kA)', value: 'kamp' },
  857. { text: 'Milliampere (mA)', value: 'mamp' },
  858. { text: 'Volt (V)', value: 'volt' },
  859. { text: 'Kilovolt (kV)', value: 'kvolt' },
  860. { text: 'Millivolt (mV)', value: 'mvolt' },
  861. { text: 'Decibel-milliwatt (dBm)', value: 'dBm' },
  862. { text: 'Ohm (Ω)', value: 'ohm' },
  863. { text: 'Lumens (Lm)', value: 'lumens' },
  864. ],
  865. },
  866. {
  867. text: 'temperature',
  868. submenu: [
  869. { text: 'Celsius (°C)', value: 'celsius' },
  870. { text: 'Farenheit (°F)', value: 'farenheit' },
  871. { text: 'Kelvin (K)', value: 'kelvin' },
  872. ],
  873. },
  874. {
  875. text: 'pressure',
  876. submenu: [
  877. { text: 'Millibars', value: 'pressurembar' },
  878. { text: 'Bars', value: 'pressurebar' },
  879. { text: 'Kilobars', value: 'pressurekbar' },
  880. { text: 'Hectopascals', value: 'pressurehpa' },
  881. { text: 'Inches of mercury', value: 'pressurehg' },
  882. { text: 'PSI', value: 'pressurepsi' },
  883. ],
  884. },
  885. {
  886. text: 'force',
  887. submenu: [
  888. { text: 'Newton-meters (Nm)', value: 'forceNm' },
  889. { text: 'Kilonewton-meters (kNm)', value: 'forcekNm' },
  890. { text: 'Newtons (N)', value: 'forceN' },
  891. { text: 'Kilonewtons (kN)', value: 'forcekN' },
  892. ],
  893. },
  894. {
  895. text: 'flow',
  896. submenu: [
  897. { text: 'Gallons/min (gpm)', value: 'flowgpm' },
  898. { text: 'Cubic meters/sec (cms)', value: 'flowcms' },
  899. { text: 'Cubic feet/sec (cfs)', value: 'flowcfs' },
  900. { text: 'Cubic feet/min (cfm)', value: 'flowcfm' },
  901. ],
  902. },
  903. {
  904. text: 'angle',
  905. submenu: [
  906. { text: 'Degrees (°)', value: 'degree' },
  907. { text: 'Radians', value: 'radian' },
  908. { text: 'Gradian', value: 'grad' },
  909. ],
  910. },
  911. {
  912. text: 'acceleration',
  913. submenu: [
  914. { text: 'Meters/sec²', value: 'accMS2' },
  915. { text: 'Feet/sec²', value: 'accFS2' },
  916. { text: 'G unit', value: 'accG' },
  917. ],
  918. },
  919. ];
  920. };
  921. export default kbn;