metric_agg.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q, $rootScope) {
  10. var metricAggs = $scope.target.metrics;
  11. $scope.metricAggTypes = queryDef.getMetricAggTypes($scope.esVersion);
  12. $scope.extendedStats = queryDef.extendedStats;
  13. $scope.pipelineAggOptions = [];
  14. $scope.init = function() {
  15. $scope.agg = metricAggs[$scope.index];
  16. $scope.validateModel();
  17. $scope.updatePipelineAggOptions();
  18. };
  19. $scope.updatePipelineAggOptions = function() {
  20. $scope.pipelineAggOptions = queryDef.getPipelineAggOptions($scope.target);
  21. };
  22. $rootScope.onAppEvent('elastic-query-updated', function() {
  23. $scope.index = _.indexOf(metricAggs, $scope.agg);
  24. $scope.updatePipelineAggOptions();
  25. $scope.validateModel();
  26. }, $scope);
  27. $scope.validateModel = function() {
  28. $scope.isFirst = $scope.index === 0;
  29. $scope.isSingle = metricAggs.length === 1;
  30. $scope.settingsLinkText = '';
  31. $scope.aggDef = _.findWhere($scope.metricAggTypes, {value: $scope.agg.type});
  32. if (queryDef.isPipelineAgg($scope.agg.type)) {
  33. $scope.agg.pipelineAgg = $scope.agg.pipelineAgg || 'select metric';
  34. $scope.agg.field = $scope.agg.pipelineAgg;
  35. var pipelineOptions = queryDef.getPipelineOptions($scope.agg);
  36. if (pipelineOptions.length > 0) {
  37. _.each(pipelineOptions, function(opt) {
  38. $scope.agg.settings[opt.text] = $scope.agg.settings[opt.text] || opt.default;
  39. });
  40. $scope.settingsLinkText = 'Options';
  41. }
  42. } else if (!$scope.agg.field) {
  43. $scope.agg.field = 'select field';
  44. }
  45. switch($scope.agg.type) {
  46. case 'percentiles': {
  47. $scope.agg.settings.percents = $scope.agg.settings.percents || [25,50,75,95,99];
  48. $scope.settingsLinkText = 'Values: ' + $scope.agg.settings.percents.join(',');
  49. break;
  50. }
  51. case 'extended_stats': {
  52. if (_.keys($scope.agg.meta).length === 0) {
  53. $scope.agg.meta.std_deviation_bounds_lower = true;
  54. $scope.agg.meta.std_deviation_bounds_upper = true;
  55. }
  56. var stats = _.reduce($scope.agg.meta, function(memo, val, key) {
  57. if (val) {
  58. var def = _.findWhere($scope.extendedStats, {value: key});
  59. memo.push(def.text);
  60. }
  61. return memo;
  62. }, []);
  63. $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
  64. break;
  65. }
  66. case 'raw_document': {
  67. $scope.target.metrics = [$scope.agg];
  68. $scope.target.bucketAggs = [];
  69. break;
  70. }
  71. }
  72. if ($scope.aggDef.supportsInlineScript) {
  73. // I know this stores the inline script twice
  74. // but having it like this simplifes the query_builder
  75. var inlineScript = $scope.agg.inlineScript;
  76. if (inlineScript) {
  77. $scope.agg.settings.script = {inline: inlineScript};
  78. } else {
  79. delete $scope.agg.settings.script;
  80. }
  81. if ($scope.settingsLinkText === '') {
  82. $scope.settingsLinkText = 'Options';
  83. }
  84. }
  85. };
  86. $scope.toggleOptions = function() {
  87. $scope.showOptions = !$scope.showOptions;
  88. $scope.updatePipelineAggOptions();
  89. };
  90. $scope.onChangeInternal = function() {
  91. $scope.onChange();
  92. };
  93. $scope.onTypeChange = function() {
  94. $scope.agg.settings = {};
  95. $scope.agg.meta = {};
  96. $scope.showOptions = false;
  97. $scope.updatePipelineAggOptions();
  98. $scope.onChange();
  99. };
  100. $scope.getFieldsInternal = function() {
  101. return $scope.getFields({$fieldType: 'number'});
  102. };
  103. $scope.addMetricAgg = function() {
  104. var addIndex = metricAggs.length;
  105. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  106. return parseInt(val.id) > max ? parseInt(val.id) : max;
  107. }, 0);
  108. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  109. $scope.onChange();
  110. };
  111. $scope.removeMetricAgg = function() {
  112. metricAggs.splice($scope.index, 1);
  113. $scope.onChange();
  114. };
  115. $scope.toggleShowMetric = function() {
  116. $scope.agg.hide = !$scope.agg.hide;
  117. if (!$scope.agg.hide) {
  118. delete $scope.agg.hide;
  119. }
  120. $scope.onChange();
  121. };
  122. $scope.init();
  123. });
  124. });