diff --git a/Plan/src/main/java/com/djrapitops/plan/api/API.java b/Plan/src/main/java/com/djrapitops/plan/api/API.java index 5c83b97d8..6c3dcea9a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/api/API.java +++ b/Plan/src/main/java/com/djrapitops/plan/api/API.java @@ -154,6 +154,7 @@ public class API { * Check if the data is cached to AnalysisCache before calling this. * * @return server.html with all placeholders replaced. + * @throws NullPointerException if AnalysisData has not been cached. */ public String getAnalysisHtmlAsString() { return plugin.getInfoManager().getAnalysisHtml(); diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java index 308936e9d..c19fbe86f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java @@ -144,7 +144,7 @@ public class ActivityPart extends RawData { } public Map getPlayersOnline() { - return tpsPart.getTpsData().stream().collect(Collectors.toMap(TPS::getDate, TPS::getPlayers)); + return tpsPart.getTpsData().stream().distinct().collect(Collectors.toMap(TPS::getDate, TPS::getPlayers)); } public List getRecentPlayers() { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java index df182e15d..749adee11 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java @@ -173,6 +173,9 @@ public class BukkitInformationManager extends InformationManager { @Override public boolean isAnalysisCached(UUID serverUUID) { + if (Plan.getServerUUID().equals(serverUUID)) { + return analysisData != null; + } if (usingAnotherWebServer) { try { return getWebAPI().getAPI(IsCachedWebAPI.class).isAnalysisCached(webServerAddress, serverUUID); @@ -188,8 +191,17 @@ public class BukkitInformationManager extends InformationManager { return plugin.getWebServer().getWebAPI(); } + /** + * Get the HTML for analysis page of this server. + * + * @return Html for Analysis page + * @throws NullPointerException if AnalysisData has not been cached. + */ @Override public String getAnalysisHtml() { + if (analysisData == null) { + throw new NullPointerException("Analysis Data has not been cached."); + } try { return Theme.replaceColors(new AnalysisPageParser(analysisData, plugin).parse()); } catch (ParseException e) { @@ -229,6 +241,10 @@ public class BukkitInformationManager extends InformationManager { } private void cacheAnalysisHtml() { + cacheAnalysisHtml(getAnalysisHtml()); + } + + public void cacheAnalysisHtml(String html) { if (usingAnotherWebServer) { try { getWebAPI().getAPI(PostHtmlWebAPI.class).sendAnalysisHtml(webServerAddress, getAnalysisHtml()); @@ -237,7 +253,7 @@ public class BukkitInformationManager extends InformationManager { cacheAnalysisHtml(); } } else { - PageCache.cachePage("analysisPage:" + Plan.getServerUUID(), () -> new AnalysisPageResponse(this)); + PageCache.cachePage("analysisPage:" + Plan.getServerUUID(), () -> new AnalysisPageResponse(html)); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java index dc1ba14e8..8cc7dffed 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/ResponseHandler.java @@ -191,11 +191,6 @@ public class ResponseHandler extends APIResponseHandler { } private Response serverResponse(UUID serverUUID) { - if (!plugin.getInfoManager().isAnalysisCached(serverUUID)) { - String error = "Analysis Data was not cached.
Use /plan analyze to cache the Data."; - PageCache.loadPage("notFound: " + error, () -> new NotFoundResponse(error)); - } - return PageCache.loadPage("analysisPage:" + serverUUID, () -> new AnalysisPageResponse(plugin.getInfoManager())); } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java index 62e63f3ce..4bf724549 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java @@ -1,7 +1,10 @@ package main.java.com.djrapitops.plan.systems.webserver.response; +import com.djrapitops.plugin.task.AbsRunnable; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.data.AnalysisData; +import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager; import main.java.com.djrapitops.plan.systems.info.InformationManager; -import main.java.com.djrapitops.plan.systems.webserver.theme.Theme; /** * @author Rsl1122 @@ -9,13 +12,37 @@ import main.java.com.djrapitops.plan.systems.webserver.theme.Theme; */ public class AnalysisPageResponse extends Response { + /** + * @param informationManager + * @throws NullPointerException if AnalysisData has not been cached after 1 second. + */ public AnalysisPageResponse(InformationManager informationManager) { super.setHeader("HTTP/1.1 200 OK"); + + if (informationManager instanceof BukkitInformationManager) { + AnalysisData analysisData = ((BukkitInformationManager) informationManager).getAnalysisData(); + if (analysisData == null) { + Plan.getInstance().getRunnableFactory().createNew("OnRequestAnalysisRefreshTask", new AbsRunnable() { + @Override + public void run() { + informationManager.refreshAnalysis(Plan.getServerUUID()); + } + }).runTaskAsynchronously(); + + ErrorResponse analysisRefreshPage = new ErrorResponse(); + analysisRefreshPage.setTitle("Analysis is being refreshed.."); + analysisRefreshPage.setParagraph(" Analysis is being run, refresh the page after a few seconds.. (F5)"); + analysisRefreshPage.replacePlaceholders(); + super.setContent(analysisRefreshPage.getContent()); + + return; + } + } super.setContent(informationManager.getAnalysisHtml()); } public AnalysisPageResponse(String html) { super.setHeader("HTTP/1.1 200 OK"); - super.setContent(Theme.replaceColors(html)); + super.setContent(html); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/ErrorResponse.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/ErrorResponse.java index ce42b63e2..c9712e638 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/ErrorResponse.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/ErrorResponse.java @@ -32,7 +32,7 @@ public class ErrorResponse extends Response { } } - protected void replacePlaceholders() { + public void replacePlaceholders() { Map placeHolders = new HashMap<>(); placeHolders.put("title", title); placeHolders.put("paragraph", paragraph); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java index d41ddc765..df980f33b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java @@ -17,6 +17,8 @@ import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Msg; import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager; import main.java.com.djrapitops.plan.systems.info.InformationManager; +import main.java.com.djrapitops.plan.systems.webserver.response.ErrorResponse; +import main.java.com.djrapitops.plan.systems.webserver.response.InternalErrorResponse; import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.comparators.UserInfoLastPlayedComparator; @@ -63,6 +65,11 @@ public class Analysis { plugin.getRunnableFactory().createNew(new AbsRunnable("AnalysisTask") { @Override public void run() { + ErrorResponse analysisRefreshPage = new ErrorResponse(); + analysisRefreshPage.setTitle("Analysis is being refreshed.."); + analysisRefreshPage.setParagraph(" Analysis is being run, refresh the page after a few seconds.. (F5)"); + analysisRefreshPage.replacePlaceholders(); + ((BukkitInformationManager) plugin.getInfoManager()).cacheAnalysisHtml(analysisRefreshPage.getContent()); taskId = this.getTaskId(); analyze(infoManager, plugin.getDB()); taskId = -1; @@ -127,6 +134,7 @@ public class Analysis { // ExportUtility.export(analysisData, rawData); } catch (Exception e) { Log.toLog(this.getClass().getName(), e); + ((BukkitInformationManager) plugin.getInfoManager()).cacheAnalysisHtml(new InternalErrorResponse(e, "Analysis").getContent()); Log.debug("Analysis", "Error: " + e); Log.logDebug("Analysis"); return false; @@ -238,7 +246,7 @@ public class Analysis { tpsPart.addTpsData(tpsData); Log.debug("Analysis", "TPS Data Size: " + tpsData.size()); - List userInfo = db.getUserInfoTable().getServerUserInfo(); + List userInfo = db.getUserInfoTable().getServerUserInfo().stream().distinct().collect(Collectors.toList()); for (UserInfo user : userInfo) { if (user.isBanned()) { diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java index c2c3e9ed2..69be40d59 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java @@ -21,6 +21,7 @@ public class WorldPieCreator { public static String[] createSeriesData(WorldTimes worldTimes) { StringBuilder seriesBuilder = new StringBuilder("["); int i = 0; + // WorldTimes Map (GMTimes.getTotal) Map playtimePerWorld = worldTimes.getWorldTimes().entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getTotal()));