module.js 8.9 KB

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