module.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.percent = kbn.to_percent;
  63. $scope.toggle_micropanel = function(field) {
  64. var docs = _.pluck($scope.data,'_source');
  65. $scope.micropanel = {
  66. field: field,
  67. values : kbn.top_field_values(docs,field,10),
  68. related : kbn.get_related_fields(docs,field),
  69. count: _.countBy(docs,function(doc){return _.contains(_.keys(doc),field);})['true']
  70. };
  71. };
  72. $scope.micropanelColor = function(index) {
  73. var _c = ['bar-success','bar-warning','bar-danger','bar-info','bar-primary'];
  74. return index > _c.length ? '' : _c[index];
  75. };
  76. $scope.set_sort = function(field) {
  77. if($scope.panel.sort[0] === field) {
  78. $scope.panel.sort[1] = $scope.panel.sort[1] === 'asc' ? 'desc' : 'asc';
  79. } else {
  80. $scope.panel.sort[0] = field;
  81. }
  82. $scope.get_data();
  83. };
  84. $scope.toggle_field = function(field) {
  85. if (_.indexOf($scope.panel.fields,field) > -1) {
  86. $scope.panel.fields = _.without($scope.panel.fields,field);
  87. } else {
  88. $scope.panel.fields.push(field);
  89. }
  90. };
  91. $scope.toggle_highlight = function(field) {
  92. if (_.indexOf($scope.panel.highlight,field) > -1) {
  93. $scope.panel.highlight = _.without($scope.panel.highlight,field);
  94. } else {
  95. $scope.panel.highlight.push(field);
  96. }
  97. };
  98. $scope.toggle_details = function(row) {
  99. row.kibana = row.kibana || {};
  100. row.kibana.details = !row.kibana.details ? $scope.without_kibana(row) : false;
  101. };
  102. $scope.page = function(page) {
  103. $scope.panel.offset = page*$scope.panel.size;
  104. $scope.get_data();
  105. };
  106. $scope.build_search = function(field,value,negate) {
  107. var query;
  108. // This needs to be abstracted somewhere
  109. if(_.isArray(value)) {
  110. query = "(" + _.map(value,function(v){return angular.toJson(v);}).join(" AND ") + ")";
  111. } else {
  112. query = angular.toJson(value);
  113. }
  114. filterSrv.set({type:'field',field:field,query:query,mandate:(negate ? 'mustNot':'must')});
  115. $scope.panel.offset = 0;
  116. dashboard.refresh();
  117. };
  118. $scope.fieldExists = function(field,mandate) {
  119. filterSrv.set({type:'exists',field:field,mandate:mandate});
  120. dashboard.refresh();
  121. };
  122. $scope.get_data = function(segment,query_id) {
  123. $scope.panel.error = false;
  124. // Make sure we have everything for the request to complete
  125. if(dashboard.indices.length === 0) {
  126. return;
  127. }
  128. $scope.panelMeta.loading = true;
  129. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  130. var _segment = _.isUndefined(segment) ? 0 : segment;
  131. $scope.segment = _segment;
  132. var request = $scope.ejs.Request().indices(dashboard.indices[_segment]);
  133. var boolQuery = $scope.ejs.BoolQuery();
  134. _.each($scope.panel.queries.ids,function(id) {
  135. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  136. });
  137. request = request.query(
  138. $scope.ejs.FilteredQuery(
  139. boolQuery,
  140. filterSrv.getBoolFilter(filterSrv.ids)
  141. ))
  142. .highlight(
  143. $scope.ejs.Highlight($scope.panel.highlight)
  144. .fragmentSize(2147483647) // Max size of a 32bit unsigned int
  145. .preTags('@start-highlight@')
  146. .postTags('@end-highlight@')
  147. )
  148. .size($scope.panel.size*$scope.panel.pages)
  149. .sort($scope.panel.sort[0],$scope.panel.sort[1]);
  150. $scope.populate_modal(request);
  151. var results = request.doSearch();
  152. // Populate scope when we have results
  153. results.then(function(results) {
  154. $scope.panelMeta.loading = false;
  155. if(_segment === 0) {
  156. $scope.hits = 0;
  157. $scope.data = [];
  158. query_id = $scope.query_id = new Date().getTime();
  159. }
  160. // Check for error and abort if found
  161. if(!(_.isUndefined(results.error))) {
  162. $scope.panel.error = $scope.parse_error(results.error);
  163. return;
  164. }
  165. // Check that we're still on the same query, if not stop
  166. if($scope.query_id === query_id) {
  167. $scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
  168. return {
  169. _source : kbn.flatten_json(hit._source),
  170. highlight : kbn.flatten_json(hit.highlight||{}),
  171. _type : hit._type,
  172. _index : hit._index,
  173. _id : hit._id,
  174. _sort : hit.sort
  175. };
  176. }));
  177. $scope.hits += results.hits.total;
  178. // Sort the data
  179. $scope.data = _.sortBy($scope.data, function(v){
  180. return v._sort[0];
  181. });
  182. // Reverse if needed
  183. if($scope.panel.sort[1] === 'desc') {
  184. $scope.data.reverse();
  185. }
  186. // Keep only what we need for the set
  187. $scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages);
  188. } else {
  189. return;
  190. }
  191. // If we're not sorting in reverse chrono order, query every index for
  192. // size*pages results
  193. // Otherwise, only get size*pages results then stop querying
  194. if (($scope.data.length < $scope.panel.size*$scope.panel.pages ||
  195. !((_.contains(filterSrv.timeField(),$scope.panel.sort[0])) && $scope.panel.sort[1] === 'desc')) &&
  196. _segment+1 < dashboard.indices.length) {
  197. $scope.get_data(_segment+1,$scope.query_id);
  198. }
  199. });
  200. };
  201. $scope.populate_modal = function(request) {
  202. $scope.inspector = angular.toJson(JSON.parse(request.toString()),true);
  203. };
  204. $scope.without_kibana = function (row) {
  205. return {
  206. _source : row._source,
  207. highlight : row.highlight
  208. };
  209. };
  210. $scope.set_refresh = function (state) {
  211. $scope.refresh = state;
  212. };
  213. $scope.close_edit = function() {
  214. if($scope.refresh) {
  215. $scope.get_data();
  216. }
  217. $scope.refresh = false;
  218. };
  219. })
  220. .filter('tableHighlight', function() {
  221. return function(text) {
  222. if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
  223. return text.toString().
  224. replace(/&/g, '&amp;').
  225. replace(/</g, '&lt;').
  226. replace(/>/g, '&gt;').
  227. replace(/\r?\n/g, '<br/>').
  228. replace(/@start-highlight@/g, '<code class="highlight">').
  229. replace(/@end-highlight@/g, '</code>');
  230. }
  231. return '';
  232. };
  233. })
  234. .filter('tableTruncate', function() {
  235. return function(text,length,factor) {
  236. if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
  237. return text.length > length/factor ? text.substr(0,length/factor)+'...' : text;
  238. }
  239. return '';
  240. };
  241. // WIP
  242. }).filter('tableFieldFormat', function(fields){
  243. return function(text,field,event,scope) {
  244. var type;
  245. if(
  246. !_.isUndefined(fields.mapping[event._index]) &&
  247. !_.isUndefined(fields.mapping[event._index][event._type])
  248. ) {
  249. type = fields.mapping[event._index][event._type][field]['type'];
  250. if(type === 'date' && scope.panel.normTimes) {
  251. return moment(text).format('YYYY-MM-DD HH:mm:ss');
  252. }
  253. }
  254. return text;
  255. };
  256. });