TemplateQueryComponent.tsx 6.9 KB

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