shared.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. case (interval <= 500): return 100;
  149. case (interval <= 5000): return 1000;
  150. case (interval <= 7500): return 5000;
  151. case (interval <= 15000): return 10000;
  152. case (interval <= 45000): return 30000;
  153. case (interval <= 180000): return 60000;
  154. case (interval <= 450000): return 300000;
  155. case (interval <= 1200000): return 600000;
  156. case (interval <= 2700000): return 1800000;
  157. case (interval <= 7200000): return 3600000;
  158. case (interval <= 21600000): return 10800000;
  159. default: return 43200000;
  160. }
  161. }
  162. function secondsToHms(seconds){
  163. var numyears = Math.floor(seconds / 31536000);
  164. if(numyears){
  165. return numyears + 'y';
  166. }
  167. var numdays = Math.floor((seconds % 31536000) / 86400);
  168. if(numdays){
  169. return numdays + 'd';
  170. }
  171. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  172. if(numhours){
  173. return numhours + 'h';
  174. }
  175. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  176. if(numminutes){
  177. return numminutes + 'm';
  178. }
  179. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  180. if(numseconds){
  181. return numseconds + 's';
  182. }
  183. return 'less then a second'; //'just now' //or other string you like;
  184. }
  185. function to_percent(number,outof) {
  186. return Math.round((number/outof)*10000)/100 + "%";
  187. }
  188. function addslashes(str) {
  189. str = str.replace(/\\/g, '\\\\');
  190. str = str.replace(/\'/g, '\\\'');
  191. str = str.replace(/\"/g, '\\"');
  192. str = str.replace(/\0/g, '\\0');
  193. return str;
  194. }
  195. // Create an ISO8601 compliant timestamp for ES
  196. //function ISODateString(unixtime) {
  197. //var d = new Date(parseInt(unixtime));
  198. function ISODateString(d) {
  199. if(is_int(d)) {
  200. d = new Date(parseInt(d));
  201. }
  202. function pad(n) {
  203. return n < 10 ? '0' + n : n
  204. }
  205. return d.getFullYear() + '-' +
  206. pad(d.getMonth() + 1) + '-' +
  207. pad(d.getDate()) + 'T' +
  208. pad(d.getHours()) + ':' +
  209. pad(d.getMinutes()) + ':' +
  210. pad(d.getSeconds());
  211. }
  212. function pickDateString(d) {
  213. return dateFormat(d,'yyyy-mm-dd HH:MM:ss')
  214. }
  215. function prettyDateString(d) {
  216. d = new Date(parseInt(d));
  217. d = utc_date_obj(d);
  218. return dateFormat(d,window.time_format);
  219. }
  220. function utc_date_obj(d) {
  221. return new Date(
  222. d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(),
  223. d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(),
  224. d.getUTCMilliseconds());
  225. }
  226. function local_date_obj(d) {
  227. return new Date(Date.UTC(
  228. d.getFullYear(), d.getMonth(), d.getDate(),
  229. d.getHours(), d.getMinutes(), d.getSeconds()));
  230. }
  231. function is_int(value) {
  232. if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
  233. return true;
  234. } else {
  235. return false;
  236. }
  237. }
  238. function interval_to_seconds(string) {
  239. var matches = string.match(/(\d+)([Mwdhms])/);
  240. switch (matches[2]) {
  241. case 'M': return matches[1]*2592000;;
  242. case 'w': return matches[1]*604800;;
  243. case 'd': return matches[1]*86400;;
  244. case 'h': return matches[1]*3600;;
  245. case 'm': return matches[1]*60;;
  246. case 's': return matches[1];
  247. }
  248. }
  249. function time_ago(string) {
  250. return new Date(new Date().getTime() - (interval_to_seconds(string)*1000))
  251. }
  252. function flatten_json(object,root,array) {
  253. if (typeof array === 'undefined')
  254. var array = {};
  255. if (typeof root === 'undefined')
  256. var root = '';
  257. for(var index in object) {
  258. var obj = object[index]
  259. var rootname = root.length == 0 ? index : root + '.' + index;
  260. if(typeof obj == 'object' ) {
  261. if(_.isArray(obj))
  262. array[rootname] = typeof obj === 'undefined' ? null : obj.join(',');
  263. else
  264. flatten_json(obj,rootname,array)
  265. } else {
  266. array[rootname] = typeof obj === 'undefined' ? null : obj;
  267. }
  268. }
  269. return sortObj(array);
  270. }
  271. function xmlEnt(value) {
  272. if(_.isString(value)) {
  273. var stg1 = value.replace(/</g, '&lt;')
  274. .replace(/>/g, '&gt;')
  275. .replace(/\r\n/g, '<br/>')
  276. .replace(/\r/g, '<br/>')
  277. .replace(/\n/g, '<br/>')
  278. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  279. .replace(/ /g, '&nbsp;&nbsp;')
  280. .replace(/&lt;del&gt;/g, '<del>')
  281. .replace(/&lt;\/del&gt;/g, '</del>');
  282. return stg1
  283. } else {
  284. return value
  285. }
  286. }
  287. function sortObj(arr) {
  288. // Setup Arrays
  289. var sortedKeys = new Array();
  290. var sortedObj = {};
  291. // Separate keys and sort them
  292. for (var i in arr) {
  293. sortedKeys.push(i);
  294. }
  295. sortedKeys.sort();
  296. // Reconstruct sorted obj based on keys
  297. for (var i in sortedKeys) {
  298. sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
  299. }
  300. return sortedObj;
  301. }
  302. // WTF. Has to be a better way to do this. Hi Tyler.
  303. function int_to_tz(offset) {
  304. var hour = offset / 1000 / 3600
  305. var str = ""
  306. if (hour == 0) {
  307. str = "+0000"
  308. }
  309. if (hour < 0) {
  310. if (hour > -10)
  311. str = "-0" + (hour * -100)
  312. else
  313. str = "-" + (hour * -100)
  314. }
  315. if (hour > 0) {
  316. if (hour < 10)
  317. str = "+0" + (hour * 100)
  318. else
  319. str = "+" + (hour * 100)
  320. }
  321. str = str.substring(0,3) + ":" + str.substring(3);
  322. return str
  323. }
  324. // Sets #hash, thus refreshing results
  325. function setHash(json) {
  326. window.location.hash = encodeURIComponent(Base64.encode(JSON.stringify(json)));
  327. }
  328. // Add commas to numbers
  329. function addCommas(nStr) {
  330. nStr += '';
  331. var x = nStr.split('.');
  332. var x1 = x[0];
  333. var x2 = x.length > 1 ? '.' + x[1] : '';
  334. var rgx = /(\d+)(\d{3})/;
  335. while (rgx.test(x1)) {
  336. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  337. }
  338. return x1 + x2;
  339. }
  340. // Split up log spaceless strings
  341. // Str = string to split
  342. // num = number of letters between <wbr> tags
  343. function wbr(str, num) {
  344. str = htmlEntities(str);
  345. return str.replace(
  346. RegExp("(@?\\w{" + num + "}|[:;,])([\\w\"'])([\\w@]*)", "g"),
  347. function (all, text, char, trailer) {
  348. if (/@KIBANA_\w+_(START|END)@/.test(all)) {
  349. return text + char + trailer;
  350. } else {
  351. return text + "<del>&#8203;</del>" + char + trailer;
  352. }
  353. }
  354. );
  355. }
  356. function htmlEntities(str) {
  357. return String(str).replace(
  358. /&/g, '&amp;').replace(
  359. /</g, '&lt;').replace(
  360. />/g, '&gt;').replace(
  361. /"/g, '&quot;');
  362. }
  363. _.mixin({
  364. move: function (array, fromIndex, toIndex) {
  365. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
  366. return array;
  367. }
  368. });
  369. _.mixin({
  370. remove: function (array, index) {
  371. array.splice(index, 1);
  372. return array;
  373. }
  374. });