module.js 9.5 KB

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