shared.js 8.5 KB

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