TemplateQueryComponent.tsx 7.0 KB

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