Parcourir la source

stackdriver: add aligner query

Erik Sundell il y a 7 ans
Parent
commit
a0b8c4acba

+ 16 - 1
public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts

@@ -1,4 +1,8 @@
-import { extractServicesFromMetricDescriptors, getMetricTypesByService } from './functions';
+import {
+  extractServicesFromMetricDescriptors,
+  getMetricTypesByService,
+  getAlignmentOptionsByMetric,
+} from './functions';
 import has from 'lodash/has';
 
 export default class StackdriverMetricFindQuery {
@@ -15,6 +19,8 @@ export default class StackdriverMetricFindQuery {
         return this.handleLabelQueryType(query);
       case 'resourceTypes':
         return this.handleResourceType(query);
+      case 'alignerns':
+        return this.handleAlignersType(query);
       default:
         return [];
     }
@@ -85,4 +91,13 @@ export default class StackdriverMetricFindQuery {
       return [];
     }
   }
+
+  async handleAlignersType({ metricType }) {
+    if (!metricType) {
+      return [];
+    }
+    const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
+    const { valueType, metricKind } = metricDescriptors.find(m => m.type === metricType);
+    return getAlignmentOptionsByMetric(valueType, metricKind);
+  }
 }

+ 11 - 0
public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx

@@ -133,6 +133,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue
             {dropdown}
           </React.Fragment>
         );
+      case 'alignerns':
+        return (
+          <React.Fragment>
+            <ServiceSelector metricDescriptors={this.state.metricDescriptors} onServiceChange={this.onServiceChange} />
+            <MetricTypeSelector
+              selectedService={this.state.service}
+              metricDescriptors={this.state.metricDescriptors}
+              onMetricTypeChange={this.onMetricTypeChange}
+            />
+          </React.Fragment>
+        );
       default:
         return '';
     }

+ 9 - 0
public/app/plugins/datasource/stackdriver/functions.ts

@@ -1,6 +1,15 @@
+import { alignOptions } from './constants';
 import uniqBy from 'lodash/uniqBy';
 
 export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy(metricDescriptors, 'service');
 
 export const getMetricTypesByService = (metricDescriptors, service) =>
   metricDescriptors.filter(m => m.service === service);
+
+export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => {
+  return !metricValueType
+    ? []
+    : alignOptions.filter(i => {
+        return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
+      });
+};

+ 2 - 7
public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts

@@ -1,6 +1,7 @@
 import coreModule from 'app/core/core_module';
 import _ from 'lodash';
 import * as options from './constants';
+import { getAlignmentOptionsByMetric } from './functions';
 import kbn from 'app/core/utils/kbn';
 
 export class StackdriverAggregation {
@@ -41,13 +42,7 @@ export class StackdriverAggregationCtrl {
   }
 
   setAlignOptions() {
-    this.alignOptions = !this.target.valueType
-      ? []
-      : options.alignOptions.filter(i => {
-          return (
-            i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
-          );
-        });
+    this.alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
     if (!this.alignOptions.find(o => o.value === this.target.aggregation.perSeriesAligner)) {
       this.target.aggregation.perSeriesAligner = this.alignOptions.length > 0 ? this.alignOptions[0].value : '';
     }