metric_agg.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.metricAggTypes;
  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. var stats = _.reduce($scope.agg.meta, function(memo, val, key) {
  53. if (val) {
  54. var def = _.findWhere($scope.extendedStats, {value: key});
  55. memo.push(def.text);
  56. }
  57. return memo;
  58. }, []);
  59. $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
  60. if (stats.length === 0) {
  61. $scope.agg.meta.std_deviation_bounds_lower = true;
  62. $scope.agg.meta.std_deviation_bounds_upper = true;
  63. }
  64. break;
  65. }
  66. case 'raw_document': {
  67. $scope.target.metrics = [$scope.agg];
  68. $scope.target.bucketAggs = [];
  69. }
  70. }
  71. };
  72. $scope.toggleOptions = function() {
  73. $scope.showOptions = !$scope.showOptions;
  74. $scope.updatePipelineAggOptions();
  75. };
  76. $scope.onChangeInternal = function() {
  77. $scope.onChange();
  78. };
  79. $scope.onTypeChange = function() {
  80. $scope.agg.settings = {};
  81. $scope.agg.meta = {};
  82. $scope.showOptions = false;
  83. $scope.updatePipelineAggOptions();
  84. $scope.onChange();
  85. };
  86. $scope.getFieldsInternal = function() {
  87. return $scope.getFields({$fieldType: 'number'});
  88. };
  89. $scope.addMetricAgg = function() {
  90. var addIndex = metricAggs.length;
  91. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  92. return parseInt(val.id) > max ? parseInt(val.id) : max;
  93. }, 0);
  94. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  95. $scope.onChange();
  96. };
  97. $scope.removeMetricAgg = function() {
  98. metricAggs.splice($scope.index, 1);
  99. $scope.onChange();
  100. };
  101. $scope.toggleShowMetric = function() {
  102. $scope.agg.hide = !$scope.agg.hide;
  103. if (!$scope.agg.hide) {
  104. delete $scope.agg.hide;
  105. }
  106. $scope.onChange();
  107. };
  108. $scope.init();
  109. });
  110. });