QueryEditorRow.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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: _.has(datasource, 'components.QueryCtrl.prototype.toggleEditorMode'),
  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. }
  109. onToggleCollapse = () => {
  110. this.setState({ isCollapsed: !this.state.isCollapsed });
  111. };
  112. onRunQuery = () => {
  113. this.props.panel.refresh();
  114. };
  115. renderPluginEditor() {
  116. const { query, data, onChange } = this.props;
  117. const { datasource, queryResponse } = this.state;
  118. if (datasource.components.QueryCtrl) {
  119. return <div ref={element => (this.element = element)} />;
  120. }
  121. if (datasource.components.QueryEditor) {
  122. const QueryEditor = datasource.components.QueryEditor;
  123. return (
  124. <QueryEditor
  125. query={query}
  126. datasource={datasource}
  127. onChange={onChange}
  128. onRunQuery={this.onRunQuery}
  129. queryResponse={queryResponse}
  130. panelData={data}
  131. />
  132. );
  133. }
  134. return <div>Data source plugin does not export any Query Editor component</div>;
  135. }
  136. onToggleEditMode = () => {
  137. if (this.angularScope && this.angularScope.toggleEditorMode) {
  138. this.angularScope.toggleEditorMode();
  139. this.angularQueryEditor.digest();
  140. }
  141. if (this.state.isCollapsed) {
  142. this.setState({ isCollapsed: false });
  143. }
  144. };
  145. onRemoveQuery = () => {
  146. this.props.onRemoveQuery(this.props.query);
  147. };
  148. onCopyQuery = () => {
  149. const copy = _.cloneDeep(this.props.query);
  150. this.props.onAddQuery(copy);
  151. };
  152. onDisableQuery = () => {
  153. this.props.query.hide = !this.props.query.hide;
  154. this.onRunQuery();
  155. this.forceUpdate();
  156. };
  157. renderCollapsedText(): string | null {
  158. const { datasource } = this.state;
  159. if (datasource.getQueryDisplayText) {
  160. return datasource.getQueryDisplayText(this.props.query);
  161. }
  162. if (this.angularScope && this.angularScope.getCollapsedText) {
  163. return this.angularScope.getCollapsedText();
  164. }
  165. return null;
  166. }
  167. render() {
  168. const { query, inMixedMode } = this.props;
  169. const { datasource, isCollapsed, hasTextEditMode } = this.state;
  170. const isDisabled = query.hide;
  171. const bodyClasses = classNames('query-editor-row__body gf-form-query', {
  172. 'query-editor-row__body--collapsed': isCollapsed,
  173. });
  174. const rowClasses = classNames('query-editor-row', {
  175. 'query-editor-row--disabled': isDisabled,
  176. 'gf-form-disabled': isDisabled,
  177. });
  178. if (!datasource) {
  179. return null;
  180. }
  181. return (
  182. <div className={rowClasses}>
  183. <div className="query-editor-row__header">
  184. <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
  185. {isCollapsed && <i className="fa fa-caret-right" />}
  186. {!isCollapsed && <i className="fa fa-caret-down" />}
  187. <span>{query.refId}</span>
  188. {inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
  189. {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
  190. </div>
  191. <div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
  192. {isCollapsed && <div>{this.renderCollapsedText()}</div>}
  193. </div>
  194. <div className="query-editor-row__actions">
  195. {hasTextEditMode && (
  196. <button
  197. className="query-editor-row__action"
  198. onClick={this.onToggleEditMode}
  199. title="Toggle text edit mode"
  200. >
  201. <i className="fa fa-fw fa-pencil" />
  202. </button>
  203. )}
  204. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
  205. <i className="fa fa-fw fa-arrow-down" />
  206. </button>
  207. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
  208. <i className="fa fa-fw fa-arrow-up" />
  209. </button>
  210. <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
  211. <i className="fa fa-fw fa-copy" />
  212. </button>
  213. <button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
  214. {isDisabled && <i className="fa fa-fw fa-eye-slash" />}
  215. {!isDisabled && <i className="fa fa-fw fa-eye" />}
  216. </button>
  217. <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
  218. <i className="fa fa-fw fa-trash" />
  219. </button>
  220. </div>
  221. </div>
  222. <div className={bodyClasses}>{this.renderPluginEditor()}</div>
  223. </div>
  224. );
  225. }
  226. }
  227. export interface AngularQueryComponentScope {
  228. target: DataQuery;
  229. panel: PanelModel;
  230. dashboard: DashboardModel;
  231. events: Emitter;
  232. refresh: () => void;
  233. render: () => void;
  234. datasource: DataSourceApi;
  235. toggleEditorMode?: () => void;
  236. getCollapsedText?: () => string;
  237. range: TimeRange;
  238. }
  239. /**
  240. * Get a version of the PanelData limited to the query we are looking at
  241. */
  242. export function filterPanelDataToQuery(data: PanelData, refId: string): PanelData | undefined {
  243. const series = data.series.filter(series => series.refId === refId);
  244. // No matching series
  245. if (!series.length) {
  246. return undefined;
  247. }
  248. // Don't pass the request if all requests are the same
  249. const request: DataQueryRequest = undefined;
  250. // TODO: look in sub-requets to match the info
  251. // Only say this is an error if the error links to the query
  252. let state = LoadingState.Done;
  253. const error = data.error && data.error.refId === refId ? data.error : undefined;
  254. if (error) {
  255. state = LoadingState.Error;
  256. }
  257. return {
  258. state,
  259. series,
  260. request,
  261. error,
  262. };
  263. }