module.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Table
  5. A paginated table of events matching a query
  6. ### Parameters
  7. * size :: Number of events per page to show
  8. * pages :: Number of pages to show. size * pages = number of cached events.
  9. Bigger = more memory usage byh the browser
  10. * offset :: Position from which to start in the array of hits
  11. * sort :: An array with 2 elements. sort[0]: field, sort[1]: direction ('asc' or 'desc')
  12. * style :: hash of css properties
  13. * fields :: columns to show in table
  14. * overflow :: 'height' or 'min-height' controls wether the row will expand (min-height) to
  15. to fit the table, or if the table will scroll to fit the row (height)
  16. * sortable :: Allow sorting?
  17. * spyable :: Show the 'eye' icon that reveals the last ES query for this panel
  18. ### Group Events
  19. #### Sends
  20. * table_documents :: An array containing all of the documents in the table.
  21. Only used by the fields panel so far.
  22. #### Receives
  23. * selected_fields :: An array of fields to show
  24. */
  25. 'use strict';
  26. angular.module('kibana.table', [])
  27. .controller('table', function($rootScope, $scope, eventBus, fields, querySrv, dashboard, filterSrv) {
  28. // Set and populate defaults
  29. var _d = {
  30. status : "Stable",
  31. queries : {
  32. mode : 'all',
  33. ids : []
  34. },
  35. size : 100, // Per page
  36. pages : 5, // Pages available
  37. offset : 0,
  38. sort : ['@timestamp','desc'],
  39. group : "default",
  40. style : {'font-size': '9pt'},
  41. overflow: 'height',
  42. fields : [],
  43. highlight : [],
  44. sortable: true,
  45. header : true,
  46. paging : true,
  47. spyable: true
  48. };
  49. _.defaults($scope.panel,_d);
  50. $scope.init = function () {
  51. $scope.set_listeners($scope.panel.group);
  52. $scope.get_data();
  53. };
  54. $scope.set_listeners = function(group) {
  55. $scope.$on('refresh',function(){$scope.get_data();});
  56. eventBus.register($scope,'sort', function(event,sort){
  57. $scope.panel.sort = _.clone(sort);
  58. $scope.get_data();
  59. });
  60. eventBus.register($scope,'selected_fields', function(event, fields) {
  61. $scope.panel.fields = _.clone(fields);
  62. });
  63. eventBus.register($scope,'table_documents', function(event, docs) {
  64. querySrv.list[querySrv.ids[0]].query = docs.query;
  65. $scope.data = docs.docs;
  66. });
  67. };
  68. $scope.set_sort = function(field) {
  69. if($scope.panel.sort[0] === field) {
  70. $scope.panel.sort[1] = $scope.panel.sort[1] === 'asc' ? 'desc' : 'asc';
  71. } else {
  72. $scope.panel.sort[0] = field;
  73. }
  74. $scope.get_data();
  75. };
  76. $scope.toggle_field = function(field) {
  77. if (_.indexOf($scope.panel.fields,field) > -1) {
  78. $scope.panel.fields = _.without($scope.panel.fields,field);
  79. } else {
  80. $scope.panel.fields.push(field);
  81. }
  82. broadcast_results();
  83. };
  84. $scope.toggle_highlight = function(field) {
  85. if (_.indexOf($scope.panel.highlight,field) > -1) {
  86. $scope.panel.highlight = _.without($scope.panel.highlight,field);
  87. } else {
  88. $scope.panel.highlight.push(field);
  89. }
  90. };
  91. $scope.toggle_details = function(row) {
  92. row.kibana = row.kibana || {};
  93. row.kibana.details = !row.kibana.details ? $scope.without_kibana(row) : false;
  94. };
  95. $scope.page = function(page) {
  96. $scope.panel.offset = page*$scope.panel.size;
  97. $scope.get_data();
  98. };
  99. $scope.build_search = function(field,value,negate) {
  100. var query = field+":";
  101. // This needs to be abstracted somewhere
  102. if(_.isArray(value)) {
  103. query = query+"(" + _.map(value,function(v){return angular.toJson(v);}).join(" AND ") + ")";
  104. } else {
  105. query = query+angular.toJson(value);
  106. }
  107. filterSrv.set({type:'querystring',query:query,mandate:(negate ? 'mustNot':'must')});
  108. $scope.panel.offset = 0;
  109. dashboard.refresh();
  110. };
  111. $scope.get_data = function(segment,query_id) {
  112. $scope.panel.error = false;
  113. // Make sure we have everything for the request to complete
  114. if(dashboard.indices.length === 0) {
  115. return;
  116. }
  117. $scope.panel.loading = true;
  118. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  119. var _segment = _.isUndefined(segment) ? 0 : segment;
  120. $scope.segment = _segment;
  121. var request = $scope.ejs.Request().indices(dashboard.indices[_segment]);
  122. var boolQuery = $scope.ejs.BoolQuery();
  123. _.each($scope.panel.queries.ids,function(id) {
  124. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  125. });
  126. request = request.query(
  127. $scope.ejs.FilteredQuery(
  128. boolQuery,
  129. filterSrv.getBoolFilter(filterSrv.ids)
  130. ))
  131. .highlight(
  132. $scope.ejs.Highlight($scope.panel.highlight)
  133. .fragmentSize(2147483647) // Max size of a 32bit unsigned int
  134. .preTags('@start-highlight@')
  135. .postTags('@end-highlight@')
  136. )
  137. .size($scope.panel.size*$scope.panel.pages)
  138. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  139. $scope.populate_modal(request);
  140. var results = request.doSearch();
  141. // Populate scope when we have results
  142. results.then(function(results) {
  143. $scope.panel.loading = false;
  144. if(_segment === 0) {
  145. $scope.hits = 0;
  146. $scope.data = [];
  147. query_id = $scope.query_id = new Date().getTime();
  148. }
  149. // Check for error and abort if found
  150. if(!(_.isUndefined(results.error))) {
  151. $scope.panel.error = $scope.parse_error(results.error);
  152. return;
  153. }
  154. // Check that we're still on the same query, if not stop
  155. if($scope.query_id === query_id) {
  156. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  157. return {
  158. _source : kbn.flatten_json(hit._source),
  159. highlight : kbn.flatten_json(hit.highlight||{})
  160. };
  161. }));
  162. $scope.hits += results.hits.total;
  163. // Sort the data
  164. $scope.data = _.sortBy($scope.data, function(v){
  165. return v._source[$scope.panel.sort[0]];
  166. });
  167. // Reverse if needed
  168. if($scope.panel.sort[1] === 'desc') {
  169. $scope.data.reverse();
  170. }
  171. // Keep only what we need for the set
  172. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages);
  173. } else {
  174. return;
  175. }
  176. // This breaks, use $scope.data for this
  177. $scope.all_fields = kbn.get_all_fields(_.pluck($scope.data,'_source'));
  178. broadcast_results();
  179. // If we're not sorting in reverse chrono order, query every index for
  180. // size*pages results
  181. // Otherwise, only get size*pages results then stop querying
  182. //($scope.data.length < $scope.panel.size*$scope.panel.pages
  183. // || !(($scope.panel.sort[0] === $scope.time.field) && $scope.panel.sort[1] === 'desc'))
  184. if($scope.data.length < $scope.panel.size*$scope.panel.pages &&
  185. _segment+1 < dashboard.indices.length ) {
  186. $scope.get_data(_segment+1,$scope.query_id);
  187. }
  188. });
  189. };
  190. $scope.populate_modal = function(request) {
  191. $scope.modal = {
  192. title: "Table Inspector",
  193. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  194. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  195. angular.toJson(JSON.parse(request.toString()),true)+
  196. "'</pre>",
  197. };
  198. };
  199. $scope.without_kibana = function (row) {
  200. return {
  201. _source : row._source,
  202. highlight : row.highlight
  203. };
  204. };
  205. // Broadcast a list of all fields. Note that receivers of field array
  206. // events should be able to receive from multiple sources, merge, dedupe
  207. // and sort on the fly if needed.
  208. function broadcast_results() {
  209. eventBus.broadcast($scope.$id,$scope.panel.group,"fields", {
  210. all : $scope.all_fields,
  211. sort : $scope.panel.sort,
  212. active: $scope.panel.fields
  213. });
  214. eventBus.broadcast($scope.$id,$scope.panel.group,"table_documents",
  215. {
  216. query: querySrv.list[querySrv.ids[0]].query,
  217. docs : _.pluck($scope.data,'_source'),
  218. index: $scope.index
  219. });
  220. }
  221. $scope.set_refresh = function (state) {
  222. $scope.refresh = state;
  223. };
  224. $scope.close_edit = function() {
  225. if($scope.refresh) {
  226. $scope.get_data();
  227. }
  228. $scope.refresh = false;
  229. };
  230. })
  231. .filter('highlight', function() {
  232. return function(text) {
  233. if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
  234. return text.toString().
  235. replace(/&/g, '&amp;').
  236. replace(/</g, '&lt;').
  237. replace(/>/g, '&gt;').
  238. replace(/\r?\n/g, '<br/>').
  239. replace(/@start-highlight@/g, '<code class="highlight">').
  240. replace(/@end-highlight@/g, '</code>');
  241. }
  242. return '';
  243. };
  244. });