module.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. angular.module('kibana.hits', [])
  2. .controller('hits', function($scope, eventBus) {
  3. // Set and populate defaults
  4. var _d = {
  5. query : "*",
  6. group : "default",
  7. style : { "font-size": '10pt'},
  8. aggregate : true,
  9. arrangement : 'vertical',
  10. chart : true,
  11. counters: true,
  12. count_pos: 'above'
  13. }
  14. _.defaults($scope.panel,_d)
  15. $scope.init = function () {
  16. $scope.hits = 0;
  17. eventBus.register($scope,'time', function(event,time){
  18. set_time(time)
  19. });
  20. eventBus.register($scope,'query', function(event, query) {
  21. $scope.panel.query = _.map(query,function(q) {
  22. return {query: q, label: q};
  23. })
  24. $scope.get_data();
  25. });
  26. // Now that we're all setup, request the time from our group
  27. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  28. }
  29. $scope.get_data = function(segment,query_id) {
  30. delete $scope.panel.error
  31. $scope.panel.loading = true;
  32. // Make sure we have everything for the request to complete
  33. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.time))
  34. return
  35. var _segment = _.isUndefined(segment) ? 0 : segment
  36. var request = $scope.ejs.Request().indices($scope.panel.index[_segment]);
  37. // Build the question part of the query
  38. var queries = [];
  39. _.each($scope.panel.query, function(v) {
  40. queries.push($scope.ejs.FilteredQuery(
  41. ejs.QueryStringQuery(v.query || '*'),
  42. ejs.RangeFilter($scope.time.field)
  43. .from($scope.time.from)
  44. .to($scope.time.to))
  45. )
  46. });
  47. // Build the facet part
  48. _.each(queries, function(v) {
  49. request = request
  50. .facet($scope.ejs.QueryFacet("query"+_.indexOf(queries,v))
  51. .query(v)
  52. ).size(0)
  53. })
  54. // TODO: Spy for hits panel
  55. //$scope.populate_modal(request);
  56. // Then run it
  57. var results = request.doSearch();
  58. // Populate scope when we have results
  59. results.then(function(results) {
  60. $scope.panel.loading = false;
  61. if(_segment == 0) {
  62. $scope.hits = 0;
  63. $scope.data = [];
  64. query_id = $scope.query_id = new Date().getTime();
  65. }
  66. // Check for error and abort if found
  67. if(!(_.isUndefined(results.error))) {
  68. $scope.panel.error = $scope.parse_error(results.error);
  69. return;
  70. }
  71. if($scope.query_id === query_id) {
  72. var i = 0;
  73. _.each(results.facets, function(v, k) {
  74. var hits = _.isUndefined($scope.data[i]) || _segment == 0 ?
  75. v.count : $scope.data[i].hits+v.count
  76. $scope.hits += v.count
  77. // Create series
  78. $scope.data[i] = {
  79. label: $scope.panel.query[i].label || "query"+(parseInt(i)+1),
  80. hits: hits,
  81. data: [[i,hits]]
  82. };
  83. i++;
  84. });
  85. $scope.$emit('render');
  86. if(_segment < $scope.panel.index.length-1)
  87. $scope.get_data(_segment+1,query_id)
  88. }
  89. });
  90. }
  91. $scope.remove_query = function(q) {
  92. $scope.panel.query = _.without($scope.panel.query,q);
  93. $scope.get_data();
  94. }
  95. $scope.add_query = function(label,query) {
  96. $scope.panel.query.unshift({
  97. query: query,
  98. label: label,
  99. });
  100. $scope.get_data();
  101. }
  102. function set_time(time) {
  103. $scope.time = time;
  104. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  105. $scope.get_data();
  106. }
  107. }).directive('hitsChart', function(eventBus) {
  108. return {
  109. restrict: 'A',
  110. link: function(scope, elem, attrs, ctrl) {
  111. // Receive render events
  112. scope.$on('render',function(){
  113. render_panel();
  114. });
  115. // Re-render if the window is resized
  116. angular.element(window).bind('resize', function(){
  117. render_panel();
  118. });
  119. // Function for rendering panel
  120. function render_panel() {
  121. var scripts = $LAB.script("common/lib/panels/jquery.flot.js")
  122. // Populate element. Note that jvectormap appends, does not replace.
  123. scripts.wait(function(){
  124. // Populate element
  125. try {
  126. // Add plot to scope so we can build out own legend
  127. scope.plot = $.plot(elem, scope.data, {
  128. legend: { show: false },
  129. series: {
  130. lines: { show: false, },
  131. bars: { show: true, fill: 1, barWidth: 0.8, horizontal: false },
  132. shadowSize: 1
  133. },
  134. yaxis: { show: true, min: 0, color: "#000" },
  135. xaxis: { show: false },
  136. grid: {
  137. backgroundColor: '#fff',
  138. borderWidth: 0,
  139. borderColor: '#eee',
  140. color: "#eee",
  141. hoverable: true,
  142. },
  143. colors: ['#EB6841','#00A0B0','#6A4A3C','#EDC951','#CC333F']
  144. })
  145. // Work around for missing legend at initialization
  146. if(!scope.$$phase)
  147. scope.$apply()
  148. } catch(e) {
  149. elem.text(e)
  150. }
  151. })
  152. }
  153. function tt(x, y, contents) {
  154. var tooltip = $('#pie-tooltip').length ?
  155. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  156. //var tooltip = $('#pie-tooltip')
  157. tooltip.html(contents).css({
  158. position: 'absolute',
  159. top : y + 5,
  160. left : x + 5,
  161. color : "#000",
  162. border : '2px solid #000',
  163. padding : '10px',
  164. 'font-size': '11pt',
  165. 'font-weight' : 200,
  166. 'background-color': '#FFF',
  167. 'border-radius': '10px',
  168. }).appendTo("body");
  169. }
  170. elem.bind("plothover", function (event, pos, item) {
  171. if (item) {
  172. tt(pos.pageX, pos.pageY,
  173. "<div style='vertical-align:middle;border-radius:10px;display:inline-block;background:"+item.series.color+";height:20px;width:20px'></div> "+
  174. item.datapoint[1].toFixed(0))
  175. } else {
  176. $("#pie-tooltip").remove();
  177. }
  178. });
  179. }
  180. };
  181. })