Skip to content

Commit fe3f81a

Browse files
Merge pull request #1701 from davidmundelius/sort-merged-signals
Fix sorting of merged signals in package panel
2 parents b719821 + d73a52d commit fe3f81a

File tree

5 files changed

+162
-123
lines changed

5 files changed

+162
-123
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ SPDX-License-Identifier: CC0-1.0
2525
- **[Ruiyun Xie](https://github.com/mayayunx)**
2626
- **[Sebastian Thomas](https://github.com/sebathomas)** (<sebastian.thomas@tngtech.com>)
2727
- **[Vasily Pozdnyakov](https://github.com/vasily-pozdnyakov)** (<vasily.pozdnyakov@tngtech.com>)
28+
- **[David Mundelius](https://github.com/davidmundelius)** (<david.mundelius@tngtech.com>)
2829

2930
[Contributors list on GitHub](https://github.com/opossum-tool/OpossumUI/contributors).

src/Frontend/Components/AggregatedAttributionsPanel/__tests__/accordion-panel-helper.test.ts

+106-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import {
1717
getContainedManualDisplayPackageInfosWithCount,
1818
getExternalDisplayPackageInfosWithCount,
19+
sortDisplayPackageInfosWithCountByCountAndPackageName,
1920
} from '../accordion-panel-helpers';
2021
import { PanelAttributionData } from '../../../util/get-contained-packages';
2122
import { PackagePanelTitle } from '../../../enums/enums';
@@ -195,6 +196,54 @@ describe('getExternalDisplayPackageInfosWithCount', () => {
195196
)
196197
).toEqual([expectedPackageCardIds, expectedDisplayPackageInfosWithCount]);
197198
});
199+
200+
it('sorts ordinary and merged attributions according to the count', () => {
201+
const testAttributionIdsWithCount: Array<AttributionIdWithCount> = [
202+
{ attributionId: 'uuidToMerge1', count: 3 },
203+
{ attributionId: 'uuidToMerge2', count: 2 },
204+
{ attributionId: 'uuidNotToMerge', count: 1 },
205+
];
206+
const testAttributions: Attributions = {
207+
uuidToMerge1: { packageName: 'Typescript' },
208+
uuidToMerge2: { packageName: 'Typescript' },
209+
uuidNotToMerge: { packageName: 'React' },
210+
};
211+
const testExternalAttributionsToHashes: AttributionsToHashes = {
212+
uuidToMerge1: 'a',
213+
uuidToMerge2: 'a',
214+
};
215+
216+
const expectedPackageCardIds = [
217+
`${testPackagePanelTitle}-1`,
218+
`${testPackagePanelTitle}-0`,
219+
];
220+
221+
const expectedDisplayPackageInfosWithCount: DisplayPackageInfosWithCount = {
222+
[expectedPackageCardIds[0]]: {
223+
count: 5,
224+
displayPackageInfo: {
225+
attributionIds: ['uuidToMerge1', 'uuidToMerge2'],
226+
packageName: 'Typescript',
227+
},
228+
},
229+
[expectedPackageCardIds[1]]: {
230+
count: 1,
231+
displayPackageInfo: {
232+
attributionIds: ['uuidNotToMerge'],
233+
packageName: 'React',
234+
},
235+
},
236+
};
237+
238+
expect(
239+
getExternalDisplayPackageInfosWithCount(
240+
testAttributionIdsWithCount,
241+
testAttributions,
242+
testExternalAttributionsToHashes,
243+
testPackagePanelTitle
244+
)
245+
).toEqual([expectedPackageCardIds, expectedDisplayPackageInfosWithCount]);
246+
});
198247
});
199248

200249
describe('getContainedManualDisplayPackageInfosWithCount', () => {
@@ -227,11 +276,13 @@ describe('getContainedManualDisplayPackageInfosWithCount', () => {
227276
resourcesToAttributions: testResourcesToAttributions,
228277
resourcesWithAttributedChildren: testResourcesWithAttributedChildren,
229278
};
279+
230280
const expectedPackageCardIds = [
231-
`${testPackagePanelTitle}-0`,
232281
`${testPackagePanelTitle}-1`,
233282
`${testPackagePanelTitle}-2`,
283+
`${testPackagePanelTitle}-0`,
234284
];
285+
235286
const expectedDisplayPackageInfosWithCount: DisplayPackageInfosWithCount = {
236287
[expectedPackageCardIds[0]]: {
237288
displayPackageInfo: {
@@ -265,3 +316,57 @@ describe('getContainedManualDisplayPackageInfosWithCount', () => {
265316
).toEqual([expectedPackageCardIds, expectedDisplayPackageInfosWithCount]);
266317
});
267318
});
319+
320+
describe('sortDisplayPackageInfosWithCountByCountAndPackageName', () => {
321+
it('sorts items correctly', () => {
322+
const initialPackageCardIds: Array<string> = [
323+
'pcid1',
324+
'pcid2',
325+
'pcid3',
326+
'pcid4',
327+
'pcid5',
328+
'pcid6',
329+
];
330+
const testDisplayPackageInfosWithCount: DisplayPackageInfosWithCount = {
331+
pcid1: {
332+
displayPackageInfo: { attributionIds: ['uuid1'] },
333+
count: 10,
334+
},
335+
pcid2: {
336+
displayPackageInfo: { attributionIds: ['uuid2'], packageName: 'c' },
337+
count: 11,
338+
},
339+
pcid3: {
340+
displayPackageInfo: { attributionIds: ['uuid3'], packageName: 'b' },
341+
count: 10,
342+
},
343+
pcid4: {
344+
displayPackageInfo: { attributionIds: ['uuid4'], packageName: 'e' },
345+
count: 1,
346+
},
347+
pcid5: {
348+
displayPackageInfo: { attributionIds: ['uuid5'], packageName: 'z' },
349+
count: 10,
350+
},
351+
pcid6: {
352+
displayPackageInfo: { attributionIds: ['uuid6'], packageName: 'd' },
353+
count: 1,
354+
},
355+
};
356+
const expectedPackageCardIds: Array<string> = [
357+
'pcid2',
358+
'pcid3',
359+
'pcid5',
360+
'pcid1',
361+
'pcid6',
362+
'pcid4',
363+
];
364+
365+
const result = initialPackageCardIds.sort(
366+
sortDisplayPackageInfosWithCountByCountAndPackageName(
367+
testDisplayPackageInfosWithCount
368+
)
369+
);
370+
expect(result).toEqual(expectedPackageCardIds);
371+
});
372+
});

src/Frontend/Components/AggregatedAttributionsPanel/accordion-panel-helpers.ts

+45
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import {
77
Attributions,
88
AttributionsToHashes,
9+
DisplayPackageInfo,
910
PackageInfo,
1011
} from '../../../shared/shared-types';
1112
import { PackagePanelTitle } from '../../enums/enums';
@@ -68,6 +69,12 @@ export function getContainedManualDisplayPackageInfosWithCount(args: {
6869
}
6970
);
7071

72+
packageCardIds.sort(
73+
sortDisplayPackageInfosWithCountByCountAndPackageName(
74+
displayPackageInfosWithCount
75+
)
76+
);
77+
7178
return [packageCardIds, displayPackageInfosWithCount];
7279
}
7380

@@ -112,6 +119,12 @@ export function getExternalDisplayPackageInfosWithCount(
112119
indexCounter
113120
);
114121

122+
packageCardIds.sort(
123+
sortDisplayPackageInfosWithCountByCountAndPackageName(
124+
displayPackageInfosWithCount
125+
)
126+
);
127+
115128
return [packageCardIds, displayPackageInfosWithCount];
116129
}
117130

@@ -132,3 +145,35 @@ function addMergedSignals(
132145
indexCounter++;
133146
});
134147
}
148+
149+
//exported for testing
150+
export function sortDisplayPackageInfosWithCountByCountAndPackageName(
151+
displayPackageInfosWithCount: DisplayPackageInfosWithCount
152+
) {
153+
return function (id1: string, id2: string): number {
154+
if (
155+
displayPackageInfosWithCount[id1].count !==
156+
displayPackageInfosWithCount[id2].count
157+
) {
158+
return (
159+
displayPackageInfosWithCount[id2].count -
160+
displayPackageInfosWithCount[id1].count
161+
);
162+
}
163+
164+
const p1: DisplayPackageInfo =
165+
displayPackageInfosWithCount[id1].displayPackageInfo;
166+
const p2: DisplayPackageInfo =
167+
displayPackageInfosWithCount[id2].displayPackageInfo;
168+
if (p1?.packageName && p2?.packageName) {
169+
return p1.packageName.toLowerCase() < p2.packageName.toLowerCase()
170+
? -1
171+
: 1;
172+
} else if (p1?.packageName) {
173+
return -1;
174+
} else if (p2?.packageName) {
175+
return 1;
176+
}
177+
return 0;
178+
};
179+
}

src/Frontend/util/__tests__/get-contained-packages.test.ts

+6-89
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import {
77
Attributions,
88
ResourcesToAttributions,
99
} from '../../../shared/shared-types';
10-
import {
11-
computeAggregatedAttributionsFromChildren,
12-
sortByCountAndPackageName,
13-
} from '../get-contained-packages';
10+
import { computeAggregatedAttributionsFromChildren } from '../get-contained-packages';
1411
import { AttributionIdWithCount } from '../../types/types';
1512

1613
describe('computeAggregatedAttributionsFromChildren', () => {
@@ -29,16 +26,16 @@ describe('computeAggregatedAttributionsFromChildren', () => {
2926
.add('samplepath/subfolder')
3027
.add('samplepath2/subfolder/subsubfolder');
3128

32-
it('selects aggregated children and sorts correctly', () => {
29+
it('selects aggregated children', () => {
3330
const expectedResult: Array<AttributionIdWithCount> = [
34-
{
35-
count: 2,
36-
attributionId: 'uuid_2',
37-
},
3831
{
3932
count: 1,
4033
attributionId: 'uuid_1',
4134
},
35+
{
36+
count: 2,
37+
attributionId: 'uuid_2',
38+
},
4239
{
4340
count: 1,
4441
attributionId: 'uuid_3',
@@ -79,83 +76,3 @@ describe('computeAggregatedAttributionsFromChildren', () => {
7976
expect(result).toEqual(expectedResult);
8077
});
8178
});
82-
83-
describe('sortByCountAndPackageName', () => {
84-
it('sorts items correctly', () => {
85-
const initialAttributionIdsWithCount: Array<AttributionIdWithCount> = [
86-
{
87-
attributionId: 'uuid1',
88-
count: 10,
89-
},
90-
{
91-
attributionId: 'uuid2',
92-
count: 11,
93-
},
94-
{
95-
attributionId: 'uuid3',
96-
count: 10,
97-
},
98-
{
99-
attributionId: 'uuid4',
100-
count: 1,
101-
},
102-
{
103-
attributionId: 'uuid5',
104-
count: 10,
105-
},
106-
{
107-
attributionId: 'uuid6',
108-
count: 1,
109-
},
110-
];
111-
const testAttributions: Attributions = {
112-
uuid1: {},
113-
uuid2: {
114-
packageName: 'c',
115-
},
116-
uuid3: {
117-
packageName: 'b',
118-
},
119-
uuid4: {
120-
packageName: 'e',
121-
},
122-
uuid5: {
123-
packageName: 'z',
124-
},
125-
uuid6: {
126-
packageName: 'd',
127-
},
128-
};
129-
const expectedAttributionIdsWithCount: Array<AttributionIdWithCount> = [
130-
{
131-
attributionId: 'uuid2',
132-
count: 11,
133-
},
134-
{
135-
attributionId: 'uuid3',
136-
count: 10,
137-
},
138-
{
139-
attributionId: 'uuid5',
140-
count: 10,
141-
},
142-
{
143-
attributionId: 'uuid1',
144-
count: 10,
145-
},
146-
{
147-
attributionId: 'uuid6',
148-
count: 1,
149-
},
150-
{
151-
attributionId: 'uuid4',
152-
count: 1,
153-
},
154-
];
155-
156-
const result = initialAttributionIdsWithCount.sort(
157-
sortByCountAndPackageName(testAttributions)
158-
);
159-
expect(result).toEqual(expectedAttributionIdsWithCount);
160-
});
161-
});

src/Frontend/util/get-contained-packages.ts

+4-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import {
66
AttributionData,
77
Attributions,
8-
PackageInfo,
98
ResourcesToAttributions,
109
ResourcesWithAttributedChildren,
1110
} from '../../shared/shared-types';
@@ -81,36 +80,8 @@ export function computeAggregatedAttributionsFromChildren(
8180
});
8281
});
8382

84-
return Object.keys(attributionCount)
85-
.map((attributionId: string) => ({
86-
attributionId,
87-
count: attributionCount[attributionId],
88-
}))
89-
.sort(sortByCountAndPackageName(attributions));
90-
}
91-
92-
export function sortByCountAndPackageName(
93-
attributions: Readonly<Attributions>
94-
) {
95-
return function (
96-
a1: AttributionIdWithCount,
97-
a2: AttributionIdWithCount
98-
): number {
99-
if (a1.count && a2.count && a1.count !== a2.count) {
100-
return a2.count - a1.count;
101-
}
102-
103-
const p1: PackageInfo = attributions[a1.attributionId];
104-
const p2: PackageInfo = attributions[a2.attributionId];
105-
if (p1?.packageName && p2?.packageName) {
106-
return p1.packageName.toLowerCase() < p2.packageName.toLowerCase()
107-
? -1
108-
: 1;
109-
} else if (p1?.packageName) {
110-
return -1;
111-
} else if (p2?.packageName) {
112-
return 1;
113-
}
114-
return 0;
115-
};
83+
return Object.keys(attributionCount).map((attributionId: string) => ({
84+
attributionId,
85+
count: attributionCount[attributionId],
86+
}));
11687
}

0 commit comments

Comments
 (0)