Kaynağa Gözat

add typing for metric descriptor

Erik Sundell 7 yıl önce
ebeveyn
işleme
b8c2ba3320

+ 6 - 5
public/app/plugins/datasource/stackdriver/components/Metrics.tsx

@@ -2,10 +2,11 @@ import React from 'react';
 import _ from 'lodash';
 
 import appEvents from 'app/core/app_events';
+import { MetricDescriptor } from '../types';
 import { MetricSelect } from 'app/core/components/Select/MetricSelect';
 
 export interface Props {
-  onChange: (metricDescriptor) => void;
+  onChange: (metricDescriptor: MetricDescriptor) => void;
   templateSrv: any;
   datasource: any;
   defaultProject: string;
@@ -14,12 +15,12 @@ export interface Props {
 }
 
 interface State {
-  metricDescriptors: any[];
+  metricDescriptors: MetricDescriptor[];
   metrics: any[];
   services: any[];
   service: string;
   metric: string;
-  metricDescriptor: any;
+  metricDescriptor: MetricDescriptor;
   defaultProject: string;
 }
 
@@ -84,7 +85,7 @@ export class Metrics extends React.Component<Props, State> {
     return this.state.metricDescriptors.find(md => md.type === this.props.templateSrv.replace(metricType));
   }
 
-  getMetricsList(metricDescriptors) {
+  getMetricsList(metricDescriptors: MetricDescriptor[]) {
     const selectedMetricDescriptor = this.getSelectedMetricDescriptor(this.props.metricType);
     if (!selectedMetricDescriptor) {
       return [];
@@ -122,7 +123,7 @@ export class Metrics extends React.Component<Props, State> {
     this.props.onChange({ ...metricDescriptor, type: value });
   }
 
-  getServicesList(metricDescriptors) {
+  getServicesList(metricDescriptors: MetricDescriptor[]) {
     const services = metricDescriptors.map(m => ({
       value: m.service,
       label: _.startCase(m.serviceShortName),

+ 2 - 2
public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx

@@ -8,7 +8,7 @@ import { Alignments } from './Alignments';
 import { AlignmentPeriods } from './AlignmentPeriods';
 import { AliasBy } from './AliasBy';
 import { Help } from './Help';
-import { Target } from '../types';
+import { Target, MetricDescriptor } from '../types';
 import { getAlignmentPickerData } from '../functions';
 
 export interface Props {
@@ -92,7 +92,7 @@ export class QueryEditor extends React.Component<Props, State> {
     this.setState({ lastQuery, lastQueryError });
   }
 
-  onMetricTypeChange({ valueType, metricKind, type, unit }) {
+  onMetricTypeChange({ valueType, metricKind, type, unit }: MetricDescriptor) {
     const { templateSrv, onQueryChange, onExecuteQuery } = this.props;
     const { perSeriesAligner, alignOptions } = getAlignmentPickerData(
       { valueType, metricKind, perSeriesAligner: this.state.perSeriesAligner },

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

@@ -2,6 +2,7 @@ import { stackdriverUnitMappings } from './constants';
 import appEvents from 'app/core/app_events';
 import _ from 'lodash';
 import StackdriverMetricFindQuery from './StackdriverMetricFindQuery';
+import { MetricDescriptor } from './types';
 
 export default class StackdriverDatasource {
   id: number;
@@ -253,7 +254,7 @@ export default class StackdriverDatasource {
     }
   }
 
-  async getMetricTypes(projectName: string) {
+  async getMetricTypes(projectName: string): Promise<MetricDescriptor[]> {
     try {
       if (this.metricTypes.length === 0) {
         const metricsApiPath = `v3/projects/${projectName}/metricDescriptors`;

+ 11 - 0
public/app/plugins/datasource/stackdriver/types.ts

@@ -55,3 +55,14 @@ export interface QueryMeta {
   resourceLabels: { [key: string]: string[] };
   resourceTypes: string[];
 }
+
+export interface MetricDescriptor {
+  valueType: string;
+  metricKind: string;
+  type: string;
+  unit: string;
+  service: string;
+  serviceShortName: string;
+  displayName: string;
+  description: string;
+}