metric_agg.ts 6.5 KB

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