shared.js 8.3 KB

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