module.js 7.6 KB

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