module.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * trimFactor :: If line is > this many characters, divided by the number of columns, trim it.
  16. * sortable :: Allow sorting?
  17. * spyable :: Show the 'eye' icon that reveals the last ES query for this panel
  18. */
  19. 'use strict';
  20. angular.module('kibana.table', [])
  21. .controller('table', function($rootScope, $scope, fields, querySrv, dashboard, filterSrv) {
  22. $scope.panelMeta = {
  23. status: "Stable",
  24. description: "A paginated table of records matching your query or queries. Click on a row to "+
  25. "expand it and review all of the fields associated with that document. <p>"
  26. };
  27. // Set and populate defaults
  28. var _d = {
  29. status : "Stable",
  30. queries : {
  31. mode : 'all',
  32. ids : []
  33. },
  34. size : 100, // Per page
  35. pages : 5, // Pages available
  36. offset : 0,
  37. sort : ['@timestamp','desc'],
  38. group : "default",
  39. style : {'font-size': '9pt'},
  40. overflow: 'height',
  41. fields : [],
  42. highlight : [],
  43. sortable: true,
  44. header : true,
  45. paging : true,
  46. field_list: true,
  47. trimFactor: 300,
  48. spyable : true
  49. };
  50. _.defaults($scope.panel,_d);
  51. $scope.init = function () {
  52. $scope.Math = Math;
  53. $scope.$on('refresh',function(){$scope.get_data();});
  54. $scope.fields = fields;
  55. $scope.get_data();
  56. };
  57. $scope.toggle_micropanel = function(field) {
  58. var docs = _.pluck($scope.data,'_source');
  59. $scope.micropanel = {
  60. field: field,
  61. values : kbn.top_field_values(docs,field,10),
  62. related : kbn.get_related_fields(docs,field),
  63. count: _.countBy(docs,function(doc){return _.contains(_.keys(doc),field);})['true']
  64. };
  65. };
  66. $scope.set_sort = function(field) {
  67. if($scope.panel.sort[0] === field) {
  68. $scope.panel.sort[1] = $scope.panel.sort[1] === 'asc' ? 'desc' : 'asc';
  69. } else {
  70. $scope.panel.sort[0] = field;
  71. }
  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. }
  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. };
  88. $scope.toggle_details = function(row) {
  89. row.kibana = row.kibana || {};
  90. row.kibana.details = !row.kibana.details ? $scope.without_kibana(row) : false;
  91. };
  92. $scope.page = function(page) {
  93. $scope.panel.offset = page*$scope.panel.size;
  94. $scope.get_data();
  95. };
  96. $scope.build_search = function(field,value,negate) {
  97. var query;
  98. // This needs to be abstracted somewhere
  99. if(_.isArray(value)) {
  100. query = "(" + _.map(value,function(v){return angular.toJson(v);}).join(" AND ") + ")";
  101. } else {
  102. query = angular.toJson(value);
  103. }
  104. filterSrv.set({type:'field',field:field,query:query,mandate:(negate ? 'mustNot':'must')});
  105. $scope.panel.offset = 0;
  106. dashboard.refresh();
  107. };
  108. $scope.fieldExists = function(field,mandate) {
  109. filterSrv.set({type:'exists',field:field,mandate:mandate});
  110. dashboard.refresh();
  111. };
  112. $scope.get_data = function(segment,query_id) {
  113. $scope.panel.error = false;
  114. // Make sure we have everything for the request to complete
  115. if(dashboard.indices.length === 0) {
  116. return;
  117. }
  118. $scope.panelMeta.loading = true;
  119. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  120. var _segment = _.isUndefined(segment) ? 0 : segment;
  121. $scope.segment = _segment;
  122. var request = $scope.ejs.Request().indices(dashboard.indices[_segment]);
  123. var boolQuery = $scope.ejs.BoolQuery();
  124. _.each($scope.panel.queries.ids,function(id) {
  125. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  126. });
  127. request = request.query(
  128. $scope.ejs.FilteredQuery(
  129. boolQuery,
  130. filterSrv.getBoolFilter(filterSrv.ids)
  131. ))
  132. .highlight(
  133. $scope.ejs.Highlight($scope.panel.highlight)
  134. .fragmentSize(2147483647) // Max size of a 32bit unsigned int
  135. .preTags('@start-highlight@')
  136. .postTags('@end-highlight@')
  137. )
  138. .size($scope.panel.size*$scope.panel.pages)
  139. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  140. $scope.populate_modal(request);
  141. var results = request.doSearch();
  142. // Populate scope when we have results
  143. results.then(function(results) {
  144. $scope.panelMeta.loading = false;
  145. if(_segment === 0) {
  146. $scope.hits = 0;
  147. $scope.data = [];
  148. query_id = $scope.query_id = new Date().getTime();
  149. }
  150. // Check for error and abort if found
  151. if(!(_.isUndefined(results.error))) {
  152. $scope.panel.error = $scope.parse_error(results.error);
  153. return;
  154. }
  155. // Check that we're still on the same query, if not stop
  156. if($scope.query_id === query_id) {
  157. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  158. return {
  159. _source : kbn.flatten_json(hit._source),
  160. highlight : kbn.flatten_json(hit.highlight||{}),
  161. _type : hit._type,
  162. _index : hit._index,
  163. _id : hit._id,
  164. _sort : hit.sort
  165. };
  166. }));
  167. $scope.hits += results.hits.total;
  168. // Sort the data
  169. $scope.data = _.sortBy($scope.data, function(v){
  170. return v._sort[0];
  171. });
  172. // Reverse if needed
  173. if($scope.panel.sort[1] === 'desc') {
  174. $scope.data.reverse();
  175. }
  176. // Keep only what we need for the set
  177. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages);
  178. } else {
  179. return;
  180. }
  181. // If we're not sorting in reverse chrono order, query every index for
  182. // size*pages results
  183. // Otherwise, only get size*pages results then stop querying
  184. //($scope.data.length < $scope.panel.size*$scope.panel.pages
  185. // || !(($scope.panel.sort[0] === $scope.time.field) && $scope.panel.sort[1] === 'desc'))
  186. if($scope.data.length < $scope.panel.size*$scope.panel.pages &&
  187. _segment+1 < dashboard.indices.length ) {
  188. $scope.get_data(_segment+1,$scope.query_id);
  189. }
  190. });
  191. };
  192. $scope.populate_modal = function(request) {
  193. $scope.inspector = angular.toJson(JSON.parse(request.toString()),true);
  194. };
  195. $scope.without_kibana = function (row) {
  196. return {
  197. _source : row._source,
  198. highlight : row.highlight
  199. };
  200. };
  201. $scope.set_refresh = function (state) {
  202. $scope.refresh = state;
  203. };
  204. $scope.close_edit = function() {
  205. if($scope.refresh) {
  206. $scope.get_data();
  207. }
  208. $scope.refresh = false;
  209. };
  210. })
  211. .filter('tableHighlight', function() {
  212. return function(text) {
  213. if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
  214. return text.toString().
  215. replace(/&/g, '&amp;').
  216. replace(/</g, '&lt;').
  217. replace(/>/g, '&gt;').
  218. replace(/\r?\n/g, '<br/>').
  219. replace(/@start-highlight@/g, '<code class="highlight">').
  220. replace(/@end-highlight@/g, '</code>');
  221. }
  222. return '';
  223. };
  224. })
  225. .filter('tableTruncate', function() {
  226. return function(text,length,factor) {
  227. if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
  228. return text.length > length/factor ? text.substr(0,length/factor)+'...' : text;
  229. }
  230. return '';
  231. };
  232. });