query_aggregation_ctrl.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import * as options from './constants';
  4. export class StackdriverAggregation {
  5. constructor() {
  6. return {
  7. templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
  8. controller: 'StackdriverAggregationCtrl',
  9. restrict: 'E',
  10. scope: {
  11. target: '=',
  12. refresh: '&',
  13. },
  14. };
  15. }
  16. }
  17. export class StackdriverAggregationCtrl {
  18. constructor(private $scope) {
  19. $scope.aggOptions = options.aggOptions;
  20. this.setAggOptions();
  21. this.setAlignOptions();
  22. $scope.alignmentPeriods = options.alignmentPeriods;
  23. $scope.onAlignmentChange = this.onAlignmentChange.bind(this);
  24. $scope.onAggregationChange = this.onAggregationChange.bind(this);
  25. $scope.$on('metricTypeChanged', this.setAlignOptions.bind(this));
  26. }
  27. onAlignmentChange(newVal: string) {
  28. if (newVal === 'ALIGN_NONE') {
  29. this.$scope.target.aggregation.crossSeriesReducer = 'REDUCE_NONE';
  30. }
  31. this.$scope.refresh();
  32. }
  33. onAggregationChange(newVal: string) {
  34. if (newVal !== 'REDUCE_NONE' && this.$scope.target.aggregation.perSeriesAligner === 'ALIGN_NONE') {
  35. const newAlignmentOption = options.alignOptions.find(
  36. o =>
  37. o.value !== 'ALIGN_NONE' &&
  38. o.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  39. o.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  40. );
  41. this.$scope.target.aggregation.perSeriesAligner = newAlignmentOption ? newAlignmentOption.value : '';
  42. }
  43. this.$scope.refresh();
  44. }
  45. setAlignOptions() {
  46. this.$scope.alignOptions = !this.$scope.target.valueType
  47. ? []
  48. : options.alignOptions.filter(i => {
  49. return (
  50. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  51. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  52. );
  53. });
  54. if (!this.$scope.alignOptions.find(o => o.value === this.$scope.target.aggregation.perSeriesAligner)) {
  55. const newValue = this.$scope.alignOptions.find(o => o.value !== 'ALIGN_NONE');
  56. this.$scope.target.aggregation.perSeriesAligner = newValue ? newValue.value : '';
  57. }
  58. }
  59. setAggOptions() {
  60. this.$scope.aggOptions = !this.$scope.target.metricKind
  61. ? []
  62. : options.aggOptions.filter(i => {
  63. return (
  64. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  65. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  66. );
  67. });
  68. if (!this.$scope.aggOptions.find(o => o.value === this.$scope.target.aggregation.crossSeriesReducer)) {
  69. const newValue = this.$scope.aggOptions.find(o => o.value !== 'REDUCE_NONE');
  70. this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  71. }
  72. }
  73. }
  74. angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
  75. angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);