module.js 7.9 KB

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