module.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. editorTabs : [
  31. {title:'Queries', src:'partials/querySelect.html'}
  32. ],
  33. query : { field:"_type", goal: 100},
  34. queries : {
  35. mode : 'all',
  36. ids : []
  37. },
  38. size : 10,
  39. exclude : [],
  40. donut : false,
  41. tilt : false,
  42. legend : "above",
  43. labels : true,
  44. mode : "terms",
  45. default_field : 'DEFAULT',
  46. spyable : true,
  47. };
  48. _.defaults($scope.panel,_d);
  49. $scope.init = function() {
  50. $scope.$on('refresh',function(){$scope.get_data();});
  51. $scope.get_data();
  52. };
  53. $scope.set_mode = function(mode) {
  54. switch(mode)
  55. {
  56. case 'terms':
  57. $scope.panel.query = {field:"_all"};
  58. break;
  59. case 'goal':
  60. $scope.panel.query = {goal:100};
  61. break;
  62. }
  63. };
  64. $scope.set_refresh = function (state) {
  65. $scope.refresh = state;
  66. };
  67. $scope.close_edit = function() {
  68. if($scope.refresh) {
  69. $scope.get_data();
  70. }
  71. $scope.refresh = false;
  72. $scope.$emit('render');
  73. };
  74. $scope.get_data = function() {
  75. // Make sure we have everything for the request to complete
  76. if(dashboard.indices.length === 0) {
  77. return;
  78. }
  79. $scope.panelMeta.loading = true;
  80. var request = $scope.ejs.Request().indices(dashboard.indices);
  81. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  82. // This could probably be changed to a BoolFilter
  83. var boolQuery = $scope.ejs.BoolQuery();
  84. _.each($scope.panel.queries.ids,function(id) {
  85. boolQuery = boolQuery.should(querySrv.getEjsObj(id));
  86. });
  87. var results;
  88. // Terms mode
  89. if ($scope.panel.mode === "terms") {
  90. request = request
  91. .facet($scope.ejs.TermsFacet('pie')
  92. .field($scope.panel.query.field || $scope.panel.default_field)
  93. .size($scope.panel.size)
  94. .exclude($scope.panel.exclude)
  95. .facetFilter($scope.ejs.QueryFilter(
  96. $scope.ejs.FilteredQuery(
  97. boolQuery,
  98. filterSrv.getBoolFilter(filterSrv.ids)
  99. )))).size(0);
  100. $scope.populate_modal(request);
  101. results = request.doSearch();
  102. // Populate scope when we have results
  103. results.then(function(results) {
  104. $scope.panelMeta.loading = false;
  105. $scope.hits = results.hits.total;
  106. $scope.data = [];
  107. var k = 0;
  108. _.each(results.facets.pie.terms, function(v) {
  109. var slice = { label : v.term, data : v.count };
  110. $scope.data.push();
  111. $scope.data.push(slice);
  112. k = k + 1;
  113. });
  114. $scope.$emit('render');
  115. });
  116. // Goal mode
  117. } else {
  118. request = request
  119. .query(boolQuery)
  120. .filter(filterSrv.getBoolFilter(filterSrv.ids))
  121. .size(0);
  122. $scope.populate_modal(request);
  123. results = request.doSearch();
  124. results.then(function(results) {
  125. $scope.panelMeta.loading = false;
  126. var complete = results.hits.total;
  127. var remaining = $scope.panel.query.goal - complete;
  128. $scope.data = [
  129. { label : 'Complete', data : complete, color: '#BF6730' },
  130. { data : remaining, color: '#e2d0c4' }
  131. ];
  132. $scope.$emit('render');
  133. });
  134. }
  135. };
  136. // I really don't like this function, too much dom manip. Break out into directive?
  137. $scope.populate_modal = function(request) {
  138. $scope.modal = {
  139. title: "Inspector",
  140. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  141. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  142. angular.toJson(JSON.parse(request.toString()),true)+
  143. "'</pre>",
  144. };
  145. };
  146. })
  147. .directive('pie', function(querySrv, filterSrv, dashboard) {
  148. return {
  149. restrict: 'A',
  150. link: function(scope, elem, attrs) {
  151. elem.html('<center><img src="common/img/load_big.gif"></center>');
  152. // Receive render events
  153. scope.$on('render',function(){
  154. render_panel();
  155. });
  156. // Or if the window is resized
  157. angular.element(window).bind('resize', function(){
  158. render_panel();
  159. });
  160. // Function for rendering panel
  161. function render_panel() {
  162. // IE doesn't work without this
  163. elem.css({height:scope.panel.height||scope.row.height});
  164. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  165. .script("common/lib/panels/jquery.flot.pie.js");
  166. var label;
  167. if(scope.panel.mode === 'goal') {
  168. label = {
  169. show: scope.panel.labels,
  170. radius: 0,
  171. formatter: function(label, series){
  172. var font = parseInt(scope.row.height.replace('px',''),10)/8 + String('px');
  173. if(!(_.isUndefined(label))) {
  174. return '<div style="font-size:'+font+';font-weight:bold;text-align:center;padding:2px;color:#fff;">'+
  175. Math.round(series.percent)+'%</div>';
  176. } else {
  177. return '';
  178. }
  179. },
  180. };
  181. } else {
  182. label = {
  183. show: scope.panel.labels,
  184. radius: 2/3,
  185. formatter: function(label, series){
  186. return '<div "style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  187. label+'<br/>'+Math.round(series.percent)+'%</div>';
  188. },
  189. threshold: 0.1
  190. };
  191. }
  192. var pie = {
  193. series: {
  194. pie: {
  195. innerRadius: scope.panel.donut ? 0.45 : 0,
  196. tilt: scope.panel.tilt ? 0.45 : 1,
  197. radius: 1,
  198. show: true,
  199. combine: {
  200. color: '#999',
  201. label: 'The Rest'
  202. },
  203. label: label,
  204. stroke: {
  205. width: 0
  206. }
  207. }
  208. },
  209. //grid: { hoverable: true, clickable: true },
  210. grid: {
  211. backgroundColor: null,
  212. hoverable: true,
  213. clickable: true
  214. },
  215. legend: { show: false },
  216. colors: querySrv.colors
  217. };
  218. // Populate legend
  219. if(elem.is(":visible")){
  220. scripts.wait(function(){
  221. scope.legend = $.plot(elem, scope.data, pie).getData();
  222. if(!scope.$$phase) {
  223. scope.$apply();
  224. }
  225. });
  226. }
  227. }
  228. elem.bind('plotclick', function (event, pos, object) {
  229. if (!object) {
  230. return;
  231. }
  232. if(scope.panel.mode === 'terms') {
  233. filterSrv.set({type:'terms',field:scope.panel.query.field,value:object.series.label});
  234. dashboard.refresh();
  235. }
  236. });
  237. var $tooltip = $('<div>');
  238. elem.bind('plothover', function (event, pos, item) {
  239. if (item) {
  240. $tooltip
  241. .html([
  242. kbn.query_color_dot(item.series.color, 15),
  243. (item.series.label || ''),
  244. parseFloat(item.series.percent).toFixed(1) + '%'
  245. ].join(' '))
  246. .place_tt(pos.pageX, pos.pageY, {
  247. offset: 10
  248. });
  249. } else {
  250. $tooltip.remove();
  251. }
  252. });
  253. }
  254. };
  255. });