2018-10-14 21:39:34 +08:00
|
|
|
import coreModule from 'app/core/core_module';
|
2018-09-25 20:42:47 +08:00
|
|
|
import _ from 'lodash';
|
2018-12-12 16:57:30 +08:00
|
|
|
import { alignmentPeriods } from './constants';
|
2018-10-29 23:57:07 +08:00
|
|
|
import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions';
|
2018-09-26 23:50:08 +08:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
2018-09-25 20:42:47 +08:00
|
|
|
|
|
|
|
|
export class StackdriverAggregation {
|
|
|
|
|
constructor() {
|
|
|
|
|
return {
|
|
|
|
|
templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
|
|
|
|
|
controller: 'StackdriverAggregationCtrl',
|
|
|
|
|
restrict: 'E',
|
|
|
|
|
scope: {
|
|
|
|
|
target: '=',
|
2018-09-26 23:50:08 +08:00
|
|
|
alignmentPeriod: '<',
|
2018-09-25 20:42:47 +08:00
|
|
|
refresh: '&',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class StackdriverAggregationCtrl {
|
2018-09-27 21:05:26 +08:00
|
|
|
alignmentPeriods: any[];
|
|
|
|
|
aggOptions: any[];
|
|
|
|
|
alignOptions: any[];
|
|
|
|
|
target: any;
|
|
|
|
|
|
2018-10-03 20:42:41 +08:00
|
|
|
/** @ngInject */
|
2018-11-02 17:45:43 +08:00
|
|
|
constructor(private $scope, private templateSrv) {
|
2018-09-27 21:05:26 +08:00
|
|
|
this.$scope.ctrl = this;
|
|
|
|
|
this.target = $scope.target;
|
2018-12-18 23:01:12 +08:00
|
|
|
this.alignmentPeriods = alignmentPeriods.map(ap => ({
|
|
|
|
|
...ap,
|
|
|
|
|
label: ap.text,
|
|
|
|
|
}));
|
2018-09-26 19:22:20 +08:00
|
|
|
this.setAggOptions();
|
|
|
|
|
this.setAlignOptions();
|
2018-09-28 17:25:32 +08:00
|
|
|
const self = this;
|
|
|
|
|
$scope.$on('metricTypeChanged', () => {
|
|
|
|
|
self.setAggOptions();
|
|
|
|
|
self.setAlignOptions();
|
|
|
|
|
});
|
2018-09-25 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 19:22:20 +08:00
|
|
|
setAlignOptions() {
|
2018-12-18 23:01:12 +08:00
|
|
|
this.alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
|
2018-12-12 16:57:30 +08:00
|
|
|
...a,
|
|
|
|
|
label: a.text,
|
|
|
|
|
}));
|
2018-09-25 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 19:22:20 +08:00
|
|
|
setAggOptions() {
|
2018-12-12 16:57:30 +08:00
|
|
|
let aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
|
|
|
|
|
...a,
|
|
|
|
|
label: a.text,
|
|
|
|
|
}));
|
|
|
|
|
if (!aggregations.find(o => o.value === this.templateSrv.replace(this.target.aggregation.crossSeriesReducer))) {
|
2018-09-28 23:06:24 +08:00
|
|
|
this.deselectAggregationOption('REDUCE_NONE');
|
2018-09-26 19:22:20 +08:00
|
|
|
}
|
2018-09-28 22:55:39 +08:00
|
|
|
|
|
|
|
|
if (this.target.aggregation.groupBys.length > 0) {
|
2018-12-12 19:57:57 +08:00
|
|
|
aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
|
2018-09-28 23:06:24 +08:00
|
|
|
this.deselectAggregationOption('REDUCE_NONE');
|
2018-09-28 22:55:39 +08:00
|
|
|
}
|
2018-12-12 16:57:30 +08:00
|
|
|
this.aggOptions = [
|
|
|
|
|
this.getTemplateVariablesGroup(),
|
|
|
|
|
{
|
|
|
|
|
label: 'Aggregations',
|
|
|
|
|
options: aggregations,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleAlignmentChange(value) {
|
|
|
|
|
this.target.aggregation.perSeriesAligner = value;
|
|
|
|
|
this.$scope.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleAggregationChange(value) {
|
|
|
|
|
this.target.aggregation.crossSeriesReducer = value;
|
|
|
|
|
this.$scope.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleAlignmentPeriodChange(value) {
|
|
|
|
|
this.target.aggregation.alignmentPeriod = value;
|
|
|
|
|
this.$scope.refresh();
|
2018-09-25 20:42:47 +08:00
|
|
|
}
|
2018-09-26 23:50:08 +08:00
|
|
|
|
|
|
|
|
formatAlignmentText() {
|
2018-12-12 16:57:30 +08:00
|
|
|
const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
|
|
|
|
|
const selectedAlignment = alignments.find(
|
2018-11-02 17:52:55 +08:00
|
|
|
ap => ap.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner)
|
|
|
|
|
);
|
2018-11-19 21:44:40 +08:00
|
|
|
return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${
|
|
|
|
|
selectedAlignment ? selectedAlignment.text : ''
|
|
|
|
|
})`;
|
2018-09-26 23:50:08 +08:00
|
|
|
}
|
2018-09-28 23:06:24 +08:00
|
|
|
|
|
|
|
|
deselectAggregationOption(notValidOptionValue: string) {
|
2018-12-12 19:57:57 +08:00
|
|
|
const aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind);
|
|
|
|
|
const newValue = aggregations.find(o => o.value !== notValidOptionValue);
|
2018-09-28 23:06:24 +08:00
|
|
|
this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
|
|
|
|
|
}
|
2018-12-12 16:57:30 +08:00
|
|
|
|
|
|
|
|
getTemplateVariablesGroup() {
|
|
|
|
|
return {
|
|
|
|
|
label: 'Template Variables',
|
|
|
|
|
options: this.templateSrv.variables.map(v => ({
|
|
|
|
|
label: `$${v.name}`,
|
|
|
|
|
value: `$${v.name}`,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-09-25 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-14 21:39:34 +08:00
|
|
|
coreModule.directive('stackdriverAggregation', StackdriverAggregation);
|
|
|
|
|
coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);
|