metric_agg.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './query_def'
  5. ],
  6. function (angular, _, queryDef) {
  7. 'use strict';
  8. var module = angular.module('grafana.directives');
  9. module.directive('elasticMetricAgg', function() {
  10. return {
  11. templateUrl: 'public/app/plugins/datasource/elasticsearch/partials/metric_agg.html',
  12. controller: 'ElasticMetricAggCtrl',
  13. restrict: 'E',
  14. scope: {
  15. target: "=",
  16. index: "=",
  17. onChange: "&",
  18. getFields: "&",
  19. esVersion: '='
  20. }
  21. };
  22. });
  23. module.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q, $rootScope) {
  24. var metricAggs = $scope.target.metrics;
  25. $scope.metricAggTypes = queryDef.getMetricAggTypes($scope.esVersion);
  26. $scope.extendedStats = queryDef.extendedStats;
  27. $scope.pipelineAggOptions = [];
  28. $scope.modelSettingsValues = {};
  29. $scope.init = function() {
  30. $scope.agg = metricAggs[$scope.index];
  31. $scope.validateModel();
  32. $scope.updatePipelineAggOptions();
  33. };
  34. $scope.updatePipelineAggOptions = function() {
  35. $scope.pipelineAggOptions = queryDef.getPipelineAggOptions($scope.target);
  36. };
  37. $rootScope.onAppEvent('elastic-query-updated', function() {
  38. $scope.index = _.indexOf(metricAggs, $scope.agg);
  39. $scope.updatePipelineAggOptions();
  40. $scope.validateModel();
  41. }, $scope);
  42. $scope.validateModel = function() {
  43. $scope.isFirst = $scope.index === 0;
  44. $scope.isSingle = metricAggs.length === 1;
  45. $scope.settingsLinkText = '';
  46. $scope.aggDef = _.find($scope.metricAggTypes, {value: $scope.agg.type});
  47. if (queryDef.isPipelineAgg($scope.agg.type)) {
  48. $scope.agg.pipelineAgg = $scope.agg.pipelineAgg || 'select metric';
  49. $scope.agg.field = $scope.agg.pipelineAgg;
  50. var pipelineOptions = queryDef.getPipelineOptions($scope.agg);
  51. if (pipelineOptions.length > 0) {
  52. _.each(pipelineOptions, function(opt) {
  53. $scope.agg.settings[opt.text] = $scope.agg.settings[opt.text] || opt.default;
  54. });
  55. $scope.settingsLinkText = 'Options';
  56. }
  57. } else if (!$scope.agg.field) {
  58. $scope.agg.field = 'select field';
  59. }
  60. switch($scope.agg.type) {
  61. case 'cardinality': {
  62. var precision_threshold = $scope.agg.settings.precision_threshold || '';
  63. $scope.settingsLinkText = 'Precision threshold: ' + precision_threshold;
  64. break;
  65. }
  66. case 'percentiles': {
  67. $scope.agg.settings.percents = $scope.agg.settings.percents || [25,50,75,95,99];
  68. $scope.settingsLinkText = 'Values: ' + $scope.agg.settings.percents.join(',');
  69. break;
  70. }
  71. case 'extended_stats': {
  72. if (_.keys($scope.agg.meta).length === 0) {
  73. $scope.agg.meta.std_deviation_bounds_lower = true;
  74. $scope.agg.meta.std_deviation_bounds_upper = true;
  75. }
  76. var stats = _.reduce($scope.agg.meta, function(memo, val, key) {
  77. if (val) {
  78. var def = _.find($scope.extendedStats, {value: key});
  79. memo.push(def.text);
  80. }
  81. return memo;
  82. }, []);
  83. $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
  84. break;
  85. }
  86. case 'moving_avg': {
  87. $scope.movingAvgModelTypes = queryDef.movingAvgModelOptions;
  88. $scope.modelSettings = queryDef.getMovingAvgSettings($scope.agg.settings.model, true);
  89. $scope.updateMovingAvgModelSettings();
  90. break;
  91. }
  92. case 'raw_document': {
  93. $scope.agg.settings.size = $scope.agg.settings.size || 500;
  94. $scope.settingsLinkText = 'Size: ' + $scope.agg.settings.size ;
  95. $scope.target.metrics.splice(0,$scope.target.metrics.length, $scope.agg);
  96. $scope.target.bucketAggs = [];
  97. break;
  98. }
  99. }
  100. if ($scope.aggDef.supportsInlineScript) {
  101. // I know this stores the inline script twice
  102. // but having it like this simplifes the query_builder
  103. var inlineScript = $scope.agg.inlineScript;
  104. if (inlineScript) {
  105. $scope.agg.settings.script = {inline: inlineScript};
  106. } else {
  107. delete $scope.agg.settings.script;
  108. }
  109. if ($scope.settingsLinkText === '') {
  110. $scope.settingsLinkText = 'Options';
  111. }
  112. }
  113. };
  114. $scope.toggleOptions = function() {
  115. $scope.showOptions = !$scope.showOptions;
  116. $scope.updatePipelineAggOptions();
  117. };
  118. $scope.onChangeInternal = function() {
  119. $scope.onChange();
  120. };
  121. $scope.updateMovingAvgModelSettings = function () {
  122. var modelSettingsKeys = [];
  123. var modelSettings = queryDef.getMovingAvgSettings($scope.agg.settings.model, false);
  124. for (var i=0; i < modelSettings.length; i++) {
  125. modelSettingsKeys.push(modelSettings[i].value);
  126. }
  127. for (var key in $scope.agg.settings.settings) {
  128. if (($scope.agg.settings.settings[key] === null) || (modelSettingsKeys.indexOf(key) === -1)) {
  129. delete $scope.agg.settings.settings[key];
  130. }
  131. }
  132. };
  133. $scope.onChangeClearInternal = function() {
  134. delete $scope.agg.settings.minimize;
  135. $scope.onChange();
  136. };
  137. $scope.onTypeChange = function() {
  138. $scope.agg.settings = {};
  139. $scope.agg.meta = {};
  140. $scope.showOptions = false;
  141. $scope.updatePipelineAggOptions();
  142. $scope.onChange();
  143. };
  144. $scope.getFieldsInternal = function() {
  145. if ($scope.agg.type === 'cardinality') {
  146. return $scope.getFields();
  147. }
  148. return $scope.getFields({$fieldType: 'number'});
  149. };
  150. $scope.addMetricAgg = function() {
  151. var addIndex = metricAggs.length;
  152. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  153. return parseInt(val.id) > max ? parseInt(val.id) : max;
  154. }, 0);
  155. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  156. $scope.onChange();
  157. };
  158. $scope.removeMetricAgg = function() {
  159. metricAggs.splice($scope.index, 1);
  160. $scope.onChange();
  161. };
  162. $scope.toggleShowMetric = function() {
  163. $scope.agg.hide = !$scope.agg.hide;
  164. if (!$scope.agg.hide) {
  165. delete $scope.agg.hide;
  166. }
  167. $scope.onChange();
  168. };
  169. $scope.init();
  170. });
  171. });