VariableQueryEditor.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.MetricLabels, name: 'Metric Labels' },
  11. { value: MetricFindQueryTypes.ResourceLabels, name: 'Resource Labels' },
  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.handleQueryTypeChange = this.handleQueryTypeChange.bind(this);
  30. this.onServiceChange = this.onServiceChange.bind(this);
  31. this.onMetricTypeChange = this.onMetricTypeChange.bind(this);
  32. this.onLabelKeyChange = this.onLabelKeyChange.bind(this);
  33. this.state = Object.assign(this.defaults, this.props.query);
  34. }
  35. async componentDidMount() {
  36. const metricDescriptors = await this.props.datasource.getMetricTypes(this.props.datasource.projectName);
  37. const services = uniqBy(metricDescriptors, 'service').map(m => ({
  38. value: m.service,
  39. name: m.serviceShortName,
  40. }));
  41. let selectedService = '';
  42. if (services.some(s => s.value === this.state.selectedService)) {
  43. selectedService = this.state.selectedService;
  44. } else if (services && services.length > 0) {
  45. selectedService = services[0].value;
  46. }
  47. const { metricTypes, selectedMetricType } = getMetricTypes(
  48. metricDescriptors,
  49. this.state.selectedMetricType,
  50. selectedService
  51. );
  52. const state: any = {
  53. services,
  54. selectedService,
  55. metricTypes,
  56. selectedMetricType,
  57. metricDescriptors,
  58. ...await this.getLabels(selectedMetricType),
  59. };
  60. this.setState(state);
  61. }
  62. async handleQueryTypeChange(event) {
  63. const state: any = {
  64. selectedQueryType: event.target.value,
  65. ...await this.getLabels(this.state.selectedMetricType, event.target.value),
  66. };
  67. this.setState(state);
  68. }
  69. async onServiceChange(event) {
  70. const { metricTypes, selectedMetricType } = getMetricTypes(
  71. this.state.metricDescriptors,
  72. this.state.selectedMetricType,
  73. event.target.value
  74. );
  75. const state: any = {
  76. selectedService: event.target.value,
  77. metricTypes,
  78. selectedMetricType,
  79. ...await this.getLabels(selectedMetricType),
  80. };
  81. this.setState(state);
  82. }
  83. async onMetricTypeChange(event) {
  84. const state: any = { selectedMetricType: event.target.value, ...await this.getLabels(event.target.value) };
  85. this.setState(state);
  86. }
  87. onLabelKeyChange(event) {
  88. this.setState({ labelKey: event.target.value });
  89. }
  90. componentDidUpdate() {
  91. const { metricDescriptors, labels, metricTypes, services, ...queryModel } = this.state;
  92. const query = this.queryTypes.find(q => q.value === this.state.selectedQueryType);
  93. this.props.onChange(queryModel, `Stackdriver - ${query.name}`);
  94. }
  95. isLabelQuery(queryType) {
  96. return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
  97. }
  98. async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
  99. let result = { labels: this.state.labels, labelKey: this.state.labelKey };
  100. if (selectedMetricType && this.isLabelQuery(selectedQueryType)) {
  101. const refId = 'StackdriverVariableQueryEditor';
  102. const response = await this.props.datasource.getLabels(selectedMetricType, refId);
  103. const labels = Object.keys(response.meta[selectedQueryType]);
  104. const labelKey = labels.some(l => l === this.state.labelKey) ? this.state.labelKey : labels[0];
  105. result = { labels, labelKey };
  106. }
  107. return result;
  108. }
  109. renderQueryTypeSwitch(queryType) {
  110. switch (queryType) {
  111. case MetricFindQueryTypes.MetricTypes:
  112. return (
  113. <SimpleSelect
  114. value={this.state.selectedService}
  115. options={this.state.services}
  116. onValueChange={this.onServiceChange}
  117. label="Services"
  118. />
  119. );
  120. case MetricFindQueryTypes.MetricLabels:
  121. case MetricFindQueryTypes.ResourceLabels:
  122. case MetricFindQueryTypes.ResourceTypes:
  123. return (
  124. <React.Fragment>
  125. <SimpleSelect
  126. value={this.state.selectedService}
  127. options={this.state.services}
  128. onValueChange={this.onServiceChange}
  129. label="Services"
  130. />
  131. <SimpleSelect
  132. value={this.state.selectedMetricType}
  133. options={this.state.metricTypes}
  134. onValueChange={this.onMetricTypeChange}
  135. label="Metric Types"
  136. />
  137. {queryType !== MetricFindQueryTypes.ResourceTypes && (
  138. <SimpleSelect
  139. value={this.state.labelKey}
  140. options={this.state.labels.map(l => ({ value: l, name: l }))}
  141. onValueChange={this.onLabelKeyChange}
  142. label={
  143. this.state.selectedQueryType === MetricFindQueryTypes.ResourceLabels
  144. ? 'Resource Label Key'
  145. : 'Metric Label Key'
  146. }
  147. />
  148. )}
  149. </React.Fragment>
  150. );
  151. case MetricFindQueryTypes.Aligners:
  152. case MetricFindQueryTypes.Aggregations:
  153. return (
  154. <React.Fragment>
  155. <SimpleSelect
  156. value={this.state.selectedService}
  157. options={this.state.services}
  158. onValueChange={this.onServiceChange}
  159. label="Services"
  160. />
  161. <SimpleSelect
  162. value={this.state.selectedMetricType}
  163. options={this.state.metricTypes}
  164. onValueChange={this.onMetricTypeChange}
  165. label="Metric Types"
  166. />
  167. </React.Fragment>
  168. );
  169. default:
  170. return '';
  171. }
  172. }
  173. render() {
  174. return (
  175. <React.Fragment>
  176. <SimpleSelect
  177. value={this.state.selectedQueryType}
  178. options={this.queryTypes}
  179. onValueChange={this.handleQueryTypeChange}
  180. label="Query Types"
  181. />
  182. {this.renderQueryTypeSwitch(this.state.selectedQueryType)}
  183. </React.Fragment>
  184. );
  185. }
  186. }