module.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Pie
  5. ### Parameters
  6. * query :: An object with 2 possible parameters depends on the mode:
  7. ** field: Fields to run a terms facet on. Only does anything in terms mode
  8. ** goal: How many to shoot for, only does anything in goal mode
  9. * exclude :: In terms mode, ignore these terms
  10. * donut :: Drill a big hole in the pie
  11. * tilt :: A janky 3D representation of the pie. Looks terrible 90% of the time.
  12. * legend :: Show the legend?
  13. * labels :: Label the slices of the pie?
  14. * mode :: 'terms' or 'goal'
  15. * default_field :: LOL wat? A dumb fail over field if for some reason the query object
  16. doesn't have a field
  17. * spyable :: Show the 'eye' icon that displays the last ES query for this panel
  18. */
  19. 'use strict';
  20. angular.module('kibana.pie', [])
  21. .controller('pie', function($scope, $rootScope, querySrv, dashboard, filterSrv) {
  22. $scope.panelMeta = {
  23. status : "Deprecated",
  24. description : "Uses an Elasticsearch terms facet to create a pie chart. You should really only"+
  25. " point this at not_analyzed fields for that reason. This panel is going away soon, it has"+
  26. " <strong>been replaced by the terms panel</strong>. Please use that one instead."
  27. };
  28. // Set and populate defaults
  29. var _d = {
  30. query : { field:"_type", goal: 100},
  31. queries : {
  32. mode : 'all',
  33. ids : []
  34. },
  35. size : 10,
  36. exclude : [],
  37. donut : false,
  38. tilt : false,
  39. legend : "above",
  40. labels : true,
  41. mode : "terms",
  42. default_field : 'DEFAULT',
  43. spyable : true,
  44. };
  45. _.defaults($scope.panel,_d);
  46. $scope.init = function() {
  47. $scope.$on('refresh',function(){$scope.get_data();});
  48. $scope.get_data();
  49. };
  50. $scope.set_mode = function(mode) {
  51. switch(mode)
  52. {
  53. case 'terms':
  54. $scope.panel.query = {field:"_all"};
  55. break;
  56. case 'goal':
  57. $scope.panel.query = {goal:100};
  58. break;
  59. }
  60. };
  61. $scope.set_refresh = function (state) {
  62. $scope.refresh = state;
  63. };
  64. $scope.close_edit = function() {
  65. if($scope.refresh) {
  66. $scope.get_data();
  67. }
  68. $scope.refresh = false;
  69. $scope.$emit('render');
  70. };
  71. $scope.get_data = function() {
  72. // Make sure we have everything for the request to complete
  73. if(dashboard.indices.length === 0) {
  74. return;
  75. }
  76. $scope.panelMeta.loading = true;
  77. var request = $scope.ejs.Request().indices(dashboard.indices);
  78. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  79. // This could probably be changed to a BoolFilter
  80. var boolQuery = $scope.ejs.BoolQuery();
  81. _.each($scope.panel.queries.ids,function(id) {
  82. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  83. });
  84. var results;
  85. // Terms mode
  86. if ($scope.panel.mode === "terms") {
  87. request = request
  88. .facet($scope.ejs.TermsFacet('pie')
  89. .field($scope.panel.query.field || $scope.panel.default_field)
  90. .size($scope.panel.size)
  91. .exclude($scope.panel.exclude)
  92. .facetFilter($scope.ejs.QueryFilter(
  93. $scope.ejs.FilteredQuery(
  94. boolQuery,
  95. filterSrv.getBoolFilter(filterSrv.ids)
  96. )))).size(0);
  97. $scope.populate_modal(request);
  98. results = request.doSearch();
  99. // Populate scope when we have results
  100. results.then(function(results) {
  101. $scope.panelMeta.loading = false;
  102. $scope.hits = results.hits.total;
  103. $scope.data = [];
  104. var k = 0;
  105. _.each(results.facets.pie.terms, function(v) {
  106. var slice = { label : v.term, data : v.count };
  107. $scope.data.push();
  108. $scope.data.push(slice);
  109. k = k + 1;
  110. });
  111. $scope.$emit('render');
  112. });
  113. // Goal mode
  114. } else {
  115. request = request
  116. .query(boolQuery)
  117. .filter(filterSrv.getBoolFilter(filterSrv.ids))
  118. .size(0);
  119. $scope.populate_modal(request);
  120. results = request.doSearch();
  121. results.then(function(results) {
  122. $scope.panelMeta.loading = false;
  123. var complete = results.hits.total;
  124. var remaining = $scope.panel.query.goal - complete;
  125. $scope.data = [
  126. { label : 'Complete', data : complete, color: '#BF6730' },
  127. { data : remaining, color: '#e2d0c4' }
  128. ];
  129. $scope.$emit('render');
  130. });
  131. }
  132. };
  133. // I really don't like this function, too much dom manip. Break out into directive?
  134. $scope.populate_modal = function(request) {
  135. $scope.modal = {
  136. title: "Inspector",
  137. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  138. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  139. angular.toJson(JSON.parse(request.toString()),true)+
  140. "'</pre>",
  141. };
  142. };
  143. })
  144. .directive('pie', function(querySrv, filterSrv, dashboard) {
  145. return {
  146. restrict: 'A',
  147. link: function(scope, elem, attrs) {
  148. elem.html('<center><img src="common/img/load_big.gif"></center>');
  149. // Receive render events
  150. scope.$on('render',function(){
  151. render_panel();
  152. });
  153. // Or if the window is resized
  154. angular.element(window).bind('resize', function(){
  155. render_panel();
  156. });
  157. // Function for rendering panel
  158. function render_panel() {
  159. // IE doesn't work without this
  160. elem.css({height:scope.panel.height||scope.row.height});
  161. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  162. .script("common/lib/panels/jquery.flot.pie.js");
  163. var label;
  164. if(scope.panel.mode === 'goal') {
  165. label = {
  166. show: scope.panel.labels,
  167. radius: 0,
  168. formatter: function(label, series){
  169. var font = parseInt(scope.row.height.replace('px',''),10)/8 + String('px');
  170. if(!(_.isUndefined(label))) {
  171. return '<div style="font-size:'+font+';font-weight:bold;text-align:center;padding:2px;color:#fff;">'+
  172. Math.round(series.percent)+'%</div>';
  173. } else {
  174. return '';
  175. }
  176. },
  177. };
  178. } else {
  179. label = {
  180. show: scope.panel.labels,
  181. radius: 2/3,
  182. formatter: function(label, series){
  183. return '<div "style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  184. label+'<br/>'+Math.round(series.percent)+'%</div>';
  185. },
  186. threshold: 0.1
  187. };
  188. }
  189. var pie = {
  190. series: {
  191. pie: {
  192. innerRadius: scope.panel.donut ? 0.45 : 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. label: label,
  201. stroke: {
  202. width: 0
  203. }
  204. }
  205. },
  206. //grid: { hoverable: true, clickable: true },
  207. grid: {
  208. backgroundColor: null,
  209. hoverable: true,
  210. clickable: true
  211. },
  212. legend: { show: false },
  213. colors: querySrv.colors
  214. };
  215. // Populate legend
  216. if(elem.is(":visible")){
  217. scripts.wait(function(){
  218. scope.legend = $.plot(elem, scope.data, pie).getData();
  219. if(!scope.$$phase) {
  220. scope.$apply();
  221. }
  222. });
  223. }
  224. }
  225. function tt(x, y, contents) {
  226. var tooltip = $('#pie-tooltip').length ?
  227. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  228. tooltip.html(contents).css({
  229. position: 'absolute',
  230. top : y + 10,
  231. left : x + 10,
  232. color : "#c8c8c8",
  233. padding : '10px',
  234. 'font-size': '11pt',
  235. 'font-weight' : 200,
  236. 'background-color': '#1f1f1f',
  237. 'border-radius': '5px',
  238. }).appendTo("body");
  239. }
  240. elem.bind("plotclick", function (event, pos, object) {
  241. if (!object) {
  242. return;
  243. }
  244. if(scope.panel.mode === 'terms') {
  245. filterSrv.set({type:'terms',field:scope.panel.query.field,value:object.series.label});
  246. dashboard.refresh();
  247. }
  248. });
  249. elem.bind("plothover", function (event, pos, item) {
  250. if (item) {
  251. var percent = parseFloat(item.series.percent).toFixed(1) + "%";
  252. tt(pos.pageX, pos.pageY, "<div "+"style='vertical-align:middle;display:inline-block;"+
  253. "background:"+item.series.color+";height:15px;width:15px;border-radius:10px;'></div> "+
  254. (item.series.label||"")+ " " + percent);
  255. } else {
  256. $("#pie-tooltip").remove();
  257. }
  258. });
  259. }
  260. };
  261. });