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: add Current time-range options for time filter #28637

Merged
merged 12 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -53,6 +53,7 @@
AdvancedFrame,
DateLabel,
} from './components';
import { CurrentCalendarFrame } from './components/CurrentCalendarFrame';

const StyledRangeType = styled(Select)`
width: 272px;
Expand Down Expand Up @@ -201,6 +202,7 @@
if (
guessedFrame === 'Common' ||
guessedFrame === 'Calendar' ||
guessedFrame === 'Current' ||
guessedFrame === 'No filter'
) {
setActualTimeRange(value);
Expand Down Expand Up @@ -296,6 +298,9 @@
{frame === 'Calendar' && (
<CalendarFrame value={timeRangeValue} onChange={setTimeRangeValue} />
)}
{frame === 'Current' && (
<CurrentCalendarFrame value={timeRangeValue} onChange={setTimeRangeValue} />

Check failure on line 302 in superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterLabel.tsx

View workflow job for this annotation

GitHub Actions / frontend-build

Replace `·value={timeRangeValue}·onChange={setTimeRangeValue}` with `⏎··········value={timeRangeValue}⏎··········onChange={setTimeRangeValue}⏎·······`
)}
{frame === 'Advanced' && (
<AdvancedFrame value={timeRangeValue} onChange={setTimeRangeValue} />
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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, { useEffect } from 'react';
import { t } from '@superset-ui/core';
import { Radio } from 'src/components/Radio';
import {
CURRENT_CALENDAR_RANGE_OPTIONS,
CURRENT_CALENDAR_RANGE_SET,
} from 'src/explore/components/controls/DateFilterControl/utils';
import {
CurrentCalendarRangeType,
CurrentCalendarWeek,
FrameComponentProps,
} from '../types';

export function CurrentCalendarFrame({ onChange, value }: FrameComponentProps) {
useEffect(() => {
if (!CURRENT_CALENDAR_RANGE_SET.has(value as CurrentCalendarRangeType)) {
onChange(CurrentCalendarWeek);
}
}, [onChange, value]);

if (!CURRENT_CALENDAR_RANGE_SET.has(value as CurrentCalendarRangeType)) {
return null;
}

return (
<>
<div className="section-title">
{t('Configure Time Range: Current...')}
</div>
<Radio.Group
value={value}
onChange={(e: any) => onChange(e.target.value)}
>
{CURRENT_CALENDAR_RANGE_OPTIONS.map(({ value, label }) => (
<Radio key={value} value={value} className="vertical-radio">
{label}
</Radio>
))}
</Radio.Group>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
export { CommonFrame } from './CommonFrame';
export { CalendarFrame } from './CalendarFrame';
export {CurrentCalendarFrame} from './CurrentCalendarFrame'

Check failure on line 21 in superset-frontend/src/explore/components/controls/DateFilterControl/components/index.ts

View workflow job for this annotation

GitHub Actions / frontend-build

Replace `CurrentCalendarFrame}·from·'./CurrentCalendarFrame'` with `·CurrentCalendarFrame·}·from·'./CurrentCalendarFrame';`
export { CustomFrame } from './CustomFrame';
export { AdvancedFrame } from './AdvancedFrame';
export { DateLabel } from './DateLabel';
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
export type FrameType =
| 'Common'
| 'Calendar'
| 'Current'
| 'Custom'
| 'Advanced'
| 'No filter';
Expand Down Expand Up @@ -85,6 +86,14 @@
| typeof PreviousCalendarMonth
| typeof PreviousCalendarYear;

export const CurrentCalendarWeek = 'current calendar week';
export const CurrentCalendarMonth = 'current calendar month';
export const CurrentCalendarYear = 'current calendar year';
export type CurrentCalendarRangeType =
| typeof CurrentCalendarWeek
| typeof CurrentCalendarMonth
| typeof CurrentCalendarYear;

Check failure on line 96 in superset-frontend/src/explore/components/controls/DateFilterControl/types.ts

View workflow job for this annotation

GitHub Actions / frontend-build

Delete `··`
export type FrameComponentProps = {
onChange: (timeRange: string) => void;
value: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
PreviousCalendarYear,
CommonRangeType,
CalendarRangeType,
CurrentCalendarRangeType,
CurrentCalendarWeek,
CurrentCalendarMonth,
CurrentCalendarYear,
} from 'src/explore/components/controls/DateFilterControl/types';

export const FRAME_OPTIONS: SelectOptionType[] = [
{ value: 'Common', label: t('Last') },
{ value: 'Calendar', label: t('Previous') },
{ value: 'Current', label: t('Current') },
{ value: 'Custom', label: t('Custom') },
{ value: 'Advanced', label: t('Advanced') },
{ value: 'No filter', label: t('No filter') },
Expand Down Expand Up @@ -58,6 +63,18 @@
CALENDAR_RANGE_OPTIONS.map(({ value }) => value),
);

export const CURRENT_CALENDAR_RANGE_OPTIONS: SelectOptionType[] = [
{ value: CurrentCalendarWeek, label: t('current calendar week') },
{
value: CurrentCalendarMonth,
label: t('current calendar month'),
},
{ value: CurrentCalendarYear, label: t('current calendar year') },
];
export const CURRENT_CALENDAR_RANGE_VALUES_SET = new Set(
CURRENT_CALENDAR_RANGE_OPTIONS.map(({ value }) => value),
);

const GRAIN_OPTIONS = [
{ value: 'second', label: (rel: string) => t('Seconds %s', rel) },
{ value: 'minute', label: (rel: string) => t('Minutes %s', rel) },
Expand Down Expand Up @@ -107,6 +124,12 @@
PreviousCalendarYear,
]);

export const CURRENT_CALENDAR_RANGE_SET: Set<CurrentCalendarRangeType> = new Set([

Check failure on line 127 in superset-frontend/src/explore/components/controls/DateFilterControl/utils/constants.ts

View workflow job for this annotation

GitHub Actions / frontend-build

Replace `·new·Set([⏎··CurrentCalendarWeek,⏎··CurrentCalendarMonth,⏎··CurrentCalendarYear,⏎` with `⏎··new·Set([CurrentCalendarWeek,·CurrentCalendarMonth,·CurrentCalendarYear`
CurrentCalendarWeek,
CurrentCalendarMonth,
CurrentCalendarYear,
]);

export const MOMENT_FORMAT = 'YYYY-MM-DD[T]HH:mm:ss';
export const SEVEN_DAYS_AGO = moment()
.utc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useSelector } from 'react-redux';
import {
COMMON_RANGE_VALUES_SET,
CALENDAR_RANGE_VALUES_SET,
CURRENT_CALENDAR_RANGE_VALUES_SET,
customTimeRangeDecode,
} from '.';
import { FrameType } from '../types';
Expand All @@ -32,6 +33,9 @@ export const guessFrame = (timeRange: string): FrameType => {
if (CALENDAR_RANGE_VALUES_SET.has(timeRange)) {
return 'Calendar';
}
if (CURRENT_CALENDAR_RANGE_VALUES_SET.has(timeRange)) {
return 'Current';
}
if (timeRange === NO_TIME_RANGE) {
return 'No filter';
}
Expand Down
18 changes: 18 additions & 0 deletions superset/utils/date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ def get_since_until( # pylint: disable=too-many-arguments,too-many-locals,too-m
and separator not in time_range
):
time_range = "DATETRUNC(DATEADD(DATETIME('today'), -1, YEAR), YEAR) : DATETRUNC(DATETIME('today'), YEAR)" # pylint: disable=line-too-long,useless-suppression
if (
time_range
and time_range.startswith("current calendar week")
and separator not in time_range
):
time_range = "DATETRUNC(DATEADD(DATETIME('today'), 0, WEEK), WEEK) : DATETRUNC(DATETIME('today'), DAY)" # pylint: disable=line-too-long,useless-suppression
if (
time_range
and time_range.startswith("current calendar month")
and separator not in time_range
):
time_range = "DATETRUNC(DATEADD(DATETIME('today'), 0, MONTH), MONTH) : DATETRUNC(DATETIME('today'), DAY)" # pylint: disable=line-too-long,useless-suppression
if (
time_range
and time_range.startswith("current calendar year")
and separator not in time_range
):
time_range = "DATETRUNC(DATEADD(DATETIME('today'), 0, YEAR), YEAR) : DATETRUNC(DATETIME('today'), DAY)" # pylint: disable=line-too-long,useless-suppression

if time_range and separator in time_range:
time_range_lookup = [
Expand Down
Loading