grafana/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts

84 lines
3.0 KiB
TypeScript
Raw Normal View History

import angular from 'angular';
import _ from 'lodash';
import * as options from './constants';
export class StackdriverAggregation {
constructor() {
return {
templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
controller: 'StackdriverAggregationCtrl',
restrict: 'E',
scope: {
target: '=',
refresh: '&',
},
};
}
}
export class StackdriverAggregationCtrl {
constructor(private $scope) {
$scope.aggOptions = options.aggOptions;
this.setAggOptions();
this.setAlignOptions();
2018-09-26 08:03:44 -05:00
$scope.alignmentPeriods = options.alignmentPeriods;
$scope.onAlignmentChange = this.onAlignmentChange.bind(this);
$scope.onAggregationChange = this.onAggregationChange.bind(this);
2018-09-26 06:42:53 -05:00
$scope.$on('metricTypeChanged', this.setAlignOptions.bind(this));
}
onAlignmentChange(newVal: string) {
if (newVal === 'ALIGN_NONE') {
this.$scope.target.aggregation.crossSeriesReducer = 'REDUCE_NONE';
}
this.$scope.refresh();
}
onAggregationChange(newVal: string) {
if (newVal !== 'REDUCE_NONE' && this.$scope.target.aggregation.perSeriesAligner === 'ALIGN_NONE') {
2018-09-25 10:03:10 -05:00
const newAlignmentOption = options.alignOptions.find(
o =>
o.value !== 'ALIGN_NONE' &&
o.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
o.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
2018-09-25 10:03:10 -05:00
);
this.$scope.target.aggregation.perSeriesAligner = newAlignmentOption ? newAlignmentOption.value : '';
}
this.$scope.refresh();
}
setAlignOptions() {
this.$scope.alignOptions = !this.$scope.target.valueType
? []
: options.alignOptions.filter(i => {
return (
i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
);
});
if (!this.$scope.alignOptions.find(o => o.value === this.$scope.target.aggregation.perSeriesAligner)) {
const newValue = this.$scope.alignOptions.find(o => o.value !== 'ALIGN_NONE');
this.$scope.target.aggregation.perSeriesAligner = newValue ? newValue.value : '';
}
}
setAggOptions() {
this.$scope.aggOptions = !this.$scope.target.metricKind
? []
: options.aggOptions.filter(i => {
return (
i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
);
});
if (!this.$scope.aggOptions.find(o => o.value === this.$scope.target.aggregation.crossSeriesReducer)) {
const newValue = this.$scope.aggOptions.find(o => o.value !== 'REDUCE_NONE');
this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
}
}
}
angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);