module.js 9.0 KB

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