Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/docs/using-superset/creating-your-first-dashboard.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ for how the column should be treated in the Explore workflow:

<img src={useBaseUrl('/img/tutorial/tutorial_column_properties.png')} />

In the dataset editor's **Columns** tab, use **Default time filter** beside
**Default datetime column** to set the range of the time filters Explore adds
for this dataset, for example on a new chart or when you add a datetime column
to the filters. Opening a saved chart doesn't change its range.
**Use instance default** inherits the administrator's setting, and **No filter**
means an unbounded range. Chart authors can change the range before saving
their charts.

#### Snowflake column name casing

For physical Snowflake datasets, Superset uses the dialect's automatic identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import {
ColumnOption,
ColumnMeta,
FilterOption,
} from '..';
import {
temporalColumnMixin,
datePickerInAdhocFilterMixin,
xAxisMixin,
} from '..';
} from './mixins';

type Control = {
savedMetrics?: Metric[] | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
ControlState,
ExtraControlProps,
} from '../types';
import { getTemporalColumns } from '../utils';
import { getDatasetDefaultTimeFilter, getTemporalColumns } from '../utils';

const getAxisLabel = (
formData: QueryFormData,
Expand Down Expand Up @@ -114,7 +114,11 @@ export const datePickerInAdhocFilterMixin: Pick<
clause: 'WHERE',
subject: temporalColumn,
operator: 'TEMPORAL_RANGE',
comparator: state?.common?.conf?.DEFAULT_TIME_FILTER || NO_TIME_RANGE,
comparator:
state?.initialTimeRange ||
getDatasetDefaultTimeFilter(state?.datasource) ||
state?.common?.conf?.DEFAULT_TIME_FILTER ||
NO_TIME_RANGE,
expressionType: 'SIMPLE',
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export interface ControlPanelState {
controls: ControlStateMapping;
common: JsonObject;
metadata?: JsonObject | null;
initialTimeRange?: string | null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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 '../types';

/**
* Returns the dataset's `extra.default_time_filter`, which Explore uses instead
* of the instance `DEFAULT_TIME_FILTER` when it adds a time filter. Unparseable
* metadata counts as unset.
*/
export function getDatasetDefaultTimeFilter(
dataset?: Pick<Dataset, 'extra'> | null,
): string | undefined {
let range: unknown;
try {
const extra =
typeof dataset?.extra === 'string'
? JSON.parse(dataset.extra)
: dataset?.extra;
range = extra?.default_time_filter;
} catch {
return undefined;
}
return typeof range === 'string' && range ? range : undefined;
Comment thread
efcasado marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { default as columnChoices, columnsByType } from './columnChoices';
export * from './defineSavedMetrics';
export * from './getStandardizedControls';
export * from './getTemporalColumns';
export * from './getDatasetDefaultTimeFilter';
export * from './displayTimeRelatedControls';
export * from './colorControls';
export * from './metricColumnFilter';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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 { getDatasetDefaultTimeFilter } from '../../src';

test.each([
[
'{"default_time_filter": "Last week", "warning_markdown": "x"}',
'Last week',
],
[{ default_time_filter: 'No filter' }, 'No filter'],
['{"default_time_filter": ""}', undefined],
['{"default_time_filter": 7}', undefined],
['{bad', undefined],
['[]', undefined],
[undefined, undefined],
])('getDatasetDefaultTimeFilter(%p) returns %p', (extra, expected) => {
expect(getDatasetDefaultTimeFilter({ extra })).toBe(expected);
});
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export interface DateFilterControlProps {
name: string;
onChange: (timeRange: string) => void;
value?: string;
placeholder?: string;
onOpenPopover?: () => void;
onClosePopover?: () => void;
overlayStyle?: 'Modal' | 'Popover';
Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/components/Chart/chartAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
buildV1ChartDataPayload,
getQuerySettings,
getChartDataUri,
getInitialTimeRange,
} from 'src/explore/exploreUtils';
import {
addDangerToast,
Expand Down Expand Up @@ -425,6 +426,11 @@ export const dynamicPluginControlsReady =
const exploreState = {
...state.explore,
datasource: state.explore.datasource || { type: 'table' },
initialTimeRange: getInitialTimeRange(
!state.explore.slice,
state.explore.datasource as { extra?: string } | undefined,
state.common?.conf?.DEFAULT_TIME_FILTER as string | undefined,
),
};
const controlsState = getControlsState(
exploreState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import Tabs from '@superset-ui/core/components/Tabs';
import WarningIconWithTooltip from '@superset-ui/core/components/WarningIconWithTooltip';
import TableSelector from 'src/components/TableSelector';
import CheckboxControl from 'src/explore/components/controls/CheckboxControl';
import DateFilterControl from 'src/explore/components/controls/DateFilterControl';
import { getDatasetDefaultTimeFilter } from '@superset-ui/chart-controls';
import TextControl from 'src/explore/components/controls/TextControl';
import TextAreaControl from 'src/explore/components/controls/TextAreaControl';
import withToasts from 'src/components/MessageToasts/withToasts';
Expand Down Expand Up @@ -134,6 +136,7 @@ import { DatasourceFolder } from 'src/explore/components/DatasourcePanel/types';
import {
getDatasetCertification,
isDatasetExtraValid,
parseExtra,
} from './datasetCertification';

const extensionsRegistry = getExtensionsRegistry();
Expand Down Expand Up @@ -544,6 +547,14 @@ const FieldLabelWithTooltip = styled.div`
`}
`;

const DefaultTimeFilterField = styled(Flex)`
${({ theme }) => css`
button[name='default_time_filter'] {
height: ${theme.sizeXL + 2 * (theme.sizeUnit + theme.lineWidth)}px;
}
`}
`;

const StyledButtonWrapper = styled.span`
${({ theme }) => `
margin-top: ${theme.sizeUnit * 3}px;
Expand Down Expand Up @@ -2252,34 +2263,88 @@ function DatasourceEditor({
label: col.verbose_name || col.column_name,
}));

// An unset default time filter inherits the instance default. The field is
// hidden while the Extra JSON is malformed.
const extra = parseExtra(datasource.extra);
const showDefaultTimeFilter = isSqla && !!extra;
const defaultTimeFilter = getDatasetDefaultTimeFilter({ extra });
const setDefaultTimeFilter = (range?: string) => {
// An empty range is unset. Leave the Extra JSON as written when the
// range does not change.
const default_time_filter = range || undefined;
if (default_time_filter === defaultTimeFilter) return;
onDatasourceChange({
...datasource,
extra: JSON.stringify({ ...extra, default_time_filter }),
});
};
const DateFilterComponent =
extensionsRegistry.get('filter.dateFilterControl') ?? DateFilterControl;

return (
<DefaultColumnSettingsContainer data-test="default-column-settings">
<DefaultColumnSettingsTitle>
{t('Default Column Settings')}
</DefaultColumnSettingsTitle>
<Flex vertical gap={(theme?.sizeUnit ?? 4) * 3}>
<Flex vertical gap={theme?.sizeUnit ?? 4}>
<FieldLabelWithTooltip>
<span>{t('Default datetime column')}</span>
<InfoTooltip
tooltip={t(
'Sets the default temporal column for this dataset. Automatically selected as the time column when building charts that require a time dimension and used in dashboard level time filters.',
)}
/>
</FieldLabelWithTooltip>
<Select
key={discardCancelCount}
ariaLabel={t('Default datetime column')}
options={datetimeColumns}
value={datasource.main_dttm_col}
onChange={value =>
handleMainDttmColChange(value as string | undefined)
}
placeholder={t('Select datetime column')}
allowClear
data-test="default-datetime-column-select"
/>
</Flex>
<Row gutter={[16, (theme?.sizeUnit ?? 4) * 3]}>
<Col xs={24} md={showDefaultTimeFilter ? 12 : 24}>
<Flex vertical gap={theme?.sizeUnit ?? 4}>
<FieldLabelWithTooltip>
<span>{t('Default datetime column')}</span>
<InfoTooltip
tooltip={t(
'Sets the default temporal column for this dataset. Automatically selected as the time column when building charts that require a time dimension and used in dashboard level time filters.',
)}
/>
</FieldLabelWithTooltip>
<Select
key={discardCancelCount}
ariaLabel={t('Default datetime column')}
options={datetimeColumns}
value={datasource.main_dttm_col}
onChange={value =>
handleMainDttmColChange(value as string | undefined)
}
placeholder={t('Select datetime column')}
allowClear
data-test="default-datetime-column-select"
/>
</Flex>
</Col>
{showDefaultTimeFilter && (
<Col xs={24} md={12}>
<DefaultTimeFilterField vertical gap={theme?.sizeUnit ?? 4}>
<FieldLabelWithTooltip>
<span id="filter-name-default_time_filter">
{t('Default time filter')}
</span>
<InfoTooltip
tooltip={t(
'Time range of the time filters Explore adds for this dataset, such as on new charts.',
)}
/>
</FieldLabelWithTooltip>
<DateFilterComponent
name="default_time_filter"
placeholder={t('Use instance default')}
value={defaultTimeFilter}
onChange={setDefaultTimeFilter}
/>
{defaultTimeFilter && (
<div>
<Button
buttonStyle="link"
onClick={() => setDefaultTimeFilter()}
>
{t('Use instance default')}
</Button>
</div>
)}
</DefaultTimeFilterField>
</Col>
)}
</Row>
<Flex vertical gap={theme?.sizeUnit ?? 4}>
<FieldLabelWithTooltip>
<span>{t('Currency code column')}</span>
Expand Down Expand Up @@ -2329,6 +2394,7 @@ function DatasourceEditor({
theme?.sizeUnit,
datasource,
onDatasourceChange,
isSqla,
]);

const renderCertificationFieldset = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type JsonObject = Record<string, unknown>;
const isJsonObject = (value: unknown): value is JsonObject =>
typeof value === 'object' && value !== null && !Array.isArray(value);

const parseExtra = (extra?: string): JsonObject | undefined => {
export const parseExtra = (extra?: string): JsonObject | undefined => {
if (!extra?.trim()) {
return {};
}
Expand Down
Loading