module.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Hits
  5. ### Parameters
  6. * style :: A hash of css styles
  7. * arrangement :: How should I arrange the query results? 'horizontal' or 'vertical'
  8. * chart :: Show a chart? 'none', 'bar', 'pie'
  9. * donut :: Only applies to 'pie' charts. Punches a hole in the chart for some reason
  10. * tilt :: Only 'pie' charts. Janky 3D effect. Looks terrible 90% of the time.
  11. * lables :: Only 'pie' charts. Labels on the pie?
  12. */
  13. 'use strict';
  14. angular.module('kibana.hits', [])
  15. .controller('hits', function($scope, querySrv, dashboard, filterSrv) {
  16. $scope.panelMeta = {
  17. status : "Stable",
  18. description : "The total hits for a query or set of queries. Can be a pie chart, bar chart, "+
  19. "list, or absolute total of all queries combined"
  20. };
  21. // Set and populate defaults
  22. var _d = {
  23. queries : {
  24. mode : 'all',
  25. ids : []
  26. },
  27. style : { "font-size": '10pt'},
  28. arrangement : 'horizontal',
  29. chart : 'bar',
  30. counter_pos : 'above',
  31. donut : false,
  32. tilt : false,
  33. labels : true
  34. };
  35. _.defaults($scope.panel,_d);
  36. $scope.init = function () {
  37. $scope.hits = 0;
  38. $scope.$on('refresh',function(){
  39. $scope.get_data();
  40. });
  41. $scope.get_data();
  42. };
  43. $scope.get_data = function(segment,query_id) {
  44. delete $scope.panel.error;
  45. $scope.panelMeta.loading = true;
  46. // Make sure we have everything for the request to complete
  47. if(dashboard.indices.length === 0) {
  48. return;
  49. }
  50. var _segment = _.isUndefined(segment) ? 0 : segment;
  51. var request = $scope.ejs.Request().indices(dashboard.indices[_segment]);
  52. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  53. // Build the question part of the query
  54. _.each($scope.panel.queries.ids, function(id) {
  55. var _q = $scope.ejs.FilteredQuery(
  56. querySrv.getEjsObj(id),
  57. filterSrv.getBoolFilter(filterSrv.ids));
  58. request = request
  59. .facet($scope.ejs.QueryFacet(id)
  60. .query(_q)
  61. ).size(0);
  62. });
  63. // TODO: Spy for hits panel
  64. //$scope.populate_modal(request);
  65. // Then run it
  66. var results = request.doSearch();
  67. // Populate scope when we have results
  68. results.then(function(results) {
  69. $scope.panelMeta.loading = false;
  70. if(_segment === 0) {
  71. $scope.hits = 0;
  72. $scope.data = [];
  73. query_id = $scope.query_id = new Date().getTime();
  74. }
  75. // Check for error and abort if found
  76. if(!(_.isUndefined(results.error))) {
  77. $scope.panel.error = $scope.parse_error(results.error);
  78. return;
  79. }
  80. // Convert facet ids to numbers
  81. var facetIds = _.map(_.keys(results.facets),function(k){return parseInt(k, 10);});
  82. // Make sure we're still on the same query/queries
  83. if($scope.query_id === query_id &&
  84. _.intersection(facetIds,$scope.panel.queries.ids).length === $scope.panel.queries.ids.length
  85. ) {
  86. var i = 0;
  87. _.each($scope.panel.queries.ids, function(id) {
  88. var v = results.facets[id];
  89. var hits = _.isUndefined($scope.data[i]) || _segment === 0 ?
  90. v.count : $scope.data[i].hits+v.count;
  91. $scope.hits += v.count;
  92. // Create series
  93. $scope.data[i] = {
  94. info: querySrv.list[id],
  95. id: id,
  96. hits: hits,
  97. data: [[i,hits]]
  98. };
  99. i++;
  100. });
  101. $scope.$emit('render');
  102. if(_segment < dashboard.indices.length-1) {
  103. $scope.get_data(_segment+1,query_id);
  104. }
  105. }
  106. });
  107. };
  108. $scope.set_refresh = function (state) {
  109. $scope.refresh = state;
  110. };
  111. $scope.close_edit = function() {
  112. if($scope.refresh) {
  113. $scope.get_data();
  114. }
  115. $scope.refresh = false;
  116. $scope.$emit('render');
  117. };
  118. function set_time(time) {
  119. $scope.time = time;
  120. $scope.get_data();
  121. }
  122. }).directive('hitsChart', function(querySrv) {
  123. return {
  124. restrict: 'A',
  125. link: function(scope, elem, attrs, ctrl) {
  126. // Receive render events
  127. scope.$on('render',function(){
  128. render_panel();
  129. });
  130. // Re-render if the window is resized
  131. angular.element(window).bind('resize', function(){
  132. render_panel();
  133. });
  134. // Function for rendering panel
  135. function render_panel() {
  136. // IE doesn't work without this
  137. elem.css({height:scope.panel.height||scope.row.height});
  138. try {
  139. _.each(scope.data,function(series) {
  140. series.label = series.info.alias;
  141. series.color = series.info.color;
  142. });
  143. } catch(e) {return;}
  144. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  145. .script("common/lib/panels/jquery.flot.pie.js");
  146. // Populate element.
  147. scripts.wait(function(){
  148. // Populate element
  149. try {
  150. // Add plot to scope so we can build out own legend
  151. if(scope.panel.chart === 'bar') {
  152. scope.plot = $.plot(elem, scope.data, {
  153. legend: { show: false },
  154. series: {
  155. lines: { show: false, },
  156. bars: { show: true, fill: 1, barWidth: 0.8, horizontal: false },
  157. shadowSize: 1
  158. },
  159. yaxis: { show: true, min: 0, color: "#c8c8c8" },
  160. xaxis: { show: false },
  161. grid: {
  162. borderWidth: 0,
  163. borderColor: '#eee',
  164. color: "#eee",
  165. hoverable: true,
  166. },
  167. colors: querySrv.colors
  168. });
  169. }
  170. if(scope.panel.chart === 'pie') {
  171. scope.plot = $.plot(elem, scope.data, {
  172. legend: { show: false },
  173. series: {
  174. pie: {
  175. innerRadius: scope.panel.donut ? 0.4 : 0,
  176. tilt: scope.panel.tilt ? 0.45 : 1,
  177. radius: 1,
  178. show: true,
  179. combine: {
  180. color: '#999',
  181. label: 'The Rest'
  182. },
  183. stroke: {
  184. width: 0
  185. },
  186. label: {
  187. show: scope.panel.labels,
  188. radius: 2/3,
  189. formatter: function(label, series){
  190. return '<div ng-click="build_search(panel.query.field,\''+label+'\')'+
  191. ' "style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  192. label+'<br/>'+Math.round(series.percent)+'%</div>';
  193. },
  194. threshold: 0.1
  195. }
  196. }
  197. },
  198. //grid: { hoverable: true, clickable: true },
  199. grid: { hoverable: true, clickable: true },
  200. colors: querySrv.colors
  201. });
  202. }
  203. } catch(e) {
  204. elem.text(e);
  205. }
  206. });
  207. }
  208. function tt(x, y, contents) {
  209. var tooltip = $('#pie-tooltip').length ?
  210. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  211. //var tooltip = $('#pie-tooltip')
  212. tooltip.html(contents).css({
  213. position: 'absolute',
  214. top : y + 5,
  215. left : x + 5,
  216. color : "#c8c8c8",
  217. padding : '10px',
  218. 'font-size': '11pt',
  219. 'font-weight' : 200,
  220. 'background-color': '#1f1f1f',
  221. 'border-radius': '5px',
  222. }).appendTo("body");
  223. }
  224. elem.bind("plothover", function (event, pos, item) {
  225. if (item) {
  226. var value = scope.panel.chart === 'bar' ?
  227. item.datapoint[1] : item.datapoint[1][0][1];
  228. tt(pos.pageX, pos.pageY,
  229. "<div style='vertical-align:middle;border-radius:10px;display:inline-block;background:"+
  230. item.series.color+";height:20px;width:20px'></div> "+value.toFixed(0));
  231. } else {
  232. $("#pie-tooltip").remove();
  233. }
  234. });
  235. }
  236. };
  237. });