From 1818175d4aaba80cd57a33c634461bb5c8c8432a Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Sat, 13 Jun 2020 16:09:03 -0500 Subject: [PATCH] Commit the roughest POC of an UI ever --- dev/settings.json | 14 ++++-- src/main/java/mdnet/base/MangaDexClient.java | 13 ++++- src/main/kotlin/mdnet/base/Statistics.kt | 3 +- src/main/kotlin/mdnet/base/web/WebUi.kt | 3 +- src/main/resources/webui/dataReceive.js | 52 ++++++++------------ src/main/resources/webui/index.html | 43 +--------------- 6 files changed, 46 insertions(+), 82 deletions(-) diff --git a/dev/settings.json b/dev/settings.json index 515d8e4..38458b5 100644 --- a/dev/settings.json +++ b/dev/settings.json @@ -1,8 +1,12 @@ { - "client_secret": "7rc7p00md0n0xsvqnv4rv17fthvjjrzpdghak1yq45833zvdvnb0", + "client_secret": "s76t1dazfctvtgq9dyvgw9herxc4gcz39q0q0y3taxpkgg0ahq8g", "max_cache_size_mib": 2048, - "client_port": 8080, - "max_burst_rate_kib_per_second": 100, - "max_bandwidth_mib_per_hour": 1, - "threads_per_cpu": 32 + "client_port": 443, + "max_burst_rate_kib_per_second": 0, + "max_bandwidth_mib_per_hour": 0, + "threads_per_cpu": 32, + "web_settings": + { + "ui_port": 8080 + } } diff --git a/src/main/java/mdnet/base/MangaDexClient.java b/src/main/java/mdnet/base/MangaDexClient.java index fe815c7..de38140 100644 --- a/src/main/java/mdnet/base/MangaDexClient.java +++ b/src/main/java/mdnet/base/MangaDexClient.java @@ -64,8 +64,7 @@ public class MangaDexClient { if (snapshot != null) { String json = snapshot.getString(0); snapshot.close(); - statistics.set(GSON.fromJson(json, new TypeToken>() { - }.getType())); + statistics.set(GSON.fromJson(json, Statistics.class)); } else { statistics.set(new Statistics()); } @@ -85,6 +84,16 @@ public class MangaDexClient { } statsMap.put(Instant.now(), statistics.get()); + try { + DiskLruCache.Editor editor = cache.edit("statistics"); + if (editor != null) { + String json = GSON.toJson(statistics.get(), Statistics.class); + editor.setString(0, json); + editor.setString(1, ""); + editor.setString(2, ""); + editor.commit(); + } + } catch (IOException ignored) {} if (clientSettings.getWebSettings() != null) { webUi = WebUiKt.getUiServer(clientSettings.getWebSettings(), statistics, statsMap); diff --git a/src/main/kotlin/mdnet/base/Statistics.kt b/src/main/kotlin/mdnet/base/Statistics.kt index 8c725b5..3d841ef 100644 --- a/src/main/kotlin/mdnet/base/Statistics.kt +++ b/src/main/kotlin/mdnet/base/Statistics.kt @@ -7,5 +7,6 @@ data class Statistics( @field:SerializedName("cache_hits") val cacheHits: Int = 0, @field:SerializedName("cache_misses") val cacheMisses: Int = 0, @field:SerializedName("browser_cached") val browserCached: Int = 0, - @field:SerializedName("bytes_sent") val bytesSent: Long = 0 + @field:SerializedName("bytes_sent") val bytesSent: Long = 0, + @field:SerializedName("bytes_on_disk") val bytesOnDisk: Long = 0 ) diff --git a/src/main/kotlin/mdnet/base/web/WebUi.kt b/src/main/kotlin/mdnet/base/web/WebUi.kt index 995dc56..cd068ca 100644 --- a/src/main/kotlin/mdnet/base/web/WebUi.kt +++ b/src/main/kotlin/mdnet/base/web/WebUi.kt @@ -25,7 +25,6 @@ fun getUiServer( statistics: AtomicReference, statsMap: Map ): Http4kServer { - val statisticsLens = Body.auto().toLens() val statsMapLens = Body.auto>().toLens() return catchAllHideDetails() @@ -34,7 +33,7 @@ fun getUiServer( .then( routes( "/api/stats" bind Method.GET to { - statisticsLens(statistics.get(), Response(Status.OK)) + statsMapLens(mapOf(Instant.now() to statistics.get()), Response(Status.OK)) }, "/api/pastStats" bind Method.GET to { synchronized(statsMap) { diff --git a/src/main/resources/webui/dataReceive.js b/src/main/resources/webui/dataReceive.js index 96bcac5..ab407c8 100644 --- a/src/main/resources/webui/dataReceive.js +++ b/src/main/resources/webui/dataReceive.js @@ -200,11 +200,12 @@ function loadStuff() { type: 'doughnut', data: { datasets: [{ - data: [0, 0] + data: [0, 0, 0] }], labels: [ 'Hits', - 'Misses' + 'Misses', + 'Browser Cached' ] }, options: {} @@ -235,12 +236,12 @@ function loadStuff() { byte = new Chart(document.getElementById('bytessent').getContext('2d'), { type: 'line', data: { - labels: [1, 2, 3, 4], + labels: [], datasets: [{ label: 'Bytes Sent', backgroundColor: "#f00", borderColor: "#f00", - data: [36, 98, 45, 67], + data: [], fill: false }] }, @@ -258,12 +259,12 @@ function loadStuff() { cached = new Chart(document.getElementById('browsercached').getContext('2d'), { type: 'line', data: { - labels: [1, 2, 3, 4], + labels: [], datasets: [{ - label: 'Cached', + label: 'Bytes On Disk', backgroundColor: "#f00", borderColor: "#f00", - data: [36, 98, 45, 67], + data: [], fill: false }] }, @@ -585,49 +586,38 @@ function updateWithMessage(m) { function getStats() { fetch("/api/stats") - .then(response => async function () { - let respj = JSON.parse(await response.text()); - parseResponse(respj); - console.log(respj); + .then(response => response.json()) + .then(response => { + updateValues(response); + console.log(response); }); //TODO: use values and update web info } -function parseResponse(x) { - hitmiss.data.datasets[0].data[0] = (x.cache_hits); - hitmiss.data.datasets[0].data[1] = (x.misses); - req.data.labels.push(x.snap_time); - req.data.datasets.forEach((dataset) => { - dataset.data.push(x.requests_served); - }); - byte.data.labels.push(x.snap_time); - byte.data.datasets.forEach((dataset) => { - dataset.data.push(x.bytes_sent); - }); - cached.data.labels.push(x.snap_time); - cached.data.datasets.forEach((dataset) => { - dataset.data.push(x.browser_cached); - }); -} - function updateValues(data) { for (let key in data) { if (data.hasOwnProperty(key)) { let x = data[key]; - hitmiss.data.datasets[0].data[0] = (x.cache_hits); - hitmiss.data.datasets[0].data[1] = (x.misses); + hitmiss.data.datasets[0].data[0] = x.cache_hits; + hitmiss.data.datasets[0].data[1] = x.cache_misses; + hitmiss.data.datasets[0].data[2] = x.browser_cached; + + hitmiss.update() req.data.labels.push(key); req.data.datasets.forEach((dataset) => { dataset.data.push(x.requests_served); }); + req.update() byte.data.labels.push(key); byte.data.datasets.forEach((dataset) => { dataset.data.push(x.bytes_sent); }); + byte.update() cached.data.labels.push(key); cached.data.datasets.forEach((dataset) => { - dataset.data.push(x.browser_cached); + dataset.data.push(x.bytes_on_disk); }); + cached.update() } } } diff --git a/src/main/resources/webui/index.html b/src/main/resources/webui/index.html index 08d6462..6f43fa6 100644 --- a/src/main/resources/webui/index.html +++ b/src/main/resources/webui/index.html @@ -21,7 +21,7 @@ -