metric_agg.ts 7.1 KB

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