Skip to content

Commit d1f67cf

Browse files
authored
Merge pull request #1227 from xieliuduo/dev37-datefilter
refactor: board handle rangeDateFilter to 2 DateFilter
2 parents 5d4e0ba + df69073 commit d1f67cf

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

frontend/src/app/pages/DashBoardPage/utils/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { ChartDetailConfigDTO } from 'app/types/ChartConfigDTO';
3535
import { ChartDataRequestFilter } from 'app/types/ChartDataRequest';
3636
import ChartDataView from 'app/types/ChartDataView';
3737
import { convertToChartConfigDTO } from 'app/utils/ChartDtoHelper';
38-
import { getTime } from 'app/utils/time';
38+
import { getTime, splitRangerDateFilters } from 'app/utils/time';
3939
import { FilterSqlOperator, TIME_FORMATTER } from 'globalConstants';
4040
import i18next from 'i18next';
4141
import moment from 'moment';
@@ -428,6 +428,9 @@ export const getChartWidgetRequestParams = (obj: {
428428

429429
// filter 去重
430430
requestParams.filters = getDistinctFiltersByColumn(requestParams.filters);
431+
// splitRangerDateFilters
432+
requestParams.filters = splitRangerDateFilters(requestParams.filters);
433+
431434
// 变量
432435
if (variableParams) {
433436
requestParams.params = variableParams;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Datart
3+
*
4+
* Copyright 2021
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import { splitRangerDateFilters } from '../time';
20+
21+
describe('test splitRangerDateFilters', () => {
22+
const rangerDateFilter = {
23+
aggOperator: null,
24+
column: '日期',
25+
sqlOperator: 'BETWEEN',
26+
values: [
27+
{
28+
value: '2004-01-01 00:00:00',
29+
valueType: 'DATE',
30+
},
31+
{
32+
value: '2013-01-01 00:00:00',
33+
valueType: 'DATE',
34+
},
35+
],
36+
};
37+
const badRangerDateFilter = {
38+
aggOperator: null,
39+
column: '日期',
40+
sqlOperator: 'BETWEEN',
41+
values: [
42+
{
43+
value: '2004-01-01 00:00:00',
44+
valueType: 'DATE',
45+
},
46+
],
47+
};
48+
const otherFilter1 = {
49+
aggOperator: null,
50+
column: '日期',
51+
sqlOperator: 'LT',
52+
values: [
53+
{
54+
value: '2011-01-01 00:00:00',
55+
valueType: 'DATE',
56+
},
57+
],
58+
};
59+
60+
const splittedDateFilters = [
61+
{
62+
aggOperator: null,
63+
column: '日期',
64+
sqlOperator: 'GTE',
65+
values: [
66+
{
67+
value: '2004-01-01 00:00:00',
68+
valueType: 'DATE',
69+
},
70+
],
71+
},
72+
{
73+
aggOperator: null,
74+
column: '日期',
75+
sqlOperator: 'LT',
76+
values: [
77+
{
78+
value: '2013-01-01 00:00:00',
79+
valueType: 'DATE',
80+
},
81+
],
82+
},
83+
];
84+
85+
test('should splitRangerDateFilters to 2 filter', () => {
86+
const res1 = splitRangerDateFilters([rangerDateFilter, otherFilter1]);
87+
expect(res1).toEqual([...splittedDateFilters, otherFilter1]);
88+
const res2 = splitRangerDateFilters([rangerDateFilter]);
89+
expect(res2).toEqual([...splittedDateFilters]);
90+
const res3 = splitRangerDateFilters([otherFilter1]);
91+
expect(res3).toEqual([otherFilter1]);
92+
});
93+
test('should splitRangerDateFilters del bad rangeDateFilter', () => {
94+
const res4 = splitRangerDateFilters([badRangerDateFilter]);
95+
expect(res4).toEqual([]);
96+
const res5 = splitRangerDateFilters([badRangerDateFilter, otherFilter1]);
97+
expect(res5).toEqual([otherFilter1]);
98+
});
99+
test('should splitRangerDateFilters not arr to []', () => {
100+
const res1 = splitRangerDateFilters(null as any);
101+
expect(res1).toEqual([]);
102+
const res2 = splitRangerDateFilters(undefined as any);
103+
expect(res2).toEqual([]);
104+
const res4 = splitRangerDateFilters({} as any);
105+
expect(res4).toEqual([]);
106+
const res5 = splitRangerDateFilters('string' as any);
107+
expect(res5).toEqual([]);
108+
});
109+
});

frontend/src/app/utils/time.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
* limitations under the License.
1717
*/
1818

19-
import { RECOMMEND_TIME, TIME_FORMATTER } from 'globalConstants';
19+
import { DataViewFieldType } from 'app/constants';
20+
import { ChartDataRequestFilter } from 'app/types/ChartDataRequest';
21+
import {
22+
FilterSqlOperator,
23+
RECOMMEND_TIME,
24+
TIME_FORMATTER,
25+
} from 'globalConstants';
2026
import moment, { Moment, unitOfTime } from 'moment';
2127

2228
export function getTimeRange(
@@ -75,3 +81,36 @@ export function recommendTimeRangeConverter(relativeTimeRange) {
7581
}
7682
return timeRange;
7783
}
84+
85+
export const splitRangerDateFilters = (filters: ChartDataRequestFilter[]) => {
86+
if (!Array.isArray(filters)) return [];
87+
const newFilter = [] as ChartDataRequestFilter[];
88+
filters.forEach(filter => {
89+
let isTargetFilter = false;
90+
if (
91+
filter.sqlOperator === FilterSqlOperator.Between &&
92+
filter.values?.[0].valueType === DataViewFieldType.DATE
93+
) {
94+
isTargetFilter = true;
95+
}
96+
if (!isTargetFilter) {
97+
newFilter.push(filter);
98+
return;
99+
}
100+
// split date range filters
101+
if (filter.values?.[0] && filter.values?.[1]) {
102+
const start: ChartDataRequestFilter = {
103+
...filter,
104+
sqlOperator: FilterSqlOperator.GreaterThanOrEqual,
105+
values: [filter.values?.[0]],
106+
};
107+
const end: ChartDataRequestFilter = {
108+
...filter,
109+
sqlOperator: FilterSqlOperator.LessThan,
110+
values: [filter.values?.[1]],
111+
};
112+
newFilter.push(start, end);
113+
}
114+
});
115+
return newFilter;
116+
};

0 commit comments

Comments
 (0)