module.js 8.8 KB

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