| 
									
										
										
										
											2018-10-24 20:11:10 +08:00
										 |  |  | import React, { PureComponent } from 'react'; | 
					
						
							| 
									
										
										
										
											2018-11-13 17:57:10 +08:00
										 |  |  | import { VariableQueryProps } from 'app/types/plugins'; | 
					
						
							| 
									
										
										
										
											2018-10-31 20:55:40 +08:00
										 |  |  | import SimpleSelect from './SimpleSelect'; | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  | import { getMetricTypes, getLabelKeys, extractServicesFromMetricDescriptors } from '../functions'; | 
					
						
							| 
									
										
										
										
											2018-11-13 17:57:10 +08:00
										 |  |  | import { MetricFindQueryTypes, VariableQueryData } from '../types'; | 
					
						
							| 
									
										
										
										
											2018-10-24 20:11:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 17:57:10 +08:00
										 |  |  | export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryProps, VariableQueryData> { | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |   queryTypes: Array<{ value: string; name: string }> = [ | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |     { value: MetricFindQueryTypes.Services, name: 'Services' }, | 
					
						
							| 
									
										
										
										
											2018-10-30 00:38:43 +08:00
										 |  |  |     { value: MetricFindQueryTypes.MetricTypes, name: 'Metric Types' }, | 
					
						
							| 
									
										
										
										
											2018-11-19 18:34:40 +08:00
										 |  |  |     { value: MetricFindQueryTypes.LabelKeys, name: 'Label Keys' }, | 
					
						
							|  |  |  |     { value: MetricFindQueryTypes.LabelValues, name: 'Label Values' }, | 
					
						
							| 
									
										
										
										
											2018-10-30 00:38:43 +08:00
										 |  |  |     { value: MetricFindQueryTypes.ResourceTypes, name: 'Resource Types' }, | 
					
						
							|  |  |  |     { value: MetricFindQueryTypes.Aggregations, name: 'Aggregations' }, | 
					
						
							| 
									
										
										
										
											2018-11-09 21:36:04 +08:00
										 |  |  |     { value: MetricFindQueryTypes.Aligners, name: 'Aligners' }, | 
					
						
							| 
									
										
										
										
											2018-10-30 00:38:43 +08:00
										 |  |  |     { value: MetricFindQueryTypes.AlignmentPeriods, name: 'Alignment Periods' }, | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |   ]; | 
					
						
							| 
									
										
										
										
											2018-10-29 21:56:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 17:57:10 +08:00
										 |  |  |   defaults: VariableQueryData = { | 
					
						
							| 
									
										
										
										
											2018-11-08 22:00:25 +08:00
										 |  |  |     selectedQueryType: this.queryTypes[0].value, | 
					
						
							| 
									
										
										
										
											2018-10-26 21:57:01 +08:00
										 |  |  |     metricDescriptors: [], | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |     selectedService: '', | 
					
						
							|  |  |  |     selectedMetricType: '', | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |     labels: [], | 
					
						
							|  |  |  |     labelKey: '', | 
					
						
							|  |  |  |     metricTypes: [], | 
					
						
							|  |  |  |     services: [], | 
					
						
							| 
									
										
										
										
											2018-10-26 21:57:01 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 17:57:10 +08:00
										 |  |  |   constructor(props: VariableQueryProps) { | 
					
						
							| 
									
										
										
										
											2018-10-24 20:11:10 +08:00
										 |  |  |     super(props); | 
					
						
							| 
									
										
										
										
											2018-11-09 16:54:11 +08:00
										 |  |  |     this.state = Object.assign(this.defaults, this.props.query); | 
					
						
							| 
									
										
										
										
											2018-10-24 20:11:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 19:53:02 +08:00
										 |  |  |   async componentDidMount() { | 
					
						
							|  |  |  |     const metricDescriptors = await this.props.datasource.getMetricTypes(this.props.datasource.projectName); | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |     const services = extractServicesFromMetricDescriptors(metricDescriptors).map(m => ({ | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |       value: m.service, | 
					
						
							|  |  |  |       name: m.serviceShortName, | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2018-11-08 22:52:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let selectedService = ''; | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |     if (services.some(s => s.value === this.props.templateSrv.replace(this.state.selectedService))) { | 
					
						
							| 
									
										
										
										
											2018-11-08 22:52:00 +08:00
										 |  |  |       selectedService = this.state.selectedService; | 
					
						
							|  |  |  |     } else if (services && services.length > 0) { | 
					
						
							|  |  |  |       selectedService = services[0].value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |     const { metricTypes, selectedMetricType } = getMetricTypes( | 
					
						
							|  |  |  |       metricDescriptors, | 
					
						
							|  |  |  |       this.state.selectedMetricType, | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |       this.props.templateSrv.replace(this.state.selectedMetricType), | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |       this.props.templateSrv.replace(selectedService) | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-10-31 18:50:38 +08:00
										 |  |  |     const state: any = { | 
					
						
							|  |  |  |       services, | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |       selectedService, | 
					
						
							| 
									
										
										
										
											2018-10-31 18:50:38 +08:00
										 |  |  |       metricTypes, | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |       selectedMetricType, | 
					
						
							| 
									
										
										
										
											2018-10-31 18:50:38 +08:00
										 |  |  |       metricDescriptors, | 
					
						
							| 
									
										
										
										
											2019-02-19 22:41:35 +08:00
										 |  |  |       ...(await this.getLabels(selectedMetricType)), | 
					
						
							| 
									
										
										
										
											2018-10-31 18:50:38 +08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-10-31 18:26:56 +08:00
										 |  |  |     this.setState(state); | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 20:37:08 +08:00
										 |  |  |   async onQueryTypeChange(event) { | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |     const state: any = { | 
					
						
							|  |  |  |       selectedQueryType: event.target.value, | 
					
						
							| 
									
										
										
										
											2019-02-19 22:41:35 +08:00
										 |  |  |       ...(await this.getLabels(this.state.selectedMetricType, event.target.value)), | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |     this.setState(state); | 
					
						
							| 
									
										
										
										
											2018-10-26 21:57:01 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |   async onServiceChange(event) { | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |     const { metricTypes, selectedMetricType } = getMetricTypes( | 
					
						
							| 
									
										
										
										
											2018-10-31 20:07:57 +08:00
										 |  |  |       this.state.metricDescriptors, | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |       this.state.selectedMetricType, | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |       this.props.templateSrv.replace(this.state.selectedMetricType), | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |       this.props.templateSrv.replace(event.target.value) | 
					
						
							| 
									
										
										
										
											2018-10-31 20:07:57 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |     const state: any = { | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |       selectedService: event.target.value, | 
					
						
							| 
									
										
										
										
											2018-10-31 20:07:57 +08:00
										 |  |  |       metricTypes, | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |       selectedMetricType, | 
					
						
							| 
									
										
										
										
											2019-02-19 22:41:35 +08:00
										 |  |  |       ...(await this.getLabels(selectedMetricType)), | 
					
						
							| 
									
										
										
										
											2018-10-31 20:07:57 +08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |     this.setState(state); | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |   async onMetricTypeChange(event) { | 
					
						
							| 
									
										
										
										
											2019-02-19 22:41:35 +08:00
										 |  |  |     const state: any = { selectedMetricType: event.target.value, ...(await this.getLabels(event.target.value)) }; | 
					
						
							| 
									
										
										
										
											2018-10-31 17:48:24 +08:00
										 |  |  |     this.setState(state); | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 20:07:57 +08:00
										 |  |  |   onLabelKeyChange(event) { | 
					
						
							|  |  |  |     this.setState({ labelKey: event.target.value }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   componentDidUpdate() { | 
					
						
							| 
									
										
										
										
											2018-11-01 17:14:28 +08:00
										 |  |  |     const { metricDescriptors, labels, metricTypes, services, ...queryModel } = this.state; | 
					
						
							| 
									
										
										
										
											2018-11-09 20:44:58 +08:00
										 |  |  |     const query = this.queryTypes.find(q => q.value === this.state.selectedQueryType); | 
					
						
							|  |  |  |     this.props.onChange(queryModel, `Stackdriver - ${query.name}`); | 
					
						
							| 
									
										
										
										
											2018-10-31 20:07:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |   async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) { | 
					
						
							| 
									
										
										
										
											2018-10-31 18:50:38 +08:00
										 |  |  |     let result = { labels: this.state.labels, labelKey: this.state.labelKey }; | 
					
						
							| 
									
										
										
										
											2018-11-19 18:34:40 +08:00
										 |  |  |     if (selectedMetricType && selectedQueryType === MetricFindQueryTypes.LabelValues) { | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |       const labels = await getLabelKeys(this.props.datasource, selectedMetricType); | 
					
						
							|  |  |  |       const labelKey = labels.some(l => l === this.props.templateSrv.replace(this.state.labelKey)) | 
					
						
							|  |  |  |         ? this.state.labelKey | 
					
						
							|  |  |  |         : labels[0]; | 
					
						
							| 
									
										
										
										
											2018-10-31 18:50:38 +08:00
										 |  |  |       result = { labels, labelKey }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |   insertTemplateVariables(options) { | 
					
						
							|  |  |  |     const templateVariables = this.props.templateSrv.variables.map(v => ({ name: `$${v.name}`, value: `$${v.name}` })); | 
					
						
							|  |  |  |     return [...templateVariables, ...options]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-30 00:42:15 +08:00
										 |  |  |   renderQueryTypeSwitch(queryType) { | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |     switch (queryType) { | 
					
						
							| 
									
										
										
										
											2018-10-30 00:38:43 +08:00
										 |  |  |       case MetricFindQueryTypes.MetricTypes: | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |         return ( | 
					
						
							|  |  |  |           <SimpleSelect | 
					
						
							|  |  |  |             value={this.state.selectedService} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |             options={this.insertTemplateVariables(this.state.services)} | 
					
						
							| 
									
										
										
										
											2018-11-13 18:06:08 +08:00
										 |  |  |             onValueChange={e => this.onServiceChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |             label="Service" | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |           /> | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-11-19 18:34:40 +08:00
										 |  |  |       case MetricFindQueryTypes.LabelKeys: | 
					
						
							|  |  |  |       case MetricFindQueryTypes.LabelValues: | 
					
						
							| 
									
										
										
										
											2018-10-30 00:38:43 +08:00
										 |  |  |       case MetricFindQueryTypes.ResourceTypes: | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |         return ( | 
					
						
							| 
									
										
										
										
											2019-01-08 20:00:31 +08:00
										 |  |  |           <> | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |             <SimpleSelect | 
					
						
							|  |  |  |               value={this.state.selectedService} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |               options={this.insertTemplateVariables(this.state.services)} | 
					
						
							| 
									
										
										
										
											2018-11-13 18:06:08 +08:00
										 |  |  |               onValueChange={e => this.onServiceChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |               label="Service" | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |             /> | 
					
						
							|  |  |  |             <SimpleSelect | 
					
						
							|  |  |  |               value={this.state.selectedMetricType} | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |               options={this.insertTemplateVariables(this.state.metricTypes)} | 
					
						
							| 
									
										
										
										
											2018-11-13 18:06:08 +08:00
										 |  |  |               onValueChange={e => this.onMetricTypeChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |               label="Metric Type" | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |             /> | 
					
						
							| 
									
										
										
										
											2018-11-19 18:34:40 +08:00
										 |  |  |             {queryType === MetricFindQueryTypes.LabelValues && ( | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |               <SimpleSelect | 
					
						
							|  |  |  |                 value={this.state.labelKey} | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |                 options={this.insertTemplateVariables(this.state.labels.map(l => ({ value: l, name: l })))} | 
					
						
							| 
									
										
										
										
											2018-11-13 18:06:08 +08:00
										 |  |  |                 onValueChange={e => this.onLabelKeyChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |                 label="Label Key" | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |               /> | 
					
						
							|  |  |  |             )} | 
					
						
							| 
									
										
										
										
											2019-01-08 20:00:31 +08:00
										 |  |  |           </> | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-11-09 21:36:04 +08:00
										 |  |  |       case MetricFindQueryTypes.Aligners: | 
					
						
							| 
									
										
										
										
											2018-10-30 00:38:43 +08:00
										 |  |  |       case MetricFindQueryTypes.Aggregations: | 
					
						
							| 
									
										
										
										
											2018-10-29 23:27:53 +08:00
										 |  |  |         return ( | 
					
						
							| 
									
										
										
										
											2019-01-08 20:00:31 +08:00
										 |  |  |           <> | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |             <SimpleSelect | 
					
						
							|  |  |  |               value={this.state.selectedService} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:06:30 +08:00
										 |  |  |               options={this.insertTemplateVariables(this.state.services)} | 
					
						
							| 
									
										
										
										
											2018-11-13 18:06:08 +08:00
										 |  |  |               onValueChange={e => this.onServiceChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |               label="Service" | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |             /> | 
					
						
							|  |  |  |             <SimpleSelect | 
					
						
							|  |  |  |               value={this.state.selectedMetricType} | 
					
						
							| 
									
										
										
										
											2018-11-19 21:44:40 +08:00
										 |  |  |               options={this.insertTemplateVariables(this.state.metricTypes)} | 
					
						
							| 
									
										
										
										
											2018-11-13 18:06:08 +08:00
										 |  |  |               onValueChange={e => this.onMetricTypeChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |               label="Metric Type" | 
					
						
							| 
									
										
										
										
											2018-11-01 16:38:51 +08:00
										 |  |  |             /> | 
					
						
							| 
									
										
										
										
											2019-01-08 20:00:31 +08:00
										 |  |  |           </> | 
					
						
							| 
									
										
										
										
											2018-10-29 23:27:53 +08:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-10-25 23:00:32 +08:00
										 |  |  |       default: | 
					
						
							|  |  |  |         return ''; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-24 20:11:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   render() { | 
					
						
							| 
									
										
										
										
											2018-10-25 19:53:02 +08:00
										 |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2019-01-08 20:00:31 +08:00
										 |  |  |       <> | 
					
						
							| 
									
										
										
										
											2018-10-31 20:55:40 +08:00
										 |  |  |         <SimpleSelect | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |           value={this.state.selectedQueryType} | 
					
						
							| 
									
										
										
										
											2018-10-31 17:57:24 +08:00
										 |  |  |           options={this.queryTypes} | 
					
						
							| 
									
										
										
										
											2019-01-08 20:37:08 +08:00
										 |  |  |           onValueChange={e => this.onQueryTypeChange(e)} | 
					
						
							| 
									
										
										
										
											2018-11-19 23:26:06 +08:00
										 |  |  |           label="Query Type" | 
					
						
							| 
									
										
										
										
											2018-10-31 17:57:24 +08:00
										 |  |  |         /> | 
					
						
							| 
									
										
										
										
											2018-10-31 20:36:41 +08:00
										 |  |  |         {this.renderQueryTypeSwitch(this.state.selectedQueryType)} | 
					
						
							| 
									
										
										
										
											2019-01-08 20:00:31 +08:00
										 |  |  |       </> | 
					
						
							| 
									
										
										
										
											2018-10-25 19:53:02 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-10-24 20:11:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |