module.js 9.6 KB

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