module.js 7.9 KB

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