metric_agg.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.target.metrics = [$scope.agg];
  94. $scope.target.bucketAggs = [];
  95. break;
  96. }
  97. }
  98. if ($scope.aggDef.supportsInlineScript) {
  99. // I know this stores the inline script twice
  100. // but having it like this simplifes the query_builder
  101. var inlineScript = $scope.agg.inlineScript;
  102. if (inlineScript) {
  103. $scope.agg.settings.script = {inline: inlineScript};
  104. } else {
  105. delete $scope.agg.settings.script;
  106. }
  107. if ($scope.settingsLinkText === '') {
  108. $scope.settingsLinkText = 'Options';
  109. }
  110. }
  111. };
  112. $scope.toggleOptions = function() {
  113. $scope.showOptions = !$scope.showOptions;
  114. $scope.updatePipelineAggOptions();
  115. };
  116. $scope.onChangeInternal = function() {
  117. $scope.onChange();
  118. };
  119. $scope.updateMovingAvgModelSettings = function () {
  120. var modelSettingsKeys = [];
  121. var modelSettings = queryDef.getMovingAvgSettings($scope.agg.settings.model, false);
  122. for (var i=0; i < modelSettings.length; i++) {
  123. modelSettingsKeys.push(modelSettings[i].value);
  124. }
  125. for (var key in $scope.agg.settings.settings) {
  126. if (($scope.agg.settings.settings[key] === null) || (modelSettingsKeys.indexOf(key) === -1)) {
  127. delete $scope.agg.settings.settings[key];
  128. }
  129. }
  130. };
  131. $scope.onChangeClearInternal = function() {
  132. delete $scope.agg.settings.minimize;
  133. $scope.onChange();
  134. };
  135. $scope.onTypeChange = function() {
  136. $scope.agg.settings = {};
  137. $scope.agg.meta = {};
  138. $scope.showOptions = false;
  139. $scope.updatePipelineAggOptions();
  140. $scope.onChange();
  141. };
  142. $scope.getFieldsInternal = function() {
  143. return $scope.getFields({$fieldType: 'number'});
  144. };
  145. $scope.addMetricAgg = function() {
  146. var addIndex = metricAggs.length;
  147. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  148. return parseInt(val.id) > max ? parseInt(val.id) : max;
  149. }, 0);
  150. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  151. $scope.onChange();
  152. };
  153. $scope.removeMetricAgg = function() {
  154. metricAggs.splice($scope.index, 1);
  155. $scope.onChange();
  156. };
  157. $scope.toggleShowMetric = function() {
  158. $scope.agg.hide = !$scope.agg.hide;
  159. if (!$scope.agg.hide) {
  160. delete $scope.agg.hide;
  161. }
  162. $scope.onChange();
  163. };
  164. $scope.init();
  165. });
  166. });