VariableQueryEditor.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 } 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.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. selectedService
  47. );
  48. const state: any = {
  49. services,
  50. selectedService,
  51. metricTypes,
  52. selectedMetricType,
  53. metricDescriptors,
  54. ...await this.getLabelValues(selectedMetricType),
  55. };
  56. this.setState(state);
  57. }
  58. async handleQueryTypeChange(event) {
  59. const state: any = {
  60. selectedQueryType: event.target.value,
  61. ...await this.getLabelValues(this.state.selectedMetricType, event.target.value),
  62. };
  63. this.setState(state);
  64. }
  65. async onServiceChange(event) {
  66. const { metricTypes, selectedMetricType } = getMetricTypes(
  67. this.state.metricDescriptors,
  68. this.state.selectedMetricType,
  69. event.target.value
  70. );
  71. const state: any = {
  72. selectedService: event.target.value,
  73. metricTypes,
  74. selectedMetricType,
  75. ...await this.getLabelValues(selectedMetricType),
  76. };
  77. this.setState(state);
  78. }
  79. async onMetricTypeChange(event) {
  80. const state: any = { selectedMetricType: event.target.value, ...await this.getLabelValues(event.target.value) };
  81. this.setState(state);
  82. }
  83. onLabelKeyChange(event) {
  84. this.setState({ labelKey: event.target.value });
  85. }
  86. componentDidUpdate() {
  87. const { metricDescriptors, labels, metricTypes, services, ...queryModel } = this.state;
  88. const query = this.queryTypes.find(q => q.value === this.state.selectedQueryType);
  89. this.props.onChange(queryModel, `Stackdriver - ${query.name}`);
  90. }
  91. async getLabelValues(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
  92. let result = { labels: this.state.labels, labelKey: this.state.labelKey };
  93. if (selectedMetricType && selectedQueryType === MetricFindQueryTypes.LabelValues) {
  94. const refId = 'StackdriverVariableQueryEditor';
  95. const response = await this.props.datasource.getLabels(selectedMetricType, refId);
  96. const labels = [...Object.keys(response.meta.resourceLabels), ...Object.keys(response.meta.metricLabels)];
  97. const labelKey = labels.some(l => l === this.state.labelKey) ? this.state.labelKey : labels[0];
  98. result = { labels, labelKey };
  99. }
  100. return result;
  101. }
  102. renderQueryTypeSwitch(queryType) {
  103. switch (queryType) {
  104. case MetricFindQueryTypes.MetricTypes:
  105. return (
  106. <SimpleSelect
  107. value={this.state.selectedService}
  108. options={this.state.services}
  109. onValueChange={e => this.onServiceChange(e)}
  110. label="Services"
  111. />
  112. );
  113. case MetricFindQueryTypes.LabelKeys:
  114. case MetricFindQueryTypes.LabelValues:
  115. case MetricFindQueryTypes.ResourceTypes:
  116. return (
  117. <React.Fragment>
  118. <SimpleSelect
  119. value={this.state.selectedService}
  120. options={this.state.services}
  121. onValueChange={e => this.onServiceChange(e)}
  122. label="Services"
  123. />
  124. <SimpleSelect
  125. value={this.state.selectedMetricType}
  126. options={this.state.metricTypes}
  127. onValueChange={e => this.onMetricTypeChange(e)}
  128. label="Metric Types"
  129. />
  130. {queryType === MetricFindQueryTypes.LabelValues && (
  131. <SimpleSelect
  132. value={this.state.labelKey}
  133. options={this.state.labels.map(l => ({ value: l, name: l }))}
  134. onValueChange={e => this.onLabelKeyChange(e)}
  135. label="Label Keys"
  136. />
  137. )}
  138. </React.Fragment>
  139. );
  140. case MetricFindQueryTypes.Aligners:
  141. case MetricFindQueryTypes.Aggregations:
  142. return (
  143. <React.Fragment>
  144. <SimpleSelect
  145. value={this.state.selectedService}
  146. options={this.state.services}
  147. onValueChange={e => this.onServiceChange(e)}
  148. label="Services"
  149. />
  150. <SimpleSelect
  151. value={this.state.selectedMetricType}
  152. options={this.state.metricTypes}
  153. onValueChange={e => this.onMetricTypeChange(e)}
  154. label="Metric Types"
  155. />
  156. </React.Fragment>
  157. );
  158. default:
  159. return '';
  160. }
  161. }
  162. render() {
  163. return (
  164. <React.Fragment>
  165. <SimpleSelect
  166. value={this.state.selectedQueryType}
  167. options={this.queryTypes}
  168. onValueChange={e => this.handleQueryTypeChange(e)}
  169. label="Query Types"
  170. />
  171. {this.renderQueryTypeSwitch(this.state.selectedQueryType)}
  172. </React.Fragment>
  173. );
  174. }
  175. }