Commit f243da75 by Erik Sundell

stackdriver: fix init labels bug

parent 0b41303e
...@@ -30,7 +30,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -30,7 +30,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
defaultFilterValue = 'select value'; defaultFilterValue = 'select value';
defaultRemoveGroupByValue = '-- remove group by --'; defaultRemoveGroupByValue = '-- remove group by --';
defaultRemoveFilterValue = '-- remove filter --'; defaultRemoveFilterValue = '-- remove filter --';
initPromise: Promise<any>; loadLabelsPromise: Promise<any>;
defaults = { defaults = {
project: { project: {
...@@ -80,15 +80,9 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -80,15 +80,9 @@ export class StackdriverQueryCtrl extends QueryCtrl {
this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope); this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope); this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
this.initPromise = new Promise(async resolve => { this.getCurrentProject()
this.getCurrentProject() .then(this.getMetricTypes.bind(this))
.then(this.getMetricTypes.bind(this)) .then(this.getLabels.bind(this));
.then(this.getLabels.bind(this))
.then(resolve)
.catch(err => {
console.log(err);
});
});
this.initSegments(); this.initSegments();
} }
...@@ -161,23 +155,30 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -161,23 +155,30 @@ export class StackdriverQueryCtrl extends QueryCtrl {
} }
async getLabels() { async getLabels() {
const data = await this.datasource.getTimeSeries({ this.loadLabelsPromise = new Promise(async resolve => {
targets: [ try {
{ const data = await this.datasource.getTimeSeries({
refId: this.target.refId, targets: [
datasourceId: this.datasource.id, {
metricType: this.target.metricType, refId: this.target.refId,
aggregation: { datasourceId: this.datasource.id,
crossSeriesReducer: 'REDUCE_NONE', metricType: this.target.metricType,
}, aggregation: {
view: 'HEADERS', crossSeriesReducer: 'REDUCE_NONE',
}, },
], view: 'HEADERS',
range: this.timeSrv.timeRange(), },
}); ],
range: this.timeSrv.timeRange(),
});
this.metricLabels = data.results[this.target.refId].meta.metricLabels; this.metricLabels = data.results[this.target.refId].meta.metricLabels;
this.resourceLabels = data.results[this.target.refId].meta.resourceLabels; this.resourceLabels = data.results[this.target.refId].meta.resourceLabels;
resolve();
} catch (error) {
resolve();
}
});
} }
async onMetricTypeChange() { async onMetricTypeChange() {
...@@ -186,9 +187,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -186,9 +187,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
} }
async getGroupBys(segment, index, removeText?: string, removeUsed = true) { async getGroupBys(segment, index, removeText?: string, removeUsed = true) {
if (!this.metricLabels || Object.keys(this.metricLabels).length === 0) { await this.loadLabelsPromise;
await this.initPromise;
}
const metricLabels = Object.keys(this.metricLabels) const metricLabels = Object.keys(this.metricLabels)
.filter(ml => { .filter(ml => {
if (!removeUsed) { if (!removeUsed) {
...@@ -256,7 +255,17 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -256,7 +255,17 @@ export class StackdriverQueryCtrl extends QueryCtrl {
} }
if (segment.type === 'key' || segment.type === 'plus-button') { if (segment.type === 'key' || segment.type === 'plus-button') {
return this.getGroupBys(null, null, this.defaultRemoveFilterValue, false); if (
this.metricLabels &&
Object.keys(this.metricLabels).length === 0 &&
segment.value &&
segment.value !== this.defaultRemoveFilterValue
) {
this.removeSegment.value = this.defaultRemoveFilterValue;
return Promise.resolve([this.removeSegment]);
} else {
return this.getGroupBys(null, null, this.defaultRemoveFilterValue, false);
}
} }
if (segment.type === 'value') { if (segment.type === 'value') {
...@@ -339,6 +348,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -339,6 +348,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
if (anySeriesFromQuery) { if (anySeriesFromQuery) {
this.lastQueryMeta = anySeriesFromQuery.meta; this.lastQueryMeta = anySeriesFromQuery.meta;
this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery); this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
} else {
} }
} }
...@@ -359,5 +369,6 @@ export class StackdriverQueryCtrl extends QueryCtrl { ...@@ -359,5 +369,6 @@ export class StackdriverQueryCtrl extends QueryCtrl {
this.lastQueryError = jsonBody.error.message; this.lastQueryError = jsonBody.error.message;
} }
} }
console.error(err);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment