metricAgg.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './queryDef'
  5. ],
  6. function (angular, _, queryDef) {
  7. 'use strict';
  8. var module = angular.module('grafana.directives');
  9. module.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q) {
  10. var metricAggs = $scope.target.metrics;
  11. $scope.metricAggTypes = queryDef.metricAggTypes;
  12. $scope.init = function() {
  13. $scope.agg = metricAggs[$scope.index];
  14. if (!$scope.agg.field) {
  15. $scope.agg.field = 'select field';
  16. }
  17. }
  18. $scope.$watchCollection("target.metrics", function() {
  19. $scope.isFirst = $scope.index === 0;
  20. $scope.isLast = $scope.index === metricAggs.length - 1;
  21. $scope.isSingle = metricAggs.length === 1;
  22. });
  23. $scope.toggleOptions = function() {
  24. $scope.showOptions = !$scope.showOptions;
  25. }
  26. $scope.addMetricAgg = function() {
  27. var addIndex = metricAggs.length;
  28. var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
  29. return parseInt(val.id) > max ? parseInt(val.id) : max;
  30. }, 0);
  31. metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
  32. };
  33. $scope.removeMetricAgg = function() {
  34. metricAggs.splice($scope.index, 1);
  35. $scope.onChange();
  36. };
  37. $scope.init();
  38. });
  39. });