Skip to content

Commit 51de08c

Browse files
ui: Fix the bug preventing multiple sum_by entries (#5741)
* Fix the bug preventing multiple sumby entries * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 969029d commit 51de08c

File tree

4 files changed

+122
-12
lines changed

4 files changed

+122
-12
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2022 The Parca Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
import {describe, expect, it} from 'vitest';
15+
16+
import {filterEmptyParams} from './index';
17+
18+
describe('filterEmptyParams', () => {
19+
it('should return an array with 2 elements when given object with 2 valid and multiple invalid values', () => {
20+
const input = {
21+
validString: 'hello',
22+
validArray: ['item1', 'item2'],
23+
emptyString: '',
24+
undefinedValue: undefined,
25+
emptyArray: [],
26+
anotherEmptyString: '',
27+
};
28+
29+
const result = filterEmptyParams(input);
30+
const resultEntries = Object.entries(result);
31+
32+
expect(resultEntries).toHaveLength(2);
33+
expect(result).toEqual({
34+
validString: 'hello',
35+
validArray: ['item1', 'item2'],
36+
});
37+
});
38+
39+
it('should filter out empty strings', () => {
40+
const input = {
41+
valid: 'test',
42+
empty: '',
43+
};
44+
45+
const result = filterEmptyParams(input);
46+
expect(result).toEqual({valid: 'test'});
47+
});
48+
49+
it('should filter out undefined values', () => {
50+
const input = {
51+
valid: 'test',
52+
notDefined: undefined,
53+
};
54+
55+
const result = filterEmptyParams(input);
56+
expect(result).toEqual({valid: 'test'});
57+
});
58+
59+
it('should filter out empty arrays', () => {
60+
const input = {
61+
valid: 'test',
62+
emptyArray: [],
63+
nonEmptyArray: ['item'],
64+
};
65+
66+
const result = filterEmptyParams(input);
67+
expect(result).toEqual({
68+
valid: 'test',
69+
nonEmptyArray: ['item'],
70+
});
71+
});
72+
73+
it('should keep all valid values including numbers, booleans, and objects', () => {
74+
const input = {
75+
string: 'test',
76+
number: 0,
77+
boolean: false,
78+
object: {key: 'value'},
79+
array: ['item'],
80+
};
81+
82+
const result = filterEmptyParams(input);
83+
expect(result).toEqual(input);
84+
});
85+
86+
it('should return empty object when all values are invalid', () => {
87+
const input = {
88+
empty1: '',
89+
empty2: '',
90+
undefined1: undefined,
91+
emptyArray: [],
92+
};
93+
94+
const result = filterEmptyParams(input);
95+
expect(result).toEqual({});
96+
});
97+
});

ui/packages/shared/profile/src/ProfileExplorer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const sanitizeDateRange = (
7171
};
7272
/* eslint-enable @typescript-eslint/naming-convention */
7373

74-
const filterEmptyParams = (o: Record<string, any>): Record<string, any> => {
74+
export const filterEmptyParams = (o: Record<string, any>): Record<string, any> => {
7575
return Object.fromEntries(
7676
Object.entries(o)
7777
.filter(

ui/packages/shared/profile/src/ProfileView/hooks/useResetStateOnNewSearch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
import {useURLState} from '@parca/components';
1515

1616
export const useResetStateOnNewSearch = (): (() => void) => {
17-
const [, setCurPath] = useURLState('cur_path');
17+
const [val, setCurPath] = useURLState('cur_path');
1818

1919
return () => {
2020
setTimeout(() => {
21+
if (val === undefined) {
22+
return;
23+
}
2124
setCurPath(undefined);
2225
});
2326
};

ui/packages/shared/profile/src/ProfileView/hooks/useResetStateOnProfileTypeChange.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,29 @@
1414
import {useURLState} from '@parca/components';
1515

1616
export const useResetStateOnProfileTypeChange = (): (() => void) => {
17-
const [, setGroupBy] = useURLState('group_by');
18-
const [, setFilterByFunction] = useURLState('filter_by_function');
19-
const [, setExcludeFunction] = useURLState('exclude_function');
20-
const [, setSearchString] = useURLState('search_string');
21-
const [, setCurPath] = useURLState('cur_path');
17+
const [groupBy, setGroupBy] = useURLState('group_by');
18+
const [filterByFunction, setFilterByFunction] = useURLState('filter_by_function');
19+
const [excludeFunction, setExcludeFunction] = useURLState('exclude_function');
20+
const [searchString, setSearchString] = useURLState('search_string');
21+
const [curPath, setCurPath] = useURLState('cur_path');
2222

2323
return () => {
2424
setTimeout(() => {
25-
setGroupBy(undefined);
26-
setFilterByFunction(undefined);
27-
setExcludeFunction(undefined);
28-
setSearchString(undefined);
29-
setCurPath(undefined);
25+
if (groupBy !== undefined) {
26+
setGroupBy(undefined);
27+
}
28+
if (filterByFunction !== undefined) {
29+
setFilterByFunction(undefined);
30+
}
31+
if (excludeFunction !== undefined) {
32+
setExcludeFunction(undefined);
33+
}
34+
if (searchString !== undefined) {
35+
setSearchString(undefined);
36+
}
37+
if (curPath !== undefined) {
38+
setCurPath(undefined);
39+
}
3040
});
3141
};
3242
};

0 commit comments

Comments
 (0)