module.js 8.1 KB

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