Skip to content

Commit

Permalink
Merge pull request #1701 from davidmundelius/sort-merged-signals
Browse files Browse the repository at this point in the history
Fix sorting of merged signals in package panel
  • Loading branch information
benedikt-richter authored May 26, 2023
2 parents b719821 + d73a52d commit fe3f81a
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 123 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ SPDX-License-Identifier: CC0-1.0
- **[Ruiyun Xie](https://github.com/mayayunx)**
- **[Sebastian Thomas](https://github.com/sebathomas)** (<[email protected]>)
- **[Vasily Pozdnyakov](https://github.com/vasily-pozdnyakov)** (<[email protected]>)
- **[David Mundelius](https://github.com/davidmundelius)** (<[email protected]>)

[Contributors list on GitHub](https://github.com/opossum-tool/OpossumUI/contributors).
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
getContainedManualDisplayPackageInfosWithCount,
getExternalDisplayPackageInfosWithCount,
sortDisplayPackageInfosWithCountByCountAndPackageName,
} from '../accordion-panel-helpers';
import { PanelAttributionData } from '../../../util/get-contained-packages';
import { PackagePanelTitle } from '../../../enums/enums';
Expand Down Expand Up @@ -195,6 +196,54 @@ describe('getExternalDisplayPackageInfosWithCount', () => {
)
).toEqual([expectedPackageCardIds, expectedDisplayPackageInfosWithCount]);
});

it('sorts ordinary and merged attributions according to the count', () => {
const testAttributionIdsWithCount: Array<AttributionIdWithCount> = [
{ attributionId: 'uuidToMerge1', count: 3 },
{ attributionId: 'uuidToMerge2', count: 2 },
{ attributionId: 'uuidNotToMerge', count: 1 },
];
const testAttributions: Attributions = {
uuidToMerge1: { packageName: 'Typescript' },
uuidToMerge2: { packageName: 'Typescript' },
uuidNotToMerge: { packageName: 'React' },
};
const testExternalAttributionsToHashes: AttributionsToHashes = {
uuidToMerge1: 'a',
uuidToMerge2: 'a',
};

const expectedPackageCardIds = [
`${testPackagePanelTitle}-1`,
`${testPackagePanelTitle}-0`,
];

const expectedDisplayPackageInfosWithCount: DisplayPackageInfosWithCount = {
[expectedPackageCardIds[0]]: {
count: 5,
displayPackageInfo: {
attributionIds: ['uuidToMerge1', 'uuidToMerge2'],
packageName: 'Typescript',
},
},
[expectedPackageCardIds[1]]: {
count: 1,
displayPackageInfo: {
attributionIds: ['uuidNotToMerge'],
packageName: 'React',
},
},
};

expect(
getExternalDisplayPackageInfosWithCount(
testAttributionIdsWithCount,
testAttributions,
testExternalAttributionsToHashes,
testPackagePanelTitle
)
).toEqual([expectedPackageCardIds, expectedDisplayPackageInfosWithCount]);
});
});

describe('getContainedManualDisplayPackageInfosWithCount', () => {
Expand Down Expand Up @@ -227,11 +276,13 @@ describe('getContainedManualDisplayPackageInfosWithCount', () => {
resourcesToAttributions: testResourcesToAttributions,
resourcesWithAttributedChildren: testResourcesWithAttributedChildren,
};

const expectedPackageCardIds = [
`${testPackagePanelTitle}-0`,
`${testPackagePanelTitle}-1`,
`${testPackagePanelTitle}-2`,
`${testPackagePanelTitle}-0`,
];

const expectedDisplayPackageInfosWithCount: DisplayPackageInfosWithCount = {
[expectedPackageCardIds[0]]: {
displayPackageInfo: {
Expand Down Expand Up @@ -265,3 +316,57 @@ describe('getContainedManualDisplayPackageInfosWithCount', () => {
).toEqual([expectedPackageCardIds, expectedDisplayPackageInfosWithCount]);
});
});

describe('sortDisplayPackageInfosWithCountByCountAndPackageName', () => {
it('sorts items correctly', () => {
const initialPackageCardIds: Array<string> = [
'pcid1',
'pcid2',
'pcid3',
'pcid4',
'pcid5',
'pcid6',
];
const testDisplayPackageInfosWithCount: DisplayPackageInfosWithCount = {
pcid1: {
displayPackageInfo: { attributionIds: ['uuid1'] },
count: 10,
},
pcid2: {
displayPackageInfo: { attributionIds: ['uuid2'], packageName: 'c' },
count: 11,
},
pcid3: {
displayPackageInfo: { attributionIds: ['uuid3'], packageName: 'b' },
count: 10,
},
pcid4: {
displayPackageInfo: { attributionIds: ['uuid4'], packageName: 'e' },
count: 1,
},
pcid5: {
displayPackageInfo: { attributionIds: ['uuid5'], packageName: 'z' },
count: 10,
},
pcid6: {
displayPackageInfo: { attributionIds: ['uuid6'], packageName: 'd' },
count: 1,
},
};
const expectedPackageCardIds: Array<string> = [
'pcid2',
'pcid3',
'pcid5',
'pcid1',
'pcid6',
'pcid4',
];

const result = initialPackageCardIds.sort(
sortDisplayPackageInfosWithCountByCountAndPackageName(
testDisplayPackageInfosWithCount
)
);
expect(result).toEqual(expectedPackageCardIds);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import {
Attributions,
AttributionsToHashes,
DisplayPackageInfo,
PackageInfo,
} from '../../../shared/shared-types';
import { PackagePanelTitle } from '../../enums/enums';
Expand Down Expand Up @@ -68,6 +69,12 @@ export function getContainedManualDisplayPackageInfosWithCount(args: {
}
);

packageCardIds.sort(
sortDisplayPackageInfosWithCountByCountAndPackageName(
displayPackageInfosWithCount
)
);

return [packageCardIds, displayPackageInfosWithCount];
}

Expand Down Expand Up @@ -112,6 +119,12 @@ export function getExternalDisplayPackageInfosWithCount(
indexCounter
);

packageCardIds.sort(
sortDisplayPackageInfosWithCountByCountAndPackageName(
displayPackageInfosWithCount
)
);

return [packageCardIds, displayPackageInfosWithCount];
}

Expand All @@ -132,3 +145,35 @@ function addMergedSignals(
indexCounter++;
});
}

//exported for testing
export function sortDisplayPackageInfosWithCountByCountAndPackageName(
displayPackageInfosWithCount: DisplayPackageInfosWithCount
) {
return function (id1: string, id2: string): number {
if (
displayPackageInfosWithCount[id1].count !==
displayPackageInfosWithCount[id2].count
) {
return (
displayPackageInfosWithCount[id2].count -
displayPackageInfosWithCount[id1].count
);
}

const p1: DisplayPackageInfo =
displayPackageInfosWithCount[id1].displayPackageInfo;
const p2: DisplayPackageInfo =
displayPackageInfosWithCount[id2].displayPackageInfo;
if (p1?.packageName && p2?.packageName) {
return p1.packageName.toLowerCase() < p2.packageName.toLowerCase()
? -1
: 1;
} else if (p1?.packageName) {
return -1;
} else if (p2?.packageName) {
return 1;
}
return 0;
};
}
95 changes: 6 additions & 89 deletions src/Frontend/util/__tests__/get-contained-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
Attributions,
ResourcesToAttributions,
} from '../../../shared/shared-types';
import {
computeAggregatedAttributionsFromChildren,
sortByCountAndPackageName,
} from '../get-contained-packages';
import { computeAggregatedAttributionsFromChildren } from '../get-contained-packages';
import { AttributionIdWithCount } from '../../types/types';

describe('computeAggregatedAttributionsFromChildren', () => {
Expand All @@ -29,16 +26,16 @@ describe('computeAggregatedAttributionsFromChildren', () => {
.add('samplepath/subfolder')
.add('samplepath2/subfolder/subsubfolder');

it('selects aggregated children and sorts correctly', () => {
it('selects aggregated children', () => {
const expectedResult: Array<AttributionIdWithCount> = [
{
count: 2,
attributionId: 'uuid_2',
},
{
count: 1,
attributionId: 'uuid_1',
},
{
count: 2,
attributionId: 'uuid_2',
},
{
count: 1,
attributionId: 'uuid_3',
Expand Down Expand Up @@ -79,83 +76,3 @@ describe('computeAggregatedAttributionsFromChildren', () => {
expect(result).toEqual(expectedResult);
});
});

describe('sortByCountAndPackageName', () => {
it('sorts items correctly', () => {
const initialAttributionIdsWithCount: Array<AttributionIdWithCount> = [
{
attributionId: 'uuid1',
count: 10,
},
{
attributionId: 'uuid2',
count: 11,
},
{
attributionId: 'uuid3',
count: 10,
},
{
attributionId: 'uuid4',
count: 1,
},
{
attributionId: 'uuid5',
count: 10,
},
{
attributionId: 'uuid6',
count: 1,
},
];
const testAttributions: Attributions = {
uuid1: {},
uuid2: {
packageName: 'c',
},
uuid3: {
packageName: 'b',
},
uuid4: {
packageName: 'e',
},
uuid5: {
packageName: 'z',
},
uuid6: {
packageName: 'd',
},
};
const expectedAttributionIdsWithCount: Array<AttributionIdWithCount> = [
{
attributionId: 'uuid2',
count: 11,
},
{
attributionId: 'uuid3',
count: 10,
},
{
attributionId: 'uuid5',
count: 10,
},
{
attributionId: 'uuid1',
count: 10,
},
{
attributionId: 'uuid6',
count: 1,
},
{
attributionId: 'uuid4',
count: 1,
},
];

const result = initialAttributionIdsWithCount.sort(
sortByCountAndPackageName(testAttributions)
);
expect(result).toEqual(expectedAttributionIdsWithCount);
});
});
37 changes: 4 additions & 33 deletions src/Frontend/util/get-contained-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import {
AttributionData,
Attributions,
PackageInfo,
ResourcesToAttributions,
ResourcesWithAttributedChildren,
} from '../../shared/shared-types';
Expand Down Expand Up @@ -81,36 +80,8 @@ export function computeAggregatedAttributionsFromChildren(
});
});

return Object.keys(attributionCount)
.map((attributionId: string) => ({
attributionId,
count: attributionCount[attributionId],
}))
.sort(sortByCountAndPackageName(attributions));
}

export function sortByCountAndPackageName(
attributions: Readonly<Attributions>
) {
return function (
a1: AttributionIdWithCount,
a2: AttributionIdWithCount
): number {
if (a1.count && a2.count && a1.count !== a2.count) {
return a2.count - a1.count;
}

const p1: PackageInfo = attributions[a1.attributionId];
const p2: PackageInfo = attributions[a2.attributionId];
if (p1?.packageName && p2?.packageName) {
return p1.packageName.toLowerCase() < p2.packageName.toLowerCase()
? -1
: 1;
} else if (p1?.packageName) {
return -1;
} else if (p2?.packageName) {
return 1;
}
return 0;
};
return Object.keys(attributionCount).map((attributionId: string) => ({
attributionId,
count: attributionCount[attributionId],
}));
}

0 comments on commit fe3f81a

Please sign in to comment.