module.js 8.4 KB

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