diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index d044c2b7e87..683cf42feca 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -9,6 +9,10 @@ export default class StackdriverDatasource { this.doRequest = this.doRequest; } + query() { + return Promise.resolve(); + } + testDatasource() { const path = `v3/projects/raintank-production/metricDescriptors`; return this.doRequest(`${this.baseUrl}${path}`) @@ -48,6 +52,16 @@ export default class StackdriverDatasource { return response.data.projects.map(p => ({ id: p.projectId, name: p.name })); } + async getMetricTypes(projectId: string) { + try { + const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`; + const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`); + return data.metricDescriptors.map(m => ({ id: m.name, name: m.displayName })); + } catch (error) { + console.log(error); + } + } + async doRequest(url, maxRetries = 1) { return this.backendSrv .datasourceRequest({ diff --git a/public/app/plugins/datasource/stackdriver/module.ts b/public/app/plugins/datasource/stackdriver/module.ts index 104ac13b422..eba3bcfb950 100644 --- a/public/app/plugins/datasource/stackdriver/module.ts +++ b/public/app/plugins/datasource/stackdriver/module.ts @@ -1,5 +1,5 @@ import StackdriverDatasource from './datasource'; -// import { StackdriverQueryCtrl } from './query_ctrl'; +import { StackdriverQueryCtrl } from './query_ctrl'; import { StackdriverConfigCtrl } from './config_ctrl'; // class AnnotationsQueryCtrl { @@ -8,7 +8,7 @@ import { StackdriverConfigCtrl } from './config_ctrl'; export { StackdriverDatasource as Datasource, - // StackdriverQueryCtrl as QueryCtrl, + StackdriverQueryCtrl as QueryCtrl, StackdriverConfigCtrl as ConfigCtrl, // AnnotationsQueryCtrl, }; diff --git a/public/app/plugins/datasource/stackdriver/partials/query.editor.html b/public/app/plugins/datasource/stackdriver/partials/query.editor.html index 51c25100c1e..337830d2674 100755 --- a/public/app/plugins/datasource/stackdriver/partials/query.editor.html +++ b/public/app/plugins/datasource/stackdriver/partials/query.editor.html @@ -1,81 +1,16 @@ - -
- -
- -
-
-
- -
- -
- - - - -
- - - - - -
- -
- -
-
-
-
- -
-
- -
- -
- -
- - - -
-
-
+
+
+ Project +
- - +
+
+ Metric Type + +
+
+ \ No newline at end of file diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index 94389237eb2..98c7f7447fe 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -1,11 +1,65 @@ -import './add_graphite_func'; -import './func_editor'; +import _ from 'lodash'; import { QueryCtrl } from 'app/plugins/sdk'; +import appEvents from 'app/core/app_events'; export class StackdriverQueryCtrl extends QueryCtrl { static templateUrl = 'partials/query.editor.html'; + project: { + id: string; + name: string; + }; + metricType: string; + defaultDropdownValue = 'select'; + /** @ngInject */ constructor($scope, $injector) { super($scope, $injector); + this.project = { + id: 'default', + name: 'loading project...', + }; + this.metricType = this.defaultDropdownValue; + + this.getCurrentProject().then(this.getMetricTypes.bind(this)); + } + + async getCurrentProject() { + try { + const projects = await this.datasource.getProjects(); + if (projects && projects.length > 0) { + this.project = this.project = projects[0]; + } else { + throw new Error('No projects found'); + } + } catch (error) { + let message = 'Projects cannot be fetched: '; + message += error.statusText ? error.statusText + ': ' : ''; + if (error && error.data && error.data.error && error.data.error.message) { + if (error.data.error.code === 403) { + message += ` + A list of projects could not be fetched from the Google Cloud Resource Manager API. + You might need to enable it first: + https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`; + } else { + message += error.data.error.code + '. ' + error.data.error.message; + } + } else { + message += 'Cannot connect to Stackdriver API'; + } + appEvents.emit('ds-request-error', message); + } + } + + async getMetricTypes() { + //projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/api_request_count + if (this.project.id !== 'default') { + const metricTypes = await this.datasource.getMetricTypes(this.project.id); + if (this.metricType === this.defaultDropdownValue && metricTypes.length > 0) { + this.$scope.$apply(() => (this.metricType = metricTypes[0].name)); + } + return metricTypes.map(mt => ({ value: mt.id, text: mt.id })); + } else { + return []; + } } }