module.js 8.8 KB

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