module.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Terms
  5. ### Parameters
  6. * style :: A hash of css styles
  7. * size :: top N
  8. * arrangement :: How should I arrange the query results? 'horizontal' or 'vertical'
  9. * chart :: Show a chart? 'none', 'bar', 'pie'
  10. * donut :: Only applies to 'pie' charts. Punches a hole in the chart for some reason
  11. * tilt :: Only 'pie' charts. Janky 3D effect. Looks terrible 90% of the time.
  12. * lables :: Only 'pie' charts. Labels on the pie?
  13. */
  14. 'use strict';
  15. angular.module('kibana.terms', [])
  16. .controller('terms', function($scope, querySrv, dashboard, filterSrv) {
  17. $scope.panelMeta = {
  18. editorTabs : [
  19. {title:'Queries', src:'partials/querySelect.html'}
  20. ],
  21. status : "Beta",
  22. description : "Displays the results of an elasticsearch facet as a pie chart, bar chart, or a "+
  23. "table"
  24. };
  25. // Set and populate defaults
  26. var _d = {
  27. queries : {
  28. mode : 'all',
  29. ids : []
  30. },
  31. field : '_type',
  32. exclude : [],
  33. missing : true,
  34. other : true,
  35. size : 10,
  36. order : 'count',
  37. style : { "font-size": '10pt'},
  38. donut : false,
  39. tilt : false,
  40. labels : true,
  41. arrangement : 'horizontal',
  42. chart : 'bar',
  43. counter_pos : 'above',
  44. spyable : true
  45. };
  46. _.defaults($scope.panel,_d);
  47. $scope.init = function () {
  48. $scope.hits = 0;
  49. $scope.$on('refresh',function(){
  50. $scope.get_data();
  51. });
  52. $scope.get_data();
  53. };
  54. $scope.get_data = function(segment,query_id) {
  55. // Make sure we have everything for the request to complete
  56. if(dashboard.indices.length === 0) {
  57. return;
  58. }
  59. $scope.panelMeta.loading = true;
  60. var request,
  61. results,
  62. boolQuery;
  63. request = $scope.ejs.Request().indices(dashboard.indices);
  64. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  65. // This could probably be changed to a BoolFilter
  66. boolQuery = $scope.ejs.BoolQuery();
  67. _.each($scope.panel.queries.ids,function(id) {
  68. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  69. });
  70. // Terms mode
  71. request = request
  72. .facet($scope.ejs.TermsFacet('terms')
  73. .field($scope.panel.field)
  74. .size($scope.panel.size)
  75. .order($scope.panel.order)
  76. .exclude($scope.panel.exclude)
  77. .facetFilter($scope.ejs.QueryFilter(
  78. $scope.ejs.FilteredQuery(
  79. boolQuery,
  80. filterSrv.getBoolFilter(filterSrv.ids)
  81. )))).size(0);
  82. // Populate the inspector panel
  83. $scope.inspector = angular.toJson(JSON.parse(request.toString()),true);
  84. results = request.doSearch();
  85. // Populate scope when we have results
  86. results.then(function(results) {
  87. var k = 0;
  88. $scope.panelMeta.loading = false;
  89. $scope.hits = results.hits.total;
  90. $scope.data = [];
  91. _.each(results.facets.terms.terms, function(v) {
  92. var slice = { label : v.term, data : [[k,v.count]], actions: true};
  93. $scope.data.push(slice);
  94. k = k + 1;
  95. });
  96. $scope.data.push({label:'Missing field',
  97. data:[[k,results.facets.terms.missing]],meta:"missing",color:'#aaa',opacity:0});
  98. $scope.data.push({label:'Other values',
  99. data:[[k+1,results.facets.terms.other]],meta:"other",color:'#444'});
  100. $scope.$emit('render');
  101. });
  102. };
  103. $scope.build_search = function(term,negate) {
  104. if(_.isUndefined(term.meta)) {
  105. filterSrv.set({type:'terms',field:$scope.panel.field,value:term.label,
  106. mandate:(negate ? 'mustNot':'must')});
  107. } else if(term.meta === 'missing') {
  108. filterSrv.set({type:'exists',field:$scope.panel.field,
  109. mandate:(negate ? 'must':'mustNot')});
  110. } else {
  111. return;
  112. }
  113. dashboard.refresh();
  114. };
  115. $scope.set_refresh = function (state) {
  116. $scope.refresh = state;
  117. };
  118. $scope.close_edit = function() {
  119. if($scope.refresh) {
  120. $scope.get_data();
  121. }
  122. $scope.refresh = false;
  123. $scope.$emit('render');
  124. };
  125. $scope.showMeta = function(term) {
  126. if(_.isUndefined(term.meta)) {
  127. return true;
  128. }
  129. if(term.meta === 'other' && !$scope.panel.other) {
  130. return false;
  131. }
  132. if(term.meta === 'missing' && !$scope.panel.missing) {
  133. return false;
  134. }
  135. return true;
  136. };
  137. }).directive('termsChart', function(querySrv, filterSrv, dashboard) {
  138. return {
  139. restrict: 'A',
  140. link: function(scope, elem, attrs, ctrl) {
  141. // Receive render events
  142. scope.$on('render',function(){
  143. render_panel();
  144. });
  145. // Re-render if the window is resized
  146. angular.element(window).bind('resize', function(){
  147. render_panel();
  148. });
  149. // Function for rendering panel
  150. function render_panel() {
  151. var plot, chartData;
  152. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  153. .script("common/lib/panels/jquery.flot.pie.js");
  154. // IE doesn't work without this
  155. elem.css({height:scope.panel.height||scope.row.height});
  156. // Make a clone we can operate on.
  157. chartData = _.clone(scope.data);
  158. chartData = scope.panel.missing ? chartData :
  159. _.without(chartData,_.findWhere(chartData,{meta:'missing'}));
  160. chartData = scope.panel.other ? chartData :
  161. _.without(chartData,_.findWhere(chartData,{meta:'other'}));
  162. // Populate element.
  163. scripts.wait(function(){
  164. // Populate element
  165. try {
  166. // Add plot to scope so we can build out own legend
  167. if(scope.panel.chart === 'bar') {
  168. plot = $.plot(elem, chartData, {
  169. legend: { show: false },
  170. series: {
  171. lines: { show: false, },
  172. bars: { show: true, fill: 1, barWidth: 0.8, horizontal: false },
  173. shadowSize: 1
  174. },
  175. yaxis: { show: true, min: 0, color: "#c8c8c8" },
  176. xaxis: { show: false },
  177. grid: {
  178. borderWidth: 0,
  179. borderColor: '#eee',
  180. color: "#eee",
  181. hoverable: true,
  182. clickable: true
  183. },
  184. colors: querySrv.colors
  185. });
  186. }
  187. if(scope.panel.chart === 'pie') {
  188. var labelFormat = function(label, series){
  189. return '<div ng-click="build_search(panel.field,\''+label+'\')'+
  190. ' "style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  191. label+'<br/>'+Math.round(series.percent)+'%</div>';
  192. };
  193. plot = $.plot(elem, chartData, {
  194. legend: { show: false },
  195. series: {
  196. pie: {
  197. innerRadius: scope.panel.donut ? 0.4 : 0,
  198. tilt: scope.panel.tilt ? 0.45 : 1,
  199. radius: 1,
  200. show: true,
  201. combine: {
  202. color: '#999',
  203. label: 'The Rest'
  204. },
  205. stroke: {
  206. width: 0
  207. },
  208. label: {
  209. show: scope.panel.labels,
  210. radius: 2/3,
  211. formatter: labelFormat,
  212. threshold: 0.1
  213. }
  214. }
  215. },
  216. //grid: { hoverable: true, clickable: true },
  217. grid: { hoverable: true, clickable: true },
  218. colors: querySrv.colors
  219. });
  220. }
  221. // Populate legend
  222. if(elem.is(":visible")){
  223. scripts.wait(function(){
  224. scope.legend = plot.getData();
  225. if(!scope.$$phase) {
  226. scope.$apply();
  227. }
  228. });
  229. }
  230. } catch(e) {
  231. elem.text(e);
  232. }
  233. });
  234. }
  235. elem.bind("plotclick", function (event, pos, object) {
  236. if(object) {
  237. scope.build_search(scope.data[object.seriesIndex]);
  238. }
  239. });
  240. var $tooltip = $('<div>');
  241. elem.bind("plothover", function (event, pos, item) {
  242. if (item) {
  243. var value = scope.panel.chart === 'bar' ? item.datapoint[1] : item.datapoint[1][0][1];
  244. $tooltip
  245. .html(
  246. kbn.query_color_dot(item.series.color, 20) + ' ' +
  247. item.series.label + " (" + value.toFixed(0)+")"
  248. )
  249. .place_tt(pos.pageX, pos.pageY);
  250. } else {
  251. $tooltip.remove();
  252. }
  253. });
  254. }
  255. };
  256. });