QueriesTab.tsx 7.1 KB

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