metric_agg.ts 7.1 KB

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