Skip to content

Commit

Permalink
feat: time filter shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongjie Zhao committed Nov 19, 2023
1 parent 2499a1c commit 223ba8b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ import {
FRAME_OPTIONS,
guessFrame,
useDefaultTimeFilter,
useTimeFilterShortcuts,
} from './utils';
import {
CommonFrame,
CalendarFrame,
CustomFrame,
AdvancedFrame,
DateLabel,
ShortcutsFrame,
} from './components';

const StyledRangeType = styled(Select)`
Expand Down Expand Up @@ -159,12 +161,13 @@ export default function DateFilterLabel(props: DateFilterControlProps) {
isOverflowingFilterBar = false,
} = props;
const defaultTimeFilter = useDefaultTimeFilter();
const shortcuts = useTimeFilterShortcuts();

const value = props.value ?? defaultTimeFilter;
const [actualTimeRange, setActualTimeRange] = useState<string>(value);

const [show, setShow] = useState<boolean>(false);
const guessedFrame = useMemo(() => guessFrame(value), [value]);
const guessedFrame = useMemo(() => guessFrame(value, shortcuts), [value]);
const [frame, setFrame] = useState<FrameType>(guessedFrame);
const [lastFetchedTimeRange, setLastFetchedTimeRange] = useState(value);
const [timeRangeValue, setTimeRangeValue] = useState(value);
Expand Down Expand Up @@ -302,6 +305,9 @@ export default function DateFilterLabel(props: DateFilterControlProps) {
{frame === 'Custom' && (
<CustomFrame value={timeRangeValue} onChange={setTimeRangeValue} />
)}
{frame === 'Shortcuts' && (
<ShortcutsFrame value={timeRangeValue} onChange={setTimeRangeValue} />
)}
{frame === 'No filter' && (
<div data-test={DATE_FILTER_TEST_KEY.noFilter} />
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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 { t } from '@superset-ui/core';
import { Radio } from 'src/components/Radio';
import { useTimeFilterShortcuts } from 'src/explore/components/controls/DateFilterControl/utils';
import { FrameComponentProps } from 'src/explore/components/controls/DateFilterControl/types';

export function ShortcutsFrame(props: FrameComponentProps) {
const shortCuts = useTimeFilterShortcuts();

return (
<>
<div className="section-title">{t('Configure Time Range:')}</div>
<Radio.Group
value={props.value}
onChange={(e: any) => props.onChange(e.target.value)}
>
{shortCuts.map(([key, value]: [string, string]) => (
<Radio key={key} value={value} className="vertical-radio">
{key}
</Radio>
))}
</Radio.Group>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export { CalendarFrame } from './CalendarFrame';
export { CustomFrame } from './CustomFrame';
export { AdvancedFrame } from './AdvancedFrame';
export { DateLabel } from './DateLabel';
export { ShortcutsFrame } from './ShortcutsFrame';
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export type FrameType =
| 'Calendar'
| 'Custom'
| 'Advanced'
| 'No filter';
| 'No filter'
| 'Shortcuts';

export type DateTimeGrainType =
| 'second'
Expand Down Expand Up @@ -98,4 +99,5 @@ export interface DateFilterControlProps {
onClosePopover?: () => void;
overlayStyle?: 'Modal' | 'Popover';
isOverflowingFilterBar?: boolean;
timeFilterShortcuts?: [string, string][];
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const FRAME_OPTIONS: SelectOptionType[] = [
{ value: 'Calendar', label: t('Previous') },
{ value: 'Custom', label: t('Custom') },
{ value: 'Advanced', label: t('Advanced') },
{ value: 'Shortcuts', label: t('Shortcuts') },
{ value: 'No filter', label: t('No filter') },
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export const formatTimeRange = (
)}${columnPlaceholder} < ${formatDateEndpoint(splitDateRange[1])}`;
};

export const guessFrame = (timeRange: string): FrameType => {
export const guessFrame = (
timeRange: string,
shortcuts: [string, string][],
): FrameType => {
if (COMMON_RANGE_VALUES_SET.has(timeRange)) {
return 'Common';
}
Expand All @@ -60,6 +63,9 @@ export const guessFrame = (timeRange: string): FrameType => {
if (customTimeRangeDecode(timeRange).matchedFlag) {
return 'Custom';
}
if (shortcuts.filter(s => s[1] === timeRange)) {
return 'Shortcuts';
}
return 'Advanced';
};

Expand Down Expand Up @@ -93,3 +99,11 @@ export function useDefaultTimeFilter() {
) ?? NO_TIME_RANGE
);
}

export function useTimeFilterShortcuts(): [string, string][] {
return (
useSelector(
(state: JsonObject) => state?.common?.conf?.TIME_FILTER_SHORTCUTS,
) ?? []
);
}
5 changes: 5 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def _try_json_readsha(filepath: str, length: int) -> str | None:
# default time filter in explore
# values may be "Last day", "Last week", "<ISO date> : now", etc.
DEFAULT_TIME_FILTER = NO_TIME_RANGE
TIME_FILTER_SHORTCUTS = [
["Current week", "DATETRUNC(DATETIME('today'), WEEK) : today"],
["Current month(MTD)", "DATETRUNC(DATETIME('today'), MONTH) : today"],
["Current year(YTD)", "DATETRUNC(DATETIME('today'), YEAR) : today"],
]

SUPERSET_WEBSERVER_PROTOCOL = "http"
SUPERSET_WEBSERVER_ADDRESS = "0.0.0.0"
Expand Down
1 change: 1 addition & 0 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"NATIVE_FILTER_DEFAULT_ROW_LIMIT",
"PREVENT_UNSAFE_DEFAULT_URLS_ON_DATASET",
"JWT_ACCESS_CSRF_COOKIE_NAME",
"TIME_FILTER_SHORTCUTS",
)

logger = logging.getLogger(__name__)
Expand Down

0 comments on commit 223ba8b

Please sign in to comment.