From 0b41303e1056dfc8b211e299bf79f5f169b4b797 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Fri, 14 Sep 2018 19:28:48 +0200 Subject: [PATCH] stackdriver: improve query editor to handle no data better --- .../datasource/stackdriver/datasource.ts | 36 ++++++++++--------- .../datasource/stackdriver/query_ctrl.ts | 16 +++++++-- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index c2fbeaa4488..bb1783c9a54 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -12,23 +12,27 @@ export default class StackdriverDatasource { } async getTimeSeries(options) { - const queries = options.targets.filter(target => !target.hide).map(t => { - if (!t.hasOwnProperty('aggregation')) { - t.aggregation = { - crossSeriesReducer: 'REDUCE_MEAN', - groupBys: [], + const queries = options.targets + .filter(target => { + return !target.hide && target.metricType; + }) + .map(t => { + if (!t.hasOwnProperty('aggregation')) { + t.aggregation = { + crossSeriesReducer: 'REDUCE_MEAN', + groupBys: [], + }; + } + return { + refId: t.refId, + datasourceId: this.id, + metricType: t.metricType, + primaryAggregation: t.aggregation.crossSeriesReducer, + groupBys: t.aggregation.groupBys, + view: t.view || 'FULL', + filters: t.filters, }; - } - return { - refId: t.refId, - datasourceId: this.id, - metricType: t.metricType, - primaryAggregation: t.aggregation.crossSeriesReducer, - groupBys: t.aggregation.groupBys, - view: t.view || 'FULL', - filters: t.filters, - }; - }); + }); const { data } = await this.backendSrv.datasourceRequest({ url: '/api/tsdb/query', diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index a9d649d0307..397887f2350 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -84,7 +84,10 @@ export class StackdriverQueryCtrl extends QueryCtrl { this.getCurrentProject() .then(this.getMetricTypes.bind(this)) .then(this.getLabels.bind(this)) - .then(resolve); + .then(resolve) + .catch(err => { + console.log(err); + }); }); this.initSegments(); @@ -149,7 +152,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { if (this.target.project.id !== 'default') { const metricTypes = await this.datasource.getMetricTypes(this.target.project.id); if (this.target.metricType === this.defaultDropdownValue && metricTypes.length > 0) { - this.$scope.$apply(() => (this.target.metricType = metricTypes[0].name)); + this.$scope.$apply(() => (this.target.metricType = metricTypes[0].id)); } return metricTypes.map(mt => ({ value: mt.id, text: mt.id })); } else { @@ -183,7 +186,9 @@ export class StackdriverQueryCtrl extends QueryCtrl { } async getGroupBys(segment, index, removeText?: string, removeUsed = true) { - await this.initPromise; + if (!this.metricLabels || Object.keys(this.metricLabels).length === 0) { + await this.initPromise; + } const metricLabels = Object.keys(this.metricLabels) .filter(ml => { if (!removeUsed) { @@ -213,6 +218,11 @@ export class StackdriverQueryCtrl extends QueryCtrl { }); }); + const noValueOrPlusButton = !segment || segment.type === 'plus-button'; + if (noValueOrPlusButton && metricLabels.length === 0 && resourceLabels.length === 0) { + return Promise.resolve([]); + } + this.removeSegment.value = removeText || this.defaultRemoveGroupByValue; return Promise.resolve([...metricLabels, ...resourceLabels, this.removeSegment]); }