shared.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. function get_object_fields(obj) {
  2. var field_array = [];
  3. obj = flatten_json(obj._source)
  4. for (field in obj) {
  5. field_array.push(field);
  6. }
  7. return field_array.sort();
  8. }
  9. function get_all_fields(json) {
  10. var field_array = [];
  11. var obj_fields;
  12. for (hit in json.hits.hits) {
  13. obj_fields = get_object_fields(json.hits.hits[hit]);
  14. for (index in obj_fields) {
  15. if (_.indexOf(field_array,obj_fields[index]) < 0) {
  16. field_array.push(obj_fields[index]);
  17. }
  18. }
  19. }
  20. return field_array.sort();
  21. }
  22. function has_field(obj,field) {
  23. var obj_fields = get_object_fields(obj);
  24. if (_.inArray(obj_fields,field) < 0) {
  25. return false;
  26. } else {
  27. return true;
  28. }
  29. }
  30. // Retuns a sorted array with duplicates removed
  31. function array_unique(arr) {
  32. var sorted_arr = arr.sort();
  33. var results = [];
  34. for (var i = 0; i <= arr.length - 1; i++) {
  35. if (sorted_arr[i + 1] != sorted_arr[i]) {
  36. results.push(sorted_arr[i]);
  37. }
  38. }
  39. return results
  40. }
  41. function get_objids_with_field(json,field) {
  42. var objid_array = [];
  43. for (hit in json.hits.hits) {
  44. if(has_field(json.hits.hits[hit],field)) {
  45. objid_array.push(hit);
  46. }
  47. }
  48. return objid_array;
  49. }
  50. function get_objids_with_field_value(json,field,value) {
  51. var objid_array = [];
  52. for (hit in json.hits.hits) {
  53. var hit_obj = json.hits.hits[hit];
  54. if(has_field(hit_obj,field)) {
  55. var field_val = get_field_value(hit_obj,field,'raw')
  56. if(_.isArray(field_val)) {
  57. if(_.inArray(field_val,field) >= 0) {
  58. objid_array.push(hit);
  59. }
  60. } else {
  61. if(field_val == value) {
  62. objid_array.push(hit);
  63. }
  64. }
  65. } else {
  66. if ( value == '')
  67. objid_array.push(hit);
  68. }
  69. }
  70. return objid_array;
  71. }
  72. function get_related_fields(json,field) {
  73. var field_array = []
  74. for (hit in json.hits.hits) {
  75. var obj_fields = get_object_fields(json.hits.hits[hit])
  76. if (_.inArray(obj_fields,field) >= 0) {
  77. field_array.push.apply(field_array,obj_fields);
  78. }
  79. }
  80. var counts = count_values_in_array(field_array);
  81. return counts;
  82. }
  83. function recurse_field_dots(object,field) {
  84. var value = null;
  85. if (typeof object[field] != 'undefined')
  86. value = object[field];
  87. else if (nested = field.match(/(.*?)\.(.*)/))
  88. if(typeof object[nested[1]] != 'undefined')
  89. value = (typeof object[nested[1]][nested[2]] != 'undefined') ?
  90. object[nested[1]][nested[2]] : recurse_field_dots(
  91. object[nested[1]],nested[2]);
  92. return value;
  93. }
  94. function get_field_value(object,field,opt) {
  95. var value = recurse_field_dots(object['_source'],field);
  96. if(value === null)
  97. return ''
  98. if(_.isArray(value))
  99. if (opt == 'raw') {
  100. return value;
  101. }
  102. else {
  103. var complex = false;
  104. _.each(value, function(el, index) {
  105. if (typeof(el) == 'object') {
  106. complex = true;
  107. }
  108. })
  109. if (complex) {
  110. return JSON.stringify(value, null, 4);
  111. }
  112. return value.toString();
  113. }
  114. if(typeof value === 'object' && value != null)
  115. // Leaving this out for now
  116. //return opt == 'raw' ? value : JSON.stringify(value,null,4)
  117. return JSON.stringify(value,null,4)
  118. return (value != null) ? value.toString() : '';
  119. }
  120. // Returns a big flat array of all values for a field
  121. function get_all_values_for_field(json,field) {
  122. var field_array = [];
  123. for (hit in json.hits.hits) {
  124. var value = get_field_value(json.hits.hits[hit],field,'raw')
  125. if(typeof value === 'object' && value != null) {
  126. field_array.push.apply(field_array,value);
  127. } else {
  128. field_array.push(value);
  129. }
  130. }
  131. return field_array;
  132. }
  133. // Takes a flat array of values and returns an array of arrays
  134. // reverse sorted with counts
  135. function count_values_in_array(array) {
  136. var count = {};
  137. _.each(array, function(){
  138. var num = this; // Get number
  139. count[num] = count[num]+1 || 1; // Increment counter for each value
  140. });
  141. var tuples = [];
  142. for (var key in count) tuples.push([key, count[key]]);
  143. tuples.sort(function(a, b) {
  144. a = a[1];
  145. b = b[1];
  146. return a < b ? -1 : (a > b ? 1 : 0);
  147. });
  148. tuples.reverse();
  149. var count_array = [];
  150. for (var i = 0; i < tuples.length; i++) {
  151. var key = tuples[i][0];
  152. var value = tuples[i][1];
  153. count_array.push([key,value])
  154. }
  155. return count_array;
  156. }
  157. function top_field_values(json,field,count) {
  158. var result = count_values_in_array(get_all_values_for_field(json,field));
  159. return result.slice(0,count)
  160. }
  161. /**
  162. * Calculate a graph interval
  163. *
  164. * from:: Date object containing the start time
  165. * to:: Date object containing the finish time
  166. * size:: Calculate to approximately this many bars
  167. * user_interval:: User specified histogram interval
  168. *
  169. */
  170. function calculate_interval(from,to,size,user_interval) {
  171. if(_.isObject(from))
  172. from = from.getTime();
  173. if(_.isObject(to))
  174. to = to.getTime();
  175. return user_interval == 0 ? round_interval((to - from)/size) : user_interval;
  176. }
  177. function get_bar_count(from,to,interval) {
  178. return (to - from)/interval;
  179. }
  180. function round_interval (interval) {
  181. switch (true) {
  182. case (interval <= 500): return 100;
  183. case (interval <= 5000): return 1000;
  184. case (interval <= 7500): return 5000;
  185. case (interval <= 15000): return 10000;
  186. case (interval <= 45000): return 30000;
  187. case (interval <= 180000): return 60000;
  188. case (interval <= 450000): return 300000;
  189. case (interval <= 1200000): return 600000;
  190. case (interval <= 2700000): return 1800000;
  191. case (interval <= 7200000): return 3600000;
  192. case (interval <= 21600000): return 10800000;
  193. default: return 43200000;
  194. }
  195. }
  196. function secondsToHms(seconds){
  197. var numyears = Math.floor(seconds / 31536000);
  198. if(numyears){
  199. return numyears + 'y';
  200. }
  201. var numdays = Math.floor((seconds % 31536000) / 86400);
  202. if(numdays){
  203. return numdays + 'd';
  204. }
  205. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  206. if(numhours){
  207. return numhours + 'h';
  208. }
  209. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  210. if(numminutes){
  211. return numminutes + 'm';
  212. }
  213. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  214. if(numseconds){
  215. return numseconds + 's';
  216. }
  217. return 'less then a second'; //'just now' //or other string you like;
  218. }
  219. function to_percent(number,outof) {
  220. return Math.round((number/outof)*10000)/100 + "%";
  221. }
  222. function addslashes(str) {
  223. str = str.replace(/\\/g, '\\\\');
  224. str = str.replace(/\'/g, '\\\'');
  225. str = str.replace(/\"/g, '\\"');
  226. str = str.replace(/\0/g, '\\0');
  227. return str;
  228. }
  229. // Create an ISO8601 compliant timestamp for ES
  230. //function ISODateString(unixtime) {
  231. //var d = new Date(parseInt(unixtime));
  232. function ISODateString(d) {
  233. if(is_int(d)) {
  234. d = new Date(parseInt(d));
  235. }
  236. function pad(n) {
  237. return n < 10 ? '0' + n : n
  238. }
  239. return d.getFullYear() + '-' +
  240. pad(d.getMonth() + 1) + '-' +
  241. pad(d.getDate()) + 'T' +
  242. pad(d.getHours()) + ':' +
  243. pad(d.getMinutes()) + ':' +
  244. pad(d.getSeconds());
  245. }
  246. function pickDateString(d) {
  247. return dateFormat(d,'yyyy-mm-dd HH:MM:ss')
  248. }
  249. function prettyDateString(d) {
  250. d = new Date(parseInt(d));
  251. d = utc_date_obj(d);
  252. return dateFormat(d,window.time_format);
  253. }
  254. function utc_date_obj(d) {
  255. return new Date(
  256. d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(),
  257. d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(),
  258. d.getUTCMilliseconds());
  259. }
  260. function local_date_obj(d) {
  261. return new Date(Date.UTC(
  262. d.getFullYear(), d.getMonth(), d.getDate(),
  263. d.getHours(), d.getMinutes(), d.getSeconds()));
  264. }
  265. function is_int(value) {
  266. if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
  267. return true;
  268. } else {
  269. return false;
  270. }
  271. }
  272. function interval_to_seconds(string) {
  273. var matches = string.match(/(\d+)([Mwdhms])/);
  274. switch (matches[2]) {
  275. case 'M': return matches[1]*2592000;;
  276. case 'w': return matches[1]*604800;;
  277. case 'd': return matches[1]*86400;;
  278. case 'h': return matches[1]*3600;;
  279. case 'm': return matches[1]*60;;
  280. case 's': return matches[1];
  281. }
  282. }
  283. function time_ago(string) {
  284. return new Date(new Date().getTime() - (interval_to_seconds(string)*1000))
  285. }
  286. function flatten_json(object,root,array) {
  287. if (typeof array === 'undefined')
  288. var array = {};
  289. if (typeof root === 'undefined')
  290. var root = '';
  291. for(var index in object) {
  292. var obj = object[index]
  293. var rootname = root.length == 0 ? index : root + '.' + index;
  294. if(typeof obj == 'object' ) {
  295. if(_.isArray(obj))
  296. array[rootname] = typeof obj === 'undefined' ? null : obj.join(',');
  297. else
  298. flatten_json(obj,rootname,array)
  299. } else {
  300. array[rootname] = typeof obj === 'undefined' ? null : obj;
  301. }
  302. }
  303. return sortObj(array);
  304. }
  305. function xmlEnt(value) {
  306. if(_.isString(value)) {
  307. var stg1 = value.replace(/</g, '&lt;')
  308. .replace(/>/g, '&gt;')
  309. .replace(/\r\n/g, '<br/>')
  310. .replace(/\r/g, '<br/>')
  311. .replace(/\n/g, '<br/>')
  312. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  313. .replace(/ /g, '&nbsp;&nbsp;')
  314. .replace(/&lt;del&gt;/g, '<del>')
  315. .replace(/&lt;\/del&gt;/g, '</del>');
  316. return stg1
  317. } else {
  318. return value
  319. }
  320. }
  321. function sortObj(arr) {
  322. // Setup Arrays
  323. var sortedKeys = new Array();
  324. var sortedObj = {};
  325. // Separate keys and sort them
  326. for (var i in arr) {
  327. sortedKeys.push(i);
  328. }
  329. sortedKeys.sort();
  330. // Reconstruct sorted obj based on keys
  331. for (var i in sortedKeys) {
  332. sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
  333. }
  334. return sortedObj;
  335. }
  336. // WTF. Has to be a better way to do this. Hi Tyler.
  337. function int_to_tz(offset) {
  338. var hour = offset / 1000 / 3600
  339. var str = ""
  340. if (hour == 0) {
  341. str = "+0000"
  342. }
  343. if (hour < 0) {
  344. if (hour > -10)
  345. str = "-0" + (hour * -100)
  346. else
  347. str = "-" + (hour * -100)
  348. }
  349. if (hour > 0) {
  350. if (hour < 10)
  351. str = "+0" + (hour * 100)
  352. else
  353. str = "+" + (hour * 100)
  354. }
  355. str = str.substring(0,3) + ":" + str.substring(3);
  356. return str
  357. }
  358. // Sets #hash, thus refreshing results
  359. function setHash(json) {
  360. window.location.hash = encodeURIComponent(Base64.encode(JSON.stringify(json)));
  361. }
  362. // Add commas to numbers
  363. function addCommas(nStr) {
  364. nStr += '';
  365. var x = nStr.split('.');
  366. var x1 = x[0];
  367. var x2 = x.length > 1 ? '.' + x[1] : '';
  368. var rgx = /(\d+)(\d{3})/;
  369. while (rgx.test(x1)) {
  370. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  371. }
  372. return x1 + x2;
  373. }
  374. // Split up log spaceless strings
  375. // Str = string to split
  376. // num = number of letters between <wbr> tags
  377. function wbr(str, num) {
  378. str = htmlEntities(str);
  379. return str.replace(
  380. RegExp("(@?\\w{" + num + "}|[:;,])([\\w\"'])([\\w@]*)", "g"),
  381. function (all, text, char, trailer) {
  382. if (/@KIBANA_\w+_(START|END)@/.test(all)) {
  383. return text + char + trailer;
  384. } else {
  385. return text + "<del>&#8203;</del>" + char + trailer;
  386. }
  387. }
  388. );
  389. }
  390. function htmlEntities(str) {
  391. return String(str).replace(
  392. /&/g, '&amp;').replace(
  393. /</g, '&lt;').replace(
  394. />/g, '&gt;').replace(
  395. /"/g, '&quot;');
  396. }
  397. _.mixin({
  398. move: function (array, fromIndex, toIndex) {
  399. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
  400. return array;
  401. }
  402. });
  403. _.mixin({
  404. remove: function (array, index) {
  405. array.splice(index, 1);
  406. return array;
  407. }
  408. });