module.js 8.7 KB

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