Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(native-filters): allow cascading from time and numeric filters #23319

Merged
merged 6 commits into from
Mar 14, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { useToasts } from 'src/components/MessageToasts/withToasts';
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import { cacheWrapper } from 'src/utils/cacheWrapper';
import { NativeFiltersForm } from '../types';
import { doesColumnMatchFilterType } from './utils';

interface ColumnSelectProps {
allowClear?: boolean;
Expand Down Expand Up @@ -84,10 +83,7 @@ export function ColumnSelect({
);

useEffect(() => {
if (
currentColumn &&
!doesColumnMatchFilterType(currentFilterType, currentColumn)
) {
if (currentColumn && !filterValues(currentColumn)) {
Comment on lines -87 to +86
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing the time column select to fail on numerical range filter, as it validated that the selected column was numerical (obviously not the case).

resetColumnField();
}
}, [currentColumn, currentFilterType, resetColumnField]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ import { Select } from 'src/components';
import { CollapsibleControl } from './CollapsibleControl';

interface DependencyListProps {
availableFilters: { label: string; value: string }[];
availableFilters: {
label: string;
value: string;
type: string | undefined;
}[];
dependencies: string[];
onDependenciesChange: (dependencies: string[]) => void;
getDependencySuggestion: () => string;
children?: JSX.Element;
}

const MainPanel = styled.div`
Expand Down Expand Up @@ -176,6 +181,7 @@ const DependencyList = ({
dependencies = [],
onDependenciesChange,
getDependencySuggestion,
children,
}: DependencyListProps) => {
const hasAvailableFilters = availableFilters.length > 0;
const hasDependencies = dependencies.length > 0;
Expand Down Expand Up @@ -205,6 +211,7 @@ const DependencyList = ({
onDependenciesChange={onDependenciesChange}
getDependencySuggestion={getDependencySuggestion}
/>
{children}
</CollapsibleControl>
</MainPanel>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ export interface FiltersConfigFormProps {
removedFilters: Record<string, FilterRemoval>;
restoreFilter: (filterId: string) => void;
form: FormInstance<NativeFiltersForm>;
getAvailableFilters: (filterId: string) => { label: string; value: string }[];
getAvailableFilters: (
filterId: string,
) => { label: string; value: string; type: string | undefined }[];
handleActiveFilterPanelChange: (activeFilterPanel: string | string[]) => void;
activeFilterPanelKeys: string | string[];
isActive: boolean;
Expand Down Expand Up @@ -654,6 +656,9 @@ const FiltersConfigForm = (

const availableFilters = getAvailableFilters(filterId);
const hasAvailableFilters = availableFilters.length > 0;
const hasTimeDependency = availableFilters
.filter(filter => filter.type === 'filter_time')
.some(filter => dependencies.includes(filter.value));

useEffect(() => {
if (datasetId) {
Expand Down Expand Up @@ -736,6 +741,42 @@ const FiltersConfigForm = (
return <RemovedFilter onClick={() => restoreFilter(filterId)} />;
}

const timeColumn = (
<StyledRowFormItem
name={['filters', filterId, 'granularity_sqla']}
label={
<>
<StyledLabel>{t('Time column')}</StyledLabel>&nbsp;
<InfoTooltipWithTrigger
placement="top"
tooltip={
hasTimeDependency
? t('Time column to apply dependent temporal filter to')
: t('Time column to apply time range to')
}
/>
</>
}
initialValue={filterToEdit?.granularity_sqla}
>
<ColumnSelect
allowClear
form={form}
formField="granularity_sqla"
filterId={filterId}
filterValues={(column: Column) => !!column.is_dttm}
datasetId={datasetId}
onChange={column => {
// We need reset default value when when column changed
setNativeFilterFieldValues(form, filterId, {
granularity_sqla: column,
});
forceUpdate();
}}
/>
</StyledRowFormItem>
);

return (
<StyledTabs
activeKey={activeTabKey}
Expand Down Expand Up @@ -892,7 +933,9 @@ const FiltersConfigForm = (
getDependencySuggestion={() =>
getDependencySuggestion(filterId)
}
/>
>
{hasTimeDependency ? timeColumn : undefined}
</DependencyList>
</StyledRowFormItem>
)}
{hasDataset && hasAdditionalFilters && (
Expand Down Expand Up @@ -966,39 +1009,9 @@ const FiltersConfigForm = (
/>
</StyledRowFormItem>
)}
{hasTimeRange && (
<StyledRowFormItem
name={['filters', filterId, 'granularity_sqla']}
label={
<>
<StyledLabel>{t('Time column')}</StyledLabel>&nbsp;
<InfoTooltipWithTrigger
placement="top"
tooltip={t(
'Optional time column if time range should apply to another column than the default time column',
)}
/>
</>
}
initialValue={filterToEdit?.granularity_sqla}
>
<ColumnSelect
allowClear
form={form}
formField="granularity_sqla"
filterId={filterId}
filterValues={(column: Column) => !!column.is_dttm}
datasetId={datasetId}
onChange={column => {
// We need reset default value when when column changed
setNativeFilterFieldValues(form, filterId, {
granularity_sqla: column,
});
forceUpdate();
}}
/>
</StyledRowFormItem>
)}
{hasTimeRange && !hasTimeDependency
? timeColumn
: undefined}
</CollapsibleControl>
</CleanFormItem>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export interface FiltersConfigModalProps {
onSave: (filterConfig: FilterConfiguration) => Promise<void>;
onCancel: () => void;
}
export const ALLOW_DEPENDENCIES = ['filter_select'];
export const ALLOW_DEPENDENCIES = [
'filter_range',
'filter_select',
'filter_time',
];

const DEFAULT_EMPTY_FILTERS: string[] = [];
const DEFAULT_REMOVED_FILTERS: Record<string, FilterRemoval> = {};
Expand Down Expand Up @@ -287,11 +291,12 @@ function FiltersConfigModal({
const getAvailableFilters = useCallback(
(filterId: string) =>
filterIds
.filter(key => key !== filterId)
.filter(filterId => canBeUsedAsDependency(filterId))
.map(key => ({
label: getFilterTitle(key),
value: key,
.filter(id => id !== filterId)
.filter(id => canBeUsedAsDependency(id))
.map(id => ({
label: getFilterTitle(id),
value: id,
type: filterConfigMap[id]?.filterType,
})),
[canBeUsedAsDependency, filterIds, getFilterTitle],
);
Expand Down