module.js 5.9 KB

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