import React from 'react'; import PropTypes from 'prop-types'; import moment from 'moment'; import { Table } from 'reactable-arc'; import { Label, ProgressBar, Well } from 'react-bootstrap'; import { t } from '@superset-ui/translation'; import Link from './Link'; import ResultSet from './ResultSet'; import ModalTrigger from '../../components/ModalTrigger'; import HighlightedSql from './HighlightedSql'; import { fDuration } from '../../modules/dates'; import { storeQuery } from '../../utils/common'; import QueryStateLabel from './QueryStateLabel'; const propTypes = { columns: PropTypes.array, actions: PropTypes.object, queries: PropTypes.array, onUserClicked: PropTypes.func, onDbClicked: PropTypes.func, }; const defaultProps = { columns: ['started', 'duration', 'rows'], queries: [], onUserClicked: () => {}, onDbClicked: () => {}, }; class QueryTable extends React.PureComponent { constructor(props) { super(props); const uri = window.location.toString(); const cleanUri = uri.substring(0, uri.indexOf('#')); this.state = { cleanUri, showVisualizeModal: false, activeQuery: null, }; } callback(url) { window.open(url); } openQuery(dbId, schema, sql) { const newQuery = { dbId, title: t('Untitled Query'), schema, sql, }; storeQuery(newQuery).then(url => this.callback(url)); } hideVisualizeModal() { this.setState({ showVisualizeModal: false }); } showVisualizeModal(query) { this.setState({ activeQuery: query, showVisualizeModal: true }); } restoreSql(query) { this.props.actions.queryEditorSetSql({ id: query.sqlEditorId }, query.sql); } openQueryInNewTab(query) { this.props.actions.cloneQueryToNewTab(query); } openAsyncResults(query) { this.props.actions.fetchQueryResults(query); } clearQueryResults(query) { this.props.actions.clearQueryResults(query); } removeQuery(query) { this.props.actions.removeQuery(query); } render() { const data = this.props.queries .map((query) => { const q = Object.assign({}, query); if (q.endDttm) { q.duration = fDuration(q.startDttm, q.endDttm); } const time = moment(q.startDttm) .format() .split('T'); q.time = (
{time[0]}
{time[1]}
); q.user = ( ); q.db = ( ); q.started = moment(q.startDttm).format('HH:mm:ss'); q.querylink = (
); q.sql = ( ); if (q.resultsKey) { q.output = ( {t('view results')} } modalTitle={t('Data preview')} beforeOpen={this.openAsyncResults.bind(this, query)} onExit={this.clearQueryResults.bind(this, query)} modalBody={ } /> ); } else { // if query was run using ctas and force_ctas_schema was set // tempTable will have the schema const schemaUsed = q.ctas && q.tempTable && q.tempTable.includes('.') ? '' : q.schema; q.output = [schemaUsed, q.tempTable].filter(v => v).join('.'); } q.progress = ( ); let errorTooltip; if (q.errorMessage) { errorTooltip = ( ); } q.state = (
{errorTooltip}
); q.actions = (
); return q; }) .reverse(); return (
); } } QueryTable.propTypes = propTypes; QueryTable.defaultProps = defaultProps; export default QueryTable;