metric_agg.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './query_def'
  5. ],
  6. function (angular, _, queryDef) {
  7. 'use strict';
  8. var module = angular.module('grafana.directives');
  9. module.directive('elasticMetricAgg', function() {
  10. return {
  11. templateUrl: 'public/app/plugins/datasource/elasticsearch/partials/metric_agg.html',
  12. controller: 'ElasticMetricAggCtrl',
  13. restrict: 'E',
  14. scope: {
  15. target: "=",
  16. index: "=",
  17. onChange: "&",
  18. getFields: "&",
  19. esVersion: '='
  20. }
  21. };
  22. });
  23. module.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q, $rootScope) {
  24. var metricAggs = $scope.target.metrics;
  25. $scope.metricAggTypes = queryDef.getMetricAggTypes($scope.esVersion);
  26. $scope.extendedStats = queryDef.extendedStats;
  27. $scope.pipelineAggOptions = [];
  28. $scope.init = function() {
  29. $scope.agg = metricAggs[$scope.index];
  30. $scope.validateModel();
  31. $scope.updatePipelineAggOptions();
  32. };
  33. $scope.updatePipelineAggOptions = function() {
  34. $scope.pipelineAggOptions = queryDef.getPipelineAggOptions($scope.target);
  35. };
  36. $rootScope.onAppEvent('elastic-query-updated', function() {
  37. $scope.index = _.indexOf(metricAggs, $scope.agg);
  38. $scope.updatePipelineAggOptions();
  39. $scope.validateModel();
  40. }, $scope);
  41. $scope.validateModel = function() {
  42. $scope.isFirst = $scope.index === 0;
  43. $scope.isSingle = metricAggs.length === 1;
  44. $scope.settingsLinkText = '';
  45. $scope.aggDef = _.find($scope.metricAggTypes, {value: $scope.agg.type});
  46. if (queryDef.isPipelineAgg($scope.agg.type)) {
  47. $scope.agg.pipelineAgg = $scope.agg.pipelineAgg || 'select metric';
  48. $scope.agg.field = $scope.agg.pipelineAgg;
  49. var pipelineOptions = queryDef.getPipelineOptions($scope.agg);
  50. if (pipelineOptions.length > 0) {
  51. _.each(pipelineOptions, function(opt) {
  52. $scope.agg.settings[opt.text] = $scope.agg.settings[opt.text] || opt.default;
  53. });
  54. $scope.settingsLinkText = 'Options';
  55. }
  56. } else if (!$scope.agg.field) {
  57. $scope.agg.field = 'select field';
  58. }
  59. switch($scope.agg.type) {
  60. case 'cardinality': {
  61. var precision_threshold = $scope.agg.settings.precision_threshold || '';
  62. $scope.settingsLinkText = 'Precision threshold: ' + precision_threshold;
  63. break;
  64. }
  65. case 'percentiles': {
  66. $scope.agg.settings.percents = $scope.agg.settings.percents || [25,50,75,95,99];
  67. $scope.settingsLinkText = 'Values: ' + $scope.agg.settings.percents.join(',');
  68. break;
  69. }
  70. case 'extended_stats': {
  71. if (_.keys($scope.agg.meta).length === 0) {
  72. $scope.agg.meta.std_deviation_bounds_lower = true;
  73. $scope.agg.meta.std_deviation_bounds_upper = true;
  74. }
  75. var stats = _.reduce($scope.agg.meta, function(memo, val, key) {
  76. if (val) {
  77. var def = _.find($scope.extendedStats, {value: key});
  78. memo.push(def.text);
  79. }
  80. return memo;
  81. }, []);
  82. $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
  83. break;
  84. }
  85. case 'raw_document': {
  86. $scope.target.metrics = [$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. var 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 = function() {
  106. $scope.showOptions = !$scope.showOptions;
  107. $scope.updatePipelineAggOptions();
  108. };
  109. $scope.onChangeInternal = function() {
  110. $scope.onChange();
  111. };
  112. $scope.onTypeChange = function() {
  113. $scope.agg.settings = {};
  114. $scope.agg.meta = {};
  115. $scope.showOptions = false;
  116. $scope.updatePipelineAggOptions();
  117. $scope.onChange();
  118. };
  119. $scope.getFieldsInternal = function() {
  120. return $scope.getFields({$fieldType: 'number'});
  121. };
  122. $scope.addMetricAgg = function() {
  123. var addIndex = metricAggs.length;
  124. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  125. return parseInt(val.id) > max ? parseInt(val.id) : max;
  126. }, 0);
  127. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  128. $scope.onChange();
  129. };
  130. $scope.removeMetricAgg = function() {
  131. metricAggs.splice($scope.index, 1);
  132. $scope.onChange();
  133. };
  134. $scope.toggleShowMetric = function() {
  135. $scope.agg.hide = !$scope.agg.hide;
  136. if (!$scope.agg.hide) {
  137. delete $scope.agg.hide;
  138. }
  139. $scope.onChange();
  140. };
  141. $scope.init();
  142. });
  143. });