Browse Source

stackdriver: move getLabels from query_ctrl to datasource

so can reuse for annotation query ctrl.
Daniel Lee 7 years ago
parent
commit
c9ee05422f

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

@@ -5,7 +5,7 @@ export default class StackdriverDatasource {
   baseUrl: string;
   projectName: string;
 
-  constructor(instanceSettings, private backendSrv, private templateSrv) {
+  constructor(instanceSettings, private backendSrv, private templateSrv, private timeSrv) {
     this.baseUrl = `/stackdriver/`;
     this.url = instanceSettings.url;
     this.doRequest = this.doRequest;
@@ -54,6 +54,23 @@ export default class StackdriverDatasource {
     return data;
   }
 
+  async getLabels(metricType, refId) {
+    return await this.getTimeSeries({
+      targets: [
+        {
+          refId: refId,
+          datasourceId: this.id,
+          metricType: this.templateSrv.replace(metricType),
+          aggregation: {
+            crossSeriesReducer: 'REDUCE_NONE',
+          },
+          view: 'HEADERS',
+        },
+      ],
+      range: this.timeSrv.timeRange(),
+    });
+  }
+
   interpolateGroupBys(groupBys: string[], scopedVars): string[] {
     let interpolatedGroupBys = [];
     (groupBys || []).forEach(gb => {

+ 3 - 15
public/app/plugins/datasource/stackdriver/query_ctrl.ts

@@ -62,7 +62,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
   filterSegments: any;
 
   /** @ngInject */
-  constructor($scope, $injector, private uiSegmentSrv, private timeSrv, private templateSrv) {
+  constructor($scope, $injector, private uiSegmentSrv, private templateSrv) {
     super($scope, $injector);
     _.defaultsDeep(this.target, this.defaults);
 
@@ -130,25 +130,13 @@ export class StackdriverQueryCtrl extends QueryCtrl {
   async getLabels() {
     this.loadLabelsPromise = new Promise(async resolve => {
       try {
-        const data = await this.datasource.getTimeSeries({
-          targets: [
-            {
-              refId: this.target.refId,
-              datasourceId: this.datasource.id,
-              metricType: this.templateSrv.replace(this.target.metricType),
-              aggregation: {
-                crossSeriesReducer: 'REDUCE_NONE',
-              },
-              view: 'HEADERS',
-            },
-          ],
-          range: this.timeSrv.timeRange(),
-        });
+        const data = await this.datasource.getLabels(this.target.metricType, this.target.refId);
 
         this.metricLabels = data.results[this.target.refId].meta.metricLabels;
         this.resourceLabels = data.results[this.target.refId].meta.resourceLabels;
         resolve();
       } catch (error) {
+        appEvents.emit('ds-request-error', 'Error loading metric labels for ' + this.target.metricType);
         resolve();
       }
     });

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

@@ -408,7 +408,7 @@ function createCtrlWithFakes(existingFilters?: string[]) {
       return { type: 'condition', value: val };
     },
   };
-  return new StackdriverQueryCtrl(null, null, fakeSegmentServer, null, new TemplateSrvStub());
+  return new StackdriverQueryCtrl(null, null, fakeSegmentServer, new TemplateSrvStub());
 }
 
 function createTarget(existingFilters?: string[]) {