module.js 8.7 KB

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