Browse Source

stackdriver: remove hardcoding of test project name

Daniel Lee 7 years ago
parent
commit
e05b86375b

+ 2 - 11
pkg/tsdb/stackdriver/stackdriver.go

@@ -138,7 +138,6 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
 
 
 func setAggParams(params *url.Values, query *tsdb.Query) {
 func setAggParams(params *url.Values, query *tsdb.Query) {
 	primaryAggregation := query.Model.Get("primaryAggregation").MustString()
 	primaryAggregation := query.Model.Get("primaryAggregation").MustString()
-	secondaryAggregation := query.Model.Get("secondaryAggregation").MustString()
 	perSeriesAligner := query.Model.Get("perSeriesAligner").MustString()
 	perSeriesAligner := query.Model.Get("perSeriesAligner").MustString()
 	alignmentPeriod := query.Model.Get("alignmentPeriod").MustString()
 	alignmentPeriod := query.Model.Get("alignmentPeriod").MustString()
 
 
@@ -146,18 +145,10 @@ func setAggParams(params *url.Values, query *tsdb.Query) {
 		primaryAggregation = "REDUCE_NONE"
 		primaryAggregation = "REDUCE_NONE"
 	}
 	}
 
 
-	if secondaryAggregation == "" {
-		secondaryAggregation = "REDUCE_NONE"
-	}
-
 	if perSeriesAligner == "" {
 	if perSeriesAligner == "" {
 		perSeriesAligner = "ALIGN_MEAN"
 		perSeriesAligner = "ALIGN_MEAN"
 	}
 	}
 
 
-	if secondaryAggregation == "" {
-		secondaryAggregation = "REDUCE_NONE"
-	}
-
 	if alignmentPeriod == "auto" || alignmentPeriod == "" {
 	if alignmentPeriod == "auto" || alignmentPeriod == "" {
 		alignmentPeriodValue := int(math.Max(float64(query.IntervalMs), 60.0))
 		alignmentPeriodValue := int(math.Max(float64(query.IntervalMs), 60.0))
 		alignmentPeriod = "+" + strconv.Itoa(alignmentPeriodValue) + "s"
 		alignmentPeriod = "+" + strconv.Itoa(alignmentPeriodValue) + "s"
@@ -172,7 +163,6 @@ func setAggParams(params *url.Values, query *tsdb.Query) {
 	params.Add("aggregation.crossSeriesReducer", primaryAggregation)
 	params.Add("aggregation.crossSeriesReducer", primaryAggregation)
 	params.Add("aggregation.perSeriesAligner", perSeriesAligner)
 	params.Add("aggregation.perSeriesAligner", perSeriesAligner)
 	params.Add("aggregation.alignmentPeriod", alignmentPeriod)
 	params.Add("aggregation.alignmentPeriod", alignmentPeriod)
-	// params.Add("aggregation.secondaryAggregation.crossSeriesReducer", secondaryAggregation)
 
 
 	groupBys := query.Model.Get("groupBys").MustArray()
 	groupBys := query.Model.Get("groupBys").MustArray()
 	if len(groupBys) > 0 {
 	if len(groupBys) > 0 {
@@ -317,7 +307,8 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
 	if !ok {
 	if !ok {
 		return nil, errors.New("Unable to find datasource plugin Stackdriver")
 		return nil, errors.New("Unable to find datasource plugin Stackdriver")
 	}
 	}
-	proxyPass := fmt.Sprintf("stackdriver%s", "v3/projects/raintank-production/timeSeries")
+	projectName := dsInfo.JsonData.Get("defaultProject").MustString()
+	proxyPass := fmt.Sprintf("stackdriver%s", "v3/projects/"+projectName+"/timeSeries")
 
 
 	var stackdriverRoute *plugins.AppPluginRoute
 	var stackdriverRoute *plugins.AppPluginRoute
 	for _, route := range plugin.Routes {
 	for _, route := range plugin.Routes {

+ 3 - 1
public/app/plugins/datasource/stackdriver/datasource.ts

@@ -3,12 +3,14 @@ export default class StackdriverDatasource {
   id: number;
   id: number;
   url: string;
   url: string;
   baseUrl: string;
   baseUrl: string;
+  projectName: string;
 
 
   constructor(instanceSettings, private backendSrv) {
   constructor(instanceSettings, private backendSrv) {
     this.baseUrl = `/stackdriver/`;
     this.baseUrl = `/stackdriver/`;
     this.url = instanceSettings.url;
     this.url = instanceSettings.url;
     this.doRequest = this.doRequest;
     this.doRequest = this.doRequest;
     this.id = instanceSettings.id;
     this.id = instanceSettings.id;
+    this.projectName = instanceSettings.jsonData.defaultProject || '';
   }
   }
 
 
   async getTimeSeries(options) {
   async getTimeSeries(options) {
@@ -73,7 +75,7 @@ export default class StackdriverDatasource {
   }
   }
 
 
   testDatasource() {
   testDatasource() {
-    const path = `v3/projects/raintank-production/metricDescriptors`;
+    const path = `v3/projects/${this.projectName}/metricDescriptors`;
     return this.doRequest(`${this.baseUrl}${path}`)
     return this.doRequest(`${this.baseUrl}${path}`)
       .then(response => {
       .then(response => {
         if (response.status === 200) {
         if (response.status === 200) {

+ 1 - 1
public/app/plugins/datasource/stackdriver/query_ctrl.ts

@@ -134,7 +134,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
   }
   }
 
 
   async getMetricTypes() {
   async getMetricTypes() {
-    //projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/api_request_count
+    //projects/your-project-name/metricDescriptors/agent.googleapis.com/agent/api_request_count
     if (this.target.project.id !== 'default') {
     if (this.target.project.id !== 'default') {
       const metricTypes = await this.datasource.getMetricTypes(this.target.project.id);
       const metricTypes = await this.datasource.getMetricTypes(this.target.project.id);
       if (this.target.metricType === this.defaultDropdownValue && metricTypes.length > 0) {
       if (this.target.metricType === this.defaultDropdownValue && metricTypes.length > 0) {

+ 11 - 6
public/app/plugins/datasource/stackdriver/specs/datasource.test.ts

@@ -3,6 +3,11 @@ import { metricDescriptors } from './testData';
 import moment from 'moment';
 import moment from 'moment';
 
 
 describe('StackdriverDataSource', () => {
 describe('StackdriverDataSource', () => {
+  const instanceSettings = {
+    jsonData: {
+      projectName: 'testproject',
+    },
+  };
   describe('when performing testDataSource', () => {
   describe('when performing testDataSource', () => {
     describe('and call to stackdriver api succeeds', () => {
     describe('and call to stackdriver api succeeds', () => {
       let ds;
       let ds;
@@ -13,7 +18,7 @@ describe('StackdriverDataSource', () => {
             return Promise.resolve({ status: 200 });
             return Promise.resolve({ status: 200 });
           },
           },
         };
         };
-        ds = new StackdriverDataSource({}, backendSrv);
+        ds = new StackdriverDataSource(instanceSettings, backendSrv);
         result = await ds.testDatasource();
         result = await ds.testDatasource();
       });
       });
       it('should return successfully', () => {
       it('should return successfully', () => {
@@ -28,7 +33,7 @@ describe('StackdriverDataSource', () => {
         const backendSrv = {
         const backendSrv = {
           datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
           datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
         };
         };
-        ds = new StackdriverDataSource({}, backendSrv);
+        ds = new StackdriverDataSource(instanceSettings, backendSrv);
         result = await ds.testDatasource();
         result = await ds.testDatasource();
       });
       });
       it('should return status success', () => {
       it('should return status success', () => {
@@ -47,7 +52,7 @@ describe('StackdriverDataSource', () => {
               data: { error: { code: 400, message: 'Field interval.endTime had an invalid value' } },
               data: { error: { code: 400, message: 'Field interval.endTime had an invalid value' } },
             }),
             }),
         };
         };
-        ds = new StackdriverDataSource({}, backendSrv);
+        ds = new StackdriverDataSource(instanceSettings, backendSrv);
         result = await ds.testDatasource();
         result = await ds.testDatasource();
       });
       });
 
 
@@ -83,7 +88,7 @@ describe('StackdriverDataSource', () => {
             return Promise.resolve({ status: 200, data: response });
             return Promise.resolve({ status: 200, data: response });
           },
           },
         };
         };
-        ds = new StackdriverDataSource({}, backendSrv);
+        ds = new StackdriverDataSource(instanceSettings, backendSrv);
         result = await ds.getProjects();
         result = await ds.getProjects();
       });
       });
 
 
@@ -132,7 +137,7 @@ describe('StackdriverDataSource', () => {
         const backendSrv = {
         const backendSrv = {
           datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
           datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
         };
         };
-        ds = new StackdriverDataSource({}, backendSrv);
+        ds = new StackdriverDataSource(instanceSettings, backendSrv);
       });
       });
 
 
       it('should return a list of datapoints', () => {
       it('should return a list of datapoints', () => {
@@ -166,7 +171,7 @@ describe('StackdriverDataSource', () => {
           });
           });
         },
         },
       };
       };
-      ds = new StackdriverDataSource({}, backendSrv);
+      ds = new StackdriverDataSource(instanceSettings, backendSrv);
       result = await ds.getMetricTypes();
       result = await ds.getMetricTypes();
     });
     });
     it('should return successfully', () => {
     it('should return successfully', () => {

+ 1 - 0
public/app/plugins/datasource/stackdriver/specs/query_ctrl.test.ts

@@ -401,6 +401,7 @@ function createTarget(existingFilters?: string[]) {
     refId: 'A',
     refId: 'A',
     aggregation: {
     aggregation: {
       crossSeriesReducer: '',
       crossSeriesReducer: '',
+      secondaryCrossSeriesReducer: '',
       alignmentPeriod: '',
       alignmentPeriod: '',
       perSeriesAligner: '',
       perSeriesAligner: '',
       groupBys: [],
       groupBys: [],

+ 2 - 2
public/app/plugins/datasource/stackdriver/specs/testData.ts

@@ -1,6 +1,6 @@
 export const metricDescriptors = [
 export const metricDescriptors = [
   {
   {
-    name: 'projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/api_request_count',
+    name: 'projects/grafana-prod/metricDescriptors/agent.googleapis.com/agent/api_request_count',
     labels: [
     labels: [
       {
       {
         key: 'state',
         key: 'state',
@@ -20,7 +20,7 @@ export const metricDescriptors = [
     },
     },
   },
   },
   {
   {
-    name: 'projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/log_entry_count',
+    name: 'projects/grafana-prod/metricDescriptors/agent.googleapis.com/agent/log_entry_count',
     labels: [
     labels: [
       {
       {
         key: 'response_code',
         key: 'response_code',