VariableQueryEditor.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.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.getLabels(selectedMetricType),
  55. };
  56. this.setState(state);
  57. }
  58. async handleQueryTypeChange(event) {
  59. const state: any = {
  60. selectedQueryType: event.target.value,
  61. ...await this.getLabels(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.getLabels(selectedMetricType),
  76. };
  77. this.setState(state);
  78. }
  79. async onMetricTypeChange(event) {
  80. const state: any = { selectedMetricType: event.target.value, ...await this.getLabels(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. isLabelQuery(queryType) {
  92. return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
  93. }
  94. async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
  95. let result = { labels: this.state.labels, labelKey: this.state.labelKey };
  96. if (selectedMetricType && this.isLabelQuery(selectedQueryType)) {
  97. const refId = 'StackdriverVariableQueryEditor';
  98. const response = await this.props.datasource.getLabels(selectedMetricType, refId);
  99. const labels = Object.keys(response.meta[selectedQueryType]);
  100. const labelKey = labels.some(l => l === this.state.labelKey) ? this.state.labelKey : labels[0];
  101. result = { labels, labelKey };
  102. }
  103. return result;
  104. }
  105. renderQueryTypeSwitch(queryType) {
  106. switch (queryType) {
  107. case MetricFindQueryTypes.MetricTypes:
  108. return (
  109. <SimpleSelect
  110. value={this.state.selectedService}
  111. options={this.state.services}
  112. onValueChange={e => this.onServiceChange(e)}
  113. label="Services"
  114. />
  115. );
  116. case MetricFindQueryTypes.MetricLabels:
  117. case MetricFindQueryTypes.ResourceLabels:
  118. case MetricFindQueryTypes.ResourceTypes:
  119. return (
  120. <React.Fragment>
  121. <SimpleSelect
  122. value={this.state.selectedService}
  123. options={this.state.services}
  124. onValueChange={e => this.onServiceChange(e)}
  125. label="Services"
  126. />
  127. <SimpleSelect
  128. value={this.state.selectedMetricType}
  129. options={this.state.metricTypes}
  130. onValueChange={e => this.onMetricTypeChange(e)}
  131. label="Metric Types"
  132. />
  133. {queryType !== MetricFindQueryTypes.ResourceTypes && (
  134. <SimpleSelect
  135. value={this.state.labelKey}
  136. options={this.state.labels.map(l => ({ value: l, name: l }))}
  137. onValueChange={e => this.onLabelKeyChange(e)}
  138. label={
  139. this.state.selectedQueryType === MetricFindQueryTypes.ResourceLabels
  140. ? 'Resource Label Key'
  141. : 'Metric Label Key'
  142. }
  143. />
  144. )}
  145. </React.Fragment>
  146. );
  147. case MetricFindQueryTypes.Aligners:
  148. case MetricFindQueryTypes.Aggregations:
  149. return (
  150. <React.Fragment>
  151. <SimpleSelect
  152. value={this.state.selectedService}
  153. options={this.state.services}
  154. onValueChange={e => this.onServiceChange(e)}
  155. label="Services"
  156. />
  157. <SimpleSelect
  158. value={this.state.selectedMetricType}
  159. options={this.state.metricTypes}
  160. onValueChange={e => this.onMetricTypeChange(e)}
  161. label="Metric Types"
  162. />
  163. </React.Fragment>
  164. );
  165. default:
  166. return '';
  167. }
  168. }
  169. render() {
  170. return (
  171. <React.Fragment>
  172. <SimpleSelect
  173. value={this.state.selectedQueryType}
  174. options={this.queryTypes}
  175. onValueChange={e => this.handleQueryTypeChange(e)}
  176. label="Query Types"
  177. />
  178. {this.renderQueryTypeSwitch(this.state.selectedQueryType)}
  179. </React.Fragment>
  180. );
  181. }
  182. }