module.js 8.6 KB

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