From 3f7314831fbba07f860296f84f80595145f88b8e Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Tue, 25 Sep 2018 16:51:12 +0200 Subject: [PATCH] stackdriver: wip: split metric dropdown into two parts - resource and metric --- .../datasource/stackdriver/datasource.ts | 2 +- .../stackdriver/partials/query.editor.html | 22 +++++++- .../datasource/stackdriver/query_ctrl.ts | 56 +++++++++++++++++-- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index 7d7b1e1cee1..5b6b5fd2a04 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -159,7 +159,7 @@ export default class StackdriverDatasource { try { const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`; const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`); - return data.metricDescriptors.map(m => ({ id: m.type, name: m.displayName })); + return data.metricDescriptors; } catch (error) { console.log(error); } diff --git a/public/app/plugins/datasource/stackdriver/partials/query.editor.html b/public/app/plugins/datasource/stackdriver/partials/query.editor.html index 71ae3dc6328..6696a57fa4d 100755 --- a/public/app/plugins/datasource/stackdriver/partials/query.editor.html +++ b/public/app/plugins/datasource/stackdriver/partials/query.editor.html @@ -1,5 +1,5 @@ -
+ +
+
+ Resource type + +
+
+
+
+
+
+
+ Metric + +
+
+
+
diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index 3f9cda2a875..e4a84916d46 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -19,6 +19,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { name: string; }; metricType: string; + metricService: string; refId: string; aggregation: { crossSeriesReducer: string; @@ -32,6 +33,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { valueType: any; }; defaultDropdownValue = 'select metric'; + defaultMetricResourcesValue = 'all'; defaultRemoveGroupByValue = '-- remove group by --'; loadLabelsPromise: Promise; stackdriverConstants; @@ -42,6 +44,8 @@ export class StackdriverQueryCtrl extends QueryCtrl { name: 'loading project...', }, metricType: this.defaultDropdownValue, + metricService: this.defaultMetricResourcesValue, + metric: '', aggregation: { crossSeriesReducer: 'REDUCE_MEAN', alignmentPeriod: 'auto', @@ -55,6 +59,8 @@ export class StackdriverQueryCtrl extends QueryCtrl { valueType: '', }; + metricDescriptors: any[]; + metrics: any[]; groupBySegments: any[]; removeSegment: any; showHelp: boolean; @@ -69,10 +75,10 @@ export class StackdriverQueryCtrl extends QueryCtrl { constructor($scope, $injector, private uiSegmentSrv, private templateSrv) { super($scope, $injector); _.defaultsDeep(this.target, this.defaults); - + this.metricDescriptors = []; + this.metrics = []; 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.getLabels.bind(this)); @@ -118,18 +124,60 @@ export class StackdriverQueryCtrl extends QueryCtrl { } async getMetricTypes() { - //projects/your-project-name/metricDescriptors/agent.googleapis.com/agent/api_request_count 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.id, text: mt.id })); + + return metricTypes.map(mt => ({ value: mt.type, text: mt.type })); } else { return []; } } + getMetricServices() { + const defaultValue = { value: this.defaultMetricResourcesValue, text: this.defaultMetricResourcesValue }; + const resources = this.metricDescriptors.map(m => { + const [resource] = m.type.split('/'); + const [service] = resource.split('.'); + return { + value: resource, + text: service, + }; + }); + return resources.length > 0 ? [defaultValue, ..._.uniqBy(resources, 'value')] : []; + } + + getMetrics() { + const metrics = this.metricDescriptors.map(m => { + const [resource] = m.type.split('/'); + const [service] = resource.split('.'); + return { + resource, + value: m.type, + service, + text: m.displayName, + title: m.description, + }; + }); + if (this.target.metricService === this.defaultMetricResourcesValue) { + return metrics.map(m => ({ ...m, text: `${m.service} - ${m.text}` })); + } else { + return metrics.filter(m => m.resource === this.target.metricService); + } + } + + onResourceTypeChange(resource) { + this.metrics = this.getMetrics(); + if (!this.metrics.find(m => m.value === this.target.metricType)) { + this.target.metricType = this.defaultDropdownValue; + } else { + this.refresh(); + } + } + async getLabels() { this.loadLabelsPromise = new Promise(async resolve => { try {