module.js 8.4 KB

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