kbn.js 33 KB

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