diff --git a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts index 7105f4c805a..a4865bf7074 100644 --- a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts +++ b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts @@ -2,6 +2,7 @@ import { extractServicesFromMetricDescriptors, getMetricTypesByService, getAlignmentOptionsByMetric, + getAggregationOptionsByMetric, } from './functions'; import { alignmentPeriods } from './constants'; import has from 'lodash/has'; @@ -24,6 +25,8 @@ export default class StackdriverMetricFindQuery { return this.handleAlignersType(query); case 'alignmentPeriods': return this.handleAlignmentPeriodType(); + case 'aggregations': + return this.handleAggregationType(query); default: return []; } @@ -107,6 +110,18 @@ export default class StackdriverMetricFindQuery { })); } + async handleAggregationType({ metricType }) { + if (!metricType) { + return []; + } + const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName); + const { valueType, metricKind } = metricDescriptors.find(m => m.type === metricType); + return getAggregationOptionsByMetric(valueType, metricKind).map(o => ({ + ...o, + expandable: true, + })); + } + handleAlignmentPeriodType() { return alignmentPeriods.map(s => ({ ...s, diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx index d68864d5a34..651e4ea633f 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx @@ -134,6 +134,7 @@ export class StackdriverTemplateQueryComponent extends PureComponent ); case 'alignerns': + case 'aggregations': return ( diff --git a/public/app/plugins/datasource/stackdriver/functions.ts b/public/app/plugins/datasource/stackdriver/functions.ts index 8b3551d153a..f7891203dad 100644 --- a/public/app/plugins/datasource/stackdriver/functions.ts +++ b/public/app/plugins/datasource/stackdriver/functions.ts @@ -1,4 +1,4 @@ -import { alignOptions } from './constants'; +import { alignOptions, aggOptions } from './constants'; import uniqBy from 'lodash/uniqBy'; export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy(metricDescriptors, 'service'); @@ -13,3 +13,11 @@ export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => { return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1; }); }; + +export const getAggregationOptionsByMetric = (valueType, metricKind) => { + return !metricKind + ? [] + : aggOptions.filter(i => { + return i.valueTypes.indexOf(valueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1; + }); +}; diff --git a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts index 1340aa7f690..512035103f5 100644 --- a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts @@ -1,7 +1,7 @@ import coreModule from 'app/core/core_module'; import _ from 'lodash'; import * as options from './constants'; -import { getAlignmentOptionsByMetric } from './functions'; +import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions'; import kbn from 'app/core/utils/kbn'; export class StackdriverAggregation { @@ -49,13 +49,7 @@ export class StackdriverAggregationCtrl { } setAggOptions() { - this.aggOptions = !this.target.metricKind - ? [] - : options.aggOptions.filter(i => { - return ( - i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1 - ); - }); + this.aggOptions = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind); if (!this.aggOptions.find(o => o.value === this.target.aggregation.crossSeriesReducer)) { this.deselectAggregationOption('REDUCE_NONE');