QueriesTab.tsx 6.9 KB

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