shared.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // Probably useless now
  95. function get_field_value(object,field,opt) {
  96. var value = recurse_field_dots(object['_source'],field);
  97. if(value === null)
  98. return ''
  99. if(_.isArray(value))
  100. if (opt == 'raw') {
  101. return value;
  102. }
  103. else {
  104. var complex = false;
  105. _.each(value, function(el, index) {
  106. if (typeof(el) == 'object') {
  107. complex = true;
  108. }
  109. })
  110. if (complex) {
  111. return JSON.stringify(value, null, 4);
  112. }
  113. return value.toString();
  114. }
  115. if(typeof value === 'object' && value != null)
  116. // Leaving this out for now
  117. //return opt == 'raw' ? value : JSON.stringify(value,null,4)
  118. return JSON.stringify(value,null,4)
  119. return (value != null) ? value.toString() : '';
  120. }
  121. // Returns a big flat array of all values for a field
  122. function get_all_values_for_field(docs,field) {
  123. var field_array = [];
  124. _.each(docs, function(doc,k) {
  125. var value = doc[field]
  126. if(typeof value === 'object' && value != null) {
  127. field_array.push.apply(field_array,value);
  128. } else {
  129. field_array.push(value);
  130. }
  131. })
  132. return field_array;
  133. }
  134. function top_field_values(docs,field,count) {
  135. var counts = _.countBy(get_all_values_for_field(docs,field),function(field){return field;});
  136. return _.pairs(counts).sort(function(a, b) {return a[1] - b[1]}).reverse().slice(0,count)
  137. }
  138. /**
  139. * Calculate a graph interval
  140. *
  141. * from:: Date object containing the start time
  142. * to:: Date object containing the finish time
  143. * size:: Calculate to approximately this many bars
  144. * user_interval:: User specified histogram interval
  145. *
  146. */
  147. function calculate_interval(from,to,size,user_interval) {
  148. if(_.isObject(from))
  149. from = from.getTime();
  150. if(_.isObject(to))
  151. to = to.getTime();
  152. return user_interval == 0 ? round_interval((to - from)/size) : user_interval;
  153. }
  154. function get_bar_count(from,to,interval) {
  155. return (to - from)/interval;
  156. }
  157. function round_interval (interval) {
  158. switch (true) {
  159. case (interval <= 500): return 100;
  160. case (interval <= 5000): return 1000;
  161. case (interval <= 7500): return 5000;
  162. case (interval <= 15000): return 10000;
  163. case (interval <= 45000): return 30000;
  164. case (interval <= 180000): return 60000;
  165. case (interval <= 450000): return 300000;
  166. case (interval <= 1200000): return 600000;
  167. case (interval <= 2700000): return 1800000;
  168. case (interval <= 7200000): return 3600000;
  169. case (interval <= 21600000): return 10800000;
  170. default: return 43200000;
  171. }
  172. }
  173. function secondsToHms(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. function to_percent(number,outof) {
  197. return Math.round((number/outof)*10000)/100 + "%";
  198. }
  199. function addslashes(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. // Create an ISO8601 compliant timestamp for ES
  207. //function ISODateString(unixtime) {
  208. //var d = new Date(parseInt(unixtime));
  209. function ISODateString(d) {
  210. if(is_int(d)) {
  211. d = new Date(parseInt(d));
  212. }
  213. function pad(n) {
  214. return n < 10 ? '0' + n : n
  215. }
  216. return d.getFullYear() + '-' +
  217. pad(d.getMonth() + 1) + '-' +
  218. pad(d.getDate()) + 'T' +
  219. pad(d.getHours()) + ':' +
  220. pad(d.getMinutes()) + ':' +
  221. pad(d.getSeconds());
  222. }
  223. function pickDateString(d) {
  224. return dateFormat(d,'yyyy-mm-dd HH:MM:ss')
  225. }
  226. function prettyDateString(d) {
  227. d = new Date(parseInt(d));
  228. d = utc_date_obj(d);
  229. return dateFormat(d,window.time_format);
  230. }
  231. function utc_date_obj(d) {
  232. return new Date(
  233. d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(),
  234. d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(),
  235. d.getUTCMilliseconds());
  236. }
  237. function local_date_obj(d) {
  238. return new Date(Date.UTC(
  239. d.getFullYear(), d.getMonth(), d.getDate(),
  240. d.getHours(), d.getMinutes(), d.getSeconds()));
  241. }
  242. function is_int(value) {
  243. if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
  244. return true;
  245. } else {
  246. return false;
  247. }
  248. }
  249. function interval_to_seconds(string) {
  250. var matches = string.match(/(\d+)([Mwdhms])/);
  251. switch (matches[2]) {
  252. case 'M': return matches[1]*2592000;;
  253. case 'w': return matches[1]*604800;;
  254. case 'd': return matches[1]*86400;;
  255. case 'h': return matches[1]*3600;;
  256. case 'm': return matches[1]*60;;
  257. case 's': return matches[1];
  258. }
  259. }
  260. function time_ago(string) {
  261. return new Date(new Date().getTime() - (interval_to_seconds(string)*1000))
  262. }
  263. function flatten_json(object,root,array) {
  264. if (typeof array === 'undefined')
  265. var array = {};
  266. if (typeof root === 'undefined')
  267. var root = '';
  268. for(var index in object) {
  269. var obj = object[index]
  270. var rootname = root.length == 0 ? index : root + '.' + index;
  271. if(typeof obj == 'object' ) {
  272. if(_.isArray(obj))
  273. array[rootname] = typeof obj === 'undefined' ? null : obj.join(',');
  274. else
  275. flatten_json(obj,rootname,array)
  276. } else {
  277. array[rootname] = typeof obj === 'undefined' ? null : obj;
  278. }
  279. }
  280. return sortObj(array);
  281. }
  282. function xmlEnt(value) {
  283. if(_.isString(value)) {
  284. var stg1 = value.replace(/</g, '&lt;')
  285. .replace(/>/g, '&gt;')
  286. .replace(/\r\n/g, '<br/>')
  287. .replace(/\r/g, '<br/>')
  288. .replace(/\n/g, '<br/>')
  289. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  290. .replace(/ /g, '&nbsp;&nbsp;')
  291. .replace(/&lt;del&gt;/g, '<del>')
  292. .replace(/&lt;\/del&gt;/g, '</del>');
  293. return stg1
  294. } else {
  295. return value
  296. }
  297. }
  298. function sortObj(arr) {
  299. // Setup Arrays
  300. var sortedKeys = new Array();
  301. var sortedObj = {};
  302. // Separate keys and sort them
  303. for (var i in arr) {
  304. sortedKeys.push(i);
  305. }
  306. sortedKeys.sort();
  307. // Reconstruct sorted obj based on keys
  308. for (var i in sortedKeys) {
  309. sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
  310. }
  311. return sortedObj;
  312. }
  313. // WTF. Has to be a better way to do this. Hi Tyler.
  314. function int_to_tz(offset) {
  315. var hour = offset / 1000 / 3600
  316. var str = ""
  317. if (hour == 0) {
  318. str = "+0000"
  319. }
  320. if (hour < 0) {
  321. if (hour > -10)
  322. str = "-0" + (hour * -100)
  323. else
  324. str = "-" + (hour * -100)
  325. }
  326. if (hour > 0) {
  327. if (hour < 10)
  328. str = "+0" + (hour * 100)
  329. else
  330. str = "+" + (hour * 100)
  331. }
  332. str = str.substring(0,3) + ":" + str.substring(3);
  333. return str
  334. }
  335. // Sets #hash, thus refreshing results
  336. function setHash(json) {
  337. window.location.hash = encodeURIComponent(Base64.encode(JSON.stringify(json)));
  338. }
  339. // Add commas to numbers
  340. function addCommas(nStr) {
  341. nStr += '';
  342. var x = nStr.split('.');
  343. var x1 = x[0];
  344. var x2 = x.length > 1 ? '.' + x[1] : '';
  345. var rgx = /(\d+)(\d{3})/;
  346. while (rgx.test(x1)) {
  347. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  348. }
  349. return x1 + x2;
  350. }
  351. // Split up log spaceless strings
  352. // Str = string to split
  353. // num = number of letters between <wbr> tags
  354. function wbr(str, num) {
  355. str = htmlEntities(str);
  356. return str.replace(
  357. RegExp("(@?\\w{" + num + "}|[:;,])([\\w\"'])([\\w@]*)", "g"),
  358. function (all, text, char, trailer) {
  359. if (/@KIBANA_\w+_(START|END)@/.test(all)) {
  360. return text + char + trailer;
  361. } else {
  362. return text + "<del>&#8203;</del>" + char + trailer;
  363. }
  364. }
  365. );
  366. }
  367. function htmlEntities(str) {
  368. return String(str).replace(
  369. /&/g, '&amp;').replace(
  370. /</g, '&lt;').replace(
  371. />/g, '&gt;').replace(
  372. /"/g, '&quot;');
  373. }
  374. _.mixin({
  375. move: function (array, fromIndex, toIndex) {
  376. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
  377. return array;
  378. }
  379. });
  380. _.mixin({
  381. remove: function (array, index) {
  382. array.splice(index, 1);
  383. return array;
  384. }
  385. });