From 6715244b2cc8ca53c369e823f5a81d64f2fb1386 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Thu, 11 Feb 2021 12:43:31 +0200 Subject: [PATCH] Made session accordion load again --- .../resolver/json/SessionsJSONResolver.java | 23 +++- .../assets/plan/web/js/sessionAccordion.js | 121 ++++++++++-------- .../resources/assets/plan/web/network.html | 2 +- .../resources/assets/plan/web/server.html | 2 +- 4 files changed, 88 insertions(+), 60 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/SessionsJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/SessionsJSONResolver.java index fc47016f7..5072a9185 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/SessionsJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/SessionsJSONResolver.java @@ -17,12 +17,14 @@ package com.djrapitops.plan.delivery.webserver.resolver.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; +import com.djrapitops.plan.delivery.web.resolver.MimeType; import com.djrapitops.plan.delivery.web.resolver.Resolver; import com.djrapitops.plan.delivery.web.resolver.Response; import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.web.resolver.request.WebUser; +import com.djrapitops.plan.delivery.webserver.cache.AsyncJSONResolverService; import com.djrapitops.plan.delivery.webserver.cache.DataID; -import com.djrapitops.plan.delivery.webserver.cache.JSONCache; +import com.djrapitops.plan.delivery.webserver.cache.JSONStorage; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; @@ -40,14 +42,17 @@ import java.util.UUID; public class SessionsJSONResolver implements Resolver { private final Identifiers identifiers; + private final AsyncJSONResolverService jsonResolverService; private final JSONFactory jsonFactory; @Inject public SessionsJSONResolver( Identifiers identifiers, + AsyncJSONResolverService jsonResolverService, JSONFactory jsonFactory ) { this.identifiers = identifiers; + this.jsonResolverService = jsonResolverService; this.jsonFactory = jsonFactory; } @@ -62,11 +67,23 @@ public class SessionsJSONResolver implements Resolver { } private Response getResponse(Request request) { + return Response.builder() + .setMimeType(MimeType.JSON) + .setJSONContent(getStoredJSON(request).json) + .build(); + } + + private JSONStorage.StoredJSON getStoredJSON(Request request) { + long timestamp = Identifiers.getTimestamp(request); if (request.getQuery().get("server").isPresent()) { UUID serverUUID = identifiers.getServerUUID(request); - return JSONCache.getOrCache(DataID.SESSIONS, serverUUID, () -> Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID))); + return jsonResolverService.resolve(timestamp, DataID.SESSIONS, serverUUID, + theUUID -> Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(theUUID)) + ); } // Assume network - return JSONCache.getOrCache(DataID.SESSIONS, () -> Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap())); + return jsonResolverService.resolve(timestamp, DataID.SESSIONS, + () -> Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap()) + ); } } \ No newline at end of file diff --git a/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js b/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js index 4678ec3ff..592944ba9 100644 --- a/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js +++ b/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js @@ -1,49 +1,48 @@ function loadSessionAccordion(json, error) { - sessionTable = $("#sessions-overview").find("#tableAccordion").find("tbody"); + const sessionTable = document.querySelector('#sessions-overview #tableAccordion tbody'); if (error) { - sessionTable.append(`Error: ` + error + '---'); + sessionTable.innerHTML = `Error: ${error}---`; return; } - var sessions = json.sessions; + const sessions = json.sessions; if (!sessions.length) { - sessionTable.append(`No Data---`); + sessionTable.innerHTML = `No Data---`; return; } // sessions_per_page can be undefined (-> NaN) or higher than amount of sessions. - var limit = json.sessions_per_page ? json.sessions_per_page : sessions.length; + let limit = json.sessions_per_page ? json.sessions_per_page : sessions.length; limit = Math.min(limit, sessions.length); - var sessionsHtml = ''; - for (var i = 0; i < limit; i++) { - var session = sessions[i]; - var title = createAccordionTitle(i, session); - var body = createAccordionBody(i, session); + let sessionsHtml = ''; + for (let i = 0; i < limit; i++) { + const session = sessions[i]; + const title = createAccordionTitle(i, session); + const body = createAccordionBody(i, session); sessionsHtml += title + body; } + sessionTable.innerHTML = sessionsHtml; - sessionTable.append(sessionsHtml); - - for (var i = 0; i < limit; i++) { - $('#session_h_' + i).click(onOpenSession(i, sessions)); + for (let i = 0; i < limit; i++) { + document.getElementById(`session_h_${i}`).addEventListener('click', onOpenSession(i, sessions)); } } function onOpenSession(i, sessions) { - var opened = false; + let opened = false; return function () { if (opened) { return; } setTimeout(function () { - var session = sessions[i]; - var worldSeries = {name: `World Playtime`, colorByPoint: true, data: session.world_series}; - var gmSeries = session.gm_series; + const session = sessions[i]; + const worldSeries = {name: `World Playtime`, colorByPoint: true, data: session.world_series}; + const gmSeries = session.gm_series; - worldPie("worldpie_" + i, worldSeries, gmSeries); + worldPie(`worldpie_${i}`, worldSeries, gmSeries); }, 250); opened = true; } @@ -51,7 +50,7 @@ function onOpenSession(i, sessions) { function loadPlayerKills(json, error) { if (error) { - $('#playerKillTable').replaceWith(`

