module.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. ## Pie
  3. This panel is probably going away. For now its has 2 modes:
  4. * terms: Run a terms facet on the query. You're gonna have a bad (ES crashing) day
  5. if you run in this mode on a high cardinality field
  6. * goal: Compare the query to this number and display the percentage that the query
  7. represents
  8. ### Parameters
  9. * query :: An object with 3 possible parameters depends on the mode:
  10. ** field: Fields to run a terms facet on. Only does anything in terms mode
  11. ** query: A string of the query to run
  12. ** goal: How many to shoot for, only does anything in goal mode
  13. * exclude :: In terms mode, ignore these terms
  14. * donut :: Drill a big hole in the pie
  15. * tilt :: A janky 3D representation of the pie. Looks terrible 90% of the time.
  16. * legend :: Show the legend?
  17. * labels :: Label the slices of the pie?
  18. * mode :: 'terms' or 'goal'
  19. * default_field :: LOL wat? A dumb fail over field if for some reason the query object
  20. doesn't have a field
  21. * spyable :: Show the 'eye' icon that displays the last ES query for this panel
  22. ### Group Events
  23. #### Sends
  24. * get_time :: On panel initialization get time range to query
  25. #### Receives
  26. * time :: An object containing the time range to use and the index(es) to query
  27. * query :: An Array of queries, this panel will use the first in the array
  28. */
  29. angular.module('kibana.pie', [])
  30. .controller('pie', function($scope, $rootScope, eventBus, query, dashboard, filterSrv) {
  31. // Set and populate defaults
  32. var _d = {
  33. status : "Deprecating Soon",
  34. query : { field:"_type", goal: 100},
  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. $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.panel.loading = true;
  77. var request = $scope.ejs.Request().indices(dashboard.indices);
  78. // This could probably be changed to a BoolFilter
  79. var boolQuery = ejs.BoolQuery();
  80. _.each(query.list,function(q) {
  81. boolQuery = boolQuery.should(ejs.QueryStringQuery(q.query || '*'))
  82. })
  83. // Terms mode
  84. if ($scope.panel.mode == "terms") {
  85. request = request
  86. .facet(ejs.TermsFacet('pie')
  87. .field($scope.panel.query.field || $scope.panel.default_field)
  88. .size($scope.panel['size'])
  89. .exclude($scope.panel.exclude)
  90. .facetFilter(ejs.QueryFilter(
  91. ejs.FilteredQuery(
  92. boolQuery,
  93. filterSrv.getBoolFilter(filterSrv.ids)
  94. )))).size(0)
  95. $scope.populate_modal(request);
  96. var results = request.doSearch();
  97. // Populate scope when we have results
  98. results.then(function(results) {
  99. $scope.panel.loading = false;
  100. $scope.hits = results.hits.total;
  101. $scope.data = [];
  102. var k = 0;
  103. _.each(results.facets.pie.terms, function(v) {
  104. var slice = { label : v.term, data : v.count };
  105. $scope.data.push();
  106. if(!(_.isUndefined($scope.panel.colors))
  107. && _.isArray($scope.panel.colors)
  108. && $scope.panel.colors.length > 0) {
  109. slice.color = $scope.panel.colors[k%$scope.panel.colors.length];
  110. }
  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. var results = request.doSearch();
  124. results.then(function(results) {
  125. $scope.panel.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. $scope.$emit('render');
  132. });
  133. }
  134. }
  135. // I really don't like this function, too much dom manip. Break out into directive?
  136. $scope.populate_modal = function(request) {
  137. $scope.modal = {
  138. title: "Inspector",
  139. body : "<h5>Last Elasticsearch Query</h5><pre>"+
  140. 'curl -XGET '+config.elasticsearch+'/'+dashboard.indices+"/_search?pretty -d'\n"+
  141. angular.toJson(JSON.parse(request.toString()),true)+
  142. "'</pre>",
  143. }
  144. }
  145. })
  146. .directive('pie', function(query, filterSrv, dashboard) {
  147. return {
  148. restrict: 'A',
  149. link: function(scope, elem, attrs) {
  150. elem.html('<center><img src="common/img/load_big.gif"></center>')
  151. // Receive render events
  152. scope.$on('render',function(){
  153. render_panel();
  154. });
  155. // Or if the window is resized
  156. angular.element(window).bind('resize', function(){
  157. render_panel();
  158. });
  159. // Function for rendering panel
  160. function render_panel() {
  161. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  162. .script("common/lib/panels/jquery.flot.pie.js")
  163. if(scope.panel.mode === 'goal')
  164. var label = {
  165. show: scope.panel.labels,
  166. radius: 0,
  167. formatter: function(label, series){
  168. var font = parseInt(scope.row.height.replace('px',''))/8 + String('px')
  169. if(!(_.isUndefined(label)))
  170. return '<div style="font-size:'+font+';font-weight:bold;text-align:center;padding:2px;color:#fff;">'+
  171. Math.round(series.percent)+'%</div>';
  172. else
  173. return ''
  174. },
  175. }
  176. else
  177. var label = {
  178. show: scope.panel.labels,
  179. radius: 2/3,
  180. formatter: function(label, series){
  181. return '<div "style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  182. series.info.alias+'<br/>'+Math.round(series.percent)+'%</div>';
  183. },
  184. threshold: 0.1
  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: query.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. })