diff --git a/pkg/tsdb/stackdriver/stackdriver_test.go b/pkg/tsdb/stackdriver/stackdriver_test.go index 8f1a7605e02..ef8e0e728d9 100644 --- a/pkg/tsdb/stackdriver/stackdriver_test.go +++ b/pkg/tsdb/stackdriver/stackdriver_test.go @@ -254,6 +254,24 @@ func TestStackdriver(t *testing.T) { So(res.Series[2].Name, ShouldEqual, "compute.googleapis.com/instance/cpu/usage_time collector-us-east-1 us-east1-b") }) }) + + Convey("when data from query with no aggregation and alias by", func() { + data, err := loadTestFile("./test-data/2-series-response-no-agg.json") + So(err, ShouldBeNil) + So(len(data.TimeSeries), ShouldEqual, 3) + + res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "A"} + query := &StackdriverQuery{AliasBy: "{{metric.label.instance_name}}", GroupBys: []string{"metric.label.instance_name", "resource.label.zone"}} + err = executor.parseResponse(res, data, query) + So(err, ShouldBeNil) + + Convey("Should use alias by formatting and only show instance name", func() { + So(len(res.Series), ShouldEqual, 3) + So(res.Series[0].Name, ShouldEqual, "collector-asia-east-1") + So(res.Series[1].Name, ShouldEqual, "collector-europe-west-1") + So(res.Series[2].Name, ShouldEqual, "collector-us-east-1") + }) + }) }) }) } diff --git a/pkg/tsdb/stackdriver/types.go b/pkg/tsdb/stackdriver/types.go index 88a2abd8548..ba2ed29a39c 100644 --- a/pkg/tsdb/stackdriver/types.go +++ b/pkg/tsdb/stackdriver/types.go @@ -10,6 +10,7 @@ type StackdriverQuery struct { Params url.Values RefID string GroupBys []string + AliasBy string } type StackdriverResponse struct { diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index 4d4dada3b92..ca078463e94 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -5,7 +5,7 @@ export default class StackdriverDatasource { baseUrl: string; projectName: string; - constructor(instanceSettings, private backendSrv) { + constructor(instanceSettings, private backendSrv, private templateSrv) { this.baseUrl = `/stackdriver/`; this.url = instanceSettings.url; this.doRequest = this.doRequest; @@ -22,21 +22,22 @@ export default class StackdriverDatasource { if (!t.hasOwnProperty('aggregation')) { t.aggregation = { crossSeriesReducer: 'REDUCE_MEAN', - secondaryCrossSeriesReducer: 'REDUCE_NONE', groupBys: [], }; } return { refId: t.refId, datasourceId: this.id, - metricType: t.metricType, - primaryAggregation: t.aggregation.crossSeriesReducer, - secondaryAggregation: t.aggregation.secondaryCrossSeriesReducer, - perSeriesAligner: t.aggregation.perSeriesAligner, - alignmentPeriod: t.aggregation.alignmentPeriod, - groupBys: t.aggregation.groupBys, + metricType: this.templateSrv.replace(t.metricType, options.scopedVars || {}), + primaryAggregation: this.templateSrv.replace(t.aggregation.crossSeriesReducer, options.scopedVars || {}), + perSeriesAligner: this.templateSrv.replace(t.aggregation.perSeriesAligner, options.scopedVars || {}), + alignmentPeriod: this.templateSrv.replace(t.aggregation.alignmentPeriod, options.scopedVars || {}), + groupBys: this.interpolateGroupBys(t.aggregation.groupBys, options.scopedVars), view: t.view || 'FULL', - filters: t.filters, + filters: (t.filters || []).map(f => { + return this.templateSrv.replace(f, options.scopedVars || {}); + }), + aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}), }; }); @@ -52,6 +53,19 @@ export default class StackdriverDatasource { return data; } + interpolateGroupBys(groupBys: string[], scopedVars): string[] { + let interpolatedGroupBys = []; + (groupBys || []).forEach(gb => { + const interpolated = this.templateSrv.replace(gb, scopedVars || {}, 'csv').split(','); + if (Array.isArray(interpolated)) { + interpolatedGroupBys = interpolatedGroupBys.concat(interpolated); + } else { + interpolatedGroupBys.push(interpolated); + } + }); + return interpolatedGroupBys; + } + async query(options) { const result = []; const data = await this.getTimeSeries(options); diff --git a/public/app/plugins/datasource/stackdriver/partials/query.editor.html b/public/app/plugins/datasource/stackdriver/partials/query.editor.html index e36e2b0b84d..1efe4388607 100755 --- a/public/app/plugins/datasource/stackdriver/partials/query.editor.html +++ b/public/app/plugins/datasource/stackdriver/partials/query.editor.html @@ -50,17 +50,6 @@
{{ctrl.lastQueryError}}