kbn.ts 33 KB

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