Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistency of the data returned by the API #747

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.util.concurrent.CompletionStage
import jakarta.enterprise.context.ApplicationScoped
import jakarta.inject.Inject
import jakarta.ws.rs.BadRequestException
import jakarta.ws.rs.DefaultValue
import jakarta.ws.rs.GET
import jakarta.ws.rs.Path
import jakarta.ws.rs.PathParam
Expand Down Expand Up @@ -63,17 +64,21 @@ class DownloadStatsResource {
@GET
@Schema(hidden = true)
@Path("/total/{feature_version}")
@Operation(summary = "Get download stats for feature verson", description = "stats", hidden = true)
@Operation(summary = "Get download stats for feature version", description = "stats", hidden = true)
fun getTotalDownloadStats(
@Parameter(name = "feature_version", description = "Feature version (i.e 8, 9, 10...)", required = true)
@PathParam("feature_version")
featureVersion: Int
featureVersion: Int,

@Parameter(name = "release_types", description = "List of release types to include in computation (i.e &release_types=ga&release_types=ea)", required = false)
@QueryParam("release_types")
@DefaultValue("ga")
releaseTypes: List<ReleaseType>?
): Map<String, Long> {
val release = apiDataStore.getAdoptRepos().getFeatureRelease(featureVersion)
?: throw BadRequestException("Unable to find version $featureVersion")

return getAdoptReleases(release)
.filter { it.release_type == ReleaseType.ga }
return getAdoptReleases(release, releaseTypes, null)
.map { grouped ->
Pair(
grouped.release_name,
Expand All @@ -89,20 +94,25 @@ class DownloadStatsResource {
@GET
@Schema(hidden = true)
@Path("/total/{feature_version}/{release_name}")
@Operation(summary = "Get download stats for feature verson", description = "stats", hidden = true)
@Operation(summary = "Get download stats for feature version", description = "stats", hidden = true)
fun getTotalDownloadStatsForTag(
@Parameter(name = "feature_version", description = "Feature version (i.e 8, 9, 10...)", required = true)
@PathParam("feature_version")
featureVersion: Int,

@Parameter(name = "release_name", description = "Release Name i.e jdk-11.0.4+11", required = true)
@PathParam("release_name")
releaseName: String
releaseName: String,

@Parameter(name = "release_types", description = "List of release types to include in computation (i.e &release_types=ga&release_types=ea)", required = false)
@QueryParam("release_types")
@DefaultValue("ga")
releaseTypes: List<ReleaseType>?
): Map<String, Long> {
val release = apiDataStore.getAdoptRepos().getFeatureRelease(featureVersion)
?: throw BadRequestException("Unable to find version $featureVersion")

return getAdoptReleases(release)
.filter { it.release_name == releaseName }
return getAdoptReleases(release, releaseTypes, releaseName)
.flatMap { it.binaries.asSequence() }
.flatMap {
val archive = Pair(it.`package`.name, it.download_count)
Expand All @@ -115,17 +125,27 @@ class DownloadStatsResource {
.toMap()
}

private fun getAdoptReleases(release: FeatureRelease): Sequence<Release> {
return release
private fun getAdoptReleases(release: FeatureRelease, releaseTypes: List<ReleaseType>?, releaseName: String?): Sequence<Release> {
var releases = release
.releases
.getReleases()
.filter { it.vendor == Vendor.getDefault() }

if(releaseTypes != null && releaseTypes.isNotEmpty()) {
releases = releases.filter { releaseTypes.contains(it.release_type) }
}

if(releaseName != null) {
releases = releases.filter { it.release_name == releaseName }
}

return releases
}

@GET
@Schema(hidden = true)
@Path("/tracking")
@Operation(summary = "Get download stats for feature verson", description = "stats", hidden = true)
@Operation(summary = "Get download stats for feature version", description = "stats", hidden = true)
fun tracking(
@Parameter(name = "days", description = "Number of days to display, if used in conjunction with from/to then this will limit the request to x days before the end of the given period", schema = Schema(defaultValue = "30", type = SchemaType.INTEGER), required = false)
@QueryParam("days")
Expand Down Expand Up @@ -165,7 +185,7 @@ class DownloadStatsResource {
@GET
@Schema(hidden = true)
@Path("/monthly")
@Operation(summary = "Get download stats for feature verson", description = "stats", hidden = true)
@Operation(summary = "Get download stats for feature version", description = "stats", hidden = true)
fun monthly(
@Parameter(name = "source", description = "Stats data source", schema = Schema(defaultValue = "all"), required = false)
@QueryParam("source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import java.time.format.DateTimeFormatter
import jakarta.ws.rs.BadRequestException
import net.adoptium.api.v3.models.ReleaseType

@ExtendWith(value = [DbExtension::class])
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -157,14 +158,14 @@ class DownloadStatsPathTest : FrontendTest() {

@Test
fun totalVersionReturnsSaneData() {
val stats = downloadStatsResource.getTotalDownloadStats(8)
val stats = downloadStatsResource.getTotalDownloadStats(8, listOf(ReleaseType.ga))
assertTrue { return@assertTrue stats.isNotEmpty() && !stats.containsValue(0L) }
}

@Test
fun badTotalVersionReturnsSaneData() {
assertThrows<BadRequestException> {
downloadStatsResource.getTotalDownloadStats(101)
downloadStatsResource.getTotalDownloadStats(101, listOf(ReleaseType.ga))
}
}

Expand All @@ -174,18 +175,19 @@ class DownloadStatsPathTest : FrontendTest() {
val releases = getReleases()

val release = releases
.filter { it.release_type == ReleaseType.ga }
.filter { it.vendor == Vendor.getDefault() }
.first()

val stats = downloadStatsResource.getTotalDownloadStatsForTag(release.version_data.major, release.release_name)
val stats = downloadStatsResource.getTotalDownloadStatsForTag(release.version_data.major, release.release_name, listOf(ReleaseType.ga))
assertTrue { return@assertTrue stats.isNotEmpty() && !stats.containsValue(0L) }
}
}

@Test
fun badTotalTagReturnsSaneData() {
assertThrows<BadRequestException> {
downloadStatsResource.getTotalDownloadStatsForTag(101, "fooBar")
downloadStatsResource.getTotalDownloadStatsForTag(101, "fooBar", listOf(ReleaseType.ga))
}
}

Expand Down