kbn.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. // Data (Binary)
  354. kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b');
  355. kbn.valueFormats.bytes = kbn.formatBuilders.binarySIPrefix('B');
  356. kbn.valueFormats.kbytes = kbn.formatBuilders.binarySIPrefix('B', 1);
  357. kbn.valueFormats.mbytes = kbn.formatBuilders.binarySIPrefix('B', 2);
  358. kbn.valueFormats.gbytes = kbn.formatBuilders.binarySIPrefix('B', 3);
  359. // Data (Decimal)
  360. kbn.valueFormats.decbits = kbn.formatBuilders.decimalSIPrefix('b');
  361. kbn.valueFormats.decbytes = kbn.formatBuilders.decimalSIPrefix('B');
  362. kbn.valueFormats.deckbytes = kbn.formatBuilders.decimalSIPrefix('B', 1);
  363. kbn.valueFormats.decmbytes = kbn.formatBuilders.decimalSIPrefix('B', 2);
  364. kbn.valueFormats.decgbytes = kbn.formatBuilders.decimalSIPrefix('B', 3);
  365. // Data Rate
  366. kbn.valueFormats.pps = kbn.formatBuilders.decimalSIPrefix('pps');
  367. kbn.valueFormats.bps = kbn.formatBuilders.decimalSIPrefix('bps');
  368. kbn.valueFormats.Bps = kbn.formatBuilders.decimalSIPrefix('Bps');
  369. kbn.valueFormats.KBs = kbn.formatBuilders.decimalSIPrefix('Bs', 1);
  370. kbn.valueFormats.Kbits = kbn.formatBuilders.decimalSIPrefix('bps', 1);
  371. kbn.valueFormats.MBs = kbn.formatBuilders.decimalSIPrefix('Bs', 2);
  372. kbn.valueFormats.Mbits = kbn.formatBuilders.decimalSIPrefix('bps', 2);
  373. kbn.valueFormats.GBs = kbn.formatBuilders.decimalSIPrefix('Bs', 3);
  374. kbn.valueFormats.Gbits = kbn.formatBuilders.decimalSIPrefix('bps', 3);
  375. // Throughput
  376. kbn.valueFormats.ops = kbn.formatBuilders.simpleCountUnit('ops');
  377. kbn.valueFormats.rps = kbn.formatBuilders.simpleCountUnit('rps');
  378. kbn.valueFormats.wps = kbn.formatBuilders.simpleCountUnit('wps');
  379. kbn.valueFormats.iops = kbn.formatBuilders.simpleCountUnit('iops');
  380. kbn.valueFormats.opm = kbn.formatBuilders.simpleCountUnit('opm');
  381. kbn.valueFormats.rpm = kbn.formatBuilders.simpleCountUnit('rpm');
  382. kbn.valueFormats.wpm = kbn.formatBuilders.simpleCountUnit('wpm');
  383. // Energy
  384. kbn.valueFormats.watt = kbn.formatBuilders.decimalSIPrefix('W');
  385. kbn.valueFormats.kwatt = kbn.formatBuilders.decimalSIPrefix('W', 1);
  386. kbn.valueFormats.voltamp = kbn.formatBuilders.decimalSIPrefix('VA');
  387. kbn.valueFormats.kvoltamp = kbn.formatBuilders.decimalSIPrefix('VA', 1);
  388. kbn.valueFormats.voltampreact = kbn.formatBuilders.decimalSIPrefix('var');
  389. kbn.valueFormats.kvoltampreact = kbn.formatBuilders.decimalSIPrefix('var', 1);
  390. kbn.valueFormats.watth = kbn.formatBuilders.decimalSIPrefix('Wh');
  391. kbn.valueFormats.kwatth = kbn.formatBuilders.decimalSIPrefix('Wh', 1);
  392. kbn.valueFormats.joule = kbn.formatBuilders.decimalSIPrefix('J');
  393. kbn.valueFormats.ev = kbn.formatBuilders.decimalSIPrefix('eV');
  394. kbn.valueFormats.amp = kbn.formatBuilders.decimalSIPrefix('A');
  395. kbn.valueFormats.kamp = kbn.formatBuilders.decimalSIPrefix('A', 1);
  396. kbn.valueFormats.volt = kbn.formatBuilders.decimalSIPrefix('V');
  397. kbn.valueFormats.kvolt = kbn.formatBuilders.decimalSIPrefix('V', 1);
  398. kbn.valueFormats.dBm = kbn.formatBuilders.decimalSIPrefix('dBm');
  399. // Temperature
  400. kbn.valueFormats.celsius = kbn.formatBuilders.fixedUnit('°C');
  401. kbn.valueFormats.farenheit = kbn.formatBuilders.fixedUnit('°F');
  402. kbn.valueFormats.kelvin = kbn.formatBuilders.fixedUnit('K');
  403. kbn.valueFormats.humidity = kbn.formatBuilders.fixedUnit('%H');
  404. // Pressure
  405. kbn.valueFormats.pressurebar = kbn.formatBuilders.decimalSIPrefix('bar');
  406. kbn.valueFormats.pressurembar = kbn.formatBuilders.decimalSIPrefix('bar', -1);
  407. kbn.valueFormats.pressurekbar = kbn.formatBuilders.decimalSIPrefix('bar', 1);
  408. kbn.valueFormats.pressurehpa = kbn.formatBuilders.fixedUnit('hPa');
  409. kbn.valueFormats.pressurehg = kbn.formatBuilders.fixedUnit('"Hg');
  410. kbn.valueFormats.pressurepsi = kbn.formatBuilders.scaledUnits(1000, [' psi', ' ksi', ' Mpsi']);
  411. // Force
  412. kbn.valueFormats.forceNm = kbn.formatBuilders.decimalSIPrefix('Nm');
  413. kbn.valueFormats.forcekNm = kbn.formatBuilders.decimalSIPrefix('Nm', 1);
  414. kbn.valueFormats.forceN = kbn.formatBuilders.decimalSIPrefix('N');
  415. kbn.valueFormats.forcekN = kbn.formatBuilders.decimalSIPrefix('N', 1);
  416. // Length
  417. kbn.valueFormats.lengthm = kbn.formatBuilders.decimalSIPrefix('m');
  418. kbn.valueFormats.lengthmm = kbn.formatBuilders.decimalSIPrefix('m', -1);
  419. kbn.valueFormats.lengthkm = kbn.formatBuilders.decimalSIPrefix('m', 1);
  420. kbn.valueFormats.lengthmi = kbn.formatBuilders.fixedUnit('mi');
  421. // Mass
  422. kbn.valueFormats.massmg = kbn.formatBuilders.decimalSIPrefix('g', -1);
  423. kbn.valueFormats.massg = kbn.formatBuilders.decimalSIPrefix('g');
  424. kbn.valueFormats.masskg = kbn.formatBuilders.decimalSIPrefix('g', 1);
  425. kbn.valueFormats.masst = kbn.formatBuilders.fixedUnit('t');
  426. // Velocity
  427. kbn.valueFormats.velocityms = kbn.formatBuilders.fixedUnit('m/s');
  428. kbn.valueFormats.velocitykmh = kbn.formatBuilders.fixedUnit('km/h');
  429. kbn.valueFormats.velocitymph = kbn.formatBuilders.fixedUnit('mph');
  430. kbn.valueFormats.velocityknot = kbn.formatBuilders.fixedUnit('kn');
  431. // Volume
  432. kbn.valueFormats.litre = kbn.formatBuilders.decimalSIPrefix('L');
  433. kbn.valueFormats.mlitre = kbn.formatBuilders.decimalSIPrefix('L', -1);
  434. kbn.valueFormats.m3 = kbn.formatBuilders.decimalSIPrefix('m3');
  435. kbn.valueFormats.dm3 = kbn.formatBuilders.decimalSIPrefix('dm3');
  436. kbn.valueFormats.gallons = kbn.formatBuilders.fixedUnit('gal');
  437. // Flow
  438. kbn.valueFormats.flowgpm = kbn.formatBuilders.fixedUnit('gpm');
  439. kbn.valueFormats.flowcms = kbn.formatBuilders.fixedUnit('cms');
  440. kbn.valueFormats.flowcfs = kbn.formatBuilders.fixedUnit('cfs');
  441. kbn.valueFormats.flowcfm = kbn.formatBuilders.fixedUnit('cfm');
  442. // Time
  443. kbn.valueFormats.hertz = kbn.formatBuilders.decimalSIPrefix('Hz');
  444. kbn.valueFormats.ms = function(size, decimals, scaledDecimals) {
  445. if (size === null) { return ""; }
  446. if (Math.abs(size) < 1000) {
  447. return kbn.toFixed(size, decimals) + " ms";
  448. }
  449. // Less than 1 min
  450. else if (Math.abs(size) < 60000) {
  451. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " s");
  452. }
  453. // Less than 1 hour, devide in minutes
  454. else if (Math.abs(size) < 3600000) {
  455. return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, " min");
  456. }
  457. // Less than one day, devide in hours
  458. else if (Math.abs(size) < 86400000) {
  459. return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, " hour");
  460. }
  461. // Less than one year, devide in days
  462. else if (Math.abs(size) < 31536000000) {
  463. return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, " day");
  464. }
  465. return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, " year");
  466. };
  467. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  468. if (size === null) { return ""; }
  469. // Less than 1 µs, devide in ns
  470. if (Math.abs(size) < 0.000001) {
  471. return kbn.toFixedScaled(size * 1.e9, decimals, scaledDecimals - decimals, -9, " ns");
  472. }
  473. // Less than 1 ms, devide in µs
  474. if (Math.abs(size) < 0.001) {
  475. return kbn.toFixedScaled(size * 1.e6, decimals, scaledDecimals - decimals, -6, " µs");
  476. }
  477. // Less than 1 second, devide in ms
  478. if (Math.abs(size) < 1) {
  479. return kbn.toFixedScaled(size * 1.e3, decimals, scaledDecimals - decimals, -3, " ms");
  480. }
  481. if (Math.abs(size) < 60) {
  482. return kbn.toFixed(size, decimals) + " s";
  483. }
  484. // Less than 1 hour, devide in minutes
  485. else if (Math.abs(size) < 3600) {
  486. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, " min");
  487. }
  488. // Less than one day, devide in hours
  489. else if (Math.abs(size) < 86400) {
  490. return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, " hour");
  491. }
  492. // Less than one week, devide in days
  493. else if (Math.abs(size) < 604800) {
  494. return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, " day");
  495. }
  496. // Less than one year, devide in week
  497. else if (Math.abs(size) < 31536000) {
  498. return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, " week");
  499. }
  500. return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, " year");
  501. };
  502. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  503. if (size === null) { return ""; }
  504. if (Math.abs(size) < 1000) {
  505. return kbn.toFixed(size, decimals) + " µs";
  506. }
  507. else if (Math.abs(size) < 1000000) {
  508. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " ms");
  509. }
  510. else {
  511. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " s");
  512. }
  513. };
  514. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  515. if (size === null) { return ""; }
  516. if (Math.abs(size) < 1000) {
  517. return kbn.toFixed(size, decimals) + " ns";
  518. }
  519. else if (Math.abs(size) < 1000000) {
  520. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " µs");
  521. }
  522. else if (Math.abs(size) < 1000000000) {
  523. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " ms");
  524. }
  525. else if (Math.abs(size) < 60000000000){
  526. return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, " s");
  527. }
  528. else {
  529. return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, " min");
  530. }
  531. };
  532. kbn.valueFormats.m = function(size, decimals, scaledDecimals) {
  533. if (size === null) { return ""; }
  534. if (Math.abs(size) < 60) {
  535. return kbn.toFixed(size, decimals) + " min";
  536. }
  537. else if (Math.abs(size) < 1440) {
  538. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 2, " hour");
  539. }
  540. else if (Math.abs(size) < 10080) {
  541. return kbn.toFixedScaled(size / 1440, decimals, scaledDecimals, 3, " day");
  542. }
  543. else if (Math.abs(size) < 604800) {
  544. return kbn.toFixedScaled(size / 10080, decimals, scaledDecimals, 4, " week");
  545. }
  546. else {
  547. return kbn.toFixedScaled(size / 5.25948e5, decimals, scaledDecimals, 5, " year");
  548. }
  549. };
  550. kbn.valueFormats.h = function(size, decimals, scaledDecimals) {
  551. if (size === null) { return ""; }
  552. if (Math.abs(size) < 24) {
  553. return kbn.toFixed(size, decimals) + " hour";
  554. }
  555. else if (Math.abs(size) < 168) {
  556. return kbn.toFixedScaled(size / 24, decimals, scaledDecimals, 2, " day");
  557. }
  558. else if (Math.abs(size) < 8760) {
  559. return kbn.toFixedScaled(size / 168, decimals, scaledDecimals, 3, " week");
  560. }
  561. else {
  562. return kbn.toFixedScaled(size / 8760, decimals, scaledDecimals, 4, " year");
  563. }
  564. };
  565. kbn.valueFormats.d = function(size, decimals, scaledDecimals) {
  566. if (size === null) { return ""; }
  567. if (Math.abs(size) < 7) {
  568. return kbn.toFixed(size, decimals) + " day";
  569. }
  570. else if (Math.abs(size) < 365) {
  571. return kbn.toFixedScaled(size / 7, decimals, scaledDecimals, 2, " week");
  572. }
  573. else {
  574. return kbn.toFixedScaled(size / 365, decimals, scaledDecimals, 3, " year");
  575. }
  576. };
  577. kbn.toDuration = function(size, decimals, timeScale) {
  578. if (size === null) { return ""; }
  579. if (size === 0) { return "0 " + timeScale + "s"; }
  580. if (size < 0) { return kbn.toDuration(-size, decimals, timeScale) + " ago"; }
  581. var units = [
  582. {short: "y", long: "year"},
  583. {short: "M", long: "month"},
  584. {short: "w", long: "week"},
  585. {short: "d", long: "day"},
  586. {short: "h", long: "hour"},
  587. {short: "m", long: "minute"},
  588. {short: "s", long: "second"},
  589. {short: "ms", long: "millisecond"}
  590. ];
  591. // convert $size to milliseconds
  592. // intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors
  593. size *= kbn.intervals_in_seconds[units.find(function(e) { return e.long === timeScale; }).short] * 1000;
  594. var string = [];
  595. // after first value >= 1 print only $decimals more
  596. var decrementDecimals = false;
  597. for (var i = 0; i < units.length && decimals >= 0; i++) {
  598. var interval = kbn.intervals_in_seconds[units[i].short] * 1000;
  599. var value = size / interval;
  600. if (value >= 1 || decrementDecimals) {
  601. decrementDecimals = true;
  602. var floor = Math.floor(value);
  603. var unit = units[i].long + (floor !== 1 ? "s" : "");
  604. string.push(floor + " " + unit);
  605. size = size % interval;
  606. decimals--;
  607. }
  608. }
  609. return string.join(", ");
  610. };
  611. kbn.valueFormats.dtdurationms = function(size, decimals) {
  612. return kbn.toDuration(size, decimals, 'millisecond');
  613. };
  614. kbn.valueFormats.dtdurations = function(size, decimals) {
  615. return kbn.toDuration(size, decimals, 'second');
  616. };
  617. kbn.valueFormats.dateTimeAsIso = function(epoch) {
  618. var time = moment(epoch);
  619. if (moment().isSame(epoch, 'day')) {
  620. return time.format('HH:mm:ss');
  621. }
  622. return time.format('YYYY-MM-DD HH:mm:ss');
  623. };
  624. kbn.valueFormats.dateTimeAsUS = function(epoch) {
  625. var time = moment(epoch);
  626. if (moment().isSame(epoch, 'day')) {
  627. return time.format('h:mm:ss a');
  628. }
  629. return time.format('MM/DD/YYYY h:mm:ss a');
  630. };
  631. kbn.valueFormats.dateTimeFromNow = function(epoch) {
  632. return moment(epoch).fromNow();
  633. };
  634. ///// FORMAT MENU /////
  635. kbn.getUnitFormats = function() {
  636. return [
  637. {
  638. text: 'none',
  639. submenu: [
  640. {text: 'none' , value: 'none' },
  641. {text: 'short', value: 'short' },
  642. {text: 'percent (0-100)', value: 'percent' },
  643. {text: 'percent (0.0-1.0)', value: 'percentunit'},
  644. {text: 'Humidity (%H)', value: 'humidity' },
  645. {text: 'ppm', value: 'ppm' },
  646. {text: 'decibel', value: 'dB' },
  647. {text: 'hexadecimal (0x)', value: 'hex0x' },
  648. {text: 'hexadecimal', value: 'hex' },
  649. {text: 'scientific notation', value: 'sci' },
  650. {text: 'locale format', value: 'locale' },
  651. ]
  652. },
  653. {
  654. text: 'currency',
  655. submenu: [
  656. {text: 'Dollars ($)', value: 'currencyUSD'},
  657. {text: 'Pounds (£)', value: 'currencyGBP'},
  658. {text: 'Euro (€)', value: 'currencyEUR'},
  659. {text: 'Yen (¥)', value: 'currencyJPY'},
  660. {text: 'Rubles (₽)', value: 'currencyRUB'},
  661. {text: 'Hryvnias (₴)', value: 'currencyUAH'},
  662. ]
  663. },
  664. {
  665. text: 'time',
  666. submenu: [
  667. {text: 'Hertz (1/s)', value: 'hertz'},
  668. {text: 'nanoseconds (ns)' , value: 'ns' },
  669. {text: 'microseconds (µs)', value: 'µs' },
  670. {text: 'milliseconds (ms)', value: 'ms' },
  671. {text: 'seconds (s)', value: 's' },
  672. {text: 'minutes (m)', value: 'm' },
  673. {text: 'hours (h)', value: 'h' },
  674. {text: 'days (d)', value: 'd' },
  675. {text: 'duration (ms)', value: 'dtdurationms' },
  676. {text: 'duration (s)', value: 'dtdurations' },
  677. ]
  678. },
  679. {
  680. text: 'date & time',
  681. submenu: [
  682. {text: 'YYYY-MM-DD HH:mm:ss', value: 'dateTimeAsIso' },
  683. {text: 'DD/MM/YYYY h:mm:ss a', value: 'dateTimeAsUS' },
  684. {text: 'From Now', value: 'dateTimeFromNow' },
  685. ]
  686. },
  687. {
  688. text: 'data (IEC)',
  689. submenu: [
  690. {text: 'bits', value: 'bits' },
  691. {text: 'bytes', value: 'bytes' },
  692. {text: 'kibibytes', value: 'kbytes'},
  693. {text: 'mebibytes', value: 'mbytes'},
  694. {text: 'gibibytes', value: 'gbytes'},
  695. ]
  696. },
  697. {
  698. text: 'data (Metric)',
  699. submenu: [
  700. {text: 'bits', value: 'decbits' },
  701. {text: 'bytes', value: 'decbytes' },
  702. {text: 'kilobytes', value: 'deckbytes'},
  703. {text: 'megabytes', value: 'decmbytes'},
  704. {text: 'gigabytes', value: 'decgbytes'},
  705. ]
  706. },
  707. {
  708. text: 'data rate',
  709. submenu: [
  710. {text: 'packets/sec', value: 'pps'},
  711. {text: 'bits/sec', value: 'bps'},
  712. {text: 'bytes/sec', value: 'Bps'},
  713. {text: 'kilobits/sec', value: 'Kbits'},
  714. {text: 'kilobytes/sec', value: 'KBs'},
  715. {text: 'megabits/sec', value: 'Mbits'},
  716. {text: 'megabytes/sec', value: 'MBs'},
  717. {text: 'gigabytes/sec', value: 'GBs'},
  718. {text: 'gigabits/sec', value: 'Gbits'},
  719. ]
  720. },
  721. {
  722. text: 'throughput',
  723. submenu: [
  724. {text: 'ops/sec (ops)', value: 'ops' },
  725. {text: 'reads/sec (rps)', value: 'rps' },
  726. {text: 'writes/sec (wps)', value: 'wps' },
  727. {text: 'I/O ops/sec (iops)', value: 'iops'},
  728. {text: 'ops/min (opm)', value: 'opm' },
  729. {text: 'reads/min (rpm)', value: 'rpm' },
  730. {text: 'writes/min (wpm)', value: 'wpm' },
  731. ]
  732. },
  733. {
  734. text: 'length',
  735. submenu: [
  736. {text: 'millimetre (mm)', value: 'lengthmm'},
  737. {text: 'meter (m)', value: 'lengthm' },
  738. {text: 'kilometer (km)', value: 'lengthkm'},
  739. {text: 'mile (mi)', value: 'lengthmi'},
  740. ]
  741. },
  742. {
  743. text: 'mass',
  744. submenu: [
  745. {text: 'milligram (mg)', value: 'massmg'},
  746. {text: 'gram (g)', value: 'massg' },
  747. {text: 'kilogram (kg)', value: 'masskg'},
  748. {text: 'metric ton (t)', value: 'masst' },
  749. ]
  750. },
  751. {
  752. text: 'velocity',
  753. submenu: [
  754. {text: 'm/s', value: 'velocityms' },
  755. {text: 'km/h', value: 'velocitykmh' },
  756. {text: 'mph', value: 'velocitymph' },
  757. {text: 'knot (kn)', value: 'velocityknot'},
  758. ]
  759. },
  760. {
  761. text: 'volume',
  762. submenu: [
  763. {text: 'millilitre', value: 'mlitre' },
  764. {text: 'litre', value: 'litre' },
  765. {text: 'cubic metre', value: 'm3' },
  766. {text: 'cubic decimetre', value: 'dm3' },
  767. {text: 'gallons', value: 'gallons'},
  768. ]
  769. },
  770. {
  771. text: 'energy',
  772. submenu: [
  773. {text: 'watt (W)', value: 'watt' },
  774. {text: 'kilowatt (kW)', value: 'kwatt' },
  775. {text: 'volt-ampere (VA)', value: 'voltamp' },
  776. {text: 'kilovolt-ampere (kVA)', value: 'kvoltamp' },
  777. {text: 'volt-ampere reactive (var)', value: 'voltampreact'},
  778. {text: 'kilovolt-ampere reactive (kvar)', value: 'kvoltampreact'},
  779. {text: 'watt-hour (Wh)', value: 'watth' },
  780. {text: 'kilowatt-hour (kWh)', value: 'kwatth' },
  781. {text: 'joule (J)', value: 'joule' },
  782. {text: 'electron volt (eV)', value: 'ev' },
  783. {text: 'Ampere (A)', value: 'amp' },
  784. {text: 'Kiloampere (kA)', value: 'kamp' },
  785. {text: 'Volt (V)', value: 'volt' },
  786. {text: 'Kilovolt (kV)', value: 'kvolt' },
  787. {text: 'Decibel-milliwatt (dBm)', value: 'dBm' },
  788. ]
  789. },
  790. {
  791. text: 'temperature',
  792. submenu: [
  793. {text: 'Celsius (°C)', value: 'celsius' },
  794. {text: 'Farenheit (°F)', value: 'farenheit' },
  795. {text: 'Kelvin (K)', value: 'kelvin' },
  796. ]
  797. },
  798. {
  799. text: 'pressure',
  800. submenu: [
  801. {text: 'Millibars', value: 'pressurembar'},
  802. {text: 'Bars', value: 'pressurebar' },
  803. {text: 'Kilobars', value: 'pressurekbar'},
  804. {text: 'Hectopascals', value: 'pressurehpa' },
  805. {text: 'Inches of mercury', value: 'pressurehg' },
  806. {text: 'PSI', value: 'pressurepsi' },
  807. ]
  808. },
  809. {
  810. text: 'force',
  811. submenu: [
  812. {text: 'Newton-meters (Nm)', value: 'forceNm' },
  813. {text: 'Kilonewton-meters (kNm)', value: 'forcekNm' },
  814. {text: 'Newtons (N)', value: 'forceN' },
  815. {text: 'Kilonewtons (kN)', value: 'forcekN' },
  816. ]
  817. },
  818. {
  819. text: 'flow',
  820. submenu: [
  821. {text: 'Gallons/min (gpm)', value: 'flowgpm' },
  822. {text: 'Cubic meters/sec (cms)', value: 'flowcms' },
  823. {text: 'Cubic feet/sec (cfs)', value: 'flowcfs' },
  824. {text: 'Cubic feet/min (cfm)', value: 'flowcfm' },
  825. ]
  826. }
  827. ];
  828. };
  829. return kbn;
  830. });