module.js 8.1 KB

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