VariableQueryEditor.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import React, { PureComponent } from 'react';
  2. import uniqBy from 'lodash/uniqBy';
  3. import { VariableQueryProps } from 'app/types/plugins';
  4. import SimpleSelect from './SimpleSelect';
  5. import { getMetricTypes, getLabelKeys } from '../functions';
  6. import { MetricFindQueryTypes, VariableQueryData } from '../types';
  7. export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryProps, VariableQueryData> {
  8. queryTypes: Array<{ value: string; name: string }> = [
  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 = uniqBy(metricDescriptors, 'service').map(m => ({
  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. 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 handleQueryTypeChange(event) {
  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) {
  67. const { metricTypes, selectedMetricType } = getMetricTypes(
  68. this.state.metricDescriptors,
  69. this.state.selectedMetricType,
  70. this.props.templateSrv.replace(this.state.selectedMetricType),
  71. 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) {
  82. const state: any = { selectedMetricType: event.target.value, ...await this.getLabels(event.target.value) };
  83. this.setState(state);
  84. }
  85. onLabelKeyChange(event) {
  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, 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) {
  105. const templateVariables = this.props.templateSrv.variables.map(v => ({ name: `$${v.name}`, value: `$${v.name}` }));
  106. return [...templateVariables, ...options];
  107. }
  108. renderQueryTypeSwitch(queryType) {
  109. switch (queryType) {
  110. case MetricFindQueryTypes.MetricTypes:
  111. return (
  112. <SimpleSelect
  113. value={this.state.selectedService}
  114. options={this.state.services}
  115. onValueChange={e => this.onServiceChange(e)}
  116. label="Services"
  117. />
  118. );
  119. case MetricFindQueryTypes.LabelKeys:
  120. case MetricFindQueryTypes.LabelValues:
  121. case MetricFindQueryTypes.ResourceTypes:
  122. return (
  123. <React.Fragment>
  124. <SimpleSelect
  125. value={this.state.selectedService}
  126. options={this.state.services}
  127. onValueChange={e => this.onServiceChange(e)}
  128. label="Services"
  129. />
  130. <SimpleSelect
  131. value={this.state.selectedMetricType}
  132. options={this.insertTemplateVariables(this.state.metricTypes)}
  133. onValueChange={e => this.onMetricTypeChange(e)}
  134. label="Metric Types"
  135. />
  136. {queryType === MetricFindQueryTypes.LabelValues && (
  137. <SimpleSelect
  138. value={this.state.labelKey}
  139. options={this.insertTemplateVariables(this.state.labels.map(l => ({ value: l, name: l })))}
  140. onValueChange={e => this.onLabelKeyChange(e)}
  141. label="Label Keys"
  142. />
  143. )}
  144. </React.Fragment>
  145. );
  146. case MetricFindQueryTypes.Aligners:
  147. case MetricFindQueryTypes.Aggregations:
  148. return (
  149. <React.Fragment>
  150. <SimpleSelect
  151. value={this.state.selectedService}
  152. options={this.state.services}
  153. onValueChange={e => this.onServiceChange(e)}
  154. label="Services"
  155. />
  156. <SimpleSelect
  157. value={this.state.selectedMetricType}
  158. options={this.insertTemplateVariables(this.state.metricTypes)}
  159. onValueChange={e => this.onMetricTypeChange(e)}
  160. label="Metric Types"
  161. />
  162. </React.Fragment>
  163. );
  164. default:
  165. return '';
  166. }
  167. }
  168. render() {
  169. return (
  170. <React.Fragment>
  171. <SimpleSelect
  172. value={this.state.selectedQueryType}
  173. options={this.queryTypes}
  174. onValueChange={e => this.handleQueryTypeChange(e)}
  175. label="Query Types"
  176. />
  177. {this.renderQueryTypeSwitch(this.state.selectedQueryType)}
  178. </React.Fragment>
  179. );
  180. }
  181. }