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