/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import React from 'react'; import PropTypes from 'prop-types'; import { Alert, Badge, Col, Radio, Tabs, Tab, Well } from 'react-bootstrap'; import shortid from 'shortid'; import { styled, SupersetClient, t } from '@superset-ui/core'; import Button from 'src/components/Button'; import CertifiedIconWithTooltip from 'src/components/CertifiedIconWithTooltip'; import DatabaseSelector from 'src/components/DatabaseSelector'; import Label from 'src/components/Label'; import Loading from 'src/components/Loading'; import TableSelector from 'src/components/TableSelector'; import EditableTitle from 'src/components/EditableTitle'; import getClientErrorObject from 'src/utils/getClientErrorObject'; import CheckboxControl from 'src/explore/components/controls/CheckboxControl'; import TextControl from 'src/explore/components/controls/TextControl'; import SelectControl from 'src/explore/components/controls/SelectControl'; import TextAreaControl from 'src/explore/components/controls/TextAreaControl'; import SelectAsyncControl from 'src/explore/components/controls/SelectAsyncControl'; import SpatialControl from 'src/explore/components/controls/SpatialControl'; import CollectionTable from 'src/CRUD/CollectionTable'; import Fieldset from 'src/CRUD/Fieldset'; import Field from 'src/CRUD/Field'; import withToasts from 'src/messageToasts/enhancers/withToasts'; const DatasourceContainer = styled.div` .change-warning { margin: 16px 10px 0; color: ${({ theme }) => theme.colors.warning.base}; } .change-warning .bold { font-weight: ${({ theme }) => theme.typography.weights.bold}; } `; const FlexRowContainer = styled.div` align-items: center; display: flex; > svg { margin-right: ${({ theme }) => theme.gridUnit}px; } `; const checkboxGenerator = (d, onChange) => ( ); const DATA_TYPES = ['STRING', 'NUMERIC', 'DATETIME']; const DATASOURCE_TYPES_ARR = [ { key: 'physical', label: t('Physical (table or view)') }, { key: 'virtual', label: t('Virtual (SQL)') }, ]; const DATASOURCE_TYPES = {}; DATASOURCE_TYPES_ARR.forEach(o => { DATASOURCE_TYPES[o.key] = o; }); function CollectionTabTitle({ title, collection }) { return (
{title} {collection ? collection.length : 0}
); } CollectionTabTitle.propTypes = { title: PropTypes.string, collection: PropTypes.array, }; function ColumnCollectionTable({ columns, onChange, editableColumnName, showExpression, allowAddItem, allowEditDataType, itemGenerator, }) { return (
{showExpression && ( } /> )} } /> } /> {allowEditDataType && ( } /> )} {t('The pattern of timestamp format. For strings use ')} {t('python datetime string pattern')} {t(' expression which needs to adhere to the ')} {t('ISO 8601')} {t(` standard to ensure that the lexicographical ordering coincides with the chronological ordering. If the timestamp format does not adhere to the ISO 8601 standard you will need to define an expression and type for transforming the string into a date or timestamp. Note currently time zones are not supported. If time is stored in epoch format, put \`epoch_s\` or \`epoch_ms\`. If no pattern is specified we fall back to using the optional defaults on a per database/column name level via the extra parameter.`)} } control={ } />
} columnLabels={{ column_name: t('Column'), type: t('Data Type'), groupby: t('Is Dimension'), is_dttm: t('Is Temporal'), filterable: t('Is Filterable'), }} onChange={onChange} itemRenderers={{ column_name: (v, onItemChange) => editableColumnName ? ( ) : ( v ), type: d => , is_dttm: checkboxGenerator, filterable: checkboxGenerator, groupby: checkboxGenerator, }} /> ); } ColumnCollectionTable.propTypes = { columns: PropTypes.array.isRequired, onChange: PropTypes.func.isRequired, editableColumnName: PropTypes.bool, showExpression: PropTypes.bool, allowAddItem: PropTypes.bool, allowEditDataType: PropTypes.bool, itemGenerator: PropTypes.func, }; ColumnCollectionTable.defaultProps = { editableColumnName: false, showExpression: false, allowAddItem: false, allowEditDataType: false, itemGenerator: () => ({ column_name: '', filterable: true, groupby: true, }), }; function StackedField({ label, formElement }) { return (
{label}
{formElement}
); } StackedField.propTypes = { label: PropTypes.string, formElement: PropTypes.node, }; function FormContainer({ children }) { return {children}; } FormContainer.propTypes = { children: PropTypes.node, }; const propTypes = { datasource: PropTypes.object.isRequired, onChange: PropTypes.func, addSuccessToast: PropTypes.func.isRequired, addDangerToast: PropTypes.func.isRequired, }; const defaultProps = { onChange: () => {}, }; class DatasourceEditor extends React.PureComponent { constructor(props) { super(props); this.state = { datasource: props.datasource, errors: [], isDruid: props.datasource.type === 'druid' || props.datasource.datasource_type === 'druid', isSqla: props.datasource.datasource_type === 'table' || props.datasource.type === 'table', databaseColumns: props.datasource.columns.filter(col => !col.expression), calculatedColumns: props.datasource.columns.filter( col => !!col.expression, ), metadataLoading: false, activeTabKey: 0, datasourceType: props.datasource.sql ? DATASOURCE_TYPES.virtual.key : DATASOURCE_TYPES.physical.key, }; this.onChange = this.onChange.bind(this); this.onDatasourcePropChange = this.onDatasourcePropChange.bind(this); this.onDatasourceChange = this.onDatasourceChange.bind(this); this.syncMetadata = this.syncMetadata.bind(this); this.setColumns = this.setColumns.bind(this); this.validateAndChange = this.validateAndChange.bind(this); this.handleTabSelect = this.handleTabSelect.bind(this); } onChange() { // Emptying SQL if "Physical" radio button is selected // Currently the logic to know whether the source is // physical or virtual is based on whether SQL is empty or not. const { datasourceType, datasource } = this.state; const sql = datasourceType === DATASOURCE_TYPES.physical.key ? '' : datasource.sql; const newDatasource = { ...this.state.datasource, sql, columns: [...this.state.databaseColumns, ...this.state.calculatedColumns], }; this.props.onChange(newDatasource, this.state.errors); } onDatasourceChange(datasource) { this.setState({ datasource }, this.validateAndChange); } onDatasourcePropChange(attr, value) { const datasource = { ...this.state.datasource, [attr]: value }; this.setState( prevState => ({ datasource: { ...prevState.datasource, [attr]: value } }), this.onDatasourceChange(datasource), ); } onDatasourceTypeChange(datasourceType) { this.setState({ datasourceType }); } setColumns(obj) { this.setState(obj, this.validateAndChange); } validateAndChange() { this.validate(this.onChange); } updateColumns(cols) { const { databaseColumns } = this.state; const databaseColumnNames = cols.map(col => col.name); const currentCols = databaseColumns.reduce( (agg, col) => ({ ...agg, [col.column_name]: col, }), {}, ); const finalColumns = []; const results = { added: [], modified: [], removed: databaseColumns .map(col => col.column_name) .filter(col => !databaseColumnNames.includes(col)), }; cols.forEach(col => { const currentCol = currentCols[col.name]; if (!currentCol) { // new column finalColumns.push({ id: shortid.generate(), column_name: col.name, type: col.type, groupby: true, filterable: true, }); results.added.push(col.name); } else if (currentCol.type !== col.type) { // modified column finalColumns.push({ ...currentCol, type: col.type, }); results.modified.push(col.name); } else { // unchanged finalColumns.push(currentCol); } }); if ( results.added.length || results.modified.length || results.removed.length ) { this.setColumns({ databaseColumns: finalColumns }); } return results; } syncMetadata() { const { datasource } = this.state; // Handle carefully when the schema is empty const endpoint = `/datasource/external_metadata/${ datasource.type || datasource.datasource_type }/${datasource.id}/` + `?db_id=${datasource.database.id}` + `&schema=${datasource.schema || ''}` + `&table_name=${datasource.datasource_name || datasource.table_name}`; this.setState({ metadataLoading: true }); SupersetClient.get({ endpoint }) .then(({ json }) => { const results = this.updateColumns(json); if (results.modified.length) this.props.addSuccessToast( t('Modified columns: %s', results.modified.join(', ')), ); if (results.removed.length) this.props.addSuccessToast( t('Removed columns: %s', results.removed.join(', ')), ); if (results.added.length) this.props.addSuccessToast( t('New columns added: %s', results.added.join(', ')), ); this.props.addSuccessToast(t('Metadata has been synced')); this.setState({ metadataLoading: false }); }) .catch(response => getClientErrorObject(response).then(({ error, statusText }) => { this.props.addDangerToast( error || statusText || t('An error has occurred'), ); this.setState({ metadataLoading: false }); }), ); } findDuplicates(arr, accessor) { const seen = {}; const dups = []; arr.forEach(obj => { const item = accessor(obj); if (item in seen) { dups.push(item); } else { seen[item] = null; } }); return dups; } validate(callback) { let errors = []; let dups; const { datasource } = this.state; // Looking for duplicate column_name dups = this.findDuplicates(datasource.columns, obj => obj.column_name); errors = errors.concat( dups.map(name => t('Column name [%s] is duplicated', name)), ); // Looking for duplicate metric_name dups = this.findDuplicates(datasource.metrics, obj => obj.metric_name); errors = errors.concat( dups.map(name => t('Metric name [%s] is duplicated', name)), ); // Making sure calculatedColumns have an expression defined const noFilterCalcCols = this.state.calculatedColumns.filter( col => !col.expression && !col.json, ); errors = errors.concat( noFilterCalcCols.map(col => t('Calculated column [%s] requires an expression', col.column_name), ), ); this.setState({ errors }, callback); } handleTabSelect(activeTabKey) { this.setState({ activeTabKey }); } renderSettingsFieldset() { const { datasource } = this.state; return (
} /> } /> } /> {this.state.isSqla && ( } /> )} data.result.map(pk => ({ value: pk.value, label: `${pk.text}`, })) } /> } controlProps={{}} />
); } renderAdvancedFieldset() { const { datasource } = this.state; return (
} /> } /> {this.state.isSqla && ( } /> )}
); } renderSpatialTab() { const { datasource } = this.state; const { spatials, all_cols: allCols } = datasource; return ( } eventKey={4} > ({ name: '', type: '', config: null, })} collection={spatials} allowDeletes itemRenderers={{ name: (d, onChange) => ( ), config: (v, onChange) => ( ), }} /> ); } renderSourceFieldset() { const { datasource } = this.state; return (
{DATASOURCE_TYPES_ARR.map(type => ( {type.label} ))}

{this.state.datasourceType === DATASOURCE_TYPES.virtual.key && (
{this.state.isSqla && ( <> this.onDatasourcePropChange('schema', schema) } onDbChange={database => this.onDatasourcePropChange('database', database) } formMode={false} handleError={this.props.addDangerToast} /> } /> { this.onDatasourcePropChange('table_name', table); }} placeholder={t('dataset name')} /> } /> } /> )} {this.state.isDruid && ( {t('The JSON metric or post aggregation definition.')}
} control={ } /> )}
)} {this.state.datasourceType === DATASOURCE_TYPES.physical.key && ( {this.state.isSqla && ( this.onDatasourcePropChange('schema', schema) } onDbChange={database => this.onDatasourcePropChange('database', database) } onTableChange={table => { this.onDatasourcePropChange('table_name', table); }} isDatabaseSelectEnabled={false} /> } description={t( 'The pointer to a physical table (or view). Keep in mind that the chart is ' + 'associated to this Superset logical table, and this logical table points ' + 'the physical table referenced here.', )} /> )} )} ); } renderErrors() { if (this.state.errors.length > 0) { return ( {this.state.errors.map(err => (
{err}
))}
); } return null; } renderMetricCollection() { return (
} /> } /> } /> } /> } /> } />
} collection={this.state.datasource.metrics} allowAddItem onChange={this.onDatasourcePropChange.bind(this, 'metrics')} itemGenerator={() => ({ metric_name: '', verbose_name: '', expression: '', })} itemRenderers={{ metric_name: (v, onChange, _, record) => ( {record.is_certified && ( )} ), verbose_name: (v, onChange) => ( ), expression: (v, onChange) => ( ), description: (v, onChange, label) => ( } /> ), d3format: (v, onChange, label) => ( } /> ), }} allowDeletes /> ); } render() { const { datasource, activeTabKey } = this.state; return ( {this.renderErrors()}
{t('Be careful.')} {t( 'Changing these settings will affect all charts using this dataset, including charts owned by other people.', )}
{activeTabKey === 0 && this.renderSourceFieldset()} } eventKey={1} > {activeTabKey === 1 && this.renderMetricCollection()} } eventKey={2} > {activeTabKey === 2 && (
this.setColumns({ databaseColumns }) } /> {this.state.metadataLoading && }
)}
} eventKey={3} > {activeTabKey === 3 && ( this.setColumns({ calculatedColumns }) } editableColumnName showExpression allowAddItem allowEditDataType itemGenerator={() => ({ column_name: '', filterable: true, groupby: true, expression: '', __expanded: true, })} /> )} {activeTabKey === 4 && (
{this.renderSettingsFieldset()} {this.renderAdvancedFieldset()}
)}
); } } DatasourceEditor.defaultProps = defaultProps; DatasourceEditor.propTypes = propTypes; export default withToasts(DatasourceEditor);