kbn.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. define([
  2. 'jquery',
  3. 'lodash',
  4. ],
  5. function($, _) {
  6. 'use strict';
  7. var kbn = {};
  8. kbn.valueFormats = {};
  9. kbn.round_interval = function(interval) {
  10. switch (true) {
  11. // 0.5s
  12. case (interval <= 500):
  13. return 100; // 0.1s
  14. // 5s
  15. case (interval <= 5000):
  16. return 1000; // 1s
  17. // 7.5s
  18. case (interval <= 7500):
  19. return 5000; // 5s
  20. // 15s
  21. case (interval <= 15000):
  22. return 10000; // 10s
  23. // 45s
  24. case (interval <= 45000):
  25. return 30000; // 30s
  26. // 3m
  27. case (interval <= 180000):
  28. return 60000; // 1m
  29. // 9m
  30. case (interval <= 450000):
  31. return 300000; // 5m
  32. // 20m
  33. case (interval <= 1200000):
  34. return 600000; // 10m
  35. // 45m
  36. case (interval <= 2700000):
  37. return 1800000; // 30m
  38. // 2h
  39. case (interval <= 7200000):
  40. return 3600000; // 1h
  41. // 6h
  42. case (interval <= 21600000):
  43. return 10800000; // 3h
  44. // 24h
  45. case (interval <= 86400000):
  46. return 43200000; // 12h
  47. // 48h
  48. case (interval <= 172800000):
  49. return 86400000; // 24h
  50. // 1w
  51. case (interval <= 604800000):
  52. return 86400000; // 24h
  53. // 3w
  54. case (interval <= 1814400000):
  55. return 604800000; // 1w
  56. // 2y
  57. case (interval < 3628800000):
  58. return 2592000000; // 30d
  59. default:
  60. return 31536000000; // 1y
  61. }
  62. };
  63. kbn.secondsToHms = function(seconds) {
  64. var numyears = Math.floor(seconds / 31536000);
  65. if(numyears){
  66. return numyears + 'y';
  67. }
  68. var numdays = Math.floor((seconds % 31536000) / 86400);
  69. if(numdays){
  70. return numdays + 'd';
  71. }
  72. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  73. if(numhours){
  74. return numhours + 'h';
  75. }
  76. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  77. if(numminutes){
  78. return numminutes + 'm';
  79. }
  80. var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
  81. if(numseconds){
  82. return numseconds + 's';
  83. }
  84. var nummilliseconds = Math.floor(seconds * 1000.0);
  85. if(nummilliseconds){
  86. return nummilliseconds + 'ms';
  87. }
  88. return 'less then a millisecond'; //'just now' //or other string you like;
  89. };
  90. kbn.to_percent = function(number,outof) {
  91. return Math.floor((number/outof)*10000)/100 + "%";
  92. };
  93. kbn.addslashes = function(str) {
  94. str = str.replace(/\\/g, '\\\\');
  95. str = str.replace(/\'/g, '\\\'');
  96. str = str.replace(/\"/g, '\\"');
  97. str = str.replace(/\0/g, '\\0');
  98. return str;
  99. };
  100. kbn.interval_regex = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
  101. // histogram & trends
  102. kbn.intervals_in_seconds = {
  103. y: 31536000,
  104. M: 2592000,
  105. w: 604800,
  106. d: 86400,
  107. h: 3600,
  108. m: 60,
  109. s: 1
  110. };
  111. kbn.calculateInterval = function(range, resolution, userInterval) {
  112. var lowLimitMs = 1; // 1 millisecond default low limit
  113. var intervalMs, lowLimitInterval;
  114. if (userInterval) {
  115. if (userInterval[0] === '>') {
  116. lowLimitInterval = userInterval.slice(1);
  117. lowLimitMs = kbn.interval_to_ms(lowLimitInterval);
  118. }
  119. else {
  120. return userInterval;
  121. }
  122. }
  123. intervalMs = kbn.round_interval((range.to.valueOf() - range.from.valueOf()) / resolution);
  124. if (lowLimitMs > intervalMs) {
  125. intervalMs = lowLimitMs;
  126. }
  127. return kbn.secondsToHms(intervalMs / 1000);
  128. };
  129. kbn.describe_interval = function (string) {
  130. var matches = string.match(kbn.interval_regex);
  131. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  132. throw new Error('Invalid interval string, expexcting a number followed by one of "Mwdhmsy"');
  133. } else {
  134. return {
  135. sec: kbn.intervals_in_seconds[matches[2]],
  136. type: matches[2],
  137. count: parseInt(matches[1], 10)
  138. };
  139. }
  140. };
  141. kbn.interval_to_ms = function(string) {
  142. var info = kbn.describe_interval(string);
  143. return info.sec * 1000 * info.count;
  144. };
  145. kbn.interval_to_seconds = function (string) {
  146. var info = kbn.describe_interval(string);
  147. return info.sec * info.count;
  148. };
  149. kbn.query_color_dot = function (color, diameter) {
  150. return '<div class="icon-circle" style="' + [
  151. 'display:inline-block',
  152. 'color:' + color,
  153. 'font-size:' + diameter + 'px',
  154. ].join(';') + '"></div>';
  155. };
  156. kbn.valueFormats.percent = function(size, decimals) {
  157. return kbn.toFixed(size, decimals) + '%';
  158. };
  159. kbn.formatFuncCreator = function(factor, extArray) {
  160. return function(size, decimals, scaledDecimals) {
  161. if (size === null) {
  162. return "";
  163. }
  164. var steps = 0;
  165. var limit = extArray.length;
  166. while (Math.abs(size) >= factor) {
  167. steps++;
  168. size /= factor;
  169. if (steps >= limit) { return "NA"; }
  170. }
  171. if (steps > 0 && scaledDecimals !== null) {
  172. decimals = scaledDecimals + (3 * steps);
  173. }
  174. return kbn.toFixed(size, decimals) + extArray[steps];
  175. };
  176. };
  177. kbn.toFixed = function(value, decimals) {
  178. if (value === null) {
  179. return "";
  180. }
  181. var factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
  182. var formatted = String(Math.round(value * factor) / factor);
  183. // if exponent return directly
  184. if (formatted.indexOf('e') !== -1 || value === 0) {
  185. return formatted;
  186. }
  187. // If tickDecimals was specified, ensure that we have exactly that
  188. // much precision; otherwise default to the value's own precision.
  189. if (decimals != null) {
  190. var decimalPos = formatted.indexOf(".");
  191. var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
  192. if (precision < decimals) {
  193. return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision);
  194. }
  195. }
  196. return formatted;
  197. };
  198. kbn.valueFormats.bits = kbn.formatFuncCreator(1024, [' b', ' Kib', ' Mib', ' Gib', ' Tib', ' Pib', ' Eib', ' Zib', ' Yib']);
  199. kbn.valueFormats.bytes = kbn.formatFuncCreator(1024, [' B', ' KiB', ' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  200. kbn.valueFormats.kbytes = kbn.formatFuncCreator(1024, [' KiB', ' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  201. kbn.valueFormats.mbytes = kbn.formatFuncCreator(1024, [' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  202. kbn.valueFormats.gbytes = kbn.formatFuncCreator(1024, [' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  203. kbn.valueFormats.bps = kbn.formatFuncCreator(1000, [' bps', ' Kbps', ' Mbps', ' Gbps', ' Tbps', ' Pbps', ' Ebps', ' Zbps', ' Ybps']);
  204. kbn.valueFormats.pps = kbn.formatFuncCreator(1000, [' pps', ' Kpps', ' Mpps', ' Gpps', ' Tpps', ' Ppps', ' Epps', ' Zpps', ' Ypps']);
  205. kbn.valueFormats.Bps = kbn.formatFuncCreator(1000, [' Bps', ' KBps', ' MBps', ' GBps', ' TBps', ' PBps', ' EBps', ' ZBps', ' YBps']);
  206. kbn.valueFormats.short = kbn.formatFuncCreator(1000, ['', ' K', ' Mil', ' Bil', ' Tri', ' Quadr', ' Quint', ' Sext', ' Sept']);
  207. kbn.valueFormats.joule = kbn.formatFuncCreator(1000, [' J', ' kJ', ' MJ', ' GJ', ' TJ', ' PJ', ' EJ', ' ZJ', ' YJ']);
  208. kbn.valueFormats.amp = kbn.formatFuncCreator(1000, [' A', ' kA', ' MA', ' GA', ' TA', ' PA', ' EA', ' ZA', ' YA']);
  209. kbn.valueFormats.volt = kbn.formatFuncCreator(1000, [' V', ' kV', ' MV', ' GV', ' TV', ' PV', ' EV', ' ZV', ' YV']);
  210. kbn.valueFormats.hertz = kbn.formatFuncCreator(1000, [' Hz', ' kHz', ' MHz', ' GHz', ' THz', ' PHz', ' EHz', ' ZHz', ' YHz']);
  211. kbn.valueFormats.watt = kbn.formatFuncCreator(1000, [' W', ' kW', ' MW', ' GW', ' TW', ' PW', ' EW', ' ZW', ' YW']);
  212. kbn.valueFormats.kwatt = kbn.formatFuncCreator(1000, [' kW', ' MW', ' GW', ' TW', ' PW', ' EW', ' ZW', ' YW']);
  213. kbn.valueFormats.watth = kbn.formatFuncCreator(1000, [' Wh', ' kWh', ' MWh', ' GWh', ' TWh', ' PWh', ' EWh', ' ZWh', ' YWh']);
  214. kbn.valueFormats.kwatth = kbn.formatFuncCreator(1000, [' kWh', ' MWh', ' GWh', ' TWh', ' PWh', ' EWh', ' ZWh', ' YWh']);
  215. kbn.valueFormats.ev = kbn.formatFuncCreator(1000, [' eV', ' keV', ' MeV', 'GeV', 'TeV', 'PeV', 'EeV', 'ZeV', 'YeV']);
  216. kbn.valueFormats.none = kbn.toFixed;
  217. kbn.valueFormats.celsius = function(value, decimals) { return kbn.toFixed(value, decimals) + ' °C'; };
  218. kbn.valueFormats.farenheit = function(value, decimals) { return kbn.toFixed(value, decimals) + ' °F'; };
  219. kbn.valueFormats.humidity = function(value, decimals) { return kbn.toFixed(value, decimals) + ' %H'; };
  220. kbn.valueFormats.pressurembar = function(value, decimals) { return kbn.toFixed(value, decimals) + ' mbar'; };
  221. kbn.valueFormats.pressurehpa = function(value, decimals) { return kbn.toFixed(value, decimals) + ' hPa'; };
  222. kbn.valueFormats.ppm = function(value, decimals) { return kbn.toFixed(value, decimals) + ' ppm'; };
  223. kbn.valueFormats.velocityms = function(value, decimals) { return kbn.toFixed(value, decimals) + ' m/s'; };
  224. kbn.valueFormats.velocitykmh = function(value, decimals) { return kbn.toFixed(value, decimals) + ' km/h'; };
  225. kbn.valueFormats.velocitymph = function(value, decimals) { return kbn.toFixed(value, decimals) + ' mph'; };
  226. kbn.valueFormats.velocityknot = function(value, decimals) { return kbn.toFixed(value, decimals) + ' kn'; };
  227. kbn.roundValue = function (num, decimals) {
  228. if (num === null) { return null; }
  229. var n = Math.pow(10, decimals);
  230. return Math.round((n * num).toFixed(decimals)) / n;
  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.valueFormats.ms = function(size, decimals, scaledDecimals) {
  240. if (size === null) { return ""; }
  241. if (Math.abs(size) < 1000) {
  242. return kbn.toFixed(size, decimals) + " ms";
  243. }
  244. // Less than 1 min
  245. else if (Math.abs(size) < 60000) {
  246. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " s");
  247. }
  248. // Less than 1 hour, devide in minutes
  249. else if (Math.abs(size) < 3600000) {
  250. return kbn.toFixedScaled(size / 60000, decimals, scaledDecimals, 5, " min");
  251. }
  252. // Less than one day, devide in hours
  253. else if (Math.abs(size) < 86400000) {
  254. return kbn.toFixedScaled(size / 3600000, decimals, scaledDecimals, 7, " hour");
  255. }
  256. // Less than one year, devide in days
  257. else if (Math.abs(size) < 31536000000) {
  258. return kbn.toFixedScaled(size / 86400000, decimals, scaledDecimals, 8, " day");
  259. }
  260. return kbn.toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, " year");
  261. };
  262. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  263. if (size === null) { return ""; }
  264. if (Math.abs(size) < 600) {
  265. return kbn.toFixed(size, decimals) + " s";
  266. }
  267. // Less than 1 hour, devide in minutes
  268. else if (Math.abs(size) < 3600) {
  269. return kbn.toFixedScaled(size / 60, decimals, scaledDecimals, 1, " min");
  270. }
  271. // Less than one day, devide in hours
  272. else if (Math.abs(size) < 86400) {
  273. return kbn.toFixedScaled(size / 3600, decimals, scaledDecimals, 4, " hour");
  274. }
  275. // Less than one week, devide in days
  276. else if (Math.abs(size) < 604800) {
  277. return kbn.toFixedScaled(size / 86400, decimals, scaledDecimals, 5, " day");
  278. }
  279. // Less than one year, devide in week
  280. else if (Math.abs(size) < 31536000) {
  281. return kbn.toFixedScaled(size / 604800, decimals, scaledDecimals, 6, " week");
  282. }
  283. return kbn.toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, " year");
  284. };
  285. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  286. if (size === null) { return ""; }
  287. if (Math.abs(size) < 1000) {
  288. return kbn.toFixed(size, decimals) + " µs";
  289. }
  290. else if (Math.abs(size) < 1000000) {
  291. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " ms");
  292. }
  293. else {
  294. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " s");
  295. }
  296. };
  297. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  298. if (size === null) { return ""; }
  299. if (Math.abs(size) < 1000) {
  300. return kbn.toFixed(size, decimals) + " ns";
  301. }
  302. else if (Math.abs(size) < 1000000) {
  303. return kbn.toFixedScaled(size / 1000, decimals, scaledDecimals, 3, " µs");
  304. }
  305. else if (Math.abs(size) < 1000000000) {
  306. return kbn.toFixedScaled(size / 1000000, decimals, scaledDecimals, 6, " ms");
  307. }
  308. else if (Math.abs(size) < 60000000000){
  309. return kbn.toFixedScaled(size / 1000000000, decimals, scaledDecimals, 9, " s");
  310. }
  311. else {
  312. return kbn.toFixedScaled(size / 60000000000, decimals, scaledDecimals, 12, " min");
  313. }
  314. };
  315. kbn.slugifyForUrl = function(str) {
  316. return str
  317. .toLowerCase()
  318. .replace(/[^\w ]+/g,'')
  319. .replace(/ +/g,'-');
  320. };
  321. kbn.exportSeriesListToCsv = function(seriesList) {
  322. var text = 'Series;Time;Value\n';
  323. _.each(seriesList, function(series) {
  324. _.each(series.datapoints, function(dp) {
  325. text += series.alias + ';' + new Date(dp[1]).toISOString() + ';' + dp[0] + '\n';
  326. });
  327. });
  328. var blob = new Blob([text], { type: "text/csv;charset=utf-8" });
  329. window.saveAs(blob, 'grafana_data_export.csv');
  330. };
  331. kbn.stringToJsRegex = function(str) {
  332. if (str[0] !== '/') {
  333. return new RegExp(str);
  334. }
  335. var match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
  336. return new RegExp(match[1], match[2]);
  337. };
  338. kbn.getUnitFormats = function() {
  339. return [
  340. {
  341. text: 'none',
  342. submenu: [
  343. {text: 'none' , value: 'none'},
  344. {text: 'short', value: 'short'},
  345. {text: 'percent', value: 'percent'},
  346. {text: 'ppm', value: 'ppm'},
  347. {text: 'dB', value: 'dB'},
  348. ]
  349. },
  350. {
  351. text: 'duration',
  352. submenu: [
  353. {text: 'nanoseconds (ns)' , value: 'ns'},
  354. {text: 'microseconds (µs)', value: 'µs'},
  355. {text: 'milliseconds (ms)', value: 'ms'},
  356. {text: 'seconds (s)', value: 's'},
  357. {text: 'Hertz (1/s)', value: 'hertz'},
  358. ]
  359. },
  360. {
  361. text: 'data',
  362. submenu: [
  363. {text: 'bits', value: 'bits'},
  364. {text: 'bytes', value: 'bytes'},
  365. {text: 'kilobytes', value: 'kbytes'},
  366. {text: 'megabytes', value: 'mbytes'},
  367. {text: 'gigabytes', value: 'gbytes'},
  368. ]
  369. },
  370. {
  371. text: 'data rate',
  372. submenu: [
  373. {text: 'packets/sec', value: 'pps'},
  374. {text: 'bits/sec', value: 'bps'},
  375. {text: 'bytes/sec', value: 'Bps'},
  376. ]
  377. },
  378. {
  379. text: 'energy',
  380. submenu: [
  381. {text: 'watt (W)', value: 'watt'},
  382. {text: 'kilowatt (kW)', value: 'kwatt'},
  383. {text: 'watt-hour (Wh)', value: 'watth'},
  384. {text: 'kilowatt-hour (kWh)', value: 'kwatth'},
  385. {text: 'joule (J)', value: 'joule'},
  386. {text: 'electron volt (eV)', value: 'ev'},
  387. {text: 'Ampere (A)', value: 'amp'},
  388. {text: 'Volt (V)', value: 'volt'},
  389. ]
  390. },
  391. {
  392. text: 'weather',
  393. submenu: [
  394. {text: 'Celcius (°C)', value: 'celsius' },
  395. {text: 'Farenheit (°F)', value: 'farenheit'},
  396. {text: 'Humidity (%H)', value: 'humidity' },
  397. {text: 'Pressure (mbar)', value: 'pressurembar' },
  398. {text: 'Pressure (hPa)', value: 'pressurehpa' },
  399. ]
  400. },
  401. {
  402. text: 'velocity',
  403. submenu: [
  404. {text: 'm/s', value: 'velocityms' },
  405. {text: 'km/h', value: 'velocitykmh' },
  406. {text: 'mph', value: 'velocitymph' },
  407. {text: 'knot (kn)', value: 'velocityknot' },
  408. ]
  409. },
  410. ];
  411. };
  412. return kbn;
  413. });