2016-08-30 00:55:31 -04:00
|
|
|
import React from 'react';
|
2016-09-11 10:39:07 -04:00
|
|
|
import { Alert, Button, Col, Modal } from 'react-bootstrap';
|
2016-08-30 00:55:31 -04:00
|
|
|
|
|
|
|
import Select from 'react-select';
|
|
|
|
import { Table } from 'reactable';
|
|
|
|
import shortid from 'shortid';
|
|
|
|
|
2016-09-11 10:39:07 -04:00
|
|
|
const CHART_TYPES = [
|
|
|
|
{ value: 'dist_bar', label: 'Distribution - Bar Chart', requiresTime: false },
|
|
|
|
{ value: 'pie', label: 'Pie Chart', requiresTime: false },
|
|
|
|
{ value: 'line', label: 'Time Series - Line Chart', requiresTime: true },
|
|
|
|
{ value: 'bar', label: 'Time Series - Bar Chart', requiresTime: true },
|
|
|
|
];
|
|
|
|
|
2016-10-25 19:44:32 -04:00
|
|
|
const propTypes = {
|
|
|
|
onHide: React.PropTypes.func,
|
|
|
|
query: React.PropTypes.object,
|
|
|
|
show: React.PropTypes.bool,
|
|
|
|
};
|
|
|
|
const defaultProps = {
|
|
|
|
show: false,
|
|
|
|
query: {},
|
|
|
|
onHide: () => {},
|
|
|
|
};
|
|
|
|
|
2016-10-26 20:41:46 -04:00
|
|
|
class VisualizeModal extends React.PureComponent {
|
2016-08-30 00:55:31 -04:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-09-11 10:39:07 -04:00
|
|
|
const uniqueId = shortid.generate();
|
2016-08-30 00:55:31 -04:00
|
|
|
this.state = {
|
2016-09-11 10:39:07 -04:00
|
|
|
chartType: CHART_TYPES[0],
|
|
|
|
datasourceName: uniqueId,
|
2016-08-30 00:55:31 -04:00
|
|
|
columns: {},
|
2016-09-11 10:39:07 -04:00
|
|
|
hints: [],
|
2016-08-30 00:55:31 -04:00
|
|
|
};
|
2016-09-23 14:14:38 -04:00
|
|
|
}
|
|
|
|
componentWillMount() {
|
|
|
|
this.setStateFromProps();
|
2016-09-13 02:09:18 -04:00
|
|
|
}
|
|
|
|
componentDidMount() {
|
2016-09-11 10:39:07 -04:00
|
|
|
this.validate();
|
|
|
|
}
|
2016-09-23 14:14:38 -04:00
|
|
|
setStateFromProps() {
|
2016-10-25 19:44:32 -04:00
|
|
|
if (
|
|
|
|
!this.props.query ||
|
|
|
|
!this.props.query.results ||
|
|
|
|
!this.props.query.results.columns) {
|
2016-09-23 14:14:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const columns = {};
|
|
|
|
this.props.query.results.columns.forEach((col) => {
|
|
|
|
columns[col.name] = col;
|
|
|
|
});
|
|
|
|
this.setState({ columns });
|
|
|
|
}
|
2016-09-11 10:39:07 -04:00
|
|
|
validate() {
|
|
|
|
const hints = [];
|
|
|
|
const cols = this.mergedColumns();
|
|
|
|
const re = /^\w+$/;
|
|
|
|
Object.keys(cols).forEach((colName) => {
|
|
|
|
if (!re.test(colName)) {
|
|
|
|
hints.push(
|
|
|
|
<div>
|
|
|
|
"{colName}" is not right as a column name, please alias it
|
|
|
|
(as in SELECT count(*) <strong>AS my_alias</strong>) using only
|
|
|
|
alphanumeric characters and underscores
|
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (this.state.chartType === null) {
|
|
|
|
hints.push('Pick a chart type!');
|
|
|
|
} else if (this.state.chartType.requiresTime) {
|
|
|
|
let hasTime = false;
|
|
|
|
for (const colName in cols) {
|
|
|
|
const col = cols[colName];
|
|
|
|
if (col.hasOwnProperty('is_date') && col.is_date) {
|
|
|
|
hasTime = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasTime) {
|
|
|
|
hints.push('To use this chart type you need at least one column flagged as a date');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setState({ hints });
|
2016-08-30 00:55:31 -04:00
|
|
|
}
|
|
|
|
changeChartType(option) {
|
2016-09-11 10:39:07 -04:00
|
|
|
this.setState({ chartType: option }, this.validate);
|
2016-08-30 00:55:31 -04:00
|
|
|
}
|
|
|
|
mergedColumns() {
|
|
|
|
const columns = Object.assign({}, this.state.columns);
|
|
|
|
if (this.props.query && this.props.query.results.columns) {
|
|
|
|
this.props.query.results.columns.forEach((col) => {
|
2016-09-23 14:14:38 -04:00
|
|
|
if (columns[col.name] === undefined) {
|
|
|
|
columns[col.name] = col;
|
2016-08-30 00:55:31 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
visualize() {
|
|
|
|
const vizOptions = {
|
2016-09-11 10:39:07 -04:00
|
|
|
chartType: this.state.chartType.value,
|
2016-08-30 00:55:31 -04:00
|
|
|
datasourceName: this.state.datasourceName,
|
|
|
|
columns: this.state.columns,
|
|
|
|
sql: this.props.query.sql,
|
|
|
|
dbId: this.props.query.dbId,
|
|
|
|
};
|
|
|
|
window.open('/caravel/sqllab_viz/?data=' + JSON.stringify(vizOptions));
|
|
|
|
}
|
|
|
|
changeDatasourceName(event) {
|
|
|
|
this.setState({ datasourceName: event.target.value });
|
2016-09-11 10:39:07 -04:00
|
|
|
this.validate();
|
2016-08-30 00:55:31 -04:00
|
|
|
}
|
2016-09-23 14:14:38 -04:00
|
|
|
changeCheckbox(attr, columnName, event) {
|
2016-08-30 00:55:31 -04:00
|
|
|
let columns = this.mergedColumns();
|
2016-09-23 14:14:38 -04:00
|
|
|
const column = Object.assign({}, columns[columnName], { [attr]: event.target.checked });
|
|
|
|
columns = Object.assign({}, columns, { [columnName]: column });
|
2016-09-11 10:39:07 -04:00
|
|
|
this.setState({ columns }, this.validate);
|
2016-08-30 00:55:31 -04:00
|
|
|
}
|
2016-09-23 14:14:38 -04:00
|
|
|
changeAggFunction(columnName, option) {
|
2016-08-30 00:55:31 -04:00
|
|
|
let columns = this.mergedColumns();
|
|
|
|
const val = (option) ? option.value : null;
|
2016-09-23 14:14:38 -04:00
|
|
|
const column = Object.assign({}, columns[columnName], { agg: val });
|
|
|
|
columns = Object.assign({}, columns, { [columnName]: column });
|
2016-09-11 10:39:07 -04:00
|
|
|
this.setState({ columns }, this.validate);
|
2016-08-30 00:55:31 -04:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
if (!(this.props.query)) {
|
|
|
|
return <div />;
|
|
|
|
}
|
|
|
|
const tableData = this.props.query.results.columns.map((col) => ({
|
2016-09-23 14:14:38 -04:00
|
|
|
column: col.name,
|
2016-08-30 00:55:31 -04:00
|
|
|
is_dimension: (
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
2016-09-23 14:14:38 -04:00
|
|
|
onChange={this.changeCheckbox.bind(this, 'is_dim', col.name)}
|
|
|
|
checked={(this.state.columns[col.name]) ? this.state.columns[col.name].is_dim : false}
|
2016-08-30 00:55:31 -04:00
|
|
|
className="form-control"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
is_date: (
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
className="form-control"
|
2016-09-23 14:14:38 -04:00
|
|
|
onChange={this.changeCheckbox.bind(this, 'is_date', col.name)}
|
|
|
|
checked={(this.state.columns[col.name]) ? this.state.columns[col.name].is_date : false}
|
2016-08-30 00:55:31 -04:00
|
|
|
/>
|
|
|
|
),
|
|
|
|
agg_func: (
|
|
|
|
<Select
|
|
|
|
options={[
|
|
|
|
{ value: 'sum', label: 'SUM(x)' },
|
|
|
|
{ value: 'min', label: 'MIN(x)' },
|
|
|
|
{ value: 'max', label: 'MAX(x)' },
|
|
|
|
{ value: 'avg', label: 'AVG(x)' },
|
|
|
|
{ value: 'count_distinct', label: 'COUNT(DISTINCT x)' },
|
|
|
|
]}
|
2016-09-23 14:14:38 -04:00
|
|
|
onChange={this.changeAggFunction.bind(this, col.name)}
|
|
|
|
value={(this.state.columns[col.name]) ? this.state.columns[col.name].agg : null}
|
2016-08-30 00:55:31 -04:00
|
|
|
/>
|
|
|
|
),
|
|
|
|
}));
|
2016-09-13 02:09:18 -04:00
|
|
|
const alerts = this.state.hints.map((hint, i) => (
|
|
|
|
<Alert bsStyle="warning" key={i}>{hint}</Alert>
|
2016-09-11 10:39:07 -04:00
|
|
|
));
|
2016-08-30 00:55:31 -04:00
|
|
|
const modal = (
|
|
|
|
<div className="VisualizeModal">
|
|
|
|
<Modal show={this.props.show} onHide={this.props.onHide}>
|
|
|
|
<Modal.Header closeButton>
|
2016-09-11 10:39:07 -04:00
|
|
|
<Modal.Title>Visualize</Modal.Title>
|
2016-08-30 00:55:31 -04:00
|
|
|
</Modal.Header>
|
|
|
|
<Modal.Body>
|
2016-09-11 10:39:07 -04:00
|
|
|
{alerts}
|
2016-08-30 00:55:31 -04:00
|
|
|
<div className="row">
|
|
|
|
<Col md={6}>
|
|
|
|
Chart Type
|
|
|
|
<Select
|
|
|
|
name="select-chart-type"
|
|
|
|
placeholder="[Chart Type]"
|
2016-09-11 10:39:07 -04:00
|
|
|
options={CHART_TYPES}
|
|
|
|
value={(this.state.chartType) ? this.state.chartType.value : null}
|
2016-08-30 00:55:31 -04:00
|
|
|
autosize={false}
|
|
|
|
onChange={this.changeChartType.bind(this)}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
<Col md={6}>
|
|
|
|
Datasource Name
|
|
|
|
<input
|
|
|
|
type="text"
|
2016-09-11 10:39:07 -04:00
|
|
|
className="form-control input-sm"
|
2016-08-30 00:55:31 -04:00
|
|
|
placeholder="datasource name"
|
|
|
|
onChange={this.changeDatasourceName.bind(this)}
|
|
|
|
value={this.state.datasourceName}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
</div>
|
|
|
|
<hr />
|
|
|
|
<Table
|
|
|
|
className="table table-condensed"
|
|
|
|
columns={['column', 'is_dimension', 'is_date', 'agg_func']}
|
|
|
|
data={tableData}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
onClick={this.visualize.bind(this)}
|
|
|
|
bsStyle="primary"
|
2016-09-11 10:39:07 -04:00
|
|
|
disabled={(this.state.hints.length > 0)}
|
2016-08-30 00:55:31 -04:00
|
|
|
>
|
|
|
|
Visualize
|
|
|
|
</Button>
|
|
|
|
</Modal.Body>
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
return modal;
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 19:44:32 -04:00
|
|
|
VisualizeModal.propTypes = propTypes;
|
|
|
|
VisualizeModal.defaultProps = defaultProps;
|
2016-08-30 00:55:31 -04:00
|
|
|
|
2016-10-25 19:44:32 -04:00
|
|
|
export default VisualizeModal;
|