QueriesTab.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 './DataSourcePicker';
  9. import { QueryInspector } from './QueryInspector';
  10. import { QueryOptions } from './QueryOptions';
  11. import { AngularQueryComponentScope } from 'app/features/panel/metrics_tab';
  12. // Services
  13. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  14. import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv';
  15. import { getAngularLoader, AngularComponent } 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 { DataSourceSelectItem, DataQuery } from 'app/types';
  21. interface Props {
  22. panel: PanelModel;
  23. dashboard: DashboardModel;
  24. }
  25. interface State {
  26. currentDS: DataSourceSelectItem;
  27. helpContent: JSX.Element;
  28. isLoadingHelp: boolean;
  29. isPickerOpen: boolean;
  30. isAddingMixed: boolean;
  31. }
  32. interface LoadingPlaceholderProps {
  33. text: string;
  34. }
  35. const LoadingPlaceholder: SFC<LoadingPlaceholderProps> = ({ text }) => <h2>{text}</h2>;
  36. export class QueriesTab extends PureComponent<Props, State> {
  37. element: HTMLElement;
  38. component: AngularComponent;
  39. datasources: DataSourceSelectItem[] = getDatasourceSrv().getMetricSources();
  40. backendSrv: BackendSrv = getBackendSrv();
  41. constructor(props) {
  42. super(props);
  43. const { panel } = props;
  44. this.state = {
  45. currentDS: this.datasources.find(datasource => datasource.value === panel.datasource),
  46. isLoadingHelp: false,
  47. helpContent: null,
  48. isPickerOpen: false,
  49. isAddingMixed: false,
  50. };
  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. loadHelp = () => {
  110. const { currentDS } = this.state;
  111. const hasHelp = currentDS.meta.hasQueryHelp;
  112. if (hasHelp) {
  113. this.setState({
  114. helpContent: <h3>Loading help...</h3>,
  115. isLoadingHelp: true,
  116. });
  117. this.backendSrv
  118. .get(`/api/plugins/${currentDS.meta.id}/markdown/query_help`)
  119. .then(res => {
  120. const md = new Remarkable();
  121. const helpHtml = md.render(res);
  122. this.setState({
  123. helpContent: <div className="markdown-html" dangerouslySetInnerHTML={{ __html: helpHtml }} />,
  124. isLoadingHelp: false,
  125. });
  126. })
  127. .catch(() => {
  128. this.setState({
  129. helpContent: <h3>'Error occured when loading help'</h3>,
  130. isLoadingHelp: false,
  131. });
  132. });
  133. }
  134. };
  135. renderQueryInspector = () => {
  136. const { panel } = this.props;
  137. return <QueryInspector panel={panel} LoadingPlaceholder={LoadingPlaceholder} />;
  138. };
  139. renderHelp = () => {
  140. const { helpContent, isLoadingHelp } = this.state;
  141. return isLoadingHelp ? <LoadingPlaceholder text="Loading help..." /> : helpContent;
  142. };
  143. onAddQuery = (query?: Partial<DataQuery>) => {
  144. this.props.panel.addQuery(query);
  145. this.forceUpdate();
  146. };
  147. onAddQueryClick = () => {
  148. if (this.state.currentDS.meta.mixed) {
  149. this.setState({ isAddingMixed: true });
  150. return;
  151. }
  152. this.props.panel.addQuery();
  153. this.component.digest();
  154. this.forceUpdate();
  155. };
  156. onRemoveQuery = (query: DataQuery) => {
  157. const { panel } = this.props;
  158. const index = _.indexOf(panel.targets, query);
  159. panel.targets.splice(index, 1);
  160. panel.refresh();
  161. this.forceUpdate();
  162. };
  163. onMoveQuery = (query: DataQuery, direction: number) => {
  164. const { panel } = this.props;
  165. const index = _.indexOf(panel.targets, query);
  166. _.move(panel.targets, index, index + direction);
  167. this.forceUpdate();
  168. };
  169. renderToolbar = () => {
  170. const { currentDS } = this.state;
  171. return (
  172. <DataSourcePicker
  173. datasources={this.datasources}
  174. onChangeDataSource={this.onChangeDataSource}
  175. current={currentDS}
  176. />
  177. );
  178. };
  179. renderMixedPicker = () => {
  180. return (
  181. <DataSourcePicker
  182. datasources={this.datasources}
  183. onChangeDataSource={this.onAddMixedQuery}
  184. current={null}
  185. autoFocus={true}
  186. onBlur={this.onMixedPickerBlur}
  187. />
  188. );
  189. };
  190. onAddMixedQuery = datasource => {
  191. this.onAddQuery({ datasource: datasource.name });
  192. this.component.digest();
  193. this.setState({ isAddingMixed: false });
  194. };
  195. onMixedPickerBlur = () => {
  196. this.setState({ isAddingMixed: false });
  197. };
  198. render() {
  199. const { panel } = this.props;
  200. const { currentDS, isAddingMixed } = this.state;
  201. const { hasQueryHelp } = currentDS.meta;
  202. const queryInspector = {
  203. title: 'Query Inspector',
  204. render: this.renderQueryInspector,
  205. };
  206. const dsHelp = {
  207. title: '',
  208. icon: 'fa fa-question',
  209. disabled: !hasQueryHelp,
  210. onClick: this.loadHelp,
  211. render: this.renderHelp,
  212. };
  213. return (
  214. <EditorTabBody heading="Data" renderToolbar={this.renderToolbar} toolbarItems={[queryInspector, dsHelp]}>
  215. <>
  216. <div className="panel-option-section">
  217. <div className="panel-option-section__header">Queries</div>
  218. <div className="panel-option-section__body panel-option-section__body--queries">
  219. <div className="query-editor-rows gf-form-group">
  220. <div ref={element => (this.element = element)} />
  221. <div className="gf-form-query">
  222. <div className="gf-form gf-form-query-letter-cell">
  223. <label className="gf-form-label">
  224. <span className="gf-form-query-letter-cell-carret muted">
  225. <i className="fa fa-caret-down" />
  226. </span>
  227. <span className="gf-form-query-letter-cell-letter">{panel.getNextQueryLetter()}</span>
  228. </label>
  229. {!isAddingMixed && (
  230. <button className="btn btn-secondary gf-form-btn" onClick={this.onAddQueryClick}>
  231. Add Query
  232. </button>
  233. )}
  234. {isAddingMixed && this.renderMixedPicker()}
  235. </div>
  236. </div>
  237. </div>
  238. </div>
  239. </div>
  240. <div className="panel-option-section">
  241. <div className="panel-option-section__header">Options</div>
  242. <div className="panel-option-section__body">
  243. <QueryOptions panel={panel} datasource={currentDS} />
  244. </div>
  245. </div>
  246. </>
  247. </EditorTabBody>
  248. );
  249. }
  250. }