module.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Table
  5. ### Parameters
  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. * selected_fields :: An array of fields to show
  23. */
  24. 'use strict';
  25. angular.module('kibana.table', [])
  26. .controller('table', function($rootScope, $scope, eventBus, fields, querySrv, dashboard, filterSrv) {
  27. $scope.panelMeta = {
  28. status: "Stable",
  29. description: "A paginated table of records matching your query or queries. Click on a row to "+
  30. "expand it and review all of the fields associated with that document. <p>"
  31. };
  32. // Set and populate defaults
  33. var _d = {
  34. status : "Stable",
  35. queries : {
  36. mode : 'all',
  37. ids : []
  38. },
  39. size : 100, // Per page
  40. pages : 5, // Pages available
  41. offset : 0,
  42. sort : ['@timestamp','desc'],
  43. group : "default",
  44. style : {'font-size': '9pt'},
  45. overflow: 'height',
  46. fields : [],
  47. highlight : [],
  48. sortable: true,
  49. header : true,
  50. paging : true,
  51. field_list: true,
  52. spyable : true
  53. };
  54. _.defaults($scope.panel,_d);
  55. $scope.init = function () {
  56. $scope.Math = Math;
  57. $scope.$on('refresh',function(){$scope.get_data();});
  58. $scope.get_data();
  59. };
  60. $scope.toggle_micropanel = function(field) {
  61. var docs = _.pluck($scope.data,'_source');
  62. $scope.micropanel = {
  63. field: field,
  64. values : kbn.top_field_values(docs,field,10),
  65. related : kbn.get_related_fields(docs,field),
  66. count: _.countBy(docs,function(doc){return _.contains(_.keys(doc),field);})['true']
  67. };
  68. };
  69. $scope.set_sort = function(field) {
  70. if($scope.panel.sort[0] === field) {
  71. $scope.panel.sort[1] = $scope.panel.sort[1] === 'asc' ? 'desc' : 'asc';
  72. } else {
  73. $scope.panel.sort[0] = field;
  74. }
  75. $scope.get_data();
  76. };
  77. $scope.toggle_field = function(field) {
  78. if (_.indexOf($scope.panel.fields,field) > -1) {
  79. $scope.panel.fields = _.without($scope.panel.fields,field);
  80. } else {
  81. $scope.panel.fields.push(field);
  82. }
  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.fieldExists = function(field,mandate) {
  112. filterSrv.set({type:'exists',field:field,mandate:mandate});
  113. dashboard.refresh();
  114. };
  115. $scope.get_data = function(segment,query_id) {
  116. $scope.panel.error = false;
  117. // Make sure we have everything for the request to complete
  118. if(dashboard.indices.length === 0) {
  119. return;
  120. }
  121. $scope.panelMeta.loading = true;
  122. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  123. var _segment = _.isUndefined(segment) ? 0 : segment;
  124. $scope.segment = _segment;
  125. var request = $scope.ejs.Request().indices(dashboard.indices[_segment]);
  126. var boolQuery = $scope.ejs.BoolQuery();
  127. _.each($scope.panel.queries.ids,function(id) {
  128. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  129. });
  130. request = request.query(
  131. $scope.ejs.FilteredQuery(
  132. boolQuery,
  133. filterSrv.getBoolFilter(filterSrv.ids)
  134. ))
  135. .highlight(
  136. $scope.ejs.Highlight($scope.panel.highlight)
  137. .fragmentSize(2147483647) // Max size of a 32bit unsigned int
  138. .preTags('@start-highlight@')
  139. .postTags('@end-highlight@')
  140. )
  141. .size($scope.panel.size*$scope.panel.pages)
  142. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  143. $scope.populate_modal(request);
  144. var results = request.doSearch();
  145. // Populate scope when we have results
  146. results.then(function(results) {
  147. $scope.panelMeta.loading = false;
  148. if(_segment === 0) {
  149. $scope.hits = 0;
  150. $scope.data = [];
  151. query_id = $scope.query_id = new Date().getTime();
  152. }
  153. // Check for error and abort if found
  154. if(!(_.isUndefined(results.error))) {
  155. $scope.panel.error = $scope.parse_error(results.error);
  156. return;
  157. }
  158. // Check that we're still on the same query, if not stop
  159. if($scope.query_id === query_id) {
  160. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  161. return {
  162. _source : kbn.flatten_json(hit._source),
  163. highlight : kbn.flatten_json(hit.highlight||{})
  164. };
  165. }));
  166. $scope.hits += results.hits.total;
  167. // Sort the data
  168. $scope.data = _.sortBy($scope.data, function(v){
  169. return v._source[$scope.panel.sort[0]];
  170. });
  171. // Reverse if needed
  172. if($scope.panel.sort[1] === 'desc') {
  173. $scope.data.reverse();
  174. }
  175. // Keep only what we need for the set
  176. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages);
  177. } else {
  178. return;
  179. }
  180. // This breaks, use $scope.data for this
  181. $scope.all_fields = kbn.get_all_fields(_.pluck($scope.data,'_source'));
  182. // If we're not sorting in reverse chrono order, query every index for
  183. // size*pages results
  184. // Otherwise, only get size*pages results then stop querying
  185. //($scope.data.length < $scope.panel.size*$scope.panel.pages
  186. // || !(($scope.panel.sort[0] === $scope.time.field) && $scope.panel.sort[1] === 'desc'))
  187. if($scope.data.length < $scope.panel.size*$scope.panel.pages &&
  188. _segment+1 < dashboard.indices.length ) {
  189. $scope.get_data(_segment+1,$scope.query_id);
  190. }
  191. });
  192. };
  193. $scope.populate_modal = function(request) {
  194. $scope.modal = {
  195. title: "Table Inspector",
  196. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  197. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  198. angular.toJson(JSON.parse(request.toString()),true)+
  199. "'</pre>",
  200. };
  201. };
  202. $scope.without_kibana = function (row) {
  203. return {
  204. _source : row._source,
  205. highlight : row.highlight
  206. };
  207. };
  208. $scope.set_refresh = function (state) {
  209. $scope.refresh = state;
  210. };
  211. $scope.close_edit = function() {
  212. if($scope.refresh) {
  213. $scope.get_data();
  214. }
  215. $scope.refresh = false;
  216. };
  217. })
  218. .filter('highlight', function() {
  219. return function(text) {
  220. if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
  221. return text.toString().
  222. replace(/&/g, '&amp;').
  223. replace(/</g, '&lt;').
  224. replace(/>/g, '&gt;').
  225. replace(/\r?\n/g, '<br/>').
  226. replace(/@start-highlight@/g, '<code class="highlight">').
  227. replace(/@end-highlight@/g, '</code>');
  228. }
  229. return '';
  230. };
  231. });