shared.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. // Probably useless now, leaving for cases where you might not want
  64. // a flat dot notated data structure
  65. kbn.get_field_value = function(object,field,opt) {
  66. var value = kbn.recurse_field_dots(object._source,field);
  67. if(value === null) {
  68. return '';
  69. }
  70. if(_.isArray(value)) {
  71. if (opt === 'raw') {
  72. return value;
  73. } else {
  74. var complex = false;
  75. _.each(value, function(el, index) {
  76. if (typeof(el) === 'object') {
  77. complex = true;
  78. }
  79. });
  80. if (complex) {
  81. return JSON.stringify(value, null, 4);
  82. }
  83. return value.toString();
  84. }
  85. }
  86. if(typeof value === 'object' && value !== null) {
  87. // Leaving this out for now
  88. //return opt == 'raw' ? value : JSON.stringify(value,null,4)
  89. return JSON.stringify(value,null,4);
  90. }
  91. return (value !== null) ? value.toString() : '';
  92. };
  93. kbn.top_field_values = function(docs,field,count) {
  94. var counts = _.countBy(_.pluck(docs,field),function(field){
  95. return _.isUndefined(field) ? '' : field;
  96. });
  97. return _.pairs(counts).sort(function(a, b) {
  98. return a[1] - b[1];
  99. }).reverse().slice(0,count);
  100. };
  101. /**
  102. * Calculate a graph interval
  103. *
  104. * from:: Date object containing the start time
  105. * to:: Date object containing the finish time
  106. * size:: Calculate to approximately this many bars
  107. * user_interval:: User specified histogram interval
  108. *
  109. */
  110. kbn.calculate_interval = function(from,to,size,user_interval) {
  111. if(_.isObject(from)) {
  112. from = from.valueOf();
  113. }
  114. if(_.isObject(to)) {
  115. to = to.valueOf();
  116. }
  117. return user_interval === 0 ? kbn.round_interval((to - from)/size) : user_interval;
  118. };
  119. kbn.round_interval = function(interval) {
  120. switch (true) {
  121. // 0.5s
  122. case (interval <= 500):
  123. return 100; // 0.1s
  124. // 5s
  125. case (interval <= 5000):
  126. return 1000; // 1s
  127. // 7.5s
  128. case (interval <= 7500):
  129. return 5000; // 5s
  130. // 15s
  131. case (interval <= 15000):
  132. return 10000; // 10s
  133. // 45s
  134. case (interval <= 45000):
  135. return 30000; // 30s
  136. // 3m
  137. case (interval <= 180000):
  138. return 60000; // 1m
  139. // 9m
  140. case (interval <= 450000):
  141. return 300000; // 5m
  142. // 20m
  143. case (interval <= 1200000):
  144. return 600000; // 10m
  145. // 45m
  146. case (interval <= 2700000):
  147. return 1800000; // 30m
  148. // 2h
  149. case (interval <= 7200000):
  150. return 3600000; // 1h
  151. // 6h
  152. case (interval <= 21600000):
  153. return 10800000; // 3h
  154. // 24h
  155. case (interval <= 86400000):
  156. return 43200000; // 12h
  157. // 48h
  158. case (interval <= 172800000):
  159. return 86400000; // 24h
  160. // 1w
  161. case (interval <= 604800000):
  162. return 86400000; // 24h
  163. // 3w
  164. case (interval <= 1814400000):
  165. return 604800000; // 1w
  166. // 2y
  167. case (interval < 3628800000):
  168. return 2592000000; // 30d
  169. default:
  170. return 31536000000; // 1y
  171. }
  172. };
  173. kbn.secondsToHms = function(seconds){
  174. var numyears = Math.floor(seconds / 31536000);
  175. if(numyears){
  176. return numyears + 'y';
  177. }
  178. var numdays = Math.floor((seconds % 31536000) / 86400);
  179. if(numdays){
  180. return numdays + 'd';
  181. }
  182. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  183. if(numhours){
  184. return numhours + 'h';
  185. }
  186. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  187. if(numminutes){
  188. return numminutes + 'm';
  189. }
  190. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  191. if(numseconds){
  192. return numseconds + 's';
  193. }
  194. return 'less then a second'; //'just now' //or other string you like;
  195. };
  196. kbn.to_percent = function(number,outof) {
  197. return Math.round((number/outof)*10000)/100 + "%";
  198. };
  199. kbn.addslashes = function(str) {
  200. str = str.replace(/\\/g, '\\\\');
  201. str = str.replace(/\'/g, '\\\'');
  202. str = str.replace(/\"/g, '\\"');
  203. str = str.replace(/\0/g, '\\0');
  204. return str;
  205. };
  206. // histogram & trends
  207. kbn.interval_to_seconds = function(string) {
  208. var matches = string.match(/(\d+)([Mwdhmsy])/);
  209. switch (matches[2]) {
  210. case 'y':
  211. return matches[1]*31536000;
  212. case 'M':
  213. return matches[1]*2592000;
  214. case 'w':
  215. return matches[1]*604800;
  216. case 'd':
  217. return matches[1]*86400;
  218. case 'h':
  219. return matches[1]*3600;
  220. case 'm':
  221. return matches[1]*60;
  222. case 's':
  223. return matches[1];
  224. }
  225. };
  226. // This should go away, moment.js can do this
  227. kbn.time_ago = function(string) {
  228. return new Date(new Date().getTime() - (kbn.interval_to_seconds(string)*1000));
  229. };
  230. // LOL. hahahahaha. DIE.
  231. kbn.flatten_json = function(object,root,array) {
  232. if (typeof array === 'undefined') {
  233. array = {};
  234. }
  235. if (typeof root === 'undefined') {
  236. root = '';
  237. }
  238. for(var index in object) {
  239. var obj = object[index];
  240. var rootname = root.length === 0 ? index : root + '.' + index;
  241. if(typeof obj === 'object' ) {
  242. if(_.isArray(obj)) {
  243. if(obj.length > 0 && typeof obj[0] === 'object') {
  244. var strval = '';
  245. for (var objidx = 0, objlen = obj.length; objidx < objlen; objidx++) {
  246. if (objidx > 0) {
  247. strval = strval + ', ';
  248. }
  249. strval = strval + JSON.stringify(obj[objidx]);
  250. }
  251. array[rootname] = strval;
  252. } else if(obj.length === 1 && _.isNumber(obj[0])) {
  253. array[rootname] = parseFloat(obj[0]);
  254. } else {
  255. array[rootname] = typeof obj === 'undefined' ? null : obj;
  256. }
  257. } else {
  258. kbn.flatten_json(obj,rootname,array);
  259. }
  260. } else {
  261. array[rootname] = typeof obj === 'undefined' ? null : obj;
  262. }
  263. }
  264. return kbn.sortObj(array);
  265. };
  266. kbn.xmlEnt = function(value) {
  267. if(_.isString(value)) {
  268. var stg1 = value.replace(/</g, '&lt;')
  269. .replace(/>/g, '&gt;')
  270. .replace(/\r\n/g, '<br/>')
  271. .replace(/\r/g, '<br/>')
  272. .replace(/\n/g, '<br/>')
  273. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  274. .replace(/ /g, '&nbsp;&nbsp;')
  275. .replace(/&lt;del&gt;/g, '<del>')
  276. .replace(/&lt;\/del&gt;/g, '</del>');
  277. return stg1;
  278. } else {
  279. return value;
  280. }
  281. };
  282. kbn.sortObj = function(arr) {
  283. // Setup Arrays
  284. var sortedKeys = [];
  285. var sortedObj = {};
  286. var i;
  287. // Separate keys and sort them
  288. for (i in arr) {
  289. sortedKeys.push(i);
  290. }
  291. sortedKeys.sort();
  292. // Reconstruct sorted obj based on keys
  293. for (i in sortedKeys) {
  294. sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
  295. }
  296. return sortedObj;
  297. };
  298. }).call(this);
  299. /*
  300. UNDERSCORE.js Mixins
  301. */
  302. _.mixin({
  303. move: function (array, fromIndex, toIndex) {
  304. 'use strict';
  305. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
  306. return array;
  307. }
  308. });
  309. _.mixin({
  310. remove: function (array, index) {
  311. 'use strict';
  312. array.splice(index, 1);
  313. return array;
  314. }
  315. });
  316. _.mixin({
  317. toggleInOut: function(array,value) {
  318. 'use strict';
  319. if(_.contains(array,value)) {
  320. array = _.without(array,value);
  321. } else {
  322. array.push(value);
  323. }
  324. return array;
  325. }
  326. });