kbn.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. define([
  2. 'jquery',
  3. 'lodash',
  4. 'moment'
  5. ],
  6. function($, _, moment) {
  7. 'use strict';
  8. var kbn = {};
  9. kbn.valueFormats = {};
  10. kbn.round_interval = function(interval) {
  11. switch (true) {
  12. // 0.5s
  13. case (interval <= 500):
  14. return 100; // 0.1s
  15. // 5s
  16. case (interval <= 5000):
  17. return 1000; // 1s
  18. // 7.5s
  19. case (interval <= 7500):
  20. return 5000; // 5s
  21. // 15s
  22. case (interval <= 15000):
  23. return 10000; // 10s
  24. // 45s
  25. case (interval <= 45000):
  26. return 30000; // 30s
  27. // 3m
  28. case (interval <= 180000):
  29. return 60000; // 1m
  30. // 9m
  31. case (interval <= 450000):
  32. return 300000; // 5m
  33. // 20m
  34. case (interval <= 1200000):
  35. return 600000; // 10m
  36. // 45m
  37. case (interval <= 2700000):
  38. return 1800000; // 30m
  39. // 2h
  40. case (interval <= 7200000):
  41. return 3600000; // 1h
  42. // 6h
  43. case (interval <= 21600000):
  44. return 10800000; // 3h
  45. // 24h
  46. case (interval <= 86400000):
  47. return 43200000; // 12h
  48. // 48h
  49. case (interval <= 172800000):
  50. return 86400000; // 24h
  51. // 1w
  52. case (interval <= 604800000):
  53. return 86400000; // 24h
  54. // 3w
  55. case (interval <= 1814400000):
  56. return 604800000; // 1w
  57. // 2y
  58. case (interval < 3628800000):
  59. return 2592000000; // 30d
  60. default:
  61. return 31536000000; // 1y
  62. }
  63. };
  64. kbn.secondsToHms = function(seconds) {
  65. var numyears = Math.floor(seconds / 31536000);
  66. if(numyears){
  67. return numyears + 'y';
  68. }
  69. var numdays = Math.floor((seconds % 31536000) / 86400);
  70. if(numdays){
  71. return numdays + 'd';
  72. }
  73. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  74. if(numhours){
  75. return numhours + 'h';
  76. }
  77. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  78. if(numminutes){
  79. return numminutes + 'm';
  80. }
  81. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  82. if(numseconds){
  83. return numseconds + 's';
  84. }
  85. return 'less then a second'; //'just now' //or other string you like;
  86. };
  87. kbn.to_percent = function(number,outof) {
  88. return Math.floor((number/outof)*10000)/100 + "%";
  89. };
  90. kbn.addslashes = function(str) {
  91. str = str.replace(/\\/g, '\\\\');
  92. str = str.replace(/\'/g, '\\\'');
  93. str = str.replace(/\"/g, '\\"');
  94. str = str.replace(/\0/g, '\\0');
  95. return str;
  96. };
  97. kbn.interval_regex = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
  98. // histogram & trends
  99. kbn.intervals_in_seconds = {
  100. y: 31536000,
  101. M: 2592000,
  102. w: 604800,
  103. d: 86400,
  104. h: 3600,
  105. m: 60,
  106. s: 1
  107. };
  108. kbn.calculateInterval = function(range, resolution, userInterval) {
  109. var lowLimitMs = 1; // 1 millisecond default low limit
  110. var intervalMs, lowLimitInterval;
  111. if (userInterval) {
  112. if (userInterval[0] === '>') {
  113. lowLimitInterval = userInterval.slice(1);
  114. lowLimitMs = kbn.interval_to_ms(lowLimitInterval);
  115. }
  116. else {
  117. return userInterval;
  118. }
  119. }
  120. intervalMs = kbn.round_interval((range.to.valueOf() - range.from.valueOf()) / resolution);
  121. if (lowLimitMs > intervalMs) {
  122. intervalMs = lowLimitMs;
  123. }
  124. return kbn.secondsToHms(intervalMs / 1000);
  125. };
  126. kbn.describe_interval = function (string) {
  127. var matches = string.match(kbn.interval_regex);
  128. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  129. throw new Error('Invalid interval string, expexcting a number followed by one of "Mwdhmsy"');
  130. } else {
  131. return {
  132. sec: kbn.intervals_in_seconds[matches[2]],
  133. type: matches[2],
  134. count: parseInt(matches[1], 10)
  135. };
  136. }
  137. };
  138. kbn.interval_to_ms = function(string) {
  139. var info = kbn.describe_interval(string);
  140. return info.sec * 1000 * info.count;
  141. };
  142. kbn.interval_to_seconds = function (string) {
  143. var info = kbn.describe_interval(string);
  144. return info.sec * info.count;
  145. };
  146. // This should go away, moment.js can do this
  147. kbn.time_ago = function(string) {
  148. return new Date(new Date().getTime() - (kbn.interval_to_ms(string)));
  149. };
  150. /* This is a simplified version of elasticsearch's date parser */
  151. kbn.parseDate = function(text) {
  152. if(_.isDate(text)) {
  153. return text;
  154. }
  155. var time,
  156. mathString = "",
  157. index,
  158. parseString;
  159. if (text.substring(0,3) === "now") {
  160. time = new Date();
  161. mathString = text.substring("now".length);
  162. } else {
  163. index = text.indexOf("||");
  164. parseString;
  165. if (index === -1) {
  166. parseString = text;
  167. mathString = ""; // nothing else
  168. } else {
  169. parseString = text.substring(0, index);
  170. mathString = text.substring(index + 2);
  171. }
  172. // We're going to just require ISO8601 timestamps, k?
  173. time = new Date(parseString);
  174. }
  175. if (!mathString.length) {
  176. return time;
  177. }
  178. //return [time,parseString,mathString];
  179. return kbn.parseDateMath(mathString, time);
  180. };
  181. kbn._timespanRegex = /^\d+[h,m,M,w,s,H,d]$/;
  182. kbn.isValidTimeSpan = function(str) {
  183. return kbn._timespanRegex.test(str);
  184. };
  185. kbn.parseDateMath = function(mathString, time, roundUp) {
  186. var dateTime = moment(time);
  187. for (var i = 0; i < mathString.length;) {
  188. var c = mathString.charAt(i++),
  189. type,
  190. num,
  191. unit;
  192. if (c === '/') {
  193. type = 0;
  194. } else if (c === '+') {
  195. type = 1;
  196. } else if (c === '-') {
  197. type = 2;
  198. } else {
  199. return false;
  200. }
  201. if (isNaN(mathString.charAt(i))) {
  202. num = 1;
  203. } else {
  204. var numFrom = i;
  205. while (!isNaN(mathString.charAt(i))) {
  206. i++;
  207. }
  208. num = parseInt(mathString.substring(numFrom, i),10);
  209. }
  210. if (type === 0) {
  211. // rounding is only allowed on whole numbers
  212. if (num !== 1) {
  213. return false;
  214. }
  215. }
  216. unit = mathString.charAt(i++);
  217. switch (unit) {
  218. case 'y':
  219. if (type === 0) {
  220. roundUp ? dateTime.endOf('year') : dateTime.startOf('year');
  221. } else if (type === 1) {
  222. dateTime.add(num, 'years');
  223. } else if (type === 2) {
  224. dateTime.subtract(num, 'years');
  225. }
  226. break;
  227. case 'M':
  228. if (type === 0) {
  229. roundUp ? dateTime.endOf('month') : dateTime.startOf('month');
  230. } else if (type === 1) {
  231. dateTime.add(num, 'months');
  232. } else if (type === 2) {
  233. dateTime.subtract(num, 'months');
  234. }
  235. break;
  236. case 'w':
  237. if (type === 0) {
  238. roundUp ? dateTime.endOf('week') : dateTime.startOf('week');
  239. } else if (type === 1) {
  240. dateTime.add(num, 'weeks');
  241. } else if (type === 2) {
  242. dateTime.subtract(num, 'weeks');
  243. }
  244. break;
  245. case 'd':
  246. if (type === 0) {
  247. roundUp ? dateTime.endOf('day') : dateTime.startOf('day');
  248. } else if (type === 1) {
  249. dateTime.add(num, 'days');
  250. } else if (type === 2) {
  251. dateTime.subtract(num, 'days');
  252. }
  253. break;
  254. case 'h':
  255. case 'H':
  256. if (type === 0) {
  257. roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour');
  258. } else if (type === 1) {
  259. dateTime.add(num, 'hours');
  260. } else if (type === 2) {
  261. dateTime.subtract(num,'hours');
  262. }
  263. break;
  264. case 'm':
  265. if (type === 0) {
  266. roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute');
  267. } else if (type === 1) {
  268. dateTime.add(num, 'minutes');
  269. } else if (type === 2) {
  270. dateTime.subtract(num, 'minutes');
  271. }
  272. break;
  273. case 's':
  274. if (type === 0) {
  275. roundUp ? dateTime.endOf('second') : dateTime.startOf('second');
  276. } else if (type === 1) {
  277. dateTime.add(num, 'seconds');
  278. } else if (type === 2) {
  279. dateTime.subtract(num, 'seconds');
  280. }
  281. break;
  282. default:
  283. return false;
  284. }
  285. }
  286. return dateTime.toDate();
  287. };
  288. kbn.query_color_dot = function (color, diameter) {
  289. return '<div class="icon-circle" style="' + [
  290. 'display:inline-block',
  291. 'color:' + color,
  292. 'font-size:' + diameter + 'px',
  293. ].join(';') + '"></div>';
  294. };
  295. kbn.valueFormats.percent = function(size, decimals) {
  296. return kbn.toFixed(size, decimals) + '%';
  297. };
  298. kbn.formatFuncCreator = function(factor, extArray) {
  299. return function(size, decimals, scaledDecimals) {
  300. if (size === null) {
  301. return "";
  302. }
  303. var steps = 0;
  304. while (Math.abs(size) >= factor) {
  305. steps++;
  306. size /= factor;
  307. }
  308. if (steps > 0) {
  309. decimals = scaledDecimals + (3 * steps);
  310. }
  311. return kbn.toFixed(size, decimals) + extArray[steps];
  312. };
  313. };
  314. kbn.toFixed = function(value, decimals) {
  315. if (value === null) {
  316. return "";
  317. }
  318. var factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
  319. var formatted = String(Math.round(value * factor) / factor);
  320. // if exponent return directly
  321. if (formatted.indexOf('e') !== -1 || value === 0) {
  322. return formatted;
  323. }
  324. // If tickDecimals was specified, ensure that we have exactly that
  325. // much precision; otherwise default to the value's own precision.
  326. if (decimals != null) {
  327. var decimalPos = formatted.indexOf(".");
  328. var precision = decimalPos === -1 ? 0 : formatted.length - decimalPos - 1;
  329. if (precision < decimals) {
  330. return (precision ? formatted : formatted + ".") + (String(factor)).substr(1, decimals - precision);
  331. }
  332. }
  333. return formatted;
  334. };
  335. kbn.valueFormats.bits = kbn.formatFuncCreator(1024, [' b', ' Kib', ' Mib', ' Gib', ' Tib', ' Pib', ' Eib', ' Zib', ' Yib']);
  336. kbn.valueFormats.bytes = kbn.formatFuncCreator(1024, [' B', ' KiB', ' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  337. kbn.valueFormats.kbytes = kbn.formatFuncCreator(1024, [' KiB', ' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  338. kbn.valueFormats.mbytes = kbn.formatFuncCreator(1024, [' MiB', ' GiB', ' TiB', ' PiB', ' EiB', ' ZiB', ' YiB']);
  339. kbn.valueFormats.bps = kbn.formatFuncCreator(1000, [' bps', ' Kbps', ' Mbps', ' Gbps', ' Tbps', ' Pbps', ' Ebps', ' Zbps', ' Ybps']);
  340. kbn.valueFormats.Bps = kbn.formatFuncCreator(1000, [' Bps', ' KBps', ' MBps', ' GBps', ' TBps', ' PBps', ' EBps', ' ZBps', ' YBps']);
  341. kbn.valueFormats.short = kbn.formatFuncCreator(1000, ['', ' K', ' Mil', ' Bil', ' Tri', ' Qaudr', ' Quint', ' Sext', ' Sept']);
  342. kbn.valueFormats.joule = kbn.formatFuncCreator(1000, [' J', ' kJ', ' MJ', 'GJ', 'TJ', 'PJ', 'EJ', 'ZJ', 'YJ']);
  343. kbn.valueFormats.watt = kbn.formatFuncCreator(1000, [' W', ' kW', ' MW', 'GW', 'TW', 'PW', 'EW', 'ZW', 'YW']);
  344. kbn.valueFormats.ev = kbn.formatFuncCreator(1000, [' eV', ' keV', ' MeV', 'GeV', 'TeV', 'PeV', 'EeV', 'ZeV', 'YeV']);
  345. kbn.valueFormats.none = kbn.toFixed;
  346. kbn.valueFormats.ms = function(size, decimals, scaledDecimals) {
  347. if (size === null) { return ""; }
  348. if (Math.abs(size) < 1000) {
  349. return kbn.toFixed(size, decimals) + " ms";
  350. }
  351. // Less than 1 min
  352. else if (Math.abs(size) < 60000) {
  353. return kbn.toFixed(size / 1000, scaledDecimals + 3) + " s";
  354. }
  355. // Less than 1 hour, devide in minutes
  356. else if (Math.abs(size) < 3600000) {
  357. return kbn.toFixed(size / 60000, scaledDecimals + 5) + " min";
  358. }
  359. // Less than one day, devide in hours
  360. else if (Math.abs(size) < 86400000) {
  361. return kbn.toFixed(size / 3600000, scaledDecimals + 7) + " hour";
  362. }
  363. // Less than one year, devide in days
  364. else if (Math.abs(size) < 31536000000) {
  365. return kbn.toFixed(size / 86400000, scaledDecimals + 8) + " day";
  366. }
  367. return kbn.toFixed(size / 31536000000, scaledDecimals + 10) + " year";
  368. };
  369. kbn.valueFormats.s = function(size, decimals, scaledDecimals) {
  370. if (size === null) { return ""; }
  371. if (Math.abs(size) < 600) {
  372. return kbn.toFixed(size, decimals) + " s";
  373. }
  374. // Less than 1 hour, devide in minutes
  375. else if (Math.abs(size) < 3600) {
  376. return kbn.toFixed(size / 60, scaledDecimals + 1) + " min";
  377. }
  378. // Less than one day, devide in hours
  379. else if (Math.abs(size) < 86400) {
  380. return kbn.toFixed(size / 3600, scaledDecimals + 4) + " hour";
  381. }
  382. // Less than one week, devide in days
  383. else if (Math.abs(size) < 604800) {
  384. return kbn.toFixed(size / 86400, scaledDecimals + 5) + " day";
  385. }
  386. // Less than one year, devide in week
  387. else if (Math.abs(size) < 31536000) {
  388. return kbn.toFixed(size / 604800, scaledDecimals + 6) + " week";
  389. }
  390. return kbn.toFixed(size / 3.15569e7, scaledDecimals + 7) + " year";
  391. };
  392. kbn.valueFormats['µs'] = function(size, decimals, scaledDecimals) {
  393. if (size === null) { return ""; }
  394. if (Math.abs(size) < 1000) {
  395. return kbn.toFixed(size, decimals) + " µs";
  396. }
  397. else if (Math.abs(size) < 1000000) {
  398. return kbn.toFixed(size / 1000, scaledDecimals + 3) + " ms";
  399. }
  400. else {
  401. return kbn.toFixed(size / 1000000, scaledDecimals + 6) + " s";
  402. }
  403. };
  404. kbn.valueFormats.ns = function(size, decimals, scaledDecimals) {
  405. if (size === null) { return ""; }
  406. if (Math.abs(size) < 1000) {
  407. return kbn.toFixed(size, decimals) + " ns";
  408. }
  409. else if (Math.abs(size) < 1000000) {
  410. return kbn.toFixed(size / 1000, scaledDecimals + 3) + " µs";
  411. }
  412. else if (Math.abs(size) < 1000000000) {
  413. return kbn.toFixed(size / 1000000, scaledDecimals + 6) + " ms";
  414. }
  415. else if (Math.abs(size) < 60000000000){
  416. return kbn.toFixed(size / 1000000000, scaledDecimals + 9) + " s";
  417. }
  418. else {
  419. return kbn.toFixed(size / 60000000000, scaledDecimals + 12) + " m";
  420. }
  421. };
  422. kbn.slugifyForUrl = function(str) {
  423. return str
  424. .toLowerCase()
  425. .replace(/[^\w ]+/g,'')
  426. .replace(/ +/g,'-');
  427. };
  428. kbn.exportSeriesListToCsv = function(seriesList) {
  429. var text = 'Series;Time;Value\n';
  430. _.each(seriesList, function(series) {
  431. _.each(series.datapoints, function(dp) {
  432. text += series.alias + ';' + new Date(dp[1]).toISOString() + ';' + dp[0] + '\n';
  433. });
  434. });
  435. var blob = new Blob([text], { type: "text/csv;charset=utf-8" });
  436. window.saveAs(blob, 'grafana_data_export.csv');
  437. };
  438. kbn.stringToJsRegex = function(str) {
  439. if (str[0] !== '/') {
  440. return new RegExp(str);
  441. }
  442. var match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
  443. return new RegExp(match[1], match[2]);
  444. };
  445. kbn.getUnitFormats = function() {
  446. return [
  447. {
  448. text: 'none',
  449. submenu: [
  450. {text: 'none' , value: 'none'},
  451. {text: 'short', value: 'short'},
  452. {text: 'percent', value: 'percent'},
  453. ]
  454. },
  455. {
  456. text: 'duration',
  457. submenu: [
  458. {text: 'nanoseconds (ns)' , value: 'ns'},
  459. {text: 'microseconds (µs)', value: 'µs'},
  460. {text: 'milliseconds (ms)', value: 'ms'},
  461. {text: 'seconds (s)', value: 's'},
  462. ]
  463. },
  464. {
  465. text: 'data',
  466. submenu: [
  467. {text: 'bits', value: 'bits'},
  468. {text: 'bytes', value: 'bytes'},
  469. {text: 'kilo bytes', value: 'kbytes'},
  470. {text: 'mega bytes', value: 'mbytes'},
  471. ]
  472. },
  473. {
  474. text: 'data rate',
  475. submenu: [
  476. {text: 'bits/sec', value: 'bps'},
  477. {text: 'bytes/sec', value: 'Bps'},
  478. ]
  479. },
  480. {
  481. text: 'energy',
  482. submenu: [
  483. {text: 'watt', value: 'watt'},
  484. {text: 'joule', value: 'joule'},
  485. {text: 'eV', value: 'ev'},
  486. ]
  487. },
  488. ];
  489. };
  490. return kbn;
  491. });