VariableQueryEditor.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import React, { PureComponent } from 'react';
  2. import { VariableQueryProps } from 'app/types/plugins';
  3. import SimpleSelect from './SimpleSelect';
  4. import { getMetricTypes, getLabelKeys, extractServicesFromMetricDescriptors } from '../functions';
  5. import { MetricFindQueryTypes, VariableQueryData } from '../types';
  6. export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryProps, VariableQueryData> {
  7. queryTypes: Array<{ value: string; name: string }> = [
  8. { value: MetricFindQueryTypes.Services, name: 'Services' },
  9. { value: MetricFindQueryTypes.MetricTypes, name: 'Metric Types' },
  10. { value: MetricFindQueryTypes.LabelKeys, name: 'Label Keys' },
  11. { value: MetricFindQueryTypes.LabelValues, name: 'Label Values' },
  12. { value: MetricFindQueryTypes.ResourceTypes, name: 'Resource Types' },
  13. { value: MetricFindQueryTypes.Aggregations, name: 'Aggregations' },
  14. { value: MetricFindQueryTypes.Aligners, name: 'Aligners' },
  15. { value: MetricFindQueryTypes.AlignmentPeriods, name: 'Alignment Periods' },
  16. ];
  17. defaults: VariableQueryData = {
  18. selectedQueryType: this.queryTypes[0].value,
  19. metricDescriptors: [],
  20. selectedService: '',
  21. selectedMetricType: '',
  22. labels: [],
  23. labelKey: '',
  24. metricTypes: [],
  25. services: [],
  26. };
  27. constructor(props: VariableQueryProps) {
  28. super(props);
  29. this.state = Object.assign(this.defaults, this.props.query);
  30. }
  31. async componentDidMount() {
  32. const metricDescriptors = await this.props.datasource.getMetricTypes(this.props.datasource.projectName);
  33. const services = extractServicesFromMetricDescriptors(metricDescriptors).map((m: any) => ({
  34. value: m.service,
  35. name: m.serviceShortName,
  36. }));
  37. let selectedService = '';
  38. if (services.some(s => s.value === this.props.templateSrv.replace(this.state.selectedService))) {
  39. selectedService = this.state.selectedService;
  40. } else if (services && services.length > 0) {
  41. selectedService = services[0].value;
  42. }
  43. const { metricTypes, selectedMetricType } = getMetricTypes(
  44. metricDescriptors,
  45. this.state.selectedMetricType,
  46. this.props.templateSrv.replace(this.state.selectedMetricType),
  47. this.props.templateSrv.replace(selectedService)
  48. );
  49. const state: any = {
  50. services,
  51. selectedService,
  52. metricTypes,
  53. selectedMetricType,
  54. metricDescriptors,
  55. ...(await this.getLabels(selectedMetricType)),
  56. };
  57. this.setState(state);
  58. }
  59. async onQueryTypeChange(event: any) {
  60. const state: any = {
  61. selectedQueryType: event.target.value,
  62. ...(await this.getLabels(this.state.selectedMetricType, event.target.value)),
  63. };
  64. this.setState(state);
  65. }
  66. async onServiceChange(event: any) {
  67. const { metricTypes, selectedMetricType } = getMetricTypes(
  68. this.state.metricDescriptors,
  69. this.state.selectedMetricType,
  70. this.props.templateSrv.replace(this.state.selectedMetricType),
  71. this.props.templateSrv.replace(event.target.value)
  72. );
  73. const state: any = {
  74. selectedService: event.target.value,
  75. metricTypes,
  76. selectedMetricType,
  77. ...(await this.getLabels(selectedMetricType)),
  78. };
  79. this.setState(state);
  80. }
  81. async onMetricTypeChange(event: any) {
  82. const state: any = { selectedMetricType: event.target.value, ...(await this.getLabels(event.target.value)) };
  83. this.setState(state);
  84. }
  85. onLabelKeyChange(event: any) {
  86. this.setState({ labelKey: event.target.value });
  87. }
  88. componentDidUpdate() {
  89. const { metricDescriptors, labels, metricTypes, services, ...queryModel } = this.state;
  90. const query = this.queryTypes.find(q => q.value === this.state.selectedQueryType);
  91. this.props.onChange(queryModel, `Stackdriver - ${query.name}`);
  92. }
  93. async getLabels(selectedMetricType: string, selectedQueryType = this.state.selectedQueryType) {
  94. let result = { labels: this.state.labels, labelKey: this.state.labelKey };
  95. if (selectedMetricType && selectedQueryType === MetricFindQueryTypes.LabelValues) {
  96. const labels = await getLabelKeys(this.props.datasource, selectedMetricType);
  97. const labelKey = labels.some(l => l === this.props.templateSrv.replace(this.state.labelKey))
  98. ? this.state.labelKey
  99. : labels[0];
  100. result = { labels, labelKey };
  101. }
  102. return result;
  103. }
  104. insertTemplateVariables(options: any) {
  105. const templateVariables = this.props.templateSrv.variables.map((v: any) => ({
  106. name: `$${v.name}`,
  107. value: `$${v.name}`,
  108. }));
  109. return [...templateVariables, ...options];
  110. }
  111. renderQueryTypeSwitch(queryType: string) {
  112. switch (queryType) {
  113. case MetricFindQueryTypes.MetricTypes:
  114. return (
  115. <SimpleSelect
  116. value={this.state.selectedService}
  117. options={this.insertTemplateVariables(this.state.services)}
  118. onValueChange={e => this.onServiceChange(e)}
  119. label="Service"
  120. />
  121. );
  122. case MetricFindQueryTypes.LabelKeys:
  123. case MetricFindQueryTypes.LabelValues:
  124. case MetricFindQueryTypes.ResourceTypes:
  125. return (
  126. <>
  127. <SimpleSelect
  128. value={this.state.selectedService}
  129. options={this.insertTemplateVariables(this.state.services)}
  130. onValueChange={e => this.onServiceChange(e)}
  131. label="Service"
  132. />
  133. <SimpleSelect
  134. value={this.state.selectedMetricType}
  135. options={this.insertTemplateVariables(this.state.metricTypes)}
  136. onValueChange={e => this.onMetricTypeChange(e)}
  137. label="Metric Type"
  138. />
  139. {queryType === MetricFindQueryTypes.LabelValues && (
  140. <SimpleSelect
  141. value={this.state.labelKey}
  142. options={this.insertTemplateVariables(this.state.labels.map(l => ({ value: l, name: l })))}
  143. onValueChange={e => this.onLabelKeyChange(e)}
  144. label="Label Key"
  145. />
  146. )}
  147. </>
  148. );
  149. case MetricFindQueryTypes.Aligners:
  150. case MetricFindQueryTypes.Aggregations:
  151. return (
  152. <>
  153. <SimpleSelect
  154. value={this.state.selectedService}
  155. options={this.insertTemplateVariables(this.state.services)}
  156. onValueChange={e => this.onServiceChange(e)}
  157. label="Service"
  158. />
  159. <SimpleSelect
  160. value={this.state.selectedMetricType}
  161. options={this.insertTemplateVariables(this.state.metricTypes)}
  162. onValueChange={e => this.onMetricTypeChange(e)}
  163. label="Metric Type"
  164. />
  165. </>
  166. );
  167. default:
  168. return '';
  169. }
  170. }
  171. render() {
  172. return (
  173. <>
  174. <SimpleSelect
  175. value={this.state.selectedQueryType}
  176. options={this.queryTypes}
  177. onValueChange={e => this.onQueryTypeChange(e)}
  178. label="Query Type"
  179. />
  180. {this.renderQueryTypeSwitch(this.state.selectedQueryType)}
  181. </>
  182. );
  183. }
  184. }