module.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * ago :: Date math formatted time to look back
  10. ### Group Events
  11. #### Sends
  12. * get_time :: On panel initialization get time range to query
  13. #### Receives
  14. * time :: An object containing the time range to use and the index(es) to query
  15. * query :: An Array of queries, even if its only one
  16. */
  17. angular.module('kibana.trends', [])
  18. .controller('trends', function($scope, eventBus, kbnIndex) {
  19. // Set and populate defaults
  20. var _d = {
  21. status : "Beta",
  22. query : ["*"],
  23. group : "default",
  24. style : { "font-size": '14pt'},
  25. ago : '1d',
  26. arrangement : 'vertical',
  27. }
  28. _.defaults($scope.panel,_d)
  29. $scope.init = function () {
  30. $scope.hits = 0;
  31. eventBus.register($scope,'time', function(event,time){
  32. set_time(time)
  33. });
  34. eventBus.register($scope,'query', function(event, query) {
  35. $scope.panel.query = _.map(query,function(q) {
  36. return {query: q, label: q};
  37. })
  38. $scope.get_data();
  39. });
  40. // Now that we're all setup, request the time from our group
  41. eventBus.broadcast($scope.$id,$scope.panel.group,'get_time')
  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(_.isUndefined($scope.index) || _.isUndefined($scope.time))
  48. return
  49. $scope.old_time = {
  50. from : new Date($scope.time.from.getTime() - interval_to_seconds($scope.panel.ago)*1000),
  51. to : new Date($scope.time.to.getTime() - interval_to_seconds($scope.panel.ago)*1000)
  52. }
  53. var _segment = _.isUndefined(segment) ? 0 : segment
  54. var request = $scope.ejs.Request();
  55. // Build the question part of the query
  56. var queries = [];
  57. _.each($scope.panel.query, function(v) {
  58. queries.push($scope.ejs.FilteredQuery(
  59. ejs.QueryStringQuery(v.query || '*'),
  60. ejs.RangeFilter($scope.time.field)
  61. .from($scope.time.from)
  62. .to($scope.time.to))
  63. )
  64. });
  65. // Build the facet part
  66. _.each(queries, function(v) {
  67. request = request
  68. .facet($scope.ejs.QueryFacet("new"+_.indexOf(queries,v))
  69. .query(v)
  70. ).size(0)
  71. })
  72. var queries = [];
  73. _.each($scope.panel.query, function(v) {
  74. queries.push($scope.ejs.FilteredQuery(
  75. ejs.QueryStringQuery(v.query || '*'),
  76. ejs.RangeFilter($scope.time.field)
  77. .from($scope.old_time.from)
  78. .to($scope.old_time.to))
  79. )
  80. });
  81. // Build the facet part
  82. _.each(queries, function(v) {
  83. request = request
  84. .facet($scope.ejs.QueryFacet("old"+_.indexOf(queries,v))
  85. .query(v)
  86. ).size(0)
  87. })
  88. // TODO: Spy for hits panel
  89. //$scope.populate_modal(request);
  90. // If we're on the first segment we need to get our indices
  91. if (_segment == 0) {
  92. kbnIndex.indices(
  93. $scope.old_time.from,
  94. $scope.old_time.to,
  95. $scope.time.pattern,
  96. $scope.time.interval
  97. ).then(function (p) {
  98. $scope.index = _.union(p,$scope.index);
  99. process_results(request.indices($scope.index[_segment]).doSearch());
  100. });
  101. } else {
  102. process_results(request.indices($scope.index[_segment]).doSearch());
  103. }
  104. // Populate scope when we have results
  105. function process_results(results) {
  106. results.then(function(results) {
  107. $scope.panel.loading = false;
  108. if(_segment == 0) {
  109. $scope.hits = {};
  110. $scope.data = [];
  111. query_id = $scope.query_id = new Date().getTime();
  112. }
  113. // Check for error and abort if found
  114. if(!(_.isUndefined(results.error))) {
  115. $scope.panel.error = $scope.parse_error(results.error);
  116. return;
  117. }
  118. if($scope.query_id === query_id) {
  119. var i = 0;
  120. _.each($scope.panel.query, function(k) {
  121. var n = results.facets['new'+i].count
  122. var o = results.facets['old'+i].count
  123. var hits = {
  124. new : _.isUndefined($scope.data[i]) || _segment == 0 ? n : $scope.data[i].hits.new+n,
  125. old : _.isUndefined($scope.data[i]) || _segment == 0 ? o : $scope.data[i].hits.old+o
  126. }
  127. $scope.hits.new += n;
  128. $scope.hits.old += o;
  129. var percent = percentage(hits.old,hits.new) == null ?
  130. '?' : Math.round(percentage(hits.old,hits.new)*100)/100
  131. // Create series
  132. $scope.data[i] = {
  133. label: $scope.panel.query[i].label || "query"+(parseInt(i)+1),
  134. hits: {
  135. new : hits.new,
  136. old : hits.old
  137. },
  138. percent: percent
  139. };
  140. i++;
  141. });
  142. $scope.$emit('render');
  143. if(_segment < $scope.index.length-1)
  144. $scope.get_data(_segment+1,query_id)
  145. else
  146. $scope.trends = $scope.data
  147. }
  148. });
  149. }
  150. }
  151. function percentage(x,y) {
  152. return x == 0 ? null : 100*(y-x)/x
  153. }
  154. $scope.remove_query = function(q) {
  155. $scope.panel.query = _.without($scope.panel.query,q);
  156. $scope.get_data();
  157. }
  158. $scope.add_query = function(label,query) {
  159. $scope.panel.query.unshift({
  160. query: query,
  161. label: label,
  162. });
  163. $scope.get_data();
  164. }
  165. $scope.set_refresh = function (state) {
  166. $scope.refresh = state;
  167. }
  168. $scope.close_edit = function() {
  169. if($scope.refresh)
  170. $scope.get_data();
  171. $scope.refresh = false;
  172. $scope.$emit('render');
  173. }
  174. function set_time(time) {
  175. $scope.time = time;
  176. $scope.index = time.index || $scope.index
  177. $scope.get_data();
  178. }
  179. })