QueriesTab.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Libraries
  2. import React, { SFC, PureComponent } from 'react';
  3. import Remarkable from 'remarkable';
  4. import _ from 'lodash';
  5. // Components
  6. import './../../panel/metrics_tab';
  7. import { EditorTabBody } from './EditorTabBody';
  8. import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
  9. import { QueryInspector } from './QueryInspector';
  10. import { QueryOptions } from './QueryOptions';
  11. import { AngularQueryComponentScope } from 'app/features/panel/metrics_tab';
  12. import { PanelOptionSection } from './PanelOptionSection';
  13. // Services
  14. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  15. import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv';
  16. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  17. import config from 'app/core/config';
  18. // Types
  19. import { PanelModel } from '../panel_model';
  20. import { DashboardModel } from '../dashboard_model';
  21. import { DataSourceSelectItem, DataQuery } from 'app/types';
  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. loadHelp = () => {
  111. const { currentDS } = this.state;
  112. const hasHelp = currentDS.meta.hasQueryHelp;
  113. if (hasHelp) {
  114. this.setState({
  115. helpContent: <h3>Loading help...</h3>,
  116. isLoadingHelp: true,
  117. });
  118. this.backendSrv
  119. .get(`/api/plugins/${currentDS.meta.id}/markdown/query_help`)
  120. .then(res => {
  121. const md = new Remarkable();
  122. const helpHtml = md.render(res);
  123. this.setState({
  124. helpContent: <div className="markdown-html" dangerouslySetInnerHTML={{ __html: helpHtml }} />,
  125. isLoadingHelp: false,
  126. });
  127. })
  128. .catch(() => {
  129. this.setState({
  130. helpContent: <h3>'Error occured when loading help'</h3>,
  131. isLoadingHelp: false,
  132. });
  133. });
  134. }
  135. };
  136. renderQueryInspector = () => {
  137. const { panel } = this.props;
  138. return <QueryInspector panel={panel} LoadingPlaceholder={LoadingPlaceholder} />;
  139. };
  140. renderHelp = () => {
  141. const { helpContent, isLoadingHelp } = this.state;
  142. return isLoadingHelp ? <LoadingPlaceholder text="Loading help..." /> : helpContent;
  143. };
  144. onAddQuery = (query?: Partial<DataQuery>) => {
  145. this.props.panel.addQuery(query);
  146. this.forceUpdate();
  147. };
  148. onAddQueryClick = () => {
  149. if (this.state.currentDS.meta.mixed) {
  150. this.setState({ isAddingMixed: true });
  151. return;
  152. }
  153. this.props.panel.addQuery();
  154. this.component.digest();
  155. this.forceUpdate();
  156. };
  157. onRemoveQuery = (query: DataQuery) => {
  158. const { panel } = this.props;
  159. const index = _.indexOf(panel.targets, query);
  160. panel.targets.splice(index, 1);
  161. panel.refresh();
  162. this.forceUpdate();
  163. };
  164. onMoveQuery = (query: DataQuery, direction: number) => {
  165. const { panel } = this.props;
  166. const index = _.indexOf(panel.targets, query);
  167. _.move(panel.targets, index, index + direction);
  168. this.forceUpdate();
  169. };
  170. renderToolbar = () => {
  171. const { currentDS } = this.state;
  172. return <DataSourcePicker datasources={this.datasources} onChange={this.onChangeDataSource} current={currentDS} />;
  173. };
  174. renderMixedPicker = () => {
  175. return (
  176. <DataSourcePicker
  177. datasources={this.datasources}
  178. onChange={this.onAddMixedQuery}
  179. current={null}
  180. autoFocus={true}
  181. onBlur={this.onMixedPickerBlur}
  182. />
  183. );
  184. };
  185. onAddMixedQuery = datasource => {
  186. this.onAddQuery({ datasource: datasource.name });
  187. this.component.digest();
  188. this.setState({ isAddingMixed: false });
  189. };
  190. onMixedPickerBlur = () => {
  191. this.setState({ isAddingMixed: false });
  192. };
  193. render() {
  194. const { panel } = this.props;
  195. const { currentDS, isAddingMixed } = this.state;
  196. const { hasQueryHelp } = currentDS.meta;
  197. const queryInspector = {
  198. title: 'Query Inspector',
  199. render: this.renderQueryInspector,
  200. };
  201. const dsHelp = {
  202. heading: 'Help',
  203. icon: 'fa fa-question',
  204. disabled: !hasQueryHelp,
  205. onClick: this.loadHelp,
  206. render: this.renderHelp,
  207. };
  208. return (
  209. <EditorTabBody heading="Queries" renderToolbar={this.renderToolbar} toolbarItems={[queryInspector, dsHelp]}>
  210. <>
  211. <PanelOptionSection>
  212. <div className="query-editor-rows gf-form-group">
  213. <div ref={element => (this.element = element)} />
  214. <div className="gf-form-query">
  215. <div className="gf-form gf-form-query-letter-cell">
  216. <label className="gf-form-label">
  217. <span className="gf-form-query-letter-cell-carret muted">
  218. <i className="fa fa-caret-down" />
  219. </span>
  220. <span className="gf-form-query-letter-cell-letter">{panel.getNextQueryLetter()}</span>
  221. </label>
  222. {!isAddingMixed && (
  223. <button className="btn btn-secondary gf-form-btn" onClick={this.onAddQueryClick}>
  224. Add Query
  225. </button>
  226. )}
  227. {isAddingMixed && this.renderMixedPicker()}
  228. </div>
  229. </div>
  230. </div>
  231. </PanelOptionSection>
  232. <PanelOptionSection>
  233. <QueryOptions panel={panel} datasource={currentDS} />
  234. </PanelOptionSection>
  235. </>
  236. </EditorTabBody>
  237. );
  238. }
  239. }