QueryRows.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React, { PureComponent } from 'react';
  2. import { QueryTransaction, HistoryItem, QueryHint } from 'app/types/explore';
  3. import { Emitter } from 'app/core/utils/emitter';
  4. // import DefaultQueryField from './QueryField';
  5. import QueryEditor from './QueryEditor';
  6. import QueryTransactionStatus from './QueryTransactionStatus';
  7. import { DataSource, DataQuery } from 'app/types';
  8. import { RawTimeRange } from 'app/types/series';
  9. function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint {
  10. const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0);
  11. if (transaction) {
  12. return transaction.hints[0];
  13. }
  14. return undefined;
  15. }
  16. interface QueryRowEventHandlers {
  17. onAddQueryRow: (index: number) => void;
  18. onChangeQuery: (value: DataQuery, index: number, override?: boolean) => void;
  19. onClickHintFix: (action: object, index?: number) => void;
  20. onExecuteQuery: () => void;
  21. onRemoveQueryRow: (index: number) => void;
  22. }
  23. interface QueryRowCommonProps {
  24. className?: string;
  25. datasource: DataSource;
  26. history: HistoryItem[];
  27. transactions: QueryTransaction[];
  28. exploreEvents: Emitter;
  29. range: RawTimeRange;
  30. }
  31. type QueryRowProps = QueryRowCommonProps &
  32. QueryRowEventHandlers & {
  33. index: number;
  34. initialQuery: DataQuery;
  35. };
  36. class QueryRow extends PureComponent<QueryRowProps> {
  37. onExecuteQuery = () => {
  38. const { onExecuteQuery } = this.props;
  39. onExecuteQuery();
  40. };
  41. onChangeQuery = (value: DataQuery, override?: boolean) => {
  42. const { index, onChangeQuery } = this.props;
  43. if (onChangeQuery) {
  44. onChangeQuery(value, index, override);
  45. }
  46. };
  47. onClickAddButton = () => {
  48. const { index, onAddQueryRow } = this.props;
  49. if (onAddQueryRow) {
  50. onAddQueryRow(index);
  51. }
  52. };
  53. onClickClearButton = () => {
  54. this.onChangeQuery(null, true);
  55. };
  56. onClickHintFix = action => {
  57. const { index, onClickHintFix } = this.props;
  58. if (onClickHintFix) {
  59. onClickHintFix(action, index);
  60. }
  61. };
  62. onClickRemoveButton = () => {
  63. const { index, onRemoveQueryRow } = this.props;
  64. if (onRemoveQueryRow) {
  65. onRemoveQueryRow(index);
  66. }
  67. };
  68. onPressEnter = () => {
  69. const { onExecuteQuery } = this.props;
  70. if (onExecuteQuery) {
  71. onExecuteQuery();
  72. }
  73. };
  74. render() {
  75. const { datasource, history, initialQuery, transactions, exploreEvents, range } = this.props;
  76. const transactionWithError = transactions.find(t => t.error !== undefined);
  77. const hint = getFirstHintFromTransactions(transactions);
  78. const queryError = transactionWithError ? transactionWithError.error : null;
  79. const QueryField = datasource.pluginExports.ExploreQueryField;
  80. return (
  81. <div className="query-row">
  82. <div className="query-row-status">
  83. <QueryTransactionStatus transactions={transactions} />
  84. </div>
  85. <div className="query-row-field">
  86. {QueryField ? (
  87. <QueryField
  88. datasource={datasource}
  89. error={queryError}
  90. hint={hint}
  91. initialQuery={initialQuery}
  92. history={history}
  93. onClickHintFix={this.onClickHintFix}
  94. onPressEnter={this.onPressEnter}
  95. onQueryChange={this.onChangeQuery}
  96. />
  97. ) : (
  98. <QueryEditor
  99. datasource={datasource}
  100. error={queryError}
  101. onQueryChange={this.onChangeQuery}
  102. onExecuteQuery={this.onExecuteQuery}
  103. initialQuery={initialQuery}
  104. exploreEvents={exploreEvents}
  105. range={range}
  106. />
  107. )}
  108. </div>
  109. <div className="query-row-tools">
  110. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  111. <i className="fa fa-times" />
  112. </button>
  113. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  114. <i className="fa fa-plus" />
  115. </button>
  116. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  117. <i className="fa fa-minus" />
  118. </button>
  119. </div>
  120. </div>
  121. );
  122. }
  123. }
  124. type QueryRowsProps = QueryRowCommonProps &
  125. QueryRowEventHandlers & {
  126. initialQueries: DataQuery[];
  127. };
  128. export default class QueryRows extends PureComponent<QueryRowsProps> {
  129. render() {
  130. const { className = '', initialQueries, transactions, ...handlers } = this.props;
  131. return (
  132. <div className={className}>
  133. {initialQueries.map((query, index) => (
  134. <QueryRow
  135. key={query.key}
  136. index={index}
  137. initialQuery={query}
  138. transactions={transactions.filter(t => t.rowIndex === index)}
  139. {...handlers}
  140. />
  141. ))}
  142. </div>
  143. );
  144. }
  145. }