TemplateQueryComponent.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import React, { PureComponent } from 'react';
  2. import uniqBy from 'lodash/uniqBy';
  3. import { TemplateQueryProps } from 'app/types/plugins';
  4. import SimpleSelect from './SimpleSelect';
  5. import { getMetricTypes } 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.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.Alignerns, name: 'Aligners' },
  15. { value: MetricFindQueryTypes.AlignmentPeriods, name: 'Alignment Periods' },
  16. ];
  17. defaults: TemplateQueryComponentData = {
  18. selectedQueryType: this.queryTypes[0].value,
  19. metricDescriptors: [],
  20. selectedService: '',
  21. selectedMetricType: '',
  22. labels: [],
  23. labelKey: '',
  24. metricTypes: [],
  25. services: [],
  26. };
  27. constructor(props: TemplateQueryProps) {
  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. console.log(state);
  61. this.setState(state);
  62. }
  63. async handleQueryTypeChange(event) {
  64. const state: any = {
  65. selectedQueryType: event.target.value,
  66. ...await this.getLabels(this.state.selectedMetricType, event.target.value),
  67. };
  68. this.setState(state);
  69. }
  70. async onServiceChange(event) {
  71. const { metricTypes, selectedMetricType } = getMetricTypes(
  72. this.state.metricDescriptors,
  73. this.state.selectedMetricType,
  74. event.target.value
  75. );
  76. const state: any = {
  77. selectedService: event.target.value,
  78. metricTypes,
  79. selectedMetricType,
  80. ...await this.getLabels(selectedMetricType),
  81. };
  82. this.setState(state);
  83. }
  84. async onMetricTypeChange(event) {
  85. const state: any = { selectedMetricType: event.target.value, ...await this.getLabels(event.target.value) };
  86. this.setState(state);
  87. }
  88. onLabelKeyChange(event) {
  89. this.setState({ labelKey: event.target.value });
  90. }
  91. componentDidUpdate() {
  92. const { metricDescriptors, labels, metricTypes, services, ...queryModel } = this.state;
  93. const queryName = this.queryTypes.find(q => q.value === this.state.selectedQueryType);
  94. this.props.onChange(queryModel, `Stackdriver - ${queryName.name}`);
  95. }
  96. isLabelQuery(queryType) {
  97. return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
  98. }
  99. async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
  100. let result = { labels: this.state.labels, labelKey: this.state.labelKey };
  101. if (selectedMetricType && this.isLabelQuery(selectedQueryType)) {
  102. const refId = 'StackdriverTemplateQueryComponent';
  103. const response = await this.props.datasource.getLabels(selectedMetricType, refId);
  104. const labels = Object.keys(response.meta[selectedQueryType]);
  105. const labelKey = labels.some(l => l === this.state.labelKey) ? this.state.labelKey : labels[0];
  106. result = { labels, labelKey };
  107. }
  108. return result;
  109. }
  110. renderQueryTypeSwitch(queryType) {
  111. switch (queryType) {
  112. case MetricFindQueryTypes.MetricTypes:
  113. return (
  114. <SimpleSelect
  115. value={this.state.selectedService}
  116. options={this.state.services}
  117. onValueChange={this.onServiceChange}
  118. label="Services"
  119. />
  120. );
  121. case MetricFindQueryTypes.MetricLabels:
  122. case MetricFindQueryTypes.ResourceLabels:
  123. case MetricFindQueryTypes.ResourceTypes:
  124. return (
  125. <React.Fragment>
  126. <SimpleSelect
  127. value={this.state.selectedService}
  128. options={this.state.services}
  129. onValueChange={this.onServiceChange}
  130. label="Services"
  131. />
  132. <SimpleSelect
  133. value={this.state.selectedMetricType}
  134. options={this.state.metricTypes}
  135. onValueChange={this.onMetricTypeChange}
  136. label="Metric Types"
  137. />
  138. {queryType !== MetricFindQueryTypes.ResourceTypes && (
  139. <SimpleSelect
  140. value={this.state.labelKey}
  141. options={this.state.labels.map(l => ({ value: l, name: l }))}
  142. onValueChange={this.onLabelKeyChange}
  143. label={
  144. this.state.selectedQueryType === MetricFindQueryTypes.ResourceLabels
  145. ? 'Resource Label Key'
  146. : 'Metric Label Key'
  147. }
  148. />
  149. )}
  150. </React.Fragment>
  151. );
  152. case MetricFindQueryTypes.Alignerns:
  153. case MetricFindQueryTypes.Aggregations:
  154. return (
  155. <React.Fragment>
  156. <SimpleSelect
  157. value={this.state.selectedService}
  158. options={this.state.services}
  159. onValueChange={this.onServiceChange}
  160. label="Services"
  161. />
  162. <SimpleSelect
  163. value={this.state.selectedMetricType}
  164. options={this.state.metricTypes}
  165. onValueChange={this.onMetricTypeChange}
  166. label="Metric Types"
  167. />
  168. </React.Fragment>
  169. );
  170. default:
  171. return '';
  172. }
  173. }
  174. render() {
  175. return (
  176. <React.Fragment>
  177. <SimpleSelect
  178. value={this.state.selectedQueryType}
  179. options={this.queryTypes}
  180. onValueChange={this.handleQueryTypeChange}
  181. label="Query Types"
  182. />
  183. {this.renderQueryTypeSwitch(this.state.selectedQueryType)}
  184. </React.Fragment>
  185. );
  186. }
  187. }