kbn.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. // 24h
  89. case (interval <= 86400000):
  90. return 43200000; // 12h
  91. // 48h
  92. case (interval <= 172800000):
  93. return 86400000; // 24h
  94. // 1w
  95. case (interval <= 604800000):
  96. return 86400000; // 24h
  97. // 3w
  98. case (interval <= 1814400000):
  99. return 604800000; // 1w
  100. // 2y
  101. case (interval < 3628800000):
  102. return 2592000000; // 30d
  103. default:
  104. return 31536000000; // 1y
  105. }
  106. };
  107. kbn.secondsToHms = function(seconds) {
  108. var numyears = Math.floor(seconds / 31536000);
  109. if(numyears){
  110. return numyears + 'y';
  111. }
  112. var numdays = Math.floor((seconds % 31536000) / 86400);
  113. if(numdays){
  114. return numdays + 'd';
  115. }
  116. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  117. if(numhours){
  118. return numhours + 'h';
  119. }
  120. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  121. if(numminutes){
  122. return numminutes + 'm';
  123. }
  124. var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
  125. if(numseconds){
  126. return numseconds + 's';
  127. }
  128. var nummilliseconds = Math.floor(seconds * 1000.0);
  129. if(nummilliseconds){
  130. return nummilliseconds + 'ms';
  131. }
  132. return 'less then a millisecond'; //'just now' //or other string you like;
  133. };
  134. kbn.to_percent = function(number,outof) {
  135. return Math.floor((number/outof)*10000)/100 + "%";
  136. };
  137. kbn.addslashes = function(str) {
  138. str = str.replace(/\\/g, '\\\\');
  139. str = str.replace(/\'/g, '\\\'');
  140. str = str.replace(/\"/g, '\\"');
  141. str = str.replace(/\0/g, '\\0');
  142. return str;
  143. };
  144. kbn.interval_regex = /(\d+(?:\.\d+)?)(ms|[Mwdhmsy])/;
  145. // histogram & trends
  146. kbn.intervals_in_seconds = {
  147. y: 31536000,
  148. M: 2592000,
  149. w: 604800,
  150. d: 86400,
  151. h: 3600,
  152. m: 60,
  153. s: 1,
  154. ms: 0.001
  155. };
  156. kbn.calculateInterval = function(range, resolution, userInterval) {
  157. var lowLimitMs = 1; // 1 millisecond default low limit
  158. var intervalMs, lowLimitInterval;
  159. if (userInterval) {
  160. if (userInterval[0] === '>') {
  161. lowLimitInterval = userInterval.slice(1);
  162. lowLimitMs = kbn.interval_to_ms(lowLimitInterval);
  163. }
  164. else {
  165. return {
  166. intervalMs: kbn.interval_to_ms(userInterval),
  167. interval: userInterval,
  168. };
  169. }
  170. }
  171. intervalMs = kbn.round_interval((range.to.valueOf() - range.from.valueOf()) / resolution);
  172. if (lowLimitMs > intervalMs) {
  173. intervalMs = lowLimitMs;
  174. }
  175. return {
  176. intervalMs: intervalMs,
  177. interval: kbn.secondsToHms(intervalMs / 1000),
  178. };
  179. };
  180. kbn.describe_interval = function (string) {
  181. var matches = string.match(kbn.interval_regex);
  182. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  183. throw new Error('Invalid interval string, expecting a number followed by one of "Mwdhmsy"');
  184. } else {
  185. return {
  186. sec: kbn.intervals_in_seconds[matches[2]],
  187. type: matches[2],
  188. count: parseInt(matches[1], 10)
  189. };
  190. }
  191. };
  192. kbn.interval_to_ms = function(string) {
  193. var info = kbn.describe_interval(string);
  194. return info.sec * 1000 * info.count;
  195. };
  196. kbn.interval_to_seconds = function (string) {
  197. var info = kbn.describe_interval(string);
  198. return info.sec * info.count;
  199. };
  200. kbn.query_color_dot = function (color, diameter) {
  201. return '<div class="icon-circle" style="' + [
  202. 'display:inline-block',
  203. 'color:' + color,
  204. 'font-size:' + diameter + 'px',
  205. ].join(';') + '"></div>';
  206. };
  207. kbn.slugifyForUrl = function(str) {
  208. return str
  209. .toLowerCase()
  210. .replace(/[^\w ]+/g,'')
  211. .replace(/ +/g,'-');
  212. };
  213. kbn.stringToJsRegex = function(str) {
  214. if (str[0] !== '/') {
  215. return new RegExp('^' + str + '$');
  216. }
  217. var match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
  218. return new RegExp(match[1], match[2]);
  219. };
  220. kbn.toFixed = function(value, decimals) {
  221. if (value === null) {
  222. return "";
  223. }
  224. var factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
  225. var formatted = String(Math.round(value * factor) / factor);
  226. // if exponent return directly
  227. if (formatted.indexOf('e') !== -1 || value === 0) {
  228. return formatted;
  229. }
  230. // If tickDecimals was specified, ensure that we have exactly that
  231. // much precision; otherwise default to the value's own precision.
  232. if (decimals != null) {
  233. var decimalPos = formatted.indexOf(".");
  234. var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
  235. if (precision < decimals) {
  236. return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision);
  237. }
  238. }
  239. return formatted;
  240. };
  241. kbn.toFixedScaled = function(value, decimals, scaledDecimals, additionalDecimals, ext) {
  242. if (scaledDecimals === null) {
  243. return kbn.toFixed(value, decimals) + ext;
  244. } else {
  245. return kbn.toFixed(value, scaledDecimals + additionalDecimals) + ext;
  246. }
  247. };
  248. kbn.roundValue = function (num, decimals) {
  249. if (num === null) { return null; }
  250. var n = Math.pow(10, decimals);
  251. return Math.round((n * num).toFixed(decimals)) / n;
  252. };
  253. ///// FORMAT FUNCTION CONSTRUCTORS /////
  254. kbn.formatBuilders = {};
  255. // Formatter which always appends a fixed unit string to the value. No
  256. // scaling of the value is performed.
  257. kbn.formatBuilders.fixedUnit = function(unit) {
  258. return function(size, decimals) {
  259. if (size === null) { return ""; }
  260. return kbn.toFixed(size, decimals) + ' ' + unit;
  261. };
  262. };
  263. // Formatter which scales the unit string geometrically according to the given
  264. // numeric factor. Repeatedly scales the value down by the factor until it is
  265. // less than the factor in magnitude, or the end of the array is reached.
  266. kbn.formatBuilders.scaledUnits = function(factor, extArray) {
  267. return function(size, decimals, scaledDecimals) {
  268. if (size === null) {
  269. return "";
  270. }
  271. var steps = 0;
  272. var limit = extArray.length;
  273. while (Math.abs(size) >= factor) {
  274. steps++;
  275. size /= factor;
  276. if (steps >= limit) { return "NA"; }
  277. }
  278. if (steps > 0 && scaledDecimals !== null) {
  279. decimals = scaledDecimals + (3 * steps);
  280. }
  281. return kbn.toFixed(size, decimals) + extArray[steps];
  282. };
  283. };
  284. // Extension of the scaledUnits builder which uses SI decimal prefixes. If an
  285. // offset is given, it adjusts the starting units at the given prefix; a value
  286. // of 0 starts at no scale; -3 drops to nano, +2 starts at mega, etc.
  287. kbn.formatBuilders.decimalSIPrefix = function(unit, offset) {
  288. var prefixes = ['n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
  289. prefixes = prefixes.slice(3 + (offset || 0));
  290. var units = prefixes.map(function(p) { return ' ' + p + unit; });
  291. return kbn.formatBuilders.scaledUnits(1000, units);
  292. };
  293. // Extension of the scaledUnits builder which uses SI binary prefixes. If
  294. // offset is given, it starts the units at the given prefix; otherwise, the
  295. // offset defaults to zero and the initial unit is not prefixed.
  296. kbn.formatBuilders.binarySIPrefix = function(unit, offset) {
  297. var prefixes = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'].slice(offset);
  298. var units = prefixes.map(function(p) { return ' ' + p + unit; });
  299. return kbn.formatBuilders.scaledUnits(1024, units);
  300. };
  301. // Currency formatter for prefixing a symbol onto a number. Supports scaling
  302. // up to the trillions.
  303. kbn.formatBuilders.currency = 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 symbol + scaled;
  310. };
  311. };
  312. kbn.formatBuilders.simpleCountUnit = function(symbol) {
  313. var units = ['', 'K', 'M', 'B', 'T'];
  314. var scaler = kbn.formatBuilders.scaledUnits(1000, units);
  315. return function(size, decimals, scaledDecimals) {
  316. if (size === null) { return ""; }
  317. var scaled = scaler(size, decimals, scaledDecimals);
  318. return scaled + " " + symbol;
  319. };
  320. };
  321. ///// VALUE FORMATS /////
  322. // Dimensionless Units
  323. kbn.valueFormats.none = kbn.toFixed;
  324. kbn.valueFormats.short = kbn.formatBuilders.scaledUnits(1000, ['', ' K', ' Mil', ' Bil', ' Tri', ' Quadr', ' Quint', ' Sext', ' Sept']);
  325. kbn.valueFormats.dB = kbn.formatBuilders.fixedUnit('dB');
  326. kbn.valueFormats.ppm = kbn.formatBuilders.fixedUnit('ppm');
  327. kbn.valueFormats.percent = function(size, decimals) {
  328. if (size === null) { return ""; }
  329. return kbn.toFixed(size, decimals) + '%';
  330. };
  331. kbn.valueFormats.percentunit = function(size, decimals) {
  332. if (size === null) { return ""; }
  333. return kbn.toFixed(100*size, decimals) + '%';
  334. };
  335. /* Formats the value to hex. Uses float if specified decimals are not 0.
  336. * There are two options, one with 0x, and one without */
  337. kbn.valueFormats.hex = function(value, decimals) {
  338. if (value == null) { return ""; }
  339. return parseFloat(kbn.toFixed(value, decimals)).toString(16).toUpperCase();
  340. };
  341. kbn.valueFormats.hex0x = function(value, decimals) {
  342. if (value == null) { return ""; }
  343. var hexString = kbn.valueFormats.hex(value, decimals);
  344. if (hexString.substring(0,1) === "-") {
  345. return "-0x" + hexString.substring(1);
  346. }
  347. return "0x" + hexString;
  348. };
  349. // Currencies
  350. kbn.valueFormats.currencyUSD = kbn.formatBuilders.currency('$');
  351. kbn.valueFormats.currencyGBP = kbn.formatBuilders.currency('£');
  352. kbn.valueFormats.currencyEUR = kbn.formatBuilders.currency('€');
  353. kbn.valueFormats.currencyJPY = kbn.formatBuilders.currency('¥');
  354. // Data (Binary)
  355. kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b');
  356. kbn.valueFormats.bytes = kbn.formatBuilders.binarySIPrefix('B');
  357. kbn.valueFormats.kbytes = kbn.formatBuilders.binarySIPrefix('B', 1);
  358. kbn.valueFormats.mbytes = kbn.formatBuilders.binarySIPrefix('B', 2);
  359. kbn.valueFormats.gbytes = kbn.formatBuilders.binarySIPrefix('B', 3);
  360. // Data (Decimal)
  361. kbn.valueFormats.decbits = kbn.formatBuilders.decimalSIPrefix('b');
  362. kbn.valueFormats.decbytes = kbn.formatBuilders.decimalSIPrefix('B');
  363. kbn.valueFormats.deckbytes = kbn.formatBuilders.decimalSIPrefix('B', 1);
  364. kbn.valueFormats.decmbytes = kbn.formatBuilders.decimalSIPrefix('B', 2);
  365. kbn.valueFormats.decgbytes = kbn.formatBuilders.decimalSIPrefix('B', 3);
  366. // Data Rate
  367. kbn.valueFormats.pps = kbn.formatBuilders.decimalSIPrefix('pps');
  368. kbn.valueFormats.bps = kbn.formatBuilders.decimalSIPrefix('bps');
  369. kbn.valueFormats.Bps = kbn.formatBuilders.decimalSIPrefix('Bps');
  370. kbn.valueFormats.KBs = kbn.formatBuilders.decimalSIPrefix('Bs', 1);
  371. kbn.valueFormats.Kbits = kbn.formatBuilders.decimalSIPrefix('bps', 1);
  372. kbn.valueFormats.MBs = kbn.formatBuilders.decimalSIPrefix('Bs', 2);
  373. kbn.valueFormats.Mbits = kbn.formatBuilders.decimalSIPrefix('bps', 2);
  374. kbn.valueFormats.GBs = kbn.formatBuilders.decimalSIPrefix('Bs', 3);
  375. kbn.valueFormats.Gbits = kbn.formatBuilders.decimalSIPrefix('bps', 3);
  376. // Throughput
  377. kbn.valueFormats.ops = kbn.formatBuilders.simpleCountUnit('ops');
  378. kbn.valueFormats.rps = kbn.formatBuilders.simpleCountUnit('rps');
  379. kbn.valueFormats.wps = kbn.formatBuilders.simpleCountUnit('wps');
  380. kbn.valueFormats.iops = kbn.formatBuilders.simpleCountUnit('iops');
  381. kbn.valueFormats.opm = kbn.formatBuilders.simpleCountUnit('opm');
  382. kbn.valueFormats.rpm = kbn.formatBuilders.simpleCountUnit('rpm');
  383. kbn.valueFormats.wpm = kbn.formatBuilders.simpleCountUnit('wpm');
  384. // Energy
  385. kbn.valueFormats.watt = kbn.formatBuilders.decimalSIPrefix('W');
  386. kbn.valueFormats.kwatt = kbn.formatBuilders.decimalSIPrefix('W', 1);
  387. kbn.valueFormats.voltamp = kbn.formatBuilders.decimalSIPrefix('VA');
  388. kbn.valueFormats.kvoltamp = kbn.formatBuilders.decimalSIPrefix('VA', 1);
  389. kbn.valueFormats.voltampreact = kbn.formatBuilders.decimalSIPrefix('var');
  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.volt = kbn.formatBuilders.decimalSIPrefix('V');
  396. kbn.valueFormats.dBm = kbn.formatBuilders.decimalSIPrefix('dBm');
  397. // Temperature
  398. kbn.valueFormats.celsius = kbn.formatBuilders.fixedUnit('°C');
  399. kbn.valueFormats.farenheit = kbn.formatBuilders.fixedUnit('°F');
  400. kbn.valueFormats.kelvin = kbn.formatBuilders.fixedUnit('K');
  401. kbn.valueFormats.humidity = kbn.formatBuilders.fixedUnit('%H');
  402. // Pressure
  403. kbn.valueFormats.pressurembar = kbn.formatBuilders.fixedUnit('mbar');
  404. kbn.valueFormats.pressurehpa = kbn.formatBuilders.fixedUnit('hPa');
  405. kbn.valueFormats.pressurehg = kbn.formatBuilders.fixedUnit('"Hg');
  406. kbn.valueFormats.pressurepsi = kbn.formatBuilders.scaledUnits(1000, [' psi', ' ksi', ' Mpsi']);
  407. // Length
  408. kbn.valueFormats.lengthm = kbn.formatBuilders.decimalSIPrefix('m');
  409. kbn.valueFormats.lengthmm = kbn.formatBuilders.decimalSIPrefix('m', -1);
  410. kbn.valueFormats.lengthkm = kbn.formatBuilders.decimalSIPrefix('m', 1);
  411. kbn.valueFormats.lengthmi = kbn.formatBuilders.fixedUnit('mi');
  412. // Velocity
  413. kbn.valueFormats.velocityms = kbn.formatBuilders.fixedUnit('m/s');
  414. kbn.valueFormats.velocitykmh = kbn.formatBuilders.fixedUnit('km/h');
  415. kbn.valueFormats.velocitymph = kbn.formatBuilders.fixedUnit('mph');
  416. kbn.valueFormats.velocityknot = kbn.formatBuilders.fixedUnit('kn');
  417. // Volume
  418. kbn.valueFormats.litre = kbn.formatBuilders.decimalSIPrefix('L');
  419. kbn.valueFormats.mlitre = kbn.formatBuilders.decimalSIPrefix('L', -1);
  420. kbn.valueFormats.m3 = kbn.formatBuilders.decimalSIPrefix('m3');
  421. // Time
  422. kbn.valueFormats.hertz = kbn.formatBuilders.decimalSIPrefix('Hz');
  423. kbn.valueFormats.ms = function(size, decimals, scaledDecimals) {
  424. if (size === null) { return ""; }
  425. if (Math.abs(size) < 1000) {
  426. return kbn.toFixed(size, decimals) + " ms";
  427. }
  428. // Less than 1 min
  429. else if (Math.abs(size) < 60000) {
  430. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " s");
  431. }
  432. // Less than 1 hour, devide in minutes
  433. else if (Math.abs(size) < 3600000) {
  434. return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, " min");
  435. }
  436. // Less than one day, devide in hours
  437. else if (Math.abs(size) < 86400000) {
  438. return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, " hour");
  439. }
  440. // Less than one year, devide in days
  441. else if (Math.abs(size) < 31536000000) {
  442. return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, " day");
  443. }
  444. return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, " year");
  445. };
  446. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  447. if (size === null) { return ""; }
  448. // Less than 1 µs, devide in ns
  449. if (Math.abs(size) < 0.000001) {
  450. return kbn.toFixedScaled(size * 1.e9, decimals, scaledDecimals - decimals, -9, " ns");
  451. }
  452. // Less than 1 ms, devide in µs
  453. if (Math.abs(size) < 0.001) {
  454. return kbn.toFixedScaled(size * 1.e6, decimals, scaledDecimals - decimals, -6, " µs");
  455. }
  456. // Less than 1 second, devide in ms
  457. if (Math.abs(size) < 1) {
  458. return kbn.toFixedScaled(size * 1.e3, decimals, scaledDecimals - decimals, -3, " ms");
  459. }
  460. if (Math.abs(size) < 60) {
  461. return kbn.toFixed(size, decimals) + " s";
  462. }
  463. // Less than 1 hour, devide in minutes
  464. else if (Math.abs(size) < 3600) {
  465. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, " min");
  466. }
  467. // Less than one day, devide in hours
  468. else if (Math.abs(size) < 86400) {
  469. return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, " hour");
  470. }
  471. // Less than one week, devide in days
  472. else if (Math.abs(size) < 604800) {
  473. return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, " day");
  474. }
  475. // Less than one year, devide in week
  476. else if (Math.abs(size) < 31536000) {
  477. return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, " week");
  478. }
  479. return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, " year");
  480. };
  481. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  482. if (size === null) { return ""; }
  483. if (Math.abs(size) < 1000) {
  484. return kbn.toFixed(size, decimals) + " µs";
  485. }
  486. else if (Math.abs(size) < 1000000) {
  487. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " ms");
  488. }
  489. else {
  490. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " s");
  491. }
  492. };
  493. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  494. if (size === null) { return ""; }
  495. if (Math.abs(size) < 1000) {
  496. return kbn.toFixed(size, decimals) + " ns";
  497. }
  498. else if (Math.abs(size) < 1000000) {
  499. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " µs");
  500. }
  501. else if (Math.abs(size) < 1000000000) {
  502. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " ms");
  503. }
  504. else if (Math.abs(size) < 60000000000){
  505. return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, " s");
  506. }
  507. else {
  508. return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, " min");
  509. }
  510. };
  511. kbn.valueFormats.m = function(size, decimals, scaledDecimals) {
  512. if (size === null) { return ""; }
  513. if (Math.abs(size) < 60) {
  514. return kbn.toFixed(size, decimals) + " min";
  515. }
  516. else if (Math.abs(size) < 1440) {
  517. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 2, " hour");
  518. }
  519. else if (Math.abs(size) < 10080) {
  520. return kbn.toFixedScaled(size / 1440, decimals, scaledDecimals, 3, " day");
  521. }
  522. else if (Math.abs(size) < 604800) {
  523. return kbn.toFixedScaled(size / 10080, decimals, scaledDecimals, 4, " week");
  524. }
  525. else {
  526. return kbn.toFixedScaled(size / 5.25948e5, decimals, scaledDecimals, 5, " year");
  527. }
  528. };
  529. kbn.valueFormats.h = function(size, decimals, scaledDecimals) {
  530. if (size === null) { return ""; }
  531. if (Math.abs(size) < 24) {
  532. return kbn.toFixed(size, decimals) + " hour";
  533. }
  534. else if (Math.abs(size) < 168) {
  535. return kbn.toFixedScaled(size / 24, decimals, scaledDecimals, 2, " day");
  536. }
  537. else if (Math.abs(size) < 8760) {
  538. return kbn.toFixedScaled(size / 168, decimals, scaledDecimals, 3, " week");
  539. }
  540. else {
  541. return kbn.toFixedScaled(size / 8760, decimals, scaledDecimals, 4, " year");
  542. }
  543. };
  544. kbn.valueFormats.d = function(size, decimals, scaledDecimals) {
  545. if (size === null) { return ""; }
  546. if (Math.abs(size) < 7) {
  547. return kbn.toFixed(size, decimals) + " day";
  548. }
  549. else if (Math.abs(size) < 365) {
  550. return kbn.toFixedScaled(size / 7, decimals, scaledDecimals, 2, " week");
  551. }
  552. else {
  553. return kbn.toFixedScaled(size / 365, decimals, scaledDecimals, 3, " year");
  554. }
  555. };
  556. kbn.toDuration = function(size, timeScale) {
  557. return moment.duration(size, timeScale);
  558. };
  559. kbn.valueFormats.dtdurationms = function(size) {
  560. return kbn.toDuration(size, 'ms').humanize();
  561. };
  562. kbn.valueFormats.dtdurations = function(size) {
  563. return kbn.toDuration(size, 's').humanize();
  564. };
  565. ///// FORMAT MENU /////
  566. kbn.getUnitFormats = function() {
  567. return [
  568. {
  569. text: 'none',
  570. submenu: [
  571. {text: 'none' , value: 'none' },
  572. {text: 'short', value: 'short' },
  573. {text: 'percent (0-100)', value: 'percent' },
  574. {text: 'percent (0.0-1.0)', value: 'percentunit'},
  575. {text: 'Humidity (%H)', value: 'humidity' },
  576. {text: 'ppm', value: 'ppm' },
  577. {text: 'decibel', value: 'dB' },
  578. {text: 'hexadecimal (0x)', value: 'hex0x' },
  579. {text: 'hexadecimal', value: 'hex' },
  580. ]
  581. },
  582. {
  583. text: 'currency',
  584. submenu: [
  585. {text: 'Dollars ($)', value: 'currencyUSD'},
  586. {text: 'Pounds (£)', value: 'currencyGBP'},
  587. {text: 'Euro (€)', value: 'currencyEUR'},
  588. {text: 'Yen (¥)', value: 'currencyJPY'},
  589. ]
  590. },
  591. {
  592. text: 'time',
  593. submenu: [
  594. {text: 'Hertz (1/s)', value: 'hertz'},
  595. {text: 'nanoseconds (ns)' , value: 'ns' },
  596. {text: 'microseconds (µs)', value: 'µs' },
  597. {text: 'milliseconds (ms)', value: 'ms' },
  598. {text: 'seconds (s)', value: 's' },
  599. {text: 'minutes (m)', value: 'm' },
  600. {text: 'hours (h)', value: 'h' },
  601. {text: 'days (d)', value: 'd' },
  602. {text: 'duration (ms)', value: 'dtdurationms' },
  603. {text: 'duration (s)', value: 'dtdurations' }
  604. ]
  605. },
  606. {
  607. text: 'data (IEC)',
  608. submenu: [
  609. {text: 'bits', value: 'bits' },
  610. {text: 'bytes', value: 'bytes' },
  611. {text: 'kibibytes', value: 'kbytes'},
  612. {text: 'mebibytes', value: 'mbytes'},
  613. {text: 'gibibytes', value: 'gbytes'},
  614. ]
  615. },
  616. {
  617. text: 'data (Metric)',
  618. submenu: [
  619. {text: 'bits', value: 'decbits' },
  620. {text: 'bytes', value: 'decbytes' },
  621. {text: 'kilobytes', value: 'deckbytes'},
  622. {text: 'megabytes', value: 'decmbytes'},
  623. {text: 'gigabytes', value: 'decgbytes'},
  624. ]
  625. },
  626. {
  627. text: 'data rate',
  628. submenu: [
  629. {text: 'packets/sec', value: 'pps'},
  630. {text: 'bits/sec', value: 'bps'},
  631. {text: 'bytes/sec', value: 'Bps'},
  632. {text: 'kilobits/sec', value: 'Kbits'},
  633. {text: 'kilobytes/sec', value: 'KBs'},
  634. {text: 'megabits/sec', value: 'Mbits'},
  635. {text: 'megabytes/sec', value: 'MBs'},
  636. {text: 'gigabytes/sec', value: 'GBs'},
  637. {text: 'gigabits/sec', value: 'Gbits'},
  638. ]
  639. },
  640. {
  641. text: 'throughput',
  642. submenu: [
  643. {text: 'ops/sec (ops)', value: 'ops' },
  644. {text: 'reads/sec (rps)', value: 'rps' },
  645. {text: 'writes/sec (wps)', value: 'wps' },
  646. {text: 'I/O ops/sec (iops)', value: 'iops'},
  647. {text: 'ops/min (opm)', value: 'opm' },
  648. {text: 'reads/min (rpm)', value: 'rpm' },
  649. {text: 'writes/min (wpm)', value: 'wpm' },
  650. ]
  651. },
  652. {
  653. text: 'length',
  654. submenu: [
  655. {text: 'millimetre (mm)', value: 'lengthmm'},
  656. {text: 'meter (m)', value: 'lengthm' },
  657. {text: 'kilometer (km)', value: 'lengthkm'},
  658. {text: 'mile (mi)', value: 'lengthmi'},
  659. ]
  660. },
  661. {
  662. text: 'velocity',
  663. submenu: [
  664. {text: 'm/s', value: 'velocityms' },
  665. {text: 'km/h', value: 'velocitykmh' },
  666. {text: 'mph', value: 'velocitymph' },
  667. {text: 'knot (kn)', value: 'velocityknot'},
  668. ]
  669. },
  670. {
  671. text: 'volume',
  672. submenu: [
  673. {text: 'millilitre', value: 'mlitre'},
  674. {text: 'litre', value: 'litre' },
  675. {text: 'cubic metre', value: 'm3' },
  676. ]
  677. },
  678. {
  679. text: 'energy',
  680. submenu: [
  681. {text: 'watt (W)', value: 'watt' },
  682. {text: 'kilowatt (kW)', value: 'kwatt' },
  683. {text: 'volt-ampere (VA)', value: 'voltamp' },
  684. {text: 'kilovolt-ampere (kVA)', value: 'kvoltamp' },
  685. {text: 'volt-ampere reactive (var)', value: 'voltampreact'},
  686. {text: 'watt-hour (Wh)', value: 'watth' },
  687. {text: 'kilowatt-hour (kWh)', value: 'kwatth' },
  688. {text: 'joule (J)', value: 'joule' },
  689. {text: 'electron volt (eV)', value: 'ev' },
  690. {text: 'Ampere (A)', value: 'amp' },
  691. {text: 'Volt (V)', value: 'volt' },
  692. {text: 'Decibel-milliwatt (dBm)', value: 'dBm' },
  693. ]
  694. },
  695. {
  696. text: 'temperature',
  697. submenu: [
  698. {text: 'Celcius (°C)', value: 'celsius' },
  699. {text: 'Farenheit (°F)', value: 'farenheit' },
  700. {text: 'Kelvin (K)', value: 'kelvin' },
  701. ]
  702. },
  703. {
  704. text: 'pressure',
  705. submenu: [
  706. {text: 'Millibars', value: 'pressurembar'},
  707. {text: 'Hectopascals', value: 'pressurehpa' },
  708. {text: 'Inches of mercury', value: 'pressurehg' },
  709. {text: 'PSI', value: 'pressurepsi' },
  710. ]
  711. }
  712. ];
  713. };
  714. return kbn;
  715. });