module.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. ### Group Events
  14. #### Sends
  15. * get_time :: On panel initialization get time range to query
  16. #### Receives
  17. * time :: An object containing the time range to use and the index(es) to query
  18. * query :: An Array of queries, even if its only one
  19. */
  20. angular.module('kibana.hits', [])
  21. .controller('hits', function($scope, eventBus, query, dashboard, filterSrv) {
  22. // Set and populate defaults
  23. var _d = {
  24. status : "Beta",
  25. query : ["*"],
  26. group : "default",
  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.panel.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. // Build the question part of the query
  53. _.each(query.ids, function(id) {
  54. var _q = $scope.ejs.FilteredQuery(
  55. ejs.QueryStringQuery(query.list[id].query || '*'),
  56. filterSrv.getBoolFilter(filterSrv.ids));
  57. request = request
  58. .facet($scope.ejs.QueryFacet(id)
  59. .query(_q)
  60. ).size(0)
  61. });
  62. // TODO: Spy for hits panel
  63. //$scope.populate_modal(request);
  64. // Then run it
  65. var results = request.doSearch();
  66. // Populate scope when we have results
  67. results.then(function(results) {
  68. $scope.panel.loading = false;
  69. if(_segment == 0) {
  70. $scope.hits = 0;
  71. $scope.data = [];
  72. query_id = $scope.query_id = new Date().getTime();
  73. }
  74. // Check for error and abort if found
  75. if(!(_.isUndefined(results.error))) {
  76. $scope.panel.error = $scope.parse_error(results.error);
  77. return;
  78. }
  79. // Convert facet ids to numbers
  80. var facetIds = _.map(_.keys(results.facets),function(k){return parseInt(k);})
  81. // Make sure we're still on the same query/queries
  82. if($scope.query_id === query_id &&
  83. _.intersection(facetIds,query.ids).length == query.ids.length
  84. ) {
  85. var i = 0;
  86. _.each(query.ids, function(id) {
  87. var v = results.facets[id]
  88. var hits = _.isUndefined($scope.data[i]) || _segment == 0 ?
  89. v.count : $scope.data[i].hits+v.count
  90. $scope.hits += v.count
  91. // Create series
  92. $scope.data[i] = {
  93. info: query.list[id],
  94. id: id,
  95. hits: hits,
  96. data: [[i,hits]]
  97. };
  98. i++;
  99. });
  100. $scope.$emit('render');
  101. if(_segment < dashboard.indices.length-1)
  102. $scope.get_data(_segment+1,query_id)
  103. }
  104. });
  105. }
  106. $scope.set_refresh = function (state) {
  107. $scope.refresh = state;
  108. }
  109. $scope.close_edit = function() {
  110. if($scope.refresh)
  111. $scope.get_data();
  112. $scope.refresh = false;
  113. $scope.$emit('render');
  114. }
  115. function set_time(time) {
  116. $scope.time = time;
  117. $scope.get_data();
  118. }
  119. }).directive('hitsChart', function(eventBus, query) {
  120. return {
  121. restrict: 'A',
  122. link: function(scope, elem, attrs, ctrl) {
  123. // Receive render events
  124. scope.$on('render',function(){
  125. render_panel();
  126. });
  127. // Re-render if the window is resized
  128. angular.element(window).bind('resize', function(){
  129. render_panel();
  130. });
  131. // Function for rendering panel
  132. function render_panel() {
  133. try {
  134. _.each(scope.data,function(series) {
  135. series.label = series.info.alias,
  136. series.color = series.info.color
  137. })
  138. } catch(e) {return}
  139. var scripts = $LAB.script("common/lib/panels/jquery.flot.js").wait()
  140. .script("common/lib/panels/jquery.flot.pie.js")
  141. // Populate element.
  142. scripts.wait(function(){
  143. // Populate element
  144. try {
  145. // Add plot to scope so we can build out own legend
  146. if(scope.panel.chart === 'bar')
  147. scope.plot = $.plot(elem, scope.data, {
  148. legend: { show: false },
  149. series: {
  150. lines: { show: false, },
  151. bars: { show: true, fill: 1, barWidth: 0.8, horizontal: false },
  152. shadowSize: 1
  153. },
  154. yaxis: { show: true, min: 0, color: "#c8c8c8" },
  155. xaxis: { show: false },
  156. grid: {
  157. borderWidth: 0,
  158. borderColor: '#eee',
  159. color: "#eee",
  160. hoverable: true,
  161. },
  162. colors: query.colors
  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. // Compensate for the height of the legend. Gross
  196. elem.height(
  197. (scope.panel.height || scope.row.height).replace('px','') - $("#"+scope.$id+"-legend").height())
  198. // Work around for missing legend at initialization
  199. if(!scope.$$phase)
  200. scope.$apply()
  201. } catch(e) {
  202. elem.text(e)
  203. }
  204. })
  205. }
  206. function tt(x, y, contents) {
  207. var tooltip = $('#pie-tooltip').length ?
  208. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  209. //var tooltip = $('#pie-tooltip')
  210. tooltip.html(contents).css({
  211. position: 'absolute',
  212. top : y + 5,
  213. left : x + 5,
  214. color : "#c8c8c8",
  215. padding : '10px',
  216. 'font-size': '11pt',
  217. 'font-weight' : 200,
  218. 'background-color': '#1f1f1f',
  219. 'border-radius': '5px',
  220. }).appendTo("body");
  221. }
  222. elem.bind("plothover", function (event, pos, item) {
  223. if (item) {
  224. var value = scope.panel.chart === 'bar' ?
  225. item.datapoint[1] : item.datapoint[1][0][1];
  226. tt(pos.pageX, pos.pageY,
  227. "<div style='vertical-align:middle;border-radius:10px;display:inline-block;background:"+
  228. item.series.color+";height:20px;width:20px'></div> "+value.toFixed(0))
  229. } else {
  230. $("#pie-tooltip").remove();
  231. }
  232. });
  233. }
  234. };
  235. })