Skip to content

Commit

Permalink
Trim down the list of recommended streams (#235)
Browse files Browse the repository at this point in the history
* Trim down the list of recommended streams

Besides including pinned streams, this will trim down the list of recommended streams up to the latest stable version.

* Improved snippet

The selected code is checking if there are any stable versions in the platformReleases list. If there are none, it adds the current item to the list. This code can be improved by using the anyMatch method instead of filter and count. The anyMatch method will return as soon as it finds a match, which can be more efficient than counting all matches with filter and count.
  • Loading branch information
gastaldi committed Jan 22, 2024
1 parent d8566b3 commit 060fb8f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/main/java/io/quarkus/registry/app/model/PlatformRelease.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ void updateSemVer() {
this.versionSortable = Version.toSortable(version);
}

public boolean isVersionStable() {
return !Version.isPreFinal(version);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -198,19 +202,15 @@ public static List<PlatformRelease> findLatest(String quarkusCore) {
query = em.createNamedQuery("PlatformRelease.findLatest", PlatformRelease.class);
}
// Check if we can perform this directly in the SQL
// We need the top 3 releases for each platform
var platformMap = new LinkedHashMap<Platform, List<PlatformRelease>>();
for (var item : query.getResultList()) {
var platformReleases = platformMap.computeIfAbsent(item.platformStream.platform,
k -> new ArrayList<>());
if (platformReleases.size() < 3) {
// Count stable versions
long stableVersions = platformReleases.stream()
.filter(pr -> !Version.isPreFinal(pr.version))
.count();
if (stableVersions < 2) {
platformReleases.add(item);
}
// Add all versions until we find the latest stable one
boolean hasStableVersions = platformReleases.stream()
.anyMatch(PlatformRelease::isVersionStable);
if (!hasStableVersions) {
platformReleases.add(item);
}
}
// Add pinned platforms to the result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void setUp() {
PlatformStream stream22 = new PlatformStream();
stream22.platform = platform;
stream22.streamKey = "2.2";
stream22.pinned = true;
stream22.persistAndFlush();

PlatformRelease release220Final = new PlatformRelease();
Expand Down Expand Up @@ -108,7 +109,7 @@ void setUp() {
}

@Test
void should_return_pinned_platform_streams() {
void should_return_pinned_and_latest_platform_streams() {
given()
.get("/client/platforms")
.then()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void should_honor_unlisted_platform_stream_flag() {
.then()
.statusCode(HttpURLConnection.HTTP_OK)
.contentType(ContentType.JSON)
.body("platforms[0].streams", hasSize(2),
.body("platforms[0].streams", hasSize(1),
"platforms[0].current-stream-id", is("10.0"));

// Setting unlisted=true to 10.0 stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ void should_return_platforms() {
.statusCode(200)
.log().ifValidationFails()
.contentType(ContentType.JSON)
.body("platforms.streams.flatten().size()", is(3),
.body("platforms.streams.flatten().size()", is(2),
"platforms[0].streams[0].id", is("2.0"),
"platforms[0].streams[0].releases[0].quarkus-core-version", is("2.0.2.Final"),
"platforms[0].streams[1].id", is("1.9"),
"platforms[0].streams[1].releases[0].quarkus-core-version", is("1.9.0.Final"),
"platforms[0].streams[2].id", is("2.1"),
"platforms[0].streams[2].releases[0].quarkus-core-version", is("2.1.0.CR1"));
"platforms[0].streams[1].id", is("2.1"),
"platforms[0].streams[1].releases[0].quarkus-core-version", is("2.1.0.CR1"));
}

@Test
Expand Down Expand Up @@ -172,4 +170,4 @@ void should_return_not_found_to_sha1_if_snapshot_version_is_invalid() {
.then()
.statusCode(HttpURLConnection.HTTP_NOT_FOUND);
}
}
}

0 comments on commit 060fb8f

Please sign in to comment.