metric_agg.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.init = function() {
  14. $scope.agg = metricAggs[$scope.index];
  15. $scope.validateModel();
  16. };
  17. $rootScope.onAppEvent('elastic-query-updated', function() {
  18. $scope.index = _.indexOf(metricAggs, $scope.agg);
  19. $scope.validateModel();
  20. }, $scope);
  21. $scope.validateModel = function() {
  22. $scope.isFirst = $scope.index === 0;
  23. $scope.isSingle = metricAggs.length === 1;
  24. $scope.settingsLinkText = '';
  25. $scope.aggDef = _.findWhere($scope.metricAggTypes, {value: $scope.agg.type});
  26. if (!$scope.agg.field) {
  27. $scope.agg.field = 'select field';
  28. }
  29. switch($scope.agg.type) {
  30. case 'moving_avg': {
  31. $scope.agg.aggregation = $scope.agg.aggregation || 'sum';
  32. break;
  33. }
  34. case 'percentiles': {
  35. $scope.agg.settings.percents = $scope.agg.settings.percents || [25,50,75,95,99];
  36. $scope.settingsLinkText = 'values: ' + $scope.agg.settings.percents.join(',');
  37. break;
  38. }
  39. case 'extended_stats': {
  40. var stats = _.reduce($scope.agg.meta, function(memo, val, key) {
  41. if (val) {
  42. var def = _.findWhere($scope.extendedStats, {value: key});
  43. memo.push(def.text);
  44. }
  45. return memo;
  46. }, []);
  47. $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
  48. if (stats.length === 0) {
  49. $scope.agg.meta.std_deviation_bounds_lower = true;
  50. $scope.agg.meta.std_deviation_bounds_upper = true;
  51. }
  52. break;
  53. }
  54. case 'raw_document': {
  55. $scope.target.metrics = [$scope.agg];
  56. $scope.target.bucketAggs = [];
  57. }
  58. }
  59. };
  60. $scope.toggleOptions = function() {
  61. $scope.showOptions = !$scope.showOptions;
  62. };
  63. $scope.onTypeChange = function() {
  64. $scope.agg.settings = {};
  65. $scope.agg.meta = {};
  66. $scope.showOptions = false;
  67. $scope.onChange();
  68. };
  69. $scope.getFieldsInternal = function() {
  70. return $scope.getFields({$fieldType: 'number'});
  71. };
  72. $scope.getMetrics = function() {
  73. var aggs = [{ text: 'Sum', type: 'sum'}, { text: 'Average', type: 'avg'}];
  74. return $q.when(aggs)
  75. .then(uiSegmentSrv.transformToSegments(false));
  76. };
  77. $scope.addMetricAgg = function() {
  78. var addIndex = metricAggs.length;
  79. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  80. return parseInt(val.id) > max ? parseInt(val.id) : max;
  81. }, 0);
  82. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  83. $scope.onChange();
  84. };
  85. $scope.removeMetricAgg = function() {
  86. metricAggs.splice($scope.index, 1);
  87. $scope.onChange();
  88. };
  89. $scope.init();
  90. });
  91. });