metric_agg.ts 7.2 KB

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