kbn.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. define(['jquery', 'underscore'],
  2. function($, _) {
  3. 'use strict';
  4. var kbn = {};
  5. kbn.get_object_fields = function(obj) {
  6. var field_array = [];
  7. obj = kbn.flatten_json(obj._source);
  8. for (var field in obj) {
  9. field_array.push(field);
  10. }
  11. return field_array.sort();
  12. };
  13. kbn.get_all_fields = function(data) {
  14. var fields = [];
  15. _.each(data,function(hit) {
  16. fields = _.uniq(fields.concat(_.keys(hit)));
  17. });
  18. // Remove stupid angular key
  19. fields = _.without(fields,'$$hashKey');
  20. return fields;
  21. };
  22. kbn.has_field = function(obj,field) {
  23. var obj_fields = kbn.get_object_fields(obj);
  24. if (_.inArray(obj_fields,field) < 0) {
  25. return false;
  26. } else {
  27. return true;
  28. }
  29. };
  30. kbn.get_related_fields = function(docs,field) {
  31. var field_array = [];
  32. _.each(docs, function(doc) {
  33. var keys = _.keys(doc);
  34. if(_.contains(keys,field)) {
  35. field_array = field_array.concat(keys);
  36. }
  37. });
  38. var counts = _.countBy(_.without(field_array,field),function(field){return field;});
  39. return counts;
  40. };
  41. kbn.recurse_field_dots = function(object,field) {
  42. var value = null;
  43. var nested;
  44. if (typeof object[field] !== 'undefined') {
  45. value = object[field];
  46. }
  47. else if (nested = field.match(/(.*?)\.(.*)/)) {
  48. if(typeof object[nested[1]] !== 'undefined') {
  49. value = (typeof object[nested[1]][nested[2]] !== 'undefined') ?
  50. object[nested[1]][nested[2]] : kbn.recurse_field_dots(
  51. object[nested[1]],nested[2]);
  52. }
  53. }
  54. return value;
  55. };
  56. kbn.top_field_values = function(docs,field,count,grouped) {
  57. var all_values = _.pluck(docs,field),
  58. groups = {},
  59. counts,
  60. hasArrays;
  61. // manually grouping into pairs allows us to keep the original value,
  62. _.each(all_values, function (value) {
  63. var k;
  64. if(_.isArray(value)) {
  65. hasArrays = true;
  66. }
  67. if(_.isArray(value) && !grouped) {
  68. k = value;
  69. } else {
  70. k = _.isUndefined(value) ? '' : [value.toString()];
  71. }
  72. _.each(k, function(key) {
  73. if (_.has(groups, key)) {
  74. groups[key][1] ++;
  75. } else {
  76. groups[key] = [(grouped ? value : key), 1];
  77. }
  78. });
  79. });
  80. counts = _.values(groups).sort(function(a, b) {
  81. return a[1] - b[1];
  82. }).reverse().slice(0,count);
  83. return {
  84. counts: counts,
  85. hasArrays : hasArrays
  86. };
  87. };
  88. /**
  89. * Calculate a graph interval
  90. *
  91. * from:: Date object containing the start time
  92. * to:: Date object containing the finish time
  93. * size:: Calculate to approximately this many bars
  94. * user_interval:: User specified histogram interval
  95. *
  96. */
  97. kbn.calculate_interval = function(from,to,size,user_interval) {
  98. if(_.isObject(from)) {
  99. from = from.valueOf();
  100. }
  101. if(_.isObject(to)) {
  102. to = to.valueOf();
  103. }
  104. return user_interval === 0 ? kbn.round_interval((to - from)/size) : user_interval;
  105. };
  106. kbn.round_interval = function(interval) {
  107. switch (true) {
  108. // 0.5s
  109. case (interval <= 500):
  110. return 100; // 0.1s
  111. // 5s
  112. case (interval <= 5000):
  113. return 1000; // 1s
  114. // 7.5s
  115. case (interval <= 7500):
  116. return 5000; // 5s
  117. // 15s
  118. case (interval <= 15000):
  119. return 10000; // 10s
  120. // 45s
  121. case (interval <= 45000):
  122. return 30000; // 30s
  123. // 3m
  124. case (interval <= 180000):
  125. return 60000; // 1m
  126. // 9m
  127. case (interval <= 450000):
  128. return 300000; // 5m
  129. // 20m
  130. case (interval <= 1200000):
  131. return 600000; // 10m
  132. // 45m
  133. case (interval <= 2700000):
  134. return 1800000; // 30m
  135. // 2h
  136. case (interval <= 7200000):
  137. return 3600000; // 1h
  138. // 6h
  139. case (interval <= 21600000):
  140. return 10800000; // 3h
  141. // 24h
  142. case (interval <= 86400000):
  143. return 43200000; // 12h
  144. // 48h
  145. case (interval <= 172800000):
  146. return 86400000; // 24h
  147. // 1w
  148. case (interval <= 604800000):
  149. return 86400000; // 24h
  150. // 3w
  151. case (interval <= 1814400000):
  152. return 604800000; // 1w
  153. // 2y
  154. case (interval < 3628800000):
  155. return 2592000000; // 30d
  156. default:
  157. return 31536000000; // 1y
  158. }
  159. };
  160. kbn.secondsToHms = function(seconds){
  161. var numyears = Math.floor(seconds / 31536000);
  162. if(numyears){
  163. return numyears + 'y';
  164. }
  165. var numdays = Math.floor((seconds % 31536000) / 86400);
  166. if(numdays){
  167. return numdays + 'd';
  168. }
  169. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  170. if(numhours){
  171. return numhours + 'h';
  172. }
  173. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  174. if(numminutes){
  175. return numminutes + 'm';
  176. }
  177. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  178. if(numseconds){
  179. return numseconds + 's';
  180. }
  181. return 'less then a second'; //'just now' //or other string you like;
  182. };
  183. kbn.to_percent = function(number,outof) {
  184. return Math.floor((number/outof)*10000)/100 + "%";
  185. };
  186. kbn.addslashes = function(str) {
  187. str = str.replace(/\\/g, '\\\\');
  188. str = str.replace(/\'/g, '\\\'');
  189. str = str.replace(/\"/g, '\\"');
  190. str = str.replace(/\0/g, '\\0');
  191. return str;
  192. };
  193. kbn.interval_regex = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
  194. // histogram & trends
  195. kbn.intervals_in_seconds = {
  196. y: 31536000,
  197. M: 2592000,
  198. w: 604800,
  199. d: 86400,
  200. h: 3600,
  201. m: 60,
  202. s: 1
  203. };
  204. kbn.describe_interval = function (string) {
  205. var matches = string.match(kbn.interval_regex);
  206. if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
  207. throw new Error('Invalid interval string, expexcting a number followed by one of "Mwdhmsy"');
  208. } else {
  209. return {
  210. sec: kbn.intervals_in_seconds[matches[2]],
  211. type: matches[2],
  212. count: parseInt(matches[1], 10)
  213. };
  214. }
  215. };
  216. kbn.interval_to_ms = function(string) {
  217. var info = kbn.describe_interval(string);
  218. return info.sec * 1000 * info.count;
  219. };
  220. kbn.interval_to_seconds = function (string) {
  221. var info = kbn.describe_interval(string);
  222. return info.sec * info.count;
  223. };
  224. // This should go away, moment.js can do this
  225. kbn.time_ago = function(string) {
  226. return new Date(new Date().getTime() - (kbn.interval_to_ms(string)));
  227. };
  228. // LOL. hahahahaha. DIE.
  229. kbn.flatten_json = function(object,root,array) {
  230. if (typeof array === 'undefined') {
  231. array = {};
  232. }
  233. if (typeof root === 'undefined') {
  234. root = '';
  235. }
  236. for(var index in object) {
  237. var obj = object[index];
  238. var rootname = root.length === 0 ? index : root + '.' + index;
  239. if(typeof obj === 'object' ) {
  240. if(_.isArray(obj)) {
  241. if(obj.length > 0 && typeof obj[0] === 'object') {
  242. var strval = '';
  243. for (var objidx = 0, objlen = obj.length; objidx < objlen; objidx++) {
  244. if (objidx > 0) {
  245. strval = strval + ', ';
  246. }
  247. strval = strval + JSON.stringify(obj[objidx]);
  248. }
  249. array[rootname] = strval;
  250. } else if(obj.length === 1 && _.isNumber(obj[0])) {
  251. array[rootname] = parseFloat(obj[0]);
  252. } else {
  253. array[rootname] = typeof obj === 'undefined' ? null : obj;
  254. }
  255. } else {
  256. kbn.flatten_json(obj,rootname,array);
  257. }
  258. } else {
  259. array[rootname] = typeof obj === 'undefined' ? null : obj;
  260. }
  261. }
  262. return kbn.sortObj(array);
  263. };
  264. kbn.xmlEnt = function(value) {
  265. if(_.isString(value)) {
  266. var stg1 = value.replace(/</g, '&lt;')
  267. .replace(/>/g, '&gt;')
  268. .replace(/\r\n/g, '<br/>')
  269. .replace(/\r/g, '<br/>')
  270. .replace(/\n/g, '<br/>')
  271. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  272. .replace(/ /g, '&nbsp;&nbsp;')
  273. .replace(/&lt;del&gt;/g, '<del>')
  274. .replace(/&lt;\/del&gt;/g, '</del>');
  275. return stg1;
  276. } else {
  277. return value;
  278. }
  279. };
  280. kbn.sortObj = function(arr) {
  281. // Setup Arrays
  282. var sortedKeys = [];
  283. var sortedObj = {};
  284. var i;
  285. // Separate keys and sort them
  286. for (i in arr) {
  287. sortedKeys.push(i);
  288. }
  289. sortedKeys.sort();
  290. // Reconstruct sorted obj based on keys
  291. for (i in sortedKeys) {
  292. sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
  293. }
  294. return sortedObj;
  295. };
  296. kbn.query_color_dot = function (color, diameter) {
  297. return '<div class="icon-circle" style="' + [
  298. 'display:inline-block',
  299. 'color:' + color,
  300. 'font-size:' + diameter + 'px',
  301. ].join(';') + '"></div>';
  302. };
  303. return kbn;
  304. });