shared.js 11 KB

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