From 71f775f282931e2cbf2cf500fc619d23c9b74cb2 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 6 Jan 2022 20:57:28 +0000 Subject: [PATCH] Add JSON version of the /caches page Signed-off-by: Graham --- archive/build.gradle.kts | 1 + .../org/openrs2/archive/ArchiveModule.kt | 5 +++++ .../org/openrs2/archive/cache/CacheExporter.kt | 18 +++++++++++++++--- .../openrs2/archive/web/CachesController.kt | 5 +++++ .../org/openrs2/archive/web/WebServer.kt | 1 + .../archive/templates/caches/index.html | 2 +- gradle/libs.versions.toml | 1 + .../org/openrs2/json/ObjectMapperProvider.kt | 1 + .../org/openrs2/yaml/ObjectMapperProvider.kt | 2 ++ 9 files changed, 32 insertions(+), 4 deletions(-) diff --git a/archive/build.gradle.kts b/archive/build.gradle.kts index c050ae25..910f3102 100644 --- a/archive/build.gradle.kts +++ b/archive/build.gradle.kts @@ -32,6 +32,7 @@ dependencies { implementation(libs.flyway) implementation(libs.guava) implementation(libs.hikaricp) + implementation(libs.jackson.jsr310) implementation(libs.jdom) implementation(libs.jquery) implementation(libs.jsoup) diff --git a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt index f4f3da96..37e1b2cf 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt @@ -1,5 +1,7 @@ package org.openrs2.archive +import com.fasterxml.jackson.databind.Module +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.google.inject.AbstractModule import com.google.inject.Scopes import com.google.inject.multibindings.Multibinder @@ -44,6 +46,9 @@ public object ArchiveModule : AbstractModule() { keyBinder.addBinding().to(PolarKeyDownloader::class.java) keyBinder.addBinding().to(RuneLiteKeyDownloader::class.java) + val moduleBinder = Multibinder.newSetBinder(binder(), Module::class.java) + moduleBinder.addBinding().to(JavaTimeModule::class.java) + val nameBinder = Multibinder.newSetBinder(binder(), NameDownloader::class.java) nameBinder.addBinding().to(RuneStarNameDownloader::class.java) } diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt index c19bdcff..a4f8d4d5 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt @@ -1,6 +1,8 @@ package org.openrs2.archive.cache +import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.JsonUnwrapped import io.netty.buffer.ByteBufAllocator import io.netty.buffer.Unpooled import org.openrs2.buffer.use @@ -35,21 +37,30 @@ public class CacheExporter @Inject constructor( val size: Long, val blocks: Long ) { + @JsonIgnore public val allIndexesValid: Boolean = indexes == validIndexes && indexes != 0L + + @JsonIgnore public val validIndexesFraction: Double = if (indexes == 0L) { 1.0 } else { validIndexes.toDouble() / indexes } + @JsonIgnore public val allGroupsValid: Boolean = groups == validGroups + + @JsonIgnore public val validGroupsFraction: Double = if (groups == 0L) { 1.0 } else { validGroups.toDouble() / groups } + @JsonIgnore public val allKeysValid: Boolean = keys == validKeys + + @JsonIgnore public val validKeysFraction: Double = if (keys == 0L) { 1.0 } else { @@ -103,7 +114,8 @@ public class CacheExporter @Inject constructor( val language: String, val builds: SortedSet, val timestamp: Instant?, - val names: SortedSet, + val sources: SortedSet, + @JsonUnwrapped val stats: Stats? ) @@ -181,7 +193,7 @@ public class CacheExporter @Inject constructor( val language = rows.getString(4) val builds = rows.getArray(5).array as Array<*> val timestamp = rows.getTimestamp(6)?.toInstant() - @Suppress("UNCHECKED_CAST") val names = rows.getArray(7).array as Array + @Suppress("UNCHECKED_CAST") val sources = rows.getArray(7).array as Array val validIndexes = rows.getLong(8) val stats = if (!rows.wasNull()) { @@ -204,7 +216,7 @@ public class CacheExporter @Inject constructor( language, builds.mapNotNull { o -> Build.fromPgObject(o as PGobject) }.toSortedSet(), timestamp, - names.toSortedSet(), + sources.toSortedSet(), stats ) } diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt b/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt index 329afd2f..fc647d9d 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt @@ -40,6 +40,11 @@ public class CachesController @Inject constructor( call.respond(ThymeleafContent("caches/index.html", mapOf("caches" to caches))) } + public suspend fun indexJson(call: ApplicationCall) { + val caches = exporter.list() + call.respond(caches) + } + public suspend fun show(call: ApplicationCall) { val id = call.parameters["id"]?.toIntOrNull() if (id == null) { diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt index 8c5665c1..37b5bbe3 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt @@ -55,6 +55,7 @@ public class WebServer @Inject constructor( routing { get("/") { call.respond(ThymeleafContent("index.html", emptyMap())) } get("/caches") { cachesController.index(call) } + get("/caches.json") { cachesController.indexJson(call) } get("/caches/{id}") { cachesController.show(call) } get("/caches/{id}.zip") { val id = call.parameters["id"] diff --git a/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html b/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html index b983408f..fff9e75d 100644 --- a/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html +++ b/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html @@ -45,7 +45,7 @@
- +