QueryEditorRow.tsx 9.3 KB

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