QueriesTab.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Libraries
  2. import React, { SFC, PureComponent } from 'react';
  3. import Remarkable from 'remarkable';
  4. import _ from 'lodash';
  5. // Components
  6. import DataSourceOption from './DataSourceOption';
  7. import { EditorTabBody } from './EditorTabBody';
  8. import { DataSourcePicker } from './DataSourcePicker';
  9. import { QueryInspector } from './QueryInspector';
  10. import { TimeRangeOptions } from './TimeRangeOptions';
  11. import './../../panel/metrics_tab';
  12. import { AngularQueryComponentScope } from 'app/features/panel/metrics_tab';
  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: string;
  30. isPickerOpen: 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. };
  50. }
  51. getAngularQueryComponentScope(): AngularQueryComponentScope {
  52. const { panel, dashboard } = this.props;
  53. return {
  54. panel: panel,
  55. dashboard: dashboard,
  56. refresh: () => panel.refresh(),
  57. render: () => panel.render,
  58. addQuery: this.onAddQuery,
  59. moveQuery: this.onMoveQuery,
  60. removeQuery: this.onRemoveQuery,
  61. events: panel.events,
  62. };
  63. }
  64. componentDidMount() {
  65. if (!this.element) {
  66. return;
  67. }
  68. const loader = getAngularLoader();
  69. const template = '<metrics-tab />';
  70. const scopeProps = {
  71. ctrl: this.getAngularQueryComponentScope(),
  72. };
  73. this.component = loader.load(this.element, scopeProps, template);
  74. }
  75. componentWillUnmount() {
  76. if (this.component) {
  77. this.component.destroy();
  78. }
  79. }
  80. onChangeDataSource = datasource => {
  81. const { panel } = this.props;
  82. const { currentDS } = this.state;
  83. // switching to mixed
  84. if (datasource.meta.mixed) {
  85. panel.targets.forEach(target => {
  86. target.datasource = panel.datasource;
  87. if (!target.datasource) {
  88. target.datasource = config.defaultDatasource;
  89. }
  90. });
  91. } else if (currentDS) {
  92. // if switching from mixed
  93. if (currentDS.meta.mixed) {
  94. for (const target of panel.targets) {
  95. delete target.datasource;
  96. }
  97. } else if (currentDS.meta.id !== datasource.meta.id) {
  98. // we are changing data source type, clear queries
  99. panel.targets = [{ refId: 'A' }];
  100. }
  101. }
  102. panel.datasource = datasource.value;
  103. panel.refresh();
  104. this.setState({
  105. currentDS: datasource,
  106. });
  107. };
  108. // loadHelp = () => {
  109. // const { currentDatasource } = this.state;
  110. // const hasHelp = currentDatasource.meta.hasQueryHelp;
  111. //
  112. // if (hasHelp) {
  113. // this.setState({
  114. // helpContent: <h2>Loading help...</h2>,
  115. // isLoadingHelp: true
  116. // });
  117. //
  118. // this.backendSrv
  119. // .get(`/api/plugins/${currentDatasource.meta.id}/markdown/query_help`)
  120. // .then(res => {
  121. // const md = new Remarkable();
  122. // const helpHtml = md.render(res); // TODO: Clean out dangerous code? Previous: this.helpHtml = this.$sce.trustAsHtml(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: 'Error occured when loading help',
  131. // isLoadingHelp: false,
  132. // });
  133. // });
  134. // }
  135. // };
  136. // renderOptions = close => {
  137. // const { currentDatasource } = this.state;
  138. // const { queryOptions } = currentDatasource.meta;
  139. // const { panel } = this.props;
  140. //
  141. // const onChangeFn = (panelKey: string) => {
  142. // return (value: string | number) => {
  143. // panel[panelKey] = value;
  144. // panel.refresh();
  145. // };
  146. // };
  147. //
  148. // const allOptions = {
  149. // cacheTimeout: {
  150. // label: 'Cache timeout',
  151. // placeholder: '60',
  152. // name: 'cacheTimeout',
  153. // value: panel.cacheTimeout,
  154. // tooltipInfo: (
  155. // <>
  156. // If your time series store has a query cache this option can override the default cache timeout. Specify a
  157. // numeric value in seconds.
  158. // </>
  159. // ),
  160. // },
  161. // maxDataPoints: {
  162. // label: 'Max data points',
  163. // placeholder: 'auto',
  164. // name: 'maxDataPoints',
  165. // value: panel.maxDataPoints,
  166. // tooltipInfo: (
  167. // <>
  168. // The maximum data points the query should return. For graphs this is automatically set to one data point per
  169. // pixel.
  170. // </>
  171. // ),
  172. // },
  173. // minInterval: {
  174. // label: 'Min time interval',
  175. // placeholder: '0',
  176. // name: 'minInterval',
  177. // value: panel.interval,
  178. // panelKey: 'interval',
  179. // tooltipInfo: (
  180. // <>
  181. // A lower limit for the auto group by time interval. Recommended to be set to write frequency, for example{' '}
  182. // <code>1m</code> if your data is written every minute. Access auto interval via variable{' '}
  183. // <code>$__interval</code> for time range string and <code>$__interval_ms</code> for numeric variable that can
  184. // be used in math expressions.
  185. // </>
  186. // ),
  187. // },
  188. // };
  189. //
  190. // const dsOptions = queryOptions
  191. // ? Object.keys(queryOptions).map(key => {
  192. // const options = allOptions[key];
  193. // return <DataSourceOption key={key} {...options} onChange={onChangeFn(allOptions[key].panelKey || key)} />;
  194. // })
  195. // : null;
  196. //
  197. // return (
  198. // <>
  199. // <TimeRangeOptions panel={this.props.panel} />
  200. // {dsOptions}
  201. // </>
  202. // );
  203. // };
  204. renderQueryInspector = () => {
  205. const { panel } = this.props;
  206. return <QueryInspector panel={panel} LoadingPlaceholder={LoadingPlaceholder} />;
  207. };
  208. // renderHelp = () => {
  209. // const { helpHtml, isLoading } = this.state.help;
  210. // return isLoading ? <LoadingPlaceholder text="Loading help..." /> : helpHtml;
  211. // };
  212. onAddQuery = (query?: DataQuery) => {
  213. this.props.panel.addQuery(query);
  214. this.forceUpdate();
  215. };
  216. onAddQueryClick = () => {
  217. this.props.panel.addQuery();
  218. this.component.digest();
  219. this.forceUpdate();
  220. };
  221. onRemoveQuery = (query: DataQuery) => {
  222. const { panel } = this.props;
  223. const index = _.indexOf(panel.targets, query);
  224. panel.targets.splice(index, 1);
  225. panel.refresh();
  226. this.forceUpdate();
  227. };
  228. onMoveQuery = (query: DataQuery, direction: number) => {
  229. const { panel } = this.props;
  230. const index = _.indexOf(panel.targets, query);
  231. _.move(panel.targets, index, index + direction);
  232. this.forceUpdate();
  233. };
  234. renderToolbar = () => {
  235. const { currentDS } = this.state;
  236. return (
  237. <DataSourcePicker
  238. datasources={this.datasources}
  239. onChangeDataSource={this.onChangeDataSource}
  240. current={currentDS}
  241. />
  242. );
  243. };
  244. render() {
  245. const { panel } = this.props;
  246. // const dsInformation = {
  247. // title: currentDatasource.name,
  248. // imgSrc: currentDatasource.meta.info.logos.small,
  249. // render: closeOpenView => (
  250. // <DataSourcePicker
  251. // datasources={this.datasources}
  252. // onChangeDataSource={ds => {
  253. // closeOpenView();
  254. // this.onChangeDataSource(ds);
  255. // }}
  256. // />
  257. // ),
  258. // };
  259. //
  260. // const queryInspector = {
  261. // title: 'Query Inspector',
  262. // render: this.renderQueryInspector,
  263. // };
  264. //
  265. // const dsHelp = {
  266. // title: '',
  267. // icon: 'fa fa-question',
  268. // disabled: !hasQueryHelp,
  269. // onClick: this.loadHelp,
  270. // render: this.renderHelp,
  271. // };
  272. //
  273. // const options = {
  274. // title: 'Time Range',
  275. // icon: '',
  276. // disabled: false,
  277. // render: this.renderOptions,
  278. // };
  279. return (
  280. <EditorTabBody heading="Queries" renderToolbar={this.renderToolbar}>
  281. <div className="query-editor-rows gf-form-group">
  282. <div ref={element => (this.element = element)} />
  283. <div className="gf-form-query">
  284. <div className="gf-form gf-form-query-letter-cell">
  285. <label className="gf-form-label">
  286. <span className="gf-form-query-letter-cell-carret muted">
  287. <i className="fa fa-caret-down" />
  288. </span>
  289. <span className="gf-form-query-letter-cell-letter">{panel.getNextQueryLetter()}</span>
  290. </label>
  291. <button className="btn btn-secondary gf-form-btn" onClick={this.onAddQueryClick}>
  292. Add Query
  293. </button>
  294. </div>
  295. </div>
  296. </div>
  297. </EditorTabBody>
  298. );
  299. }
  300. }