metric_agg.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import * as queryDef from './query_def';
  4. export function elasticMetricAgg() {
  5. return {
  6. templateUrl: 'public/app/plugins/datasource/elasticsearch/partials/metric_agg.html',
  7. controller: 'ElasticMetricAggCtrl',
  8. restrict: 'E',
  9. scope: {
  10. target: '=',
  11. index: '=',
  12. onChange: '&',
  13. getFields: '&',
  14. esVersion: '=',
  15. },
  16. };
  17. }
  18. export class ElasticMetricAggCtrl {
  19. constructor($scope, uiSegmentSrv, $q, $rootScope) {
  20. const metricAggs = $scope.target.metrics;
  21. $scope.metricAggTypes = queryDef.getMetricAggTypes($scope.esVersion);
  22. $scope.extendedStats = queryDef.extendedStats;
  23. $scope.pipelineAggOptions = [];
  24. $scope.modelSettingsValues = {};
  25. $scope.init = () => {
  26. $scope.agg = metricAggs[$scope.index];
  27. $scope.validateModel();
  28. $scope.updatePipelineAggOptions();
  29. };
  30. $scope.updatePipelineAggOptions = () => {
  31. $scope.pipelineAggOptions = queryDef.getPipelineAggOptions($scope.target);
  32. };
  33. $rootScope.onAppEvent(
  34. 'elastic-query-updated',
  35. () => {
  36. $scope.index = _.indexOf(metricAggs, $scope.agg);
  37. $scope.updatePipelineAggOptions();
  38. $scope.validateModel();
  39. },
  40. $scope
  41. );
  42. $scope.validateModel = () => {
  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. const pipelineOptions = queryDef.getPipelineOptions($scope.agg);
  51. if (pipelineOptions.length > 0) {
  52. _.each(pipelineOptions, 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. const precisionThreshold = $scope.agg.settings.precision_threshold || '';
  63. $scope.settingsLinkText = 'Precision threshold: ' + precisionThreshold;
  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. const stats = _.reduce(
  77. $scope.agg.meta,
  78. (memo, val, key) => {
  79. if (val) {
  80. const def = _.find($scope.extendedStats, { value: key });
  81. memo.push(def.text);
  82. }
  83. return memo;
  84. },
  85. []
  86. );
  87. $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
  88. break;
  89. }
  90. case 'moving_avg': {
  91. $scope.movingAvgModelTypes = queryDef.movingAvgModelOptions;
  92. $scope.modelSettings = queryDef.getMovingAvgSettings($scope.agg.settings.model, true);
  93. $scope.updateMovingAvgModelSettings();
  94. break;
  95. }
  96. case 'raw_document': {
  97. $scope.agg.settings.size = $scope.agg.settings.size || 500;
  98. $scope.settingsLinkText = 'Size: ' + $scope.agg.settings.size;
  99. $scope.target.metrics.splice(0, $scope.target.metrics.length, $scope.agg);
  100. $scope.target.bucketAggs = [];
  101. break;
  102. }
  103. }
  104. if ($scope.aggDef.supportsInlineScript) {
  105. // I know this stores the inline script twice
  106. // but having it like this simplifes the query_builder
  107. const inlineScript = $scope.agg.inlineScript;
  108. if (inlineScript) {
  109. $scope.agg.settings.script = { inline: inlineScript };
  110. } else {
  111. delete $scope.agg.settings.script;
  112. }
  113. if ($scope.settingsLinkText === '') {
  114. $scope.settingsLinkText = 'Options';
  115. }
  116. }
  117. };
  118. $scope.toggleOptions = () => {
  119. $scope.showOptions = !$scope.showOptions;
  120. $scope.updatePipelineAggOptions();
  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. $scope.updatePipelineAggOptions();
  146. $scope.onChange();
  147. };
  148. $scope.getFieldsInternal = () => {
  149. if ($scope.agg.type === 'cardinality') {
  150. return $scope.getFields();
  151. }
  152. return $scope.getFields({ $fieldType: 'number' });
  153. };
  154. $scope.addMetricAgg = () => {
  155. const addIndex = metricAggs.length;
  156. const id = _.reduce(
  157. $scope.target.bucketAggs.concat($scope.target.metrics),
  158. (max, val) => {
  159. return parseInt(val.id, 10) > max ? parseInt(val.id, 10) : max;
  160. },
  161. 0
  162. );
  163. metricAggs.splice(addIndex, 0, { type: 'count', field: 'select field', id: (id + 1).toString() });
  164. $scope.onChange();
  165. };
  166. $scope.removeMetricAgg = () => {
  167. metricAggs.splice($scope.index, 1);
  168. $scope.onChange();
  169. };
  170. $scope.toggleShowMetric = () => {
  171. $scope.agg.hide = !$scope.agg.hide;
  172. if (!$scope.agg.hide) {
  173. delete $scope.agg.hide;
  174. }
  175. $scope.onChange();
  176. };
  177. $scope.init();
  178. }
  179. }
  180. const module = angular.module('grafana.directives');
  181. module.directive('elasticMetricAgg', elasticMetricAgg);
  182. module.controller('ElasticMetricAggCtrl', ElasticMetricAggCtrl);