QueryEditorRow.tsx 9.8 KB

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