module.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*jshint globalstrict:true */
  2. /*global angular:true */
  3. /*
  4. ## Trends
  5. ### Parameters
  6. * style :: A hash of css styles
  7. * arrangement :: How should I arrange the query results? 'horizontal' or 'vertical'
  8. * ago :: Date math formatted time to look back
  9. */
  10. 'use strict';
  11. angular.module('kibana.trends', [])
  12. .controller('trends', function($scope, kbnIndex, querySrv, dashboard, filterSrv) {
  13. $scope.panelMeta = {
  14. status : "Beta",
  15. description : "A stock-ticker style representation of how queries are moving over time. "+
  16. "For example, if the time is 1:10pm, your time picker was set to \"Last 10m\", and the \"Time "+
  17. "Ago\" parameter was set to '1h', the panel would show how much the query results have changed"+
  18. " since 12:00-12:10pm"
  19. };
  20. // Set and populate defaults
  21. var _d = {
  22. queries : {
  23. mode : 'all',
  24. ids : []
  25. },
  26. style : { "font-size": '14pt'},
  27. ago : '1d',
  28. arrangement : 'vertical',
  29. };
  30. _.defaults($scope.panel,_d);
  31. $scope.init = function () {
  32. $scope.hits = 0;
  33. $scope.$on('refresh',function(){$scope.get_data();});
  34. $scope.get_data();
  35. };
  36. $scope.get_data = function(segment,query_id) {
  37. delete $scope.panel.error;
  38. $scope.panelMeta.loading = true;
  39. // Make sure we have everything for the request to complete
  40. if(dashboard.indices.length === 0) {
  41. return;
  42. } else {
  43. $scope.index = segment > 0 ? $scope.index : dashboard.indices;
  44. }
  45. $scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
  46. // Determine a time field
  47. var timeField = _.uniq(_.pluck(filterSrv.getByType('time'),'field'));
  48. if(timeField.length > 1) {
  49. $scope.panel.error = "Time field must be consistent amongst time filters";
  50. return;
  51. } else if(timeField.length === 0) {
  52. $scope.panel.error = "A time filter must exist for this panel to function";
  53. return;
  54. } else {
  55. timeField = timeField[0];
  56. }
  57. $scope.time = filterSrv.timeRange('min');
  58. $scope.old_time = {
  59. from : new Date($scope.time.from.getTime() - kbn.interval_to_seconds($scope.panel.ago)*1000),
  60. to : new Date($scope.time.to.getTime() - kbn.interval_to_seconds($scope.panel.ago)*1000)
  61. };
  62. var _segment = _.isUndefined(segment) ? 0 : segment;
  63. var request = $scope.ejs.Request();
  64. var _ids_without_time = _.difference(filterSrv.ids,filterSrv.idsByType('time'));
  65. // Build the question part of the query
  66. _.each($scope.panel.queries.ids, function(id) {
  67. var q = $scope.ejs.FilteredQuery(
  68. querySrv.getEjsObj(id),
  69. filterSrv.getBoolFilter(_ids_without_time).must(
  70. $scope.ejs.RangeFilter(timeField)
  71. .from($scope.time.from)
  72. .to($scope.time.to)
  73. ));
  74. request = request
  75. .facet($scope.ejs.QueryFacet(id)
  76. .query(q)
  77. ).size(0);
  78. });
  79. // And again for the old time period
  80. _.each($scope.panel.queries.ids, function(id) {
  81. var q = $scope.ejs.FilteredQuery(
  82. querySrv.getEjsObj(id),
  83. filterSrv.getBoolFilter(_ids_without_time).must(
  84. $scope.ejs.RangeFilter(timeField)
  85. .from($scope.old_time.from)
  86. .to($scope.old_time.to)
  87. ));
  88. request = request
  89. .facet($scope.ejs.QueryFacet("old_"+id)
  90. .query(q)
  91. ).size(0);
  92. });
  93. // TODO: Spy for trend panel
  94. //$scope.populate_modal(request);
  95. // If we're on the first segment we need to get our indices
  96. if (_segment === 0) {
  97. kbnIndex.indices(
  98. $scope.old_time.from,
  99. $scope.old_time.to,
  100. dashboard.current.index.pattern,
  101. dashboard.current.index.interval
  102. ).then(function (p) {
  103. $scope.index = _.union(p,$scope.index);
  104. request = request.indices($scope.index[_segment]);
  105. process_results(request.doSearch(),_segment,query_id);
  106. });
  107. } else {
  108. process_results(request.indices($scope.index[_segment]).doSearch(),_segment,query_id);
  109. }
  110. };
  111. // Populate scope when we have results
  112. var process_results = function(results,_segment,query_id) {
  113. results.then(function(results) {
  114. $scope.panelMeta.loading = false;
  115. if(_segment === 0) {
  116. $scope.hits = {};
  117. $scope.data = [];
  118. query_id = $scope.query_id = new Date().getTime();
  119. }
  120. // Check for error and abort if found
  121. if(!(_.isUndefined(results.error))) {
  122. $scope.panel.error = $scope.parse_error(results.error);
  123. return;
  124. }
  125. // Convert facet ids to numbers
  126. var facetIds = _.map(_.keys(results.facets),function(k){if(!isNaN(k)){return parseInt(k, 10);}});
  127. // Make sure we're still on the same query/queries
  128. if($scope.query_id === query_id &&
  129. _.intersection(facetIds,$scope.panel.queries.ids).length === $scope.panel.queries.ids.length
  130. ) {
  131. var i = 0;
  132. _.each($scope.panel.queries.ids, function(id) {
  133. var v = results.facets[id];
  134. var n = results.facets[id].count;
  135. var o = results.facets['old_'+id].count;
  136. var hits = {
  137. new : _.isUndefined($scope.data[i]) || _segment === 0 ? n : $scope.data[i].hits.new+n,
  138. old : _.isUndefined($scope.data[i]) || _segment === 0 ? o : $scope.data[i].hits.old+o
  139. };
  140. $scope.hits.new += n;
  141. $scope.hits.old += o;
  142. var percent = percentage(hits.old,hits.new) == null ?
  143. '?' : Math.round(percentage(hits.old,hits.new)*100)/100;
  144. // Create series
  145. $scope.data[i] = {
  146. info: querySrv.list[id],
  147. hits: {
  148. new : hits.new,
  149. old : hits.old
  150. },
  151. percent: percent
  152. };
  153. i++;
  154. });
  155. $scope.$emit('render');
  156. if(_segment < $scope.index.length-1) {
  157. $scope.get_data(_segment+1,query_id);
  158. } else {
  159. $scope.trends = $scope.data;
  160. }
  161. }
  162. });
  163. };
  164. function percentage(x,y) {
  165. return x === 0 ? null : 100*(y-x)/x;
  166. }
  167. $scope.set_refresh = function (state) {
  168. $scope.refresh = state;
  169. };
  170. $scope.close_edit = function() {
  171. if($scope.refresh) {
  172. $scope.get_data();
  173. }
  174. $scope.refresh = false;
  175. $scope.$emit('render');
  176. };
  177. });