mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
refactor: reorganize dateFilterUtils.tx (#14309)
* refactor: reorganize dateFilterUtils.tx * move spec to src dirctory
This commit is contained in:
@@ -28,7 +28,11 @@ import {
|
||||
import {
|
||||
buildTimeRangeString,
|
||||
formatTimeRange,
|
||||
} from 'src/explore/dateFilterUtils';
|
||||
COMMON_RANGE_VALUES_SET,
|
||||
CALENDAR_RANGE_VALUES_SET,
|
||||
FRAME_OPTIONS,
|
||||
customTimeRangeDecode,
|
||||
} from 'src/explore/components/controls/DateFilterControl/utils';
|
||||
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
|
||||
import Button from 'src/components/Button';
|
||||
import ControlHeader from 'src/explore/components/ControlHeader';
|
||||
@@ -43,12 +47,7 @@ import { useDebouncedEffect } from 'src/explore/exploreUtils';
|
||||
import { SLOW_DEBOUNCE } from 'src/constants';
|
||||
import { testWithId } from 'src/utils/testUtils';
|
||||
import { SelectOptionType, FrameType } from './types';
|
||||
import {
|
||||
COMMON_RANGE_VALUES_SET,
|
||||
CALENDAR_RANGE_VALUES_SET,
|
||||
FRAME_OPTIONS,
|
||||
customTimeRangeDecode,
|
||||
} from './utils';
|
||||
|
||||
import {
|
||||
CommonFrame,
|
||||
CalendarFrame,
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
*/
|
||||
import React from 'react';
|
||||
import { t } from '@superset-ui/core';
|
||||
import { SEPARATOR } from 'src/explore/dateFilterUtils';
|
||||
import { SEPARATOR } from 'src/explore/components/controls/DateFilterControl/utils';
|
||||
import { Input } from 'src/common/components';
|
||||
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
|
||||
import { FrameComponentProps } from 'src/explore/components/controls/DateFilterControl/types';
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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 { TimeRangeEndpoints } from '@superset-ui/core';
|
||||
|
||||
export const SEPARATOR = ' : ';
|
||||
|
||||
export const buildTimeRangeString = (since: string, until: string): string =>
|
||||
`${since}${SEPARATOR}${until}`;
|
||||
|
||||
const formatDateEndpoint = (dttm: string, isStart?: boolean): string =>
|
||||
dttm.replace('T00:00:00', '') || (isStart ? '-∞' : '∞');
|
||||
|
||||
export const formatTimeRange = (
|
||||
timeRange: string,
|
||||
endpoints?: TimeRangeEndpoints,
|
||||
) => {
|
||||
const splitDateRange = timeRange.split(SEPARATOR);
|
||||
if (splitDateRange.length === 1) return timeRange;
|
||||
const formattedEndpoints = (
|
||||
endpoints || ['unknown', 'unknown']
|
||||
).map((endpoint: string) => (endpoint === 'inclusive' ? '≤' : '<'));
|
||||
|
||||
return `${formatDateEndpoint(splitDateRange[0], true)} ${
|
||||
formattedEndpoints[0]
|
||||
} col ${formattedEndpoints[1]} ${formatDateEndpoint(splitDateRange[1])}`;
|
||||
};
|
||||
@@ -17,13 +17,13 @@
|
||||
* under the License.
|
||||
*/
|
||||
import moment, { Moment } from 'moment';
|
||||
import { SEPARATOR } from 'src/explore/dateFilterUtils';
|
||||
import {
|
||||
CustomRangeDecodeType,
|
||||
CustomRangeType,
|
||||
DateTimeGrainType,
|
||||
DateTimeModeType,
|
||||
} from 'src/explore/components/controls/DateFilterControl/types';
|
||||
import { SEPARATOR } from './dateFilterUtils';
|
||||
import { SEVEN_DAYS_AGO, MIDNIGHT, MOMENT_FORMAT } from './constants';
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,3 +18,4 @@
|
||||
*/
|
||||
export * from './dateParser';
|
||||
export * from './constants';
|
||||
export * from './dateFilterUtils';
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
/**
|
||||
* 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 {
|
||||
customTimeRangeEncode,
|
||||
customTimeRangeDecode,
|
||||
buildTimeRangeString,
|
||||
formatTimeRange,
|
||||
} from 'src/explore/components/controls/DateFilterControl/utils';
|
||||
|
||||
describe('Custom TimeRange', () => {
|
||||
describe('customTimeRangeEncode', () => {
|
||||
it('1) specific : specific', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: '2021-01-20T00:00:00',
|
||||
sinceMode: 'specific',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-27T00:00:00',
|
||||
untilMode: 'specific',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual('2021-01-20T00:00:00 : 2021-01-27T00:00:00');
|
||||
});
|
||||
|
||||
it('2) specific : relative', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: '2021-01-20T00:00:00',
|
||||
sinceMode: 'specific',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-20T00:00:00',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual(
|
||||
'2021-01-20T00:00:00 : DATEADD(DATETIME("2021-01-20T00:00:00"), 7, day)',
|
||||
);
|
||||
});
|
||||
|
||||
it('3) now : relative', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: 'now',
|
||||
sinceMode: 'now',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: 'now',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual('now : DATEADD(DATETIME("now"), 7, day)');
|
||||
});
|
||||
|
||||
it('4) today : relative', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: 'today',
|
||||
sinceMode: 'today',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: 'today',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual('today : DATEADD(DATETIME("today"), 7, day)');
|
||||
});
|
||||
|
||||
it('5) relative : specific', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: '2021-01-27T00:00:00',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-27T00:00:00',
|
||||
untilMode: 'specific',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual(
|
||||
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 2021-01-27T00:00:00',
|
||||
);
|
||||
});
|
||||
|
||||
it('6) relative : now', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: 'now',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: 'now',
|
||||
untilMode: 'now',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual('DATEADD(DATETIME("now"), -7, day) : now');
|
||||
});
|
||||
|
||||
it('7) relative : today', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: 'today',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: 'today',
|
||||
untilMode: 'today',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual('DATEADD(DATETIME("today"), -7, day) : today');
|
||||
});
|
||||
|
||||
it('8) relative : relative (now)', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: 'now',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: 'now',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
}),
|
||||
).toEqual(
|
||||
'DATEADD(DATETIME("now"), -7, day) : DATEADD(DATETIME("now"), 7, day)',
|
||||
);
|
||||
});
|
||||
|
||||
it('9) relative : relative (date/time)', () => {
|
||||
expect(
|
||||
customTimeRangeEncode({
|
||||
sinceDatetime: '2021-01-27T00:00:00',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-27T00:00:00',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'specific',
|
||||
anchorValue: '2021-01-27T00:00:00',
|
||||
}),
|
||||
).toEqual(
|
||||
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('customTimeRangeDecode', () => {
|
||||
it('1) specific : specific', () => {
|
||||
expect(
|
||||
customTimeRangeDecode('2021-01-20T00:00:00 : 2021-01-27T00:00:00'),
|
||||
).toEqual({
|
||||
customRange: {
|
||||
sinceDatetime: '2021-01-20T00:00:00',
|
||||
sinceMode: 'specific',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-27T00:00:00',
|
||||
untilMode: 'specific',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
},
|
||||
matchedFlag: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('2) specific : relative', () => {
|
||||
expect(
|
||||
customTimeRangeDecode(
|
||||
'2021-01-20T00:00:00 : DATEADD(DATETIME("2021-01-20T00:00:00"), 7, day)',
|
||||
),
|
||||
).toEqual({
|
||||
customRange: {
|
||||
sinceDatetime: '2021-01-20T00:00:00',
|
||||
sinceMode: 'specific',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-20T00:00:00',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
},
|
||||
matchedFlag: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('3) relative : specific', () => {
|
||||
expect(
|
||||
customTimeRangeDecode(
|
||||
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 2021-01-27T00:00:00',
|
||||
),
|
||||
).toEqual({
|
||||
customRange: {
|
||||
sinceDatetime: '2021-01-27T00:00:00',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-27T00:00:00',
|
||||
untilMode: 'specific',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
},
|
||||
matchedFlag: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('4) relative : relative (now)', () => {
|
||||
expect(
|
||||
customTimeRangeDecode(
|
||||
'DATEADD(DATETIME("now"), -7, day) : DATEADD(DATETIME("now"), 7, day)',
|
||||
),
|
||||
).toEqual({
|
||||
customRange: {
|
||||
sinceDatetime: 'now',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: 'now',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'now',
|
||||
anchorValue: 'now',
|
||||
},
|
||||
matchedFlag: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('5) relative : relative (date/time)', () => {
|
||||
expect(
|
||||
customTimeRangeDecode(
|
||||
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
|
||||
),
|
||||
).toEqual({
|
||||
customRange: {
|
||||
sinceDatetime: '2021-01-27T00:00:00',
|
||||
sinceMode: 'relative',
|
||||
sinceGrain: 'day',
|
||||
sinceGrainValue: -7,
|
||||
untilDatetime: '2021-01-27T00:00:00',
|
||||
untilMode: 'relative',
|
||||
untilGrain: 'day',
|
||||
untilGrainValue: 7,
|
||||
anchorMode: 'specific',
|
||||
anchorValue: '2021-01-27T00:00:00',
|
||||
},
|
||||
matchedFlag: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildTimeRangeString', () => {
|
||||
it('generates proper time range string', () => {
|
||||
expect(
|
||||
buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'),
|
||||
).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00');
|
||||
expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe(
|
||||
' : 2020-07-30T00:00:00',
|
||||
);
|
||||
expect(buildTimeRangeString('', '')).toBe(' : ');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatTimeRange', () => {
|
||||
it('generates a readable time range', () => {
|
||||
expect(formatTimeRange('Last 7 days')).toBe('Last 7 days');
|
||||
expect(formatTimeRange('No filter')).toBe('No filter');
|
||||
expect(formatTimeRange('Yesterday : Tomorrow')).toBe(
|
||||
'Yesterday < col < Tomorrow',
|
||||
);
|
||||
expect(
|
||||
formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00', [
|
||||
'inclusive',
|
||||
'exclusive',
|
||||
]),
|
||||
).toBe('2010-07-30 ≤ col < 2020-07-30');
|
||||
expect(
|
||||
formatTimeRange('2010-07-30T01:00:00 : ', ['exclusive', 'inclusive']),
|
||||
).toBe('2010-07-30T01:00:00 < col ≤ ∞');
|
||||
expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe(
|
||||
'-∞ < col < 2020-07-30',
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user