From b68abaebc86019696a403c76484961ea6c6d815e Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Mon, 13 Dec 2021 17:58:36 -0800 Subject: [PATCH 1/2] feat: add main dttm col to dataset editor --- .../Datasource/DatasourceEditor.jsx | 55 ++++++++++++++++--- .../datasets/examples/cleaned_sales_data.yaml | 2 +- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/superset-frontend/src/components/Datasource/DatasourceEditor.jsx b/superset-frontend/src/components/Datasource/DatasourceEditor.jsx index cd215190b1fdd..a8295b2109847 100644 --- a/superset-frontend/src/components/Datasource/DatasourceEditor.jsx +++ b/superset-frontend/src/components/Datasource/DatasourceEditor.jsx @@ -156,7 +156,9 @@ CollectionTabTitle.propTypes = { function ColumnCollectionTable({ columns, - onChange, + datasource, + onColumnsChange, + onDatasourceChange, editableColumnName, showExpression, allowAddItem, @@ -166,8 +168,22 @@ function ColumnCollectionTable({ return ( editableColumnName ? ( @@ -310,6 +327,24 @@ function ColumnCollectionTable({ {v} ), + main_dttm_col: (value, _onItemChange, _label, record) => { + const checked = datasource.main_dttm_col === record.column_name; + const disabled = !columns.find( + column => column.column_name === record.column_name, + ).is_dttm; + return ( + + onDatasourceChange({ + ...datasource, + main_dttm_col: record.column_name, + }) + } + /> + ); + }, type: d => (d ? : null), is_dttm: checkboxGenerator, filterable: checkboxGenerator, @@ -320,7 +355,9 @@ function ColumnCollectionTable({ } ColumnCollectionTable.propTypes = { columns: PropTypes.array.isRequired, - onChange: PropTypes.func.isRequired, + datasource: PropTypes.object.isRequired, + onColumnsChange: PropTypes.func.isRequired, + onDatasourceChange: PropTypes.func.isRequired, editableColumnName: PropTypes.bool, showExpression: PropTypes.bool, allowAddItem: PropTypes.bool, @@ -1227,9 +1264,11 @@ class DatasourceEditor extends React.PureComponent { + datasource={datasource} + onColumnsChange={databaseColumns => this.setColumns({ databaseColumns }) } + onDatasourceChange={this.onDatasourceChange} /> {this.state.metadataLoading && } @@ -1245,9 +1284,11 @@ class DatasourceEditor extends React.PureComponent { > + onColumnsChange={calculatedColumns => this.setColumns({ calculatedColumns }) } + onDatasourceChange={this.onDatasourceChange} + datasource={datasource} editableColumnName showExpression allowAddItem diff --git a/superset/examples/configs/datasets/examples/cleaned_sales_data.yaml b/superset/examples/configs/datasets/examples/cleaned_sales_data.yaml index 8832cc529d0f7..fcf91bc1a2104 100644 --- a/superset/examples/configs/datasets/examples/cleaned_sales_data.yaml +++ b/superset/examples/configs/datasets/examples/cleaned_sales_data.yaml @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. table_name: cleaned_sales_data -main_dttm_col: OrderDate +main_dttm_col: order_date description: null default_endpoint: null offset: 0 From ce4f40b32e85eb58905b8cad32b3b396a8e63e43 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Mon, 13 Dec 2021 18:32:26 -0800 Subject: [PATCH 2/2] Add tests --- .../spec/fixtures/mockDatasource.js | 1 + .../Datasource/DatasourceEditor.jsx | 1 + .../Datasource/DatasourceEditor.test.jsx | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/superset-frontend/spec/fixtures/mockDatasource.js b/superset-frontend/spec/fixtures/mockDatasource.js index ca60a712b2088..30513fc126748 100644 --- a/superset-frontend/spec/fixtures/mockDatasource.js +++ b/superset-frontend/spec/fixtures/mockDatasource.js @@ -167,6 +167,7 @@ export default { column_types: [0, 1, 2], id, granularity_sqla: [['ds', 'ds']], + main_dttm_col: 'ds', name: 'birth_names', owners: [{ first_name: 'joe', last_name: 'man', id: 1 }], database: { diff --git a/superset-frontend/src/components/Datasource/DatasourceEditor.jsx b/superset-frontend/src/components/Datasource/DatasourceEditor.jsx index a8295b2109847..001fc3cba7332 100644 --- a/superset-frontend/src/components/Datasource/DatasourceEditor.jsx +++ b/superset-frontend/src/components/Datasource/DatasourceEditor.jsx @@ -334,6 +334,7 @@ function ColumnCollectionTable({ ).is_dttm; return ( diff --git a/superset-frontend/src/components/Datasource/DatasourceEditor.test.jsx b/superset-frontend/src/components/Datasource/DatasourceEditor.test.jsx index a0d46a6f1ac0b..8d634930119b6 100644 --- a/superset-frontend/src/components/Datasource/DatasourceEditor.test.jsx +++ b/superset-frontend/src/components/Datasource/DatasourceEditor.test.jsx @@ -230,4 +230,30 @@ describe('DatasourceEditor RTL', () => { userEvent.type(certificationDetails, 'I am typing something new'); expect(certificationDetails.value).toEqual('I am typing something new'); }); + it('shows the default datetime column', async () => { + render(, { useRedux: true }); + const metricButton = screen.getByTestId('collection-tab-Columns'); + userEvent.click(metricButton); + + const dsDefaultDatetimeRadio = screen.getByTestId('radio-default-dttm-ds'); + expect(dsDefaultDatetimeRadio).toBeChecked(); + + const genderDefaultDatetimeRadio = screen.getByTestId( + 'radio-default-dttm-gender', + ); + expect(genderDefaultDatetimeRadio).not.toBeChecked(); + }); + it('allows choosing only temporal columns as the default datetime', async () => { + render(, { useRedux: true }); + const metricButton = screen.getByTestId('collection-tab-Columns'); + userEvent.click(metricButton); + + const dsDefaultDatetimeRadio = screen.getByTestId('radio-default-dttm-ds'); + expect(dsDefaultDatetimeRadio).toBeEnabled(); + + const genderDefaultDatetimeRadio = screen.getByTestId( + 'radio-default-dttm-gender', + ); + expect(genderDefaultDatetimeRadio).toBeDisabled(); + }); });