From 49cd31ab7863ab06721d2eb723be011d7def9efa Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Wed, 26 Sep 2018 11:17:24 +0200 Subject: [PATCH] stackdriver: get value type and metric kind from metric descriptor instead of from latest metric result --- pkg/tsdb/stackdriver/stackdriver.go | 3 -- .../stackdriver/partials/query.editor.html | 6 ++-- .../stackdriver/query_aggregation_ctrl.ts | 4 +-- .../datasource/stackdriver/query_ctrl.ts | 32 ++++++++----------- .../stackdriver/specs/query_ctrl.test.ts | 4 +-- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/pkg/tsdb/stackdriver/stackdriver.go b/pkg/tsdb/stackdriver/stackdriver.go index ffd065d6a18..3a903a544be 100644 --- a/pkg/tsdb/stackdriver/stackdriver.go +++ b/pkg/tsdb/stackdriver/stackdriver.go @@ -316,9 +316,6 @@ func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data Sta Name: metricName, Points: points, }) - - queryRes.Meta.Set("metricKind", series.MetricKind) - queryRes.Meta.Set("valueType", series.ValueType) } queryRes.Meta.Set("resourceLabels", resourceLabels) diff --git a/public/app/plugins/datasource/stackdriver/partials/query.editor.html b/public/app/plugins/datasource/stackdriver/partials/query.editor.html index 6696a57fa4d..979557cf1e3 100755 --- a/public/app/plugins/datasource/stackdriver/partials/query.editor.html +++ b/public/app/plugins/datasource/stackdriver/partials/query.editor.html @@ -12,7 +12,7 @@
Resource type -
@@ -22,8 +22,8 @@
Metric - +
diff --git a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts index 02dc3ec6d67..f1c86aede9a 100644 --- a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts @@ -55,7 +55,7 @@ export class StackdriverAggregationCtrl { getAlignOptions() { return !this.target.valueType - ? options.alignOptions + ? [] : options.alignOptions.filter(i => { return ( i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1 @@ -65,7 +65,7 @@ export class StackdriverAggregationCtrl { getAggOptions() { return !this.target.metricKind - ? options.aggOptions + ? [] : options.aggOptions.filter(i => { return ( i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1 diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index e4a84916d46..0babdf8fb7b 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -19,7 +19,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { name: string; }; metricType: string; - metricService: string; + resourceType: string; refId: string; aggregation: { crossSeriesReducer: string; @@ -44,7 +44,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { name: 'loading project...', }, metricType: this.defaultDropdownValue, - metricService: this.defaultMetricResourcesValue, + resourceType: this.defaultMetricResourcesValue, metric: '', aggregation: { crossSeriesReducer: 'REDUCE_MEAN', @@ -80,7 +80,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope); this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope); this.getCurrentProject() - .then(this.getMetricTypes.bind(this)) + .then(this.loadMetricDescriptors.bind(this)) .then(this.getLabels.bind(this)); this.initSegments(); } @@ -123,21 +123,17 @@ export class StackdriverQueryCtrl extends QueryCtrl { } } - async getMetricTypes() { + async loadMetricDescriptors() { if (this.target.project.id !== 'default') { - const metricTypes = await this.datasource.getMetricTypes(this.target.project.id); - this.metricDescriptors = metricTypes; - if (this.target.metricType === this.defaultDropdownValue && metricTypes.length > 0) { - this.$scope.$apply(() => (this.target.metricType = metricTypes[0].id)); - } - - return metricTypes.map(mt => ({ value: mt.type, text: mt.type })); + this.metricDescriptors = await this.datasource.getMetricTypes(this.target.project.id); + this.metrics = this.getMetrics(); + return this.metricDescriptors; } else { return []; } } - getMetricServices() { + getResourceTypes() { const defaultValue = { value: this.defaultMetricResourcesValue, text: this.defaultMetricResourcesValue }; const resources = this.metricDescriptors.map(m => { const [resource] = m.type.split('/'); @@ -162,10 +158,11 @@ export class StackdriverQueryCtrl extends QueryCtrl { title: m.description, }; }); - if (this.target.metricService === this.defaultMetricResourcesValue) { + + if (this.target.resourceType === this.defaultMetricResourcesValue) { return metrics.map(m => ({ ...m, text: `${m.service} - ${m.text}` })); } else { - return metrics.filter(m => m.resource === this.target.metricService); + return metrics.filter(m => m.resource === this.target.resourceType); } } @@ -182,12 +179,8 @@ export class StackdriverQueryCtrl extends QueryCtrl { this.loadLabelsPromise = new Promise(async resolve => { try { const data = await this.datasource.getLabels(this.target.metricType, this.target.refId); - this.metricLabels = data.results[this.target.refId].meta.metricLabels; this.resourceLabels = data.results[this.target.refId].meta.resourceLabels; - - this.target.valueType = data.results[this.target.refId].meta.valueType; - this.target.metricKind = data.results[this.target.refId].meta.metricKind; resolve(); } catch (error) { console.log(error.data.message); @@ -198,6 +191,9 @@ export class StackdriverQueryCtrl extends QueryCtrl { } async onMetricTypeChange() { + const { valueType, metricKind } = this.metricDescriptors.find(m => m.type === this.target.metricType); + this.target.valueType = valueType; + this.target.metricKind = metricKind; this.refresh(); this.getLabels(); } diff --git a/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts b/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts index 590dea22601..cbe91fbc6eb 100644 --- a/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts +++ b/public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts @@ -373,8 +373,8 @@ function createCtrlWithFakes(existingFilters?: string[]) { refresh: () => {}, }; StackdriverQueryCtrl.prototype.target = createTarget(existingFilters); - StackdriverQueryCtrl.prototype.getMetricTypes = () => { - return Promise.resolve(); + StackdriverQueryCtrl.prototype.loadMetricDescriptors = () => { + return Promise.resolve([]); }; StackdriverQueryCtrl.prototype.getLabels = () => { return Promise.resolve();