kbn.js 31 KB

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