QueriesTab.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import _ from 'lodash';
  4. // Components
  5. import 'app/features/panel/metrics_tab';
  6. import { EditorTabBody, EditorToolbarView } from './EditorTabBody';
  7. import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
  8. import { QueryInspector } from './QueryInspector';
  9. import { QueryOptions } from './QueryOptions';
  10. import { AngularQueryComponentScope } from 'app/features/panel/metrics_tab';
  11. import { PanelOptionSection } from './PanelOptionSection';
  12. // Services
  13. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  14. import { BackendSrv, getBackendSrv } from 'app/core/services/backend_srv';
  15. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  16. import config from 'app/core/config';
  17. // Types
  18. import { PanelModel } from '../panel_model';
  19. import { DashboardModel } from '../dashboard_model';
  20. import { DataQuery, DataSourceSelectItem } from 'app/types';
  21. import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
  22. interface Props {
  23. panel: PanelModel;
  24. dashboard: DashboardModel;
  25. }
  26. interface State {
  27. currentDS: DataSourceSelectItem;
  28. helpContent: JSX.Element;
  29. isLoadingHelp: boolean;
  30. isPickerOpen: boolean;
  31. isAddingMixed: boolean;
  32. }
  33. export class QueriesTab extends PureComponent<Props, State> {
  34. element: HTMLElement;
  35. component: AngularComponent;
  36. datasources: DataSourceSelectItem[] = getDatasourceSrv().getMetricSources();
  37. backendSrv: BackendSrv = getBackendSrv();
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. isLoadingHelp: false,
  42. currentDS: this.findCurrentDataSource(),
  43. helpContent: null,
  44. isPickerOpen: false,
  45. isAddingMixed: false,
  46. };
  47. }
  48. findCurrentDataSource(): DataSourceSelectItem {
  49. const { panel } = this.props;
  50. return this.datasources.find(datasource => datasource.value === panel.datasource) || this.datasources[0];
  51. }
  52. getAngularQueryComponentScope(): AngularQueryComponentScope {
  53. const { panel, dashboard } = this.props;
  54. return {
  55. panel: panel,
  56. dashboard: dashboard,
  57. refresh: () => panel.refresh(),
  58. render: () => panel.render,
  59. addQuery: this.onAddQuery,
  60. moveQuery: this.onMoveQuery,
  61. removeQuery: this.onRemoveQuery,
  62. events: panel.events,
  63. };
  64. }
  65. componentDidMount() {
  66. if (!this.element) {
  67. return;
  68. }
  69. const loader = getAngularLoader();
  70. const template = '<metrics-tab />';
  71. const scopeProps = {
  72. ctrl: this.getAngularQueryComponentScope(),
  73. };
  74. this.component = loader.load(this.element, scopeProps, template);
  75. }
  76. componentWillUnmount() {
  77. if (this.component) {
  78. this.component.destroy();
  79. }
  80. }
  81. onChangeDataSource = datasource => {
  82. const { panel } = this.props;
  83. const { currentDS } = this.state;
  84. // switching to mixed
  85. if (datasource.meta.mixed) {
  86. panel.targets.forEach(target => {
  87. target.datasource = panel.datasource;
  88. if (!target.datasource) {
  89. target.datasource = config.defaultDatasource;
  90. }
  91. });
  92. } else if (currentDS) {
  93. // if switching from mixed
  94. if (currentDS.meta.mixed) {
  95. for (const target of panel.targets) {
  96. delete target.datasource;
  97. }
  98. } else if (currentDS.meta.id !== datasource.meta.id) {
  99. // we are changing data source type, clear queries
  100. panel.targets = [{ refId: 'A' }];
  101. }
  102. }
  103. panel.datasource = datasource.value;
  104. panel.refresh();
  105. this.setState({
  106. currentDS: datasource,
  107. });
  108. };
  109. renderQueryInspector = () => {
  110. const { panel } = this.props;
  111. return <QueryInspector panel={panel} />;
  112. };
  113. renderHelp = () => {
  114. return <PluginHelp plugin={this.state.currentDS.meta} type="query_help" />;
  115. };
  116. onAddQuery = (query?: Partial<DataQuery>) => {
  117. this.props.panel.addQuery(query);
  118. this.forceUpdate();
  119. };
  120. onAddQueryClick = () => {
  121. if (this.state.currentDS.meta.mixed) {
  122. this.setState({ isAddingMixed: true });
  123. return;
  124. }
  125. this.props.panel.addQuery();
  126. this.component.digest();
  127. this.forceUpdate();
  128. };
  129. onRemoveQuery = (query: DataQuery) => {
  130. const { panel } = this.props;
  131. const index = _.indexOf(panel.targets, query);
  132. panel.targets.splice(index, 1);
  133. panel.refresh();
  134. this.forceUpdate();
  135. };
  136. onMoveQuery = (query: DataQuery, direction: number) => {
  137. const { panel } = this.props;
  138. const index = _.indexOf(panel.targets, query);
  139. _.move(panel.targets, index, index + direction);
  140. this.forceUpdate();
  141. };
  142. renderToolbar = () => {
  143. const { currentDS } = this.state;
  144. return <DataSourcePicker datasources={this.datasources} onChange={this.onChangeDataSource} current={currentDS} />;
  145. };
  146. renderMixedPicker = () => {
  147. return (
  148. <DataSourcePicker
  149. datasources={this.datasources}
  150. onChange={this.onAddMixedQuery}
  151. current={null}
  152. autoFocus={true}
  153. onBlur={this.onMixedPickerBlur}
  154. />
  155. );
  156. };
  157. onAddMixedQuery = datasource => {
  158. this.onAddQuery({ datasource: datasource.name });
  159. this.component.digest();
  160. this.setState({ isAddingMixed: false });
  161. };
  162. onMixedPickerBlur = () => {
  163. this.setState({ isAddingMixed: false });
  164. };
  165. render() {
  166. const { panel } = this.props;
  167. const { currentDS, isAddingMixed } = this.state;
  168. const queryInspector: EditorToolbarView = {
  169. title: 'Query Inspector',
  170. render: this.renderQueryInspector,
  171. };
  172. const dsHelp: EditorToolbarView = {
  173. heading: 'Help',
  174. icon: 'fa fa-question',
  175. render: this.renderHelp,
  176. };
  177. return (
  178. <EditorTabBody heading="Queries" renderToolbar={this.renderToolbar} toolbarItems={[queryInspector, dsHelp]}>
  179. <>
  180. <PanelOptionSection>
  181. <div className="query-editor-rows">
  182. <div ref={element => (this.element = element)} />
  183. <div className="gf-form-query">
  184. <div className="gf-form gf-form-query-letter-cell">
  185. <label className="gf-form-label">
  186. <span className="gf-form-query-letter-cell-carret muted">
  187. <i className="fa fa-caret-down" />
  188. </span>{' '}
  189. <span className="gf-form-query-letter-cell-letter">{panel.getNextQueryLetter()}</span>
  190. </label>
  191. </div>
  192. <div className="gf-form">
  193. {!isAddingMixed && (
  194. <button className="btn btn-secondary gf-form-btn" onClick={this.onAddQueryClick}>
  195. Add Query
  196. </button>
  197. )}
  198. {isAddingMixed && this.renderMixedPicker()}
  199. </div>
  200. </div>
  201. </div>
  202. </PanelOptionSection>
  203. <PanelOptionSection>
  204. <QueryOptions panel={panel} datasource={currentDS} />
  205. </PanelOptionSection>
  206. </>
  207. </EditorTabBody>
  208. );
  209. }
  210. }