QueryRow.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import _ from 'lodash';
  4. import { hot } from 'react-hot-loader';
  5. import { connect } from 'react-redux';
  6. // Components
  7. import QueryEditor from './QueryEditor';
  8. import QueryTransactionStatus from './QueryTransactionStatus';
  9. // Actions
  10. import { changeQuery, modifyQueries, runQueries, addQueryRow } from './state/actions';
  11. // Types
  12. import { StoreState } from 'app/types';
  13. import { RawTimeRange, DataQuery, ExploreDataSourceApi, QueryHint, QueryFixAction } from '@grafana/ui';
  14. import { QueryTransaction, HistoryItem, ExploreItemState, ExploreId } from 'app/types/explore';
  15. import { Emitter } from 'app/core/utils/emitter';
  16. import { highlightLogsExpressionAction, removeQueryRowAction } from './state/actionTypes';
  17. function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint {
  18. const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0);
  19. if (transaction) {
  20. return transaction.hints[0];
  21. }
  22. return undefined;
  23. }
  24. interface QueryRowProps {
  25. addQueryRow: typeof addQueryRow;
  26. changeQuery: typeof changeQuery;
  27. className?: string;
  28. exploreId: ExploreId;
  29. datasourceInstance: ExploreDataSourceApi;
  30. highlightLogsExpressionAction: typeof highlightLogsExpressionAction;
  31. history: HistoryItem[];
  32. index: number;
  33. query: DataQuery;
  34. modifyQueries: typeof modifyQueries;
  35. queryTransactions: QueryTransaction[];
  36. exploreEvents: Emitter;
  37. range: RawTimeRange;
  38. removeQueryRowAction: typeof removeQueryRowAction;
  39. runQueries: typeof runQueries;
  40. }
  41. export class QueryRow extends PureComponent<QueryRowProps> {
  42. onExecuteQuery = () => {
  43. const { exploreId } = this.props;
  44. this.props.runQueries(exploreId);
  45. };
  46. onChangeQuery = (query: DataQuery, override?: boolean) => {
  47. const { datasourceInstance, exploreId, index } = this.props;
  48. this.props.changeQuery(exploreId, query, index, override);
  49. if (query && !override && datasourceInstance.getHighlighterExpression && index === 0) {
  50. // Live preview of log search matches. Only use on first row for now
  51. this.updateLogsHighlights(query);
  52. }
  53. };
  54. componentWillUnmount() {
  55. console.log('QueryRow will unmount');
  56. }
  57. onClickAddButton = () => {
  58. const { exploreId, index } = this.props;
  59. this.props.addQueryRow(exploreId, index);
  60. };
  61. onClickClearButton = () => {
  62. this.onChangeQuery(null, true);
  63. };
  64. onClickHintFix = (action: QueryFixAction) => {
  65. const { datasourceInstance, exploreId, index } = this.props;
  66. if (datasourceInstance && datasourceInstance.modifyQuery) {
  67. const modifier = (queries: DataQuery, action: QueryFixAction) => datasourceInstance.modifyQuery(queries, action);
  68. this.props.modifyQueries(exploreId, action, index, modifier);
  69. }
  70. };
  71. onClickRemoveButton = () => {
  72. const { exploreId, index } = this.props;
  73. this.props.removeQueryRowAction({ exploreId, index });
  74. };
  75. updateLogsHighlights = _.debounce((value: DataQuery) => {
  76. const { datasourceInstance } = this.props;
  77. if (datasourceInstance.getHighlighterExpression) {
  78. const { exploreId } = this.props;
  79. const expressions = [datasourceInstance.getHighlighterExpression(value)];
  80. this.props.highlightLogsExpressionAction({ exploreId, expressions });
  81. }
  82. }, 500);
  83. render() {
  84. const { datasourceInstance, history, index, query, queryTransactions, exploreEvents, range } = this.props;
  85. const transactions = queryTransactions.filter(t => t.rowIndex === index);
  86. const transactionWithError = transactions.find(t => t.error !== undefined);
  87. const hint = getFirstHintFromTransactions(transactions);
  88. const queryError = transactionWithError ? transactionWithError.error : null;
  89. const QueryField = datasourceInstance.pluginExports.ExploreQueryField;
  90. return (
  91. <div className="query-row">
  92. <div className="query-row-status">
  93. <QueryTransactionStatus transactions={transactions} />
  94. </div>
  95. <div className="query-row-field flex-shrink-1">
  96. {QueryField ? (
  97. <QueryField
  98. datasource={datasourceInstance}
  99. query={query}
  100. error={queryError}
  101. hint={hint}
  102. history={history}
  103. onExecuteQuery={this.onExecuteQuery}
  104. onExecuteHint={this.onClickHintFix}
  105. onQueryChange={this.onChangeQuery}
  106. />
  107. ) : (
  108. <QueryEditor
  109. datasource={datasourceInstance}
  110. error={queryError}
  111. onQueryChange={this.onChangeQuery}
  112. onExecuteQuery={this.onExecuteQuery}
  113. initialQuery={query}
  114. exploreEvents={exploreEvents}
  115. range={range}
  116. />
  117. )}
  118. </div>
  119. <div className="gf-form-inline flex-shrink-0">
  120. <div className="gf-form">
  121. <button className="gf-form-label gf-form-label--btn" onClick={this.onClickClearButton}>
  122. <i className="fa fa-times" />
  123. </button>
  124. </div>
  125. <div className="gf-form">
  126. <button className="gf-form-label gf-form-label--btn" onClick={this.onClickAddButton}>
  127. <i className="fa fa-plus" />
  128. </button>
  129. </div>
  130. <div className="gf-form">
  131. <button className="gf-form-label gf-form-label--btn" onClick={this.onClickRemoveButton}>
  132. <i className="fa fa-minus" />
  133. </button>
  134. </div>
  135. </div>
  136. </div>
  137. );
  138. }
  139. }
  140. function mapStateToProps(state: StoreState, { exploreId, index }) {
  141. const explore = state.explore;
  142. const item: ExploreItemState = explore[exploreId];
  143. const { datasourceInstance, history, queries, queryTransactions, range } = item;
  144. const query = queries[index];
  145. return { datasourceInstance, history, query, queryTransactions, range };
  146. }
  147. const mapDispatchToProps = {
  148. addQueryRow,
  149. changeQuery,
  150. highlightLogsExpressionAction,
  151. modifyQueries,
  152. removeQueryRowAction,
  153. runQueries,
  154. };
  155. export default hot(module)(
  156. connect(
  157. mapStateToProps,
  158. mapDispatchToProps
  159. )(QueryRow)
  160. );