Skip to content

Commit

Permalink
[issues-2538] Improve how release are sorted (#2550)
Browse files Browse the repository at this point in the history
* [issues-2538] Improve how release are sorted

* Downgrade vitest-canvas-mock & jest-canvas-mock

* Downgrade vitest-canvas-mock & jest-canvas-mock (2/2)

* Improve coverage
  • Loading branch information
xavierfacq authored Jan 4, 2024
1 parent 739b6b9 commit 25fa2e0
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/TemurinDownloadTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const TemurinDownloadTable = ({results}) => {
<table id="download-table" className="table table-bordered releases-table" style={{borderSpacing: '0 10px', borderCollapse: 'separate'}}>
<tbody className="table-light">
{results ? (
results.sort((pkg1, pkg2) => pkg2.release_date - pkg1.release_date).map(
results.map(
(pkg, i): string | JSX.Element =>
pkg && (
<tr key={i}>
Expand Down
126 changes: 126 additions & 0 deletions src/hooks/__tests__/__snapshots__/fetchTemurinReleases.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,129 @@ exports[`loadLatestAssets > source image is processed correctly 1`] = `
},
]
`;

exports[`loadLatestAssets > verify that releases are well sorted 1`] = `
[
{
"architecture": "aarch64",
"binaries": [
{
"checksum": "checksum_mock",
"extension": ".tar.gz",
"installer_checksum": "installer_checksum_mock",
"installer_extension": ".msi",
"installer_link": "https://installer_link_mock/",
"installer_size": 0,
"link": "https://link_mock/",
"size": 0,
"type": "JDK",
},
],
"os": "os_mock",
"platform_name": "os_mock-aarch64",
"release_date": 2020-01-01T00:00:00.000Z,
"release_link": "https://release_link_mock/",
"release_name": "release_name_mock",
},
{
"architecture": "ppc64le",
"binaries": [
{
"checksum": "checksum_mock",
"extension": ".tar.gz",
"installer_checksum": "installer_checksum_mock",
"installer_extension": ".msi",
"installer_link": "https://installer_link_mock/",
"installer_size": 0,
"link": "https://link_mock/",
"size": 0,
"type": "JDK",
},
],
"os": "os_mock",
"platform_name": "os_mock-ppc64le",
"release_date": 2020-01-01T00:00:00.000Z,
"release_link": "https://release_link_mock/",
"release_name": "release_name_mock",
},
{
"architecture": "x64",
"binaries": [
{
"checksum": "checksum_mock",
"extension": ".tar.gz",
"installer_checksum": "installer_checksum_mock",
"installer_extension": ".msi",
"installer_link": "https://installer_link_mock/",
"installer_size": 0,
"link": "https://link_mock/",
"size": 0,
"type": "JDK",
},
],
"os": "os_mock",
"platform_name": "os_mock-x64",
"release_date": 2020-01-01T00:00:00.000Z,
"release_link": "https://release_link_mock/",
"release_name": "release_name_mock",
},
{
"architecture": "x32",
"binaries": [
{
"checksum": "checksum_mock",
"extension": ".tar.gz",
"installer_checksum": "installer_checksum_mock",
"installer_extension": ".msi",
"installer_link": "https://installer_link_mock/",
"installer_size": 0,
"link": "https://link_mock/",
"size": 0,
"type": "JDK",
},
],
"os": "os_mock",
"platform_name": "os_mock-x32",
"release_date": 2020-01-01T00:00:00.000Z,
"release_link": "https://release_link_mock/",
"release_name": "release_name_mock",
},
]
`;

exports[`loadLatestAssets > verify update the release date if this asset is newer 1`] = `
[
{
"architecture": "arch_mock",
"binaries": [
{
"checksum": "checksum_mock",
"extension": ".tar.gz",
"installer_checksum": "installer_checksum_mock",
"installer_extension": ".msi",
"installer_link": "https://installer_link_mock/",
"installer_size": 0,
"link": "https://link_mock/",
"size": 0,
"type": "JDK",
},
{
"checksum": "checksum_mock",
"extension": ".tar.gz",
"installer_checksum": "installer_checksum_mock",
"installer_extension": ".msi",
"installer_link": "https://installer_link_mock/",
"installer_size": 0,
"link": "https://link_mock/",
"size": 0,
"type": "JDK",
},
],
"os": "os_mock",
"platform_name": "os_mock-arch_mock",
"release_date": 2020-01-02T00:00:00.000Z,
"release_link": "https://release_link_mock/",
"release_name": "release_name_mock",
},
]
`;
37 changes: 37 additions & 0 deletions src/hooks/__tests__/fetchTemurinReleases.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,43 @@ describe('loadLatestAssets', () => {
});
});

it('verify update the release date if this asset is newer', async() => {
const r1 = createMockTemurinReleaseAPI(true, 'jdk');
const r2 = createMockTemurinReleaseAPI(true, 'jdk');
r2.binary.updated_at.setDate(r2.binary.updated_at.getDate() + 1);

mockResponse = [r1, r2]

mock.onGet().reply(200, mockResponse);

renderHook(async() => {
await loadLatestAssets(8, 'linux', 'x64', 'jdk').then((data) => {
expect(data).toMatchSnapshot()
})
});
});

it('verify that releases are well sorted', async() => {
const r1 = createMockTemurinReleaseAPI(true, 'jdk');
r1.binary.architecture = 'x32';
const r2 = createMockTemurinReleaseAPI(true, 'jdk');
r2.binary.architecture = 'x64';
const r3 = createMockTemurinReleaseAPI(true, 'jdk');
r3.binary.architecture = 'aarch64';
const r4 = createMockTemurinReleaseAPI(true, 'jdk');
r4.binary.architecture = 'ppc64le';

mockResponse = [r1, r2, r3, r4]

mock.onGet().reply(200, mockResponse);

renderHook(async() => {
await loadLatestAssets(8, 'linux', 'x64', 'jdk').then((data) => {
expect(data).toMatchSnapshot()
})
});
});

it('pkgsFound to be empty on error', async() => {
mock.onGet().reply(500);

Expand Down
70 changes: 50 additions & 20 deletions src/hooks/fetchTemurinReleases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,57 @@ import axios from 'axios';

const baseUrl = 'https://api.adoptium.net/v3';

Date.prototype.withoutTime = function () {
var d = new Date(this);
d.setHours(0, 0, 0, 0);
return d;
};

export async function loadLatestAssets(
version: number,
os: string,
architecture: string,
packageType: string
): Promise<ReleaseAsset[]> {
let url = new URL(`${baseUrl}/assets/latest/${version}/hotspot?`);

if (os !== 'any') {
url.searchParams.append('os', os);
}
if (architecture !== 'any') {
url.searchParams.append('architecture', architecture);
}

let pkgsFound: TemurinRelease[] = []
// NOTE: Do not filter the query by 'image_type' because we need to have 'sources
// to display the Release Notes and source download (cf src/components/TemurinDownloadTable/index.tsx)

await axios.get(url.toString())
let pkgsFound: TemurinRelease[] = await axios.get(url.toString())
.then(function (response) {
let data = response.data;

// Filter JDK/JRE if necessary
if (packageType === 'jdk') {
data = data.filter((pkg: TemurinRelease) => pkg.binary.image_type !== 'jre');
} else if (packageType === 'jre') {
data = data.filter((pkg: TemurinRelease) => pkg.binary.image_type !== 'jdk');
}

for (let pkg of data) {
pkgsFound.push(pkg);
}
return response.data;
})
.catch(function (error) {
pkgsFound = []
return []
});

// Filter JDK/JRE if necessary
if (packageType === 'jdk') {
pkgsFound = pkgsFound.filter((pkg: TemurinRelease) => pkg.binary.image_type !== 'jre');
} else if (packageType === 'jre') {
pkgsFound = pkgsFound.filter((pkg: TemurinRelease) => pkg.binary.image_type !== 'jdk');
}

return renderReleases(pkgsFound);
}

function renderReleases(pkgs: Array<TemurinRelease>): ReleaseAsset[] {
let releases: ReleaseAsset[] = []

pkgs.forEach((releaseAsset: TemurinRelease) => {
const platform = `${releaseAsset.binary.os}-${releaseAsset.binary.architecture}`

// Skip this asset if it's not a binary type we're interested in displaying
const binary_type = releaseAsset.binary.image_type.toUpperCase();
if (binary_type == 'SOURCES') {
if (binary_type === 'SOURCES') {
releases['source'] = releaseAsset;
}
if (!['INSTALLER', 'JDK', 'JRE'].includes(binary_type)) {
Expand All @@ -67,6 +72,12 @@ function renderReleases(pkgs: Array<TemurinRelease>): ReleaseAsset[] {
release_date: new Date(releaseAsset.binary.updated_at),
binaries: []
};
} else {
// update the release date if this asset is newer
const rabua = new Date(releaseAsset.binary.updated_at);
if (release.release_date < rabua) {
release.release_date = rabua;
}
}

let binary_constructor: Binary = {
Expand All @@ -91,11 +102,30 @@ function renderReleases(pkgs: Array<TemurinRelease>): ReleaseAsset[] {
if (release.binaries.length === 1) {
releases.push(release);
}

releases.forEach((release) => {
release.binaries.sort((binaryA, binaryB) => binaryA.type > binaryB.type ? 1 : binaryA.type < binaryB.type ? -1 : 0);
});
})

// well sort releases
releases.sort((pkg1: ReleaseAsset, pkg2: ReleaseAsset) => {
// order by date DESC
let comparison = pkg2.release_date.withoutTime() - pkg1.release_date.withoutTime();
if (comparison === 0) {
// for the same date, sort by OS ASC
comparison = pkg1.os.localeCompare(pkg2.os);
if (comparison === 0) {
// for the same OS, sort by architecture ASC
const arch1 = pkg1.architecture === 'x32' ? 'x86' : pkg1.architecture
const arch2 = pkg2.architecture === 'x32' ? 'x86' : pkg2.architecture
comparison = arch1.localeCompare(arch2);
}
}
return comparison;
});

// sort binaries inside releases
releases.forEach((release) => {
release.binaries.sort((binaryA, binaryB) => binaryA.type > binaryB.type ? 1 : binaryA.type < binaryB.type ? -1 : 0);
});

return releases
}

Expand Down

0 comments on commit 25fa2e0

Please sign in to comment.