QueryEditorRow.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. import _ from 'lodash';
  5. // Utils & Services
  6. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  7. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  8. import { Emitter } from 'app/core/utils/emitter';
  9. import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
  10. // Types
  11. import { PanelModel } from '../state/PanelModel';
  12. import { DataQuery, DataSourceApi, TimeRange, DataQueryError, SeriesData, PanelData } from '@grafana/ui';
  13. import { DashboardModel } from '../state/DashboardModel';
  14. interface Props {
  15. panel: PanelModel;
  16. data: PanelData;
  17. query: DataQuery;
  18. dashboard: DashboardModel;
  19. onAddQuery: (query?: DataQuery) => void;
  20. onRemoveQuery: (query: DataQuery) => void;
  21. onMoveQuery: (query: DataQuery, direction: number) => void;
  22. onChange: (query: DataQuery) => void;
  23. dataSourceValue: string | null;
  24. inMixedMode: boolean;
  25. }
  26. interface State {
  27. loadedDataSourceValue: string | null | undefined;
  28. datasource: DataSourceApi | null;
  29. isCollapsed: boolean;
  30. hasTextEditMode: boolean;
  31. queryError: DataQueryError | null;
  32. queryResponse: SeriesData[] | null;
  33. }
  34. export class QueryEditorRow extends PureComponent<Props, State> {
  35. element: HTMLElement | null = null;
  36. angularScope: AngularQueryComponentScope | null;
  37. angularQueryEditor: AngularComponent | null = null;
  38. state: State = {
  39. datasource: null,
  40. isCollapsed: false,
  41. loadedDataSourceValue: undefined,
  42. hasTextEditMode: false,
  43. queryError: null,
  44. queryResponse: null,
  45. };
  46. componentDidMount() {
  47. this.loadDatasource();
  48. }
  49. componentWillUnmount() {
  50. if (this.angularQueryEditor) {
  51. this.angularQueryEditor.destroy();
  52. }
  53. }
  54. getAngularQueryComponentScope(): AngularQueryComponentScope {
  55. const { panel, query, dashboard } = this.props;
  56. const { datasource } = this.state;
  57. return {
  58. datasource: datasource,
  59. target: query,
  60. panel: panel,
  61. dashboard: dashboard,
  62. refresh: () => panel.refresh(),
  63. render: () => panel.render(),
  64. events: panel.events,
  65. range: getTimeSrv().timeRange(),
  66. };
  67. }
  68. async loadDatasource() {
  69. const { query, panel } = this.props;
  70. const dataSourceSrv = getDatasourceSrv();
  71. const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
  72. this.setState({
  73. datasource,
  74. loadedDataSourceValue: this.props.dataSourceValue,
  75. hasTextEditMode: false,
  76. });
  77. }
  78. componentDidUpdate(prevProps: Props) {
  79. const { loadedDataSourceValue } = this.state;
  80. const { data, query } = this.props;
  81. if (data !== prevProps.data) {
  82. const queryError = data.error && data.error.refId === query.refId ? data.error : null;
  83. const queryResponse = data.series.filter(series => series.refId === query.refId);
  84. this.setState({ queryResponse, queryError });
  85. if (this.angularScope) {
  86. this.angularScope.range = getTimeSrv().timeRange();
  87. }
  88. if (this.angularQueryEditor) {
  89. // Some query controllers listen to data error events and need a digest
  90. // for some reason this needs to be done in next tick
  91. setTimeout(this.angularQueryEditor.digest);
  92. }
  93. }
  94. // check if we need to load another datasource
  95. if (loadedDataSourceValue !== this.props.dataSourceValue) {
  96. if (this.angularQueryEditor) {
  97. this.angularQueryEditor.destroy();
  98. this.angularQueryEditor = null;
  99. }
  100. this.loadDatasource();
  101. return;
  102. }
  103. if (!this.element || this.angularQueryEditor) {
  104. return;
  105. }
  106. const loader = getAngularLoader();
  107. const template = '<plugin-component type="query-ctrl" />';
  108. const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
  109. this.angularQueryEditor = loader.load(this.element, scopeProps, template);
  110. this.angularScope = scopeProps.ctrl;
  111. // give angular time to compile
  112. setTimeout(() => {
  113. this.setState({ hasTextEditMode: !!this.angularScope.toggleEditorMode });
  114. }, 100);
  115. }
  116. onToggleCollapse = () => {
  117. this.setState({ isCollapsed: !this.state.isCollapsed });
  118. };
  119. onRunQuery = () => {
  120. this.props.panel.refresh();
  121. };
  122. renderPluginEditor() {
  123. const { query, onChange } = this.props;
  124. const { datasource, queryResponse, queryError } = this.state;
  125. if (datasource.components.QueryCtrl) {
  126. return <div ref={element => (this.element = element)} />;
  127. }
  128. if (datasource.components.QueryEditor) {
  129. const QueryEditor = datasource.components.QueryEditor;
  130. return (
  131. <QueryEditor
  132. query={query}
  133. datasource={datasource}
  134. onChange={onChange}
  135. onRunQuery={this.onRunQuery}
  136. queryResponse={queryResponse}
  137. queryError={queryError}
  138. />
  139. );
  140. }
  141. return <div>Data source plugin does not export any Query Editor component</div>;
  142. }
  143. onToggleEditMode = () => {
  144. if (this.angularScope && this.angularScope.toggleEditorMode) {
  145. this.angularScope.toggleEditorMode();
  146. this.angularQueryEditor.digest();
  147. }
  148. if (this.state.isCollapsed) {
  149. this.setState({ isCollapsed: false });
  150. }
  151. };
  152. onRemoveQuery = () => {
  153. this.props.onRemoveQuery(this.props.query);
  154. };
  155. onCopyQuery = () => {
  156. const copy = _.cloneDeep(this.props.query);
  157. this.props.onAddQuery(copy);
  158. };
  159. onDisableQuery = () => {
  160. this.props.query.hide = !this.props.query.hide;
  161. this.onRunQuery();
  162. this.forceUpdate();
  163. };
  164. renderCollapsedText(): string | null {
  165. const { datasource } = this.state;
  166. if (datasource.getQueryDisplayText) {
  167. return datasource.getQueryDisplayText(this.props.query);
  168. }
  169. if (this.angularScope && this.angularScope.getCollapsedText) {
  170. return this.angularScope.getCollapsedText();
  171. }
  172. return null;
  173. }
  174. render() {
  175. const { query, inMixedMode } = this.props;
  176. const { datasource, isCollapsed, hasTextEditMode } = this.state;
  177. const isDisabled = query.hide;
  178. const bodyClasses = classNames('query-editor-row__body gf-form-query', {
  179. 'query-editor-row__body--collapsed': isCollapsed,
  180. });
  181. const rowClasses = classNames('query-editor-row', {
  182. 'query-editor-row--disabled': isDisabled,
  183. 'gf-form-disabled': isDisabled,
  184. });
  185. if (!datasource) {
  186. return null;
  187. }
  188. return (
  189. <div className={rowClasses}>
  190. <div className="query-editor-row__header">
  191. <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
  192. {isCollapsed && <i className="fa fa-caret-right" />}
  193. {!isCollapsed && <i className="fa fa-caret-down" />}
  194. <span>{query.refId}</span>
  195. {inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
  196. {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
  197. </div>
  198. <div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
  199. {isCollapsed && <div>{this.renderCollapsedText()}</div>}
  200. </div>
  201. <div className="query-editor-row__actions">
  202. {hasTextEditMode && (
  203. <button
  204. className="query-editor-row__action"
  205. onClick={this.onToggleEditMode}
  206. title="Toggle text edit mode"
  207. >
  208. <i className="fa fa-fw fa-pencil" />
  209. </button>
  210. )}
  211. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
  212. <i className="fa fa-fw fa-arrow-down" />
  213. </button>
  214. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
  215. <i className="fa fa-fw fa-arrow-up" />
  216. </button>
  217. <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
  218. <i className="fa fa-fw fa-copy" />
  219. </button>
  220. <button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
  221. {isDisabled && <i className="fa fa-fw fa-eye-slash" />}
  222. {!isDisabled && <i className="fa fa-fw fa-eye" />}
  223. </button>
  224. <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
  225. <i className="fa fa-fw fa-trash" />
  226. </button>
  227. </div>
  228. </div>
  229. <div className={bodyClasses}>{this.renderPluginEditor()}</div>
  230. </div>
  231. );
  232. }
  233. }
  234. export interface AngularQueryComponentScope {
  235. target: DataQuery;
  236. panel: PanelModel;
  237. dashboard: DashboardModel;
  238. events: Emitter;
  239. refresh: () => void;
  240. render: () => void;
  241. datasource: DataSourceApi;
  242. toggleEditorMode?: () => void;
  243. getCollapsedText?: () => string;
  244. range: TimeRange;
  245. }