module.js 8.6 KB

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