Failed to load player kills: ` + error + '

'); + $('#playerKillTable').replaceWith(`

Failed to load player kills: ${error}

`); return; } $('#playerKillTable').replaceWith(createKillsTable(json.player_kills)); @@ -59,7 +58,7 @@ function loadPlayerKills(json, error) { function loadPlayerdeaths(json, error) { if (error) { - $('#playerDeathTable').replaceWith(`

Failed to load player deaths: ` + error + '

'); + $('#playerDeathTable').replaceWith(`

Failed to load player deaths: ${error}

`); return; } $('#playerDeathTable').replaceWith(createKillsTable(json.player_deaths)); @@ -67,50 +66,62 @@ function loadPlayerdeaths(json, error) { function createAccordionTitle(i, session) { let style = session.start.includes("Online") ? 'bg-teal' : 'bg-teal-outline'; - return `` - + session.name + (session.first_session ? ` ` : ``) + `` - + `` + session.start + `` - + `` + session.length + `` - + `` + (session.network_server ? session.network_server : session.most_used_world) + `` + return ` + ${session.name}${session.first_session ? ` ` : ``} + ${session.start}${session.length} + ${session.network_server ? session.network_server : session.most_used_world} + ` } function createAccordionBody(i, session) { - return `` + - `` + - `
` + - `
` + - `

Ended` + session.end + `

` + - `

Length` + session.length + `

` + - `

AFK Time` + session.afk_time + `

` + - `

Server` + session.server_name + `

` + - (session.avg_ping ? `

Average Ping` + session.avg_ping + `

` : ``) + - `
` + - `

Player Kills` + session.player_kills.length + `

` + - `

Mob Kills` + session.mob_kills + `

` + - `

Deaths` + session.deaths + `


` + - createKillsTable(session.player_kills) + - `
` + - `
` + - ` Player Page` + - (session.network_server ? ` Server Analysis` : ``) + - `
` + - `
` + return ` + +
+
+

Ended${session.end}

+

Length${session.length}

+

AFK Time${session.afk_time}

+

Server${session.server_name}

+ ${session.avg_ping ? `

Average Ping` + session.avg_ping + `

` : ``} +
+

Player Kills${session.player_kills.length}

+

Mob Kills${session.mob_kills}

+

Deaths${session.deaths}

+
+ ${createKillsTable(session.player_kills)} +
+
+
+ + Player Page + + ${session.network_server ? ` + Server Analysis + ` : ``} +
+
+ + `; } function createKillsTable(player_kills) { - var table = ''; + let table = '
'; - if (player_kills.length === 0) { + if (!player_kills.length) { table += `` } - for (var i = 0; i < player_kills.length; i++) { - var kill = player_kills[i]; - table += '' + - '' + - '' + for (const kill of player_kills) { + table += ` + + + + ` } table += '
None--
' + kill.date + '' + kill.killer + - (kill.killer === kill.victim ? '' : '') + - kill.victim + '' + kill.weapon + '
${kill.date}${kill.killer} ${ + kill.killer === kill.victim + ? '' + : '' + } ${kill.victim}${kill.weapon}
'; diff --git a/Plan/common/src/main/resources/assets/plan/web/network.html b/Plan/common/src/main/resources/assets/plan/web/network.html index 015828da7..fe988ee7f 100644 --- a/Plan/common/src/main/resources/assets/plan/web/network.html +++ b/Plan/common/src/main/resources/assets/plan/web/network.html @@ -864,7 +864,7 @@ refreshingJsonRequest("./v1/network/servers", loadservers, 'servers'); refreshingJsonRequest("./v1/network/sessionsOverview", loadSessionValues, 'sessions-overview'); refreshingJsonRequest("./v1/network/playerbaseOverview", loadPlayerbaseOverviewValues, 'playerbase-overview'); - jsonRequest("./v1/sessions", loadSessionAccordion); + refreshingJsonRequest("./v1/sessions", loadSessionAccordion, 'sessions-overview'); setLoadingText('Rendering graphs..'); // TODO remove diff --git a/Plan/common/src/main/resources/assets/plan/web/server.html b/Plan/common/src/main/resources/assets/plan/web/server.html index 64305737f..be334707d 100644 --- a/Plan/common/src/main/resources/assets/plan/web/server.html +++ b/Plan/common/src/main/resources/assets/plan/web/server.html @@ -1402,7 +1402,7 @@ setLoadingText('Almost done..'); openPage(); - jsonRequest("../v1/sessions?server=${serverUUID}", loadSessionAccordion); + refreshingJsonRequest("../v1/sessions?server=${serverUUID}", loadSessionAccordion, 'sessions-overview'); setLoadingText('Done.'); setTimeout(function () {