shared.js 9.7 KB

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