Skip to content

Commit

Permalink
feat: explicit distribute columns on BoxPlot and apply time grain (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie committed Sep 28, 2022
1 parent 23cd5c9 commit 93f08e7
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ export { legacySortBy } from './shared-controls/legacySortBy';
export * from './shared-controls/emitFilterControl';
export * from './shared-controls/components';
export * from './types';
export { xAxisMixin, temporalColumnMixin } from './shared-controls/constants';
export { xAxisMixin, temporalColumnMixin } from './shared-controls/mixins';
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
FilterOption,
temporalColumnMixin,
} from '..';
import { xAxisMixin } from './constants';
import { xAxisMixin } from './mixins';

type Control = {
savedMetrics?: Metric[] | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ import {
FeatureFlag,
isFeatureEnabled,
QueryFormData,
QueryResponse,
t,
validateNonEmpty,
} from '@superset-ui/core';
import {
BaseControlConfig,
ControlPanelState,
ControlState,
Dataset,
} from '../types';
import { BaseControlConfig, ControlPanelState, ControlState } from '../types';
import { getTemporalColumns } from '../utils';

const getAxisLabel = (
formData: QueryFormData,
Expand Down Expand Up @@ -60,24 +55,11 @@ export const xAxisMixin = {

export const temporalColumnMixin: Pick<BaseControlConfig, 'mapStateToProps'> = {
mapStateToProps: ({ datasource }) => {
if (datasource?.columns[0]?.hasOwnProperty('column_name')) {
const temporalColumns =
(datasource as Dataset)?.columns?.filter(c => c.is_dttm) ?? [];
return {
options: temporalColumns,
default:
(datasource as Dataset)?.main_dttm_col ||
temporalColumns[0]?.column_name ||
null,
isTemporal: true,
};
}
const sortedQueryColumns = (datasource as QueryResponse)?.columns?.sort(
query => (query?.is_dttm ? -1 : 1),
);
const payload = getTemporalColumns(datasource);

return {
options: sortedQueryColumns,
default: sortedQueryColumns[0]?.name || null,
options: payload.temporalColumns,
default: payload.defaultTemporalColumn,
isTemporal: true,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
DatasourceType,
JsonValue,
Metric,
QueryColumn,
QueryFormColumn,
QueryFormData,
QueryFormMetric,
Expand Down Expand Up @@ -449,9 +450,9 @@ export type ColorFormatters = {
export default {};

export function isColumnMeta(
column: AdhocColumn | ColumnMeta,
column: AdhocColumn | ColumnMeta | QueryColumn,
): column is ColumnMeta {
return 'column_name' in column;
return !!column && 'column_name' in column;
}

export function isSavedExpression(
Expand All @@ -477,9 +478,5 @@ export function isDataset(
export function isQueryResponse(
datasource: Dataset | QueryResponse | null | undefined,
): datasource is QueryResponse {
return (
!!datasource &&
('results' in datasource ||
datasource?.type === ('query' as DatasourceType.Query))
);
return !!datasource && 'results' in datasource && 'sql' in datasource;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* 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 {
ensureIsArray,
isDefined,
QueryColumn,
ValueOf,
} from '@superset-ui/core';
import {
ColumnMeta,
ControlPanelState,
isDataset,
isQueryResponse,
} from '@superset-ui/chart-controls';

export const getTemporalColumns = (
datasource: ValueOf<Pick<ControlPanelState, 'datasource'>>,
) => {
const rv: {
temporalColumns: ColumnMeta[] | QueryColumn[];
defaultTemporalColumn: string | null | undefined;
} = {
temporalColumns: [],
defaultTemporalColumn: undefined,
};

if (isDataset(datasource)) {
rv.temporalColumns = ensureIsArray(datasource.columns).filter(
c => c.is_dttm,
);
}
if (isQueryResponse(datasource)) {
rv.temporalColumns = ensureIsArray(datasource.columns).filter(
c => c.is_dttm,
);
}

if (isDataset(datasource)) {
rv.defaultTemporalColumn = datasource.main_dttm_col;
}
if (!isDefined(rv.defaultTemporalColumn)) {
rv.defaultTemporalColumn =
(rv.temporalColumns[0] as ColumnMeta)?.column_name ??
(rv.temporalColumns[0] as QueryColumn)?.name;
}

return rv;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { default as mainMetric } from './mainMetric';
export { default as columnChoices } from './columnChoices';
export * from './defineSavedMetrics';
export * from './getStandardizedControls';
export { getTemporalColumns } from './getTemporalColumns';
149 changes: 149 additions & 0 deletions superset-frontend/packages/superset-ui-chart-controls/test/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* 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 { Dataset } from '@superset-ui/chart-controls';
import { DatasourceType } from '@superset-ui/core';

export const TestDataset: Dataset = {
column_format: {},
columns: [
{
advanced_data_type: null,
certification_details: null,
certified_by: null,
column_name: 'num',
description: null,
expression: '',
filterable: true,
groupby: true,
id: 332,
is_certified: false,
is_dttm: false,
python_date_format: null,
type: 'BIGINT',
type_generic: 0,
verbose_name: null,
warning_markdown: null,
},
{
advanced_data_type: null,
certification_details: null,
certified_by: null,
column_name: 'gender',
description: null,
expression: '',
filterable: true,
groupby: true,
id: 330,
is_certified: false,
is_dttm: false,
python_date_format: null,
type: 'VARCHAR(16)',
type_generic: 1,
verbose_name: '',
warning_markdown: null,
},
{
advanced_data_type: null,
certification_details: null,
certified_by: null,
column_name: 'state',
description: null,
expression: '',
filterable: true,
groupby: true,
id: 333,
is_certified: false,
is_dttm: false,
python_date_format: null,
type: 'VARCHAR(10)',
type_generic: 1,
verbose_name: null,
warning_markdown: null,
},
{
advanced_data_type: null,
certification_details: null,
certified_by: null,
column_name: 'ds',
description: null,
expression: '',
filterable: true,
groupby: true,
id: 329,
is_certified: false,
is_dttm: true,
python_date_format: null,
type: 'TIMESTAMP WITHOUT TIME ZONE',
type_generic: 2,
verbose_name: null,
warning_markdown: null,
},
{
advanced_data_type: null,
certification_details: null,
certified_by: null,
column_name: 'name',
description: null,
expression: '',
filterable: true,
groupby: true,
id: 331,
is_certified: false,
is_dttm: false,
python_date_format: null,
type: 'VARCHAR(255)',
type_generic: 1,
verbose_name: null,
warning_markdown: null,
},
],
datasource_name: 'birth_names',
description: null,
granularity_sqla: 'ds',
id: 2,
main_dttm_col: 'ds',
metrics: [
{
certification_details: null,
certified_by: null,
d3format: null,
description: null,
expression: 'COUNT(*)',
id: 7,
is_certified: false,
metric_name: 'count',
verbose_name: 'COUNT(*)',
warning_markdown: '',
warning_text: null,
},
],
name: 'public.birth_names',
order_by_choices: [],
owners: [
{
first_name: 'admin',
id: 1,
last_name: 'user',
username: 'admin',
},
],
type: DatasourceType.Dataset,
uid: '2__table',
verbose_map: {},
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { DatasourceType, QueryResponse, testQuery } from '@superset-ui/core';
import { DatasourceType, testQueryResponse } from '@superset-ui/core';
import { columnChoices } from '../../src';

describe('columnChoices()', () => {
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('columnChoices()', () => {
});

it('should convert columns to choices when source is a Query', () => {
expect(columnChoices(testQuery as QueryResponse)).toEqual([
expect(columnChoices(testQueryResponse)).toEqual([
['Column 1', 'Column 1'],
['Column 2', 'Column 2'],
['Column 3', 'Column 3'],
Expand Down

0 comments on commit 93f08e7

Please sign in to comment.