module.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Trends
  5. Shows how queries are moving from a specified time ago
  6. ### Parameters
  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. */
  11. 'use strict';
  12. angular.module('kibana.trends', [])
  13. .controller('trends', function($scope, kbnIndex, querySrv, dashboard, filterSrv) {
  14. // Set and populate defaults
  15. var _d = {
  16. status : "Beta",
  17. queries : {
  18. mode : 'all',
  19. ids : []
  20. },
  21. group : "default",
  22. style : { "font-size": '14pt'},
  23. ago : '1d',
  24. arrangement : 'vertical',
  25. };
  26. _.defaults($scope.panel,_d);
  27. $scope.init = function () {
  28. $scope.hits = 0;
  29. $scope.$on('refresh',function(){$scope.get_data();});
  30. $scope.get_data();
  31. };
  32. $scope.get_data = function(segment,query_id) {
  33. delete $scope.panel.error;
  34. $scope.panel.loading = true;
  35. // Make sure we have everything for the request to complete
  36. if(dashboard.indices.length === 0) {
  37. return;
  38. } else {
  39. $scope.index = segment > 0 ? $scope.index : dashboard.indices;
  40. }
  41. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  42. // Determine a time field
  43. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'));
  44. if(timeField.length > 1) {
  45. $scope.panel.error = "Time field must be consistent amongst time filters";
  46. return;
  47. } else if(timeField.length === 0) {
  48. $scope.panel.error = "A time filter must exist for this panel to function";
  49. return;
  50. } else {
  51. timeField = timeField[0];
  52. }
  53. $scope.time = filterSrv.timeRange('min');
  54. $scope.old_time = {
  55. from : new Date($scope.time.from.getTime() - kbn.interval_to_seconds($scope.panel.ago)*1000),
  56. to : new Date($scope.time.to.getTime() - kbn.interval_to_seconds($scope.panel.ago)*1000)
  57. };
  58. var _segment = _.isUndefined(segment) ? 0 : segment;
  59. var request = $scope.ejs.Request();
  60. var _ids_without_time = _.difference(filterSrv.ids,filterSrv.idsByType('time'));
  61. // Build the question part of the query
  62. _.each($scope.panel.queries.ids, function(id) {
  63. var q = $scope.ejs.FilteredQuery(
  64. querySrv.getEjsObj(id),
  65. filterSrv.getBoolFilter(_ids_without_time).must(
  66. $scope.ejs.RangeFilter(timeField)
  67. .from($scope.time.from)
  68. .to($scope.time.to)
  69. ));
  70. request = request
  71. .facet($scope.ejs.QueryFacet(id)
  72. .query(q)
  73. ).size(0);
  74. });
  75. // And again for the old time period
  76. _.each($scope.panel.queries.ids, function(id) {
  77. var q = $scope.ejs.FilteredQuery(
  78. querySrv.getEjsObj(id),
  79. filterSrv.getBoolFilter(_ids_without_time).must(
  80. $scope.ejs.RangeFilter(timeField)
  81. .from($scope.old_time.from)
  82. .to($scope.old_time.to)
  83. ));
  84. request = request
  85. .facet($scope.ejs.QueryFacet("old_"+id)
  86. .query(q)
  87. ).size(0);
  88. });
  89. // TODO: Spy for trend panel
  90. //$scope.populate_modal(request);
  91. // If we're on the first segment we need to get our indices
  92. if (_segment === 0) {
  93. kbnIndex.indices(
  94. $scope.old_time.from,
  95. $scope.old_time.to,
  96. dashboard.current.index.pattern,
  97. dashboard.current.index.interval
  98. ).then(function (p) {
  99. $scope.index = _.union(p,$scope.index);
  100. request = request.indices($scope.index[_segment]);
  101. process_results(request.doSearch(),_segment,query_id);
  102. });
  103. } else {
  104. process_results(request.indices($scope.index[_segment]).doSearch(),_segment,query_id);
  105. }
  106. };
  107. // Populate scope when we have results
  108. var process_results = function(results,_segment,query_id) {
  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, 10);}});
  123. // Make sure we're still on the same query/queries
  124. if($scope.query_id === query_id &&
  125. _.intersection(facetIds,$scope.panel.queries.ids).length === $scope.panel.queries.ids.length
  126. ) {
  127. var i = 0;
  128. _.each($scope.panel.queries.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: querySrv.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. }
  170. $scope.refresh = false;
  171. $scope.$emit('render');
  172. };
  173. });