From b8c2ba332059da29edc59762cfedd9b6f68c3446 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Tue, 8 Jan 2019 14:03:52 +0100 Subject: [PATCH] add typing for metric descriptor --- .../datasource/stackdriver/components/Metrics.tsx | 11 ++++++----- .../datasource/stackdriver/components/QueryEditor.tsx | 4 ++-- .../app/plugins/datasource/stackdriver/datasource.ts | 3 ++- public/app/plugins/datasource/stackdriver/types.ts | 11 +++++++++++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/components/Metrics.tsx b/public/app/plugins/datasource/stackdriver/components/Metrics.tsx index 52c0c8ad916..3c04cb07183 100644 --- a/public/app/plugins/datasource/stackdriver/components/Metrics.tsx +++ b/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 { 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 { this.props.onChange({ ...metricDescriptor, type: value }); } - getServicesList(metricDescriptors) { + getServicesList(metricDescriptors: MetricDescriptor[]) { const services = metricDescriptors.map(m => ({ value: m.service, label: _.startCase(m.serviceShortName), diff --git a/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx b/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx index 0b101ae3ad7..52c77abc879 100644 --- a/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx +++ b/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 { 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 }, diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index c5b66079154..3e29747cc95 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/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 { try { if (this.metricTypes.length === 0) { const metricsApiPath = `v3/projects/${projectName}/metricDescriptors`; diff --git a/public/app/plugins/datasource/stackdriver/types.ts b/public/app/plugins/datasource/stackdriver/types.ts index c3259649d81..29b12b4289d 100644 --- a/public/app/plugins/datasource/stackdriver/types.ts +++ b/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; +}