From ba4d52e0482d091ecdc70c7fb9f4a6afaf5464ee Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 25 Oct 2018 17:00:32 +0200 Subject: [PATCH] stackdriver: add selector components for service and metric type --- .../datasource/stackdriver/metricTypes.tsx | 43 +++++++++++ .../datasource/stackdriver/services.tsx | 32 ++++++++ .../stackdriver/templateQueryCtrl.tsx | 73 +++++++++++++++---- 3 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 public/app/plugins/datasource/stackdriver/metricTypes.tsx create mode 100644 public/app/plugins/datasource/stackdriver/services.tsx diff --git a/public/app/plugins/datasource/stackdriver/metricTypes.tsx b/public/app/plugins/datasource/stackdriver/metricTypes.tsx new file mode 100644 index 00000000000..0e844f49439 --- /dev/null +++ b/public/app/plugins/datasource/stackdriver/metricTypes.tsx @@ -0,0 +1,43 @@ +import React, { SFC } from 'react'; +import uniqBy from 'lodash/uniqBy'; + +interface Props { + onMetricTypeChanged: any; + selectedService: string; + metricDescriptors: any[]; +} + +const MetricTypes: SFC = props => { + const extractMetricTypes = () => { + if (!props.selectedService) { + return []; + } + + return props.metricDescriptors.filter(m => m.service === props.selectedService).map(m => ({ + value: m.service, + name: m.displayName, + })); + }; + + uniqBy(props.metricDescriptors, 'service').map(m => ({ + value: m.service, + name: m.serviceShortName, + })); + + return ( +
+ Metric Types +
+ +
+
+ ); +}; + +export default MetricTypes; diff --git a/public/app/plugins/datasource/stackdriver/services.tsx b/public/app/plugins/datasource/stackdriver/services.tsx new file mode 100644 index 00000000000..e8da82933d2 --- /dev/null +++ b/public/app/plugins/datasource/stackdriver/services.tsx @@ -0,0 +1,32 @@ +import React, { SFC } from 'react'; +import uniqBy from 'lodash/uniqBy'; + +interface Props { + onServiceChange: any; + metricDescriptors: any[]; +} + +const Services: SFC = props => { + const extractServices = () => + uniqBy(props.metricDescriptors, 'service').map(m => ({ + value: m.service, + name: m.serviceShortName, + })); + + return ( +
+ Service +
+ +
+
+ ); +}; + +export default Services; diff --git a/public/app/plugins/datasource/stackdriver/templateQueryCtrl.tsx b/public/app/plugins/datasource/stackdriver/templateQueryCtrl.tsx index 4900c4b15da..20a2cf28077 100644 --- a/public/app/plugins/datasource/stackdriver/templateQueryCtrl.tsx +++ b/public/app/plugins/datasource/stackdriver/templateQueryCtrl.tsx @@ -1,5 +1,7 @@ import React, { PureComponent } from 'react'; import StackdriverDatasource from './datasource'; +import Services from './services'; +import MetricTypes from './metricTypes'; interface Props { datasource: StackdriverDatasource; @@ -8,29 +10,74 @@ interface Props { } export class StackdriverTemplateQueryCtrl extends PureComponent { + queryTypes: Array<{ value: string; name: string }> = [ + { value: 'services', name: 'Services' }, + { value: 'metricTypes', name: 'Metric Types' }, + { value: 'metricLabels', name: 'Metric labels For Metric Type' }, + ]; + constructor(props) { super(props); + this.handleChange = this.handleChange.bind(this); + this.onServiceChange = this.onServiceChange.bind(this); + this.onMetricTypeChanged = this.onMetricTypeChanged.bind(this); + this.state = { queryType: undefined, metricDescriptors: [], service: undefined, metricType: undefined }; } async componentDidMount() { const metricDescriptors = await this.props.datasource.getMetricTypes(this.props.datasource.projectName); - console.log(metricDescriptors); + this.setState({ metricDescriptors }); + } + + handleChange(event) { + this.setState({ queryType: event.target.value }); + } + + onServiceChange(event) { + this.setState({ service: event.target.value }); + } + + onMetricTypeChanged(event) { + this.setState({ metricType: event.target.value }); + } + + renderSwitch(queryType) { + switch (queryType) { + case 'metricTypes': + return ; + case 'metricLabels': + return ( + + + + + ); + default: + return ''; + } } render() { return ( -
- Query - -
+ +
+ Query Type +
+ +
+
+ {this.renderSwitch(this.state.queryType)} +
); } }