module.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, kbnIndex, query, dashboard, filterSrv) {
  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. $scope.$on('refresh',function(){$scope.get_data()})
  32. $scope.get_data();
  33. }
  34. $scope.get_data = function(segment,query_id) {
  35. delete $scope.panel.error
  36. $scope.panel.loading = true;
  37. // Make sure we have everything for the request to complete
  38. if(dashboard.indices.length == 0) {
  39. return
  40. } else {
  41. $scope.index = segment > 0 ? $scope.index : dashboard.indices;
  42. }
  43. // Determine a time field
  44. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'))
  45. if(timeField.length > 1) {
  46. $scope.panel.error = "Time field must be consistent amongst time filters"
  47. return
  48. } else if(timeField.length == 0) {
  49. $scope.panel.error = "A time filter must exist for this panel to function"
  50. return
  51. } else {
  52. timeField = timeField[0]
  53. }
  54. $scope.time = filterSrv.timeRange('min');
  55. $scope.old_time = {
  56. from : new Date($scope.time.from.getTime() - interval_to_seconds($scope.panel.ago)*1000),
  57. to : new Date($scope.time.to.getTime() - interval_to_seconds($scope.panel.ago)*1000)
  58. }
  59. var _segment = _.isUndefined(segment) ? 0 : segment
  60. var request = $scope.ejs.Request();
  61. var _ids_without_time = _.difference(filterSrv.ids,filterSrv.idsByType('time'))
  62. // Build the question part of the query
  63. _.each(query.ids, function(id) {
  64. var q = $scope.ejs.FilteredQuery(
  65. ejs.QueryStringQuery(query.list[id].query || '*'),
  66. filterSrv.getBoolFilter(_ids_without_time).must(
  67. ejs.RangeFilter(timeField)
  68. .from($scope.time.from)
  69. .to($scope.time.to)
  70. ))
  71. request = request
  72. .facet($scope.ejs.QueryFacet(id)
  73. .query(q)
  74. ).size(0)
  75. });
  76. // And again for the old time period
  77. _.each(query.ids, function(id) {
  78. var q = $scope.ejs.FilteredQuery(
  79. ejs.QueryStringQuery(query.list[id].query || '*'),
  80. filterSrv.getBoolFilter(_ids_without_time).must(
  81. ejs.RangeFilter(timeField)
  82. .from($scope.old_time.from)
  83. .to($scope.old_time.to)
  84. ))
  85. request = request
  86. .facet($scope.ejs.QueryFacet("old_"+id)
  87. .query(q)
  88. ).size(0)
  89. });
  90. // TODO: Spy for trend panel
  91. //$scope.populate_modal(request);
  92. // If we're on the first segment we need to get our indices
  93. if (_segment == 0) {
  94. kbnIndex.indices(
  95. $scope.old_time.from,
  96. $scope.old_time.to,
  97. dashboard.current.index.pattern,
  98. dashboard.current.index.interval
  99. ).then(function (p) {
  100. $scope.index = _.union(p,$scope.index);
  101. request = request.indices($scope.index[_segment])
  102. process_results(request.doSearch());
  103. });
  104. } else {
  105. process_results(request.indices($scope.index[_segment]).doSearch());
  106. }
  107. // Populate scope when we have results
  108. function process_results(results) {
  109. results.then(function(results) {
  110. $scope.panel.loading = false;
  111. if(_segment == 0) {
  112. $scope.hits = {};
  113. $scope.data = [];
  114. query_id = $scope.query_id = new Date().getTime();
  115. }
  116. // Check for error and abort if found
  117. if(!(_.isUndefined(results.error))) {
  118. $scope.panel.error = $scope.parse_error(results.error);
  119. return;
  120. }
  121. // Convert facet ids to numbers
  122. var facetIds = _.map(_.keys(results.facets),function(k){if(!isNaN(k)){return parseInt(k)}})
  123. // Make sure we're still on the same query/queries
  124. if($scope.query_id === query_id &&
  125. _.intersection(facetIds,query.ids).length == query.ids.length
  126. ) {
  127. var i = 0;
  128. _.each(query.ids, function(id) {
  129. var v = results.facets[id]
  130. var n = results.facets[id].count
  131. var o = results.facets['old_'+id].count
  132. var hits = {
  133. new : _.isUndefined($scope.data[i]) || _segment == 0 ? n : $scope.data[i].hits.new+n,
  134. old : _.isUndefined($scope.data[i]) || _segment == 0 ? o : $scope.data[i].hits.old+o
  135. }
  136. $scope.hits.new += n;
  137. $scope.hits.old += o;
  138. var percent = percentage(hits.old,hits.new) == null ?
  139. '?' : Math.round(percentage(hits.old,hits.new)*100)/100
  140. // Create series
  141. $scope.data[i] = {
  142. info: query.list[id],
  143. hits: {
  144. new : hits.new,
  145. old : hits.old
  146. },
  147. percent: percent
  148. };
  149. i++;
  150. });
  151. $scope.$emit('render');
  152. if(_segment < $scope.index.length-1)
  153. $scope.get_data(_segment+1,query_id)
  154. else
  155. $scope.trends = $scope.data
  156. }
  157. });
  158. }
  159. }
  160. function percentage(x,y) {
  161. return x == 0 ? null : 100*(y-x)/x
  162. }
  163. $scope.set_refresh = function (state) {
  164. $scope.refresh = state;
  165. }
  166. $scope.close_edit = function() {
  167. if($scope.refresh)
  168. $scope.get_data();
  169. $scope.refresh = false;
  170. $scope.$emit('render');
  171. }
  172. })