QueryEditorRow.tsx 9.3 KB

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