shared.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Wrap this all up in a 'kbn' object so I don't have a billion globals
  2. (function() {
  3. // Save a reference to this
  4. var self = this;
  5. // Save a reference to the old versionof this
  6. var wasKbn = self.kbn;
  7. // Create a safe refernce to the kbn object, for use below
  8. var kbn = function(obj) { return new wrapper(obj); };
  9. // Create a global object for accessing these functions
  10. self.kbn = kbn;
  11. kbn.get_object_fields = function(obj) {
  12. var field_array = [];
  13. obj = kbn.flatten_json(obj._source)
  14. for (field in obj) {
  15. field_array.push(field);
  16. }
  17. return field_array.sort();
  18. }
  19. kbn.get_all_fields = function(data) {
  20. var fields = [];
  21. _.each(data,function(hit) {
  22. fields = _.uniq(fields.concat(_.keys(hit)))
  23. })
  24. // Remove stupid angular key
  25. fields = _.without(fields,'$$hashKey')
  26. return fields;
  27. }
  28. kbn.has_field = function(obj,field) {
  29. var obj_fields = get_object_fields(obj);
  30. if (_.inArray(obj_fields,field) < 0) {
  31. return false;
  32. } else {
  33. return true;
  34. }
  35. }
  36. kbn.get_related_fields = function(docs,field) {
  37. var field_array = []
  38. _.each(docs, function(doc) {
  39. var keys = _.keys(doc)
  40. if(_.contains(keys,field))
  41. field_array = field_array.concat(keys)
  42. })
  43. var counts = _.countBy(_.without(field_array,field),function(field){return field;});
  44. return counts;
  45. }
  46. kbn.recurse_field_dots = function(object,field) {
  47. var value = null;
  48. if (typeof object[field] != 'undefined')
  49. value = object[field];
  50. else if (nested = field.match(/(.*?)\.(.*)/))
  51. if(typeof object[nested[1]] != 'undefined')
  52. value = (typeof object[nested[1]][nested[2]] != 'undefined') ?
  53. object[nested[1]][nested[2]] : recurse_field_dots(
  54. object[nested[1]],nested[2]);
  55. return value;
  56. }
  57. // Probably useless now, leaving for cases where you might not want
  58. // a flat dot notated data structure
  59. kbn.get_field_value = function(object,field,opt) {
  60. var value = kbn.recurse_field_dots(object['_source'],field);
  61. if(value === null)
  62. return ''
  63. if(_.isArray(value))
  64. if (opt == 'raw') {
  65. return value;
  66. }
  67. else {
  68. var complex = false;
  69. _.each(value, function(el, index) {
  70. if (typeof(el) == 'object') {
  71. complex = true;
  72. }
  73. })
  74. if (complex) {
  75. return JSON.stringify(value, null, 4);
  76. }
  77. return value.toString();
  78. }
  79. if(typeof value === 'object' && value != null)
  80. // Leaving this out for now
  81. //return opt == 'raw' ? value : JSON.stringify(value,null,4)
  82. return JSON.stringify(value,null,4)
  83. return (value != null) ? value.toString() : '';
  84. }
  85. kbn.top_field_values = function(docs,field,count) {
  86. var counts = _.countBy(_.pluck(docs,field),function(field){
  87. return _.isUndefined(field) ? '' : field;
  88. });
  89. return _.pairs(counts).sort(function(a, b) {
  90. return a[1] - b[1]
  91. }).reverse().slice(0,count)
  92. }
  93. /**
  94. * Calculate a graph interval
  95. *
  96. * from:: Date object containing the start time
  97. * to:: Date object containing the finish time
  98. * size:: Calculate to approximately this many bars
  99. * user_interval:: User specified histogram interval
  100. *
  101. */
  102. kbn.calculate_interval = function(from,to,size,user_interval) {
  103. if(_.isObject(from))
  104. from = from.valueOf();
  105. if(_.isObject(to))
  106. to = to.valueOf();
  107. return user_interval == 0 ? kbn.round_interval((to - from)/size) : user_interval;
  108. }
  109. kbn.round_interval = function(interval) {
  110. switch (true) {
  111. // 0.5s
  112. case (interval <= 500): return 100; // 0.1s
  113. // 5s
  114. case (interval <= 5000): return 1000; // 1s
  115. // 7.5s
  116. case (interval <= 7500): return 5000; // 5s
  117. // 15s
  118. case (interval <= 15000): return 10000; // 10s
  119. // 45s
  120. case (interval <= 45000): return 30000; // 30s
  121. // 3m
  122. case (interval <= 180000): return 60000; // 1m
  123. // 9m
  124. case (interval <= 450000): return 300000; // 5m
  125. // 20m
  126. case (interval <= 1200000): return 600000; // 10m
  127. // 45m
  128. case (interval <= 2700000): return 1800000; // 30m
  129. // 2h
  130. case (interval <= 7200000): return 3600000; // 1h
  131. // 6h
  132. case (interval <= 21600000): return 10800000; // 3h
  133. // 24h
  134. case (interval <= 86400000): return 43200000; // 12h
  135. // 48h
  136. case (interval <= 172800000): return 86400000; // 24h
  137. // 1w
  138. case (interval <= 604800000): return 86400000; // 24h
  139. // 3w
  140. case (interval <= 1814400000): return 604800000; // 1w
  141. // 2y
  142. case (interval < 3628800000): return 2592000000; // 30d
  143. default: return 31536000000; // 1y
  144. }
  145. }
  146. kbn.secondsToHms = function(seconds){
  147. var numyears = Math.floor(seconds / 31536000);
  148. if(numyears){
  149. return numyears + 'y';
  150. }
  151. var numdays = Math.floor((seconds % 31536000) / 86400);
  152. if(numdays){
  153. return numdays + 'd';
  154. }
  155. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  156. if(numhours){
  157. return numhours + 'h';
  158. }
  159. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  160. if(numminutes){
  161. return numminutes + 'm';
  162. }
  163. var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
  164. if(numseconds){
  165. return numseconds + 's';
  166. }
  167. return 'less then a second'; //'just now' //or other string you like;
  168. }
  169. kbn.to_percent = function(number,outof) {
  170. return Math.round((number/outof)*10000)/100 + "%";
  171. }
  172. kbn.addslashes = function(str) {
  173. str = str.replace(/\\/g, '\\\\');
  174. str = str.replace(/\'/g, '\\\'');
  175. str = str.replace(/\"/g, '\\"');
  176. str = str.replace(/\0/g, '\\0');
  177. return str;
  178. }
  179. // histogram & trends
  180. kbn.interval_to_seconds = function(string) {
  181. var matches = string.match(/(\d+)([Mwdhmsy])/);
  182. switch (matches[2]) {
  183. case 'y': return matches[1]*31536000;;
  184. case 'M': return matches[1]*2592000;;
  185. case 'w': return matches[1]*604800;;
  186. case 'd': return matches[1]*86400;;
  187. case 'h': return matches[1]*3600;;
  188. case 'm': return matches[1]*60;;
  189. case 's': return matches[1];
  190. }
  191. }
  192. // This should go away, moment.js can do this
  193. kbn.time_ago = function(string) {
  194. return new Date(new Date().getTime() - (kbn.interval_to_seconds(string)*1000))
  195. }
  196. // LOL. hahahahaha. DIE.
  197. kbn.flatten_json = function(object,root,array) {
  198. if (typeof array === 'undefined')
  199. var array = {};
  200. if (typeof root === 'undefined')
  201. var root = '';
  202. for(var index in object) {
  203. var obj = object[index]
  204. var rootname = root.length == 0 ? index : root + '.' + index;
  205. if(typeof obj == 'object' ) {
  206. if(_.isArray(obj)) {
  207. if(obj.length > 0 && typeof obj[0] === 'object') {
  208. var strval = '';
  209. for (var objidx = 0, objlen = obj.length; objidx < objlen; objidx++) {
  210. if (objidx > 0) {
  211. strval = strval + ', ';
  212. }
  213. strval = strval + JSON.stringify(obj[objidx]);
  214. }
  215. array[rootname] = strval;
  216. } else if(obj.length === 1 && _.isNumber(obj[0])) {
  217. array[rootname] = parseFloat(obj[0]);
  218. } else {
  219. array[rootname] = typeof obj === 'undefined' ? null : obj;
  220. }
  221. } else {
  222. kbn.flatten_json(obj,rootname,array)
  223. }
  224. } else {
  225. array[rootname] = typeof obj === 'undefined' ? null : obj;
  226. }
  227. }
  228. return kbn.sortObj(array);
  229. }
  230. kbn.xmlEnt = function(value) {
  231. if(_.isString(value)) {
  232. var stg1 = value.replace(/</g, '&lt;')
  233. .replace(/>/g, '&gt;')
  234. .replace(/\r\n/g, '<br/>')
  235. .replace(/\r/g, '<br/>')
  236. .replace(/\n/g, '<br/>')
  237. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  238. .replace(/ /g, '&nbsp;&nbsp;')
  239. .replace(/&lt;del&gt;/g, '<del>')
  240. .replace(/&lt;\/del&gt;/g, '</del>');
  241. return stg1;
  242. } else {
  243. return value;
  244. }
  245. }
  246. kbn.sortObj = function(arr) {
  247. // Setup Arrays
  248. var sortedKeys = new Array();
  249. var sortedObj = {};
  250. // Separate keys and sort them
  251. for (var i in arr) {
  252. sortedKeys.push(i);
  253. }
  254. sortedKeys.sort();
  255. // Reconstruct sorted obj based on keys
  256. for (var i in sortedKeys) {
  257. sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
  258. }
  259. return sortedObj;
  260. }
  261. }).call(this);
  262. /*
  263. UNDERSCORE.js Mixins
  264. */
  265. _.mixin({
  266. move: function (array, fromIndex, toIndex) {
  267. array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
  268. return array;
  269. }
  270. });
  271. _.mixin({
  272. remove: function (array, index) {
  273. array.splice(index, 1);
  274. return array;
  275. }
  276. });