module.js 8.5 KB

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