-
Notifications
You must be signed in to change notification settings - Fork 14.2k
/
ExploreResultsButton.jsx
204 lines (195 loc) · 6.08 KB
/
ExploreResultsButton.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import moment from 'moment';
import React from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Alert } from 'react-bootstrap';
import Dialog from 'react-bootstrap-dialog';
import { t } from '@superset-ui/translation';
import shortid from 'shortid';
import { exportChart } from '../../explore/exploreUtils';
import * as actions from '../actions/sqlLab';
import InfoTooltipWithTrigger from '../../components/InfoTooltipWithTrigger';
import Button from '../../components/Button';
const propTypes = {
actions: PropTypes.object.isRequired,
query: PropTypes.object,
errorMessage: PropTypes.string,
timeout: PropTypes.number,
database: PropTypes.object.isRequired,
};
const defaultProps = {
query: {},
};
class ExploreResultsButton extends React.PureComponent {
constructor(props) {
super(props);
this.visualize = this.visualize.bind(this);
this.onClick = this.onClick.bind(this);
this.getInvalidColumns = this.getInvalidColumns.bind(this);
this.renderInvalidColumnMessage = this.renderInvalidColumnMessage.bind(this);
}
onClick() {
const timeout = this.props.timeout;
const msg = this.renderInvalidColumnMessage();
if (Math.round(this.getQueryDuration()) > timeout) {
this.dialog.show({
title: t('Explore'),
body: this.renderTimeoutWarning(),
actions: [
Dialog.CancelAction(),
Dialog.OKAction(() => {
this.visualize();
}),
],
bsSize: 'large',
onHide: (dialog) => {
dialog.hide();
},
});
} else if (msg) {
this.dialog.show({
title: t('Explore'),
body: msg,
actions: [Dialog.DefaultAction('Ok', () => {}, 'btn-primary')],
bsSize: 'large',
bsStyle: 'warning',
onHide: (dialog) => {
dialog.hide();
},
});
} else {
this.visualize();
}
}
getColumns() {
const props = this.props;
if (props.query && props.query.results && props.query.results.columns) {
return props.query.results.columns;
}
return [];
}
getQueryDuration() {
return moment.duration(this.props.query.endDttm - this.props.query.startDttm).asSeconds();
}
getInvalidColumns() {
const re1 = /^[A-Za-z_]\w*$/; // starts with char or _, then only alphanum
const re2 = /__\d+$/; // does not finish with __ and then a number which screams dup col name
return this.props.query.results.columns.map(col => col.name)
.filter(col => !re1.test(col) || re2.test(col));
}
datasourceName() {
const { query } = this.props;
const uniqueId = shortid.generate();
let datasourceName = uniqueId;
if (query) {
datasourceName = query.user ? `${query.user}-` : '';
datasourceName += `${query.tab}-${uniqueId}`;
}
return datasourceName;
}
buildVizOptions() {
const { schema, sql, dbId, templateParams } = this.props.query;
return {
dbId,
schema,
sql,
templateParams,
datasourceName: this.datasourceName(),
columns: this.getColumns(),
};
}
visualize() {
this.props.actions
.createDatasource(this.buildVizOptions())
.then((data) => {
const columns = this.getColumns();
const formData = {
datasource: `${data.table_id}__table`,
metrics: [],
groupby: [],
viz_type: 'table',
since: '100 years ago',
all_columns: columns.map(c => c.name),
row_limit: 1000,
};
this.props.actions.addInfoToast(t('Creating a data source and creating a new tab'));
// open new window for data visualization
exportChart(formData);
})
.catch(() => {
this.props.actions.addDangerToast(this.props.errorMessage || t('An error occurred'));
});
}
renderTimeoutWarning() {
return (
<Alert bsStyle="warning">
{t('This query took %s seconds to run, ', Math.round(this.getQueryDuration())) +
t('and the explore view times out at %s seconds ', this.props.timeout) +
t('following this flow will most likely lead to your query timing out. ') +
t('We recommend your summarize your data further before following that flow. ') +
t('If activated you can use the ')}
<strong>CREATE TABLE AS </strong>
{t('feature to store a summarized data set that you can then explore.')}
</Alert>
);
}
renderInvalidColumnMessage() {
const invalidColumns = this.getInvalidColumns();
if (invalidColumns.length === 0) {
return null;
}
return (
<div>
{t('Column name(s) ')}
<code>
<strong>{invalidColumns.join(', ')} </strong>
</code>
{t('cannot be used as a column name. Please use aliases (as in ')}
<code>
SELECT count(*)
<strong>AS my_alias</strong>
</code>){' '}
{t(`limited to alphanumeric characters and underscores. Column aliases ending with
double underscores followed by a numeric value are not allowed for reasons
discussed in Github issue #5739.
`)}
</div>
);
}
render() {
return (
<Button
bsSize="small"
onClick={this.onClick}
disabled={!this.props.database.allows_subquery}
tooltip={t('Explore the result set in the data exploration view')}
>
<Dialog
ref={(el) => {
this.dialog = el;
}}
/>
<InfoTooltipWithTrigger icon="line-chart" placement="top" label="explore" /> {t('Explore')}
</Button>
);
}
}
ExploreResultsButton.propTypes = propTypes;
ExploreResultsButton.defaultProps = defaultProps;
function mapStateToProps({ sqlLab, common }) {
return {
errorMessage: sqlLab.errorMessage,
timeout: common.conf ? common.conf.SUPERSET_WEBSERVER_TIMEOUT : null,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch),
};
}
export { ExploreResultsButton };
export default connect(
mapStateToProps,
mapDispatchToProps,
)(ExploreResultsButton);