module.js 5.2 KB

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