2018-04-27 15:42:35 +02:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
import { QueryTransaction, HistoryItem, Query, QueryHint } from 'app/types/explore';
|
2018-10-24 11:08:15 +02:00
|
|
|
|
2018-08-08 16:50:30 +02:00
|
|
|
// TODO make this datasource-plugin-dependent
|
2018-07-26 14:04:12 +02:00
|
|
|
import QueryField from './PromQueryField';
|
2018-10-22 17:51:42 +02:00
|
|
|
import QueryTransactions from './QueryTransactions';
|
2018-04-27 15:42:35 +02:00
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint {
|
2018-10-24 11:08:15 +02:00
|
|
|
const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0);
|
|
|
|
|
if (transaction) {
|
|
|
|
|
return transaction.hints[0];
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
interface QueryRowEventHandlers {
|
|
|
|
|
onAddQueryRow: (index: number) => void;
|
|
|
|
|
onChangeQuery: (value: string, index: number, override?: boolean) => void;
|
|
|
|
|
onClickHintFix: (action: object, index?: number) => void;
|
|
|
|
|
onExecuteQuery: () => void;
|
|
|
|
|
onRemoveQueryRow: (index: number) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface QueryRowCommonProps {
|
|
|
|
|
className?: string;
|
2018-10-25 12:24:24 +02:00
|
|
|
datasource: any;
|
2018-10-24 14:55:56 +02:00
|
|
|
history: HistoryItem[];
|
|
|
|
|
// Temporarily
|
|
|
|
|
supportsLogs?: boolean;
|
|
|
|
|
transactions: QueryTransaction[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type QueryRowProps = QueryRowCommonProps &
|
|
|
|
|
QueryRowEventHandlers & {
|
|
|
|
|
index: number;
|
|
|
|
|
query: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QueryRow extends PureComponent<QueryRowProps> {
|
2018-08-05 23:07:05 +02:00
|
|
|
onChangeQuery = (value, override?: boolean) => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { index, onChangeQuery } = this.props;
|
|
|
|
|
if (onChangeQuery) {
|
2018-08-05 23:07:05 +02:00
|
|
|
onChangeQuery(value, index, override);
|
2018-04-27 15:42:35 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onClickAddButton = () => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { index, onAddQueryRow } = this.props;
|
|
|
|
|
if (onAddQueryRow) {
|
|
|
|
|
onAddQueryRow(index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onClickClearButton = () => {
|
2018-08-05 23:07:05 +02:00
|
|
|
this.onChangeQuery('', true);
|
2018-08-04 11:58:54 +02:00
|
|
|
};
|
|
|
|
|
|
2018-08-08 16:50:30 +02:00
|
|
|
onClickHintFix = action => {
|
|
|
|
|
const { index, onClickHintFix } = this.props;
|
|
|
|
|
if (onClickHintFix) {
|
|
|
|
|
onClickHintFix(action, index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onClickRemoveButton = () => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { index, onRemoveQueryRow } = this.props;
|
|
|
|
|
if (onRemoveQueryRow) {
|
|
|
|
|
onRemoveQueryRow(index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onPressEnter = () => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { onExecuteQuery } = this.props;
|
|
|
|
|
if (onExecuteQuery) {
|
|
|
|
|
onExecuteQuery();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-10-25 12:24:24 +02:00
|
|
|
const { datasource, history, query, supportsLogs, transactions } = this.props;
|
2018-10-24 14:55:56 +02:00
|
|
|
const transactionWithError = transactions.find(t => t.error !== undefined);
|
2018-10-24 11:08:15 +02:00
|
|
|
const hint = getFirstHintFromTransactions(transactions);
|
2018-10-22 17:51:42 +02:00
|
|
|
const queryError = transactionWithError ? transactionWithError.error : null;
|
2018-04-27 15:42:35 +02:00
|
|
|
return (
|
|
|
|
|
<div className="query-row">
|
2018-10-22 17:51:42 +02:00
|
|
|
<div className="query-row-status">
|
|
|
|
|
<QueryTransactions transactions={transactions} />
|
|
|
|
|
</div>
|
2018-08-05 23:07:05 +02:00
|
|
|
<div className="query-row-field">
|
2018-04-27 18:21:20 +02:00
|
|
|
<QueryField
|
2018-10-25 12:24:24 +02:00
|
|
|
datasource={datasource}
|
2018-08-08 16:50:30 +02:00
|
|
|
error={queryError}
|
2018-10-24 11:08:15 +02:00
|
|
|
hint={hint}
|
2018-10-02 15:21:46 +02:00
|
|
|
initialQuery={query}
|
2018-08-02 16:43:33 +02:00
|
|
|
history={history}
|
2018-08-08 16:50:30 +02:00
|
|
|
onClickHintFix={this.onClickHintFix}
|
2018-08-04 11:58:54 +02:00
|
|
|
onPressEnter={this.onPressEnter}
|
|
|
|
|
onQueryChange={this.onChangeQuery}
|
2018-08-10 16:41:21 +02:00
|
|
|
supportsLogs={supportsLogs}
|
2018-04-27 18:21:20 +02:00
|
|
|
/>
|
2018-04-27 15:42:35 +02:00
|
|
|
</div>
|
2018-08-05 23:07:05 +02:00
|
|
|
<div className="query-row-tools">
|
|
|
|
|
<button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
|
|
|
|
|
<i className="fa fa-times" />
|
|
|
|
|
</button>
|
|
|
|
|
<button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
|
|
|
|
|
<i className="fa fa-plus" />
|
|
|
|
|
</button>
|
|
|
|
|
<button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
|
|
|
|
|
<i className="fa fa-minus" />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2018-04-27 15:42:35 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
type QueryRowsProps = QueryRowCommonProps &
|
|
|
|
|
QueryRowEventHandlers & {
|
|
|
|
|
queries: Query[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default class QueryRows extends PureComponent<QueryRowsProps> {
|
2018-04-27 15:42:35 +02:00
|
|
|
render() {
|
2018-10-24 14:55:56 +02:00
|
|
|
const { className = '', queries, transactions, ...handlers } = this.props;
|
2018-04-27 15:42:35 +02:00
|
|
|
return (
|
2018-04-27 18:21:20 +02:00
|
|
|
<div className={className}>
|
2018-08-03 10:20:13 +02:00
|
|
|
{queries.map((q, index) => (
|
2018-08-08 16:50:30 +02:00
|
|
|
<QueryRow
|
|
|
|
|
key={q.key}
|
|
|
|
|
index={index}
|
|
|
|
|
query={q.query}
|
2018-10-22 17:51:42 +02:00
|
|
|
transactions={transactions.filter(t => t.rowIndex === index)}
|
2018-08-08 16:50:30 +02:00
|
|
|
{...handlers}
|
|
|
|
|
/>
|
2018-08-03 10:20:13 +02:00
|
|
|
))}
|
2018-04-27 18:21:20 +02:00
|
|
|
</div>
|
2018-04-27 15:42:35 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|