Analysis command now uses serverUUID, Bungee handling of Analysis requests.

This commit is contained in:
Rsl1122 2017-09-26 10:20:34 +03:00
parent f178ed3d6a
commit 3a3fd2cadc
7 changed files with 188 additions and 62 deletions

View File

@ -11,11 +11,14 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.utilities.html.HtmlUtils;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfo;
import org.bukkit.ChatColor;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
/**
* This subcommand is used to run the analysis and access the /server link.
@ -37,25 +40,32 @@ public class AnalyzeCommand extends SubCommand {
super("analyze, analyse, analysis, a",
CommandType.CONSOLE,
Permissions.ANALYZE.getPermission(),
Locale.get(Msg.CMD_USG_ANALYZE).parse());
Locale.get(Msg.CMD_USG_ANALYZE).parse(),
"[ServerName or ID]");
this.plugin = plugin;
infoManager = plugin.getInfoManager();
}
public static void sendAnalysisMessage(Collection<ISender> senders) {
for (ISender sender : senders) {
sender.sendMessage(Locale.get(Msg.CMD_HEADER_ANALYZE).toString());
// Link
String url = HtmlUtils.getServerAnalysisUrlWithProtocol();
public static void sendAnalysisMessage(Collection<ISender> senders, UUID serverUUID) throws SQLException {
Plan plugin = Plan.getInstance();
Optional<String> serverName = plugin.getDB().getServerTable().getServerName(serverUUID);
if (serverName.isPresent()) {
String target = "/server/" + serverName.get();
String url = plugin.getInfoManager().getLinkTo(target).toString();
String message = Locale.get(Msg.CMD_INFO_LINK).toString();
boolean console = !CommandUtils.isPlayer(sender);
if (console) {
sender.sendMessage(message + url);
} else {
sender.sendMessage(message);
sender.sendLink(" ", Locale.get(Msg.CMD_INFO_CLICK_ME).toString(), url);
for (ISender sender : senders) {
sender.sendMessage(Locale.get(Msg.CMD_HEADER_ANALYZE).toString());
// Link
boolean console = !CommandUtils.isPlayer(sender);
if (console) {
sender.sendMessage(message + url);
} else {
sender.sendMessage(message);
sender.sendLink(" ", Locale.get(Msg.CMD_INFO_CLICK_ME).toString(), url);
}
sender.sendMessage(Locale.get(Msg.CMD_CONSTANT_FOOTER).toString());
}
sender.sendMessage(Locale.get(Msg.CMD_CONSTANT_FOOTER).toString());
}
}
@ -67,18 +77,29 @@ public class AnalyzeCommand extends SubCommand {
@Override
public boolean onCommand(ISender sender, String commandLabel, String[] args) {
// TODO Rewrite so that a server can be specified
// TODO Write a command for listing servers.
// Optional<Long> analysisRefreshDate = infoManager.getAnalysisRefreshDate();
// boolean forcedRefresh = args.length >= 1 && "-r".equals(args[0]);
// boolean refresh = !analysisRefreshDate.isPresent()
// || analysisRefreshDate.get() < MiscUtils.getTime() - TimeAmount.MINUTE.ms()
// || forcedRefresh;
updateCache(sender, true);
UUID serverUUID = Plan.getServerUUID();
if (args.length >= 1 && plugin.getInfoManager().isUsingAnotherWebServer()) {
try {
List<ServerInfo> bukkitServers = plugin.getDB().getServerTable().getBukkitServers();
Optional<ServerInfo> server = bukkitServers.stream().filter(info -> {
String serverIdentifier = args[0];
return Integer.toString(info.getId()).equals(serverIdentifier) || info.getName().equals(serverIdentifier);
}).findFirst();
if (server.isPresent()) {
serverUUID = server.get().getUuid();
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return true;
}
}
updateCache(sender, serverUUID);
sender.sendMessage(Locale.get(Msg.CMD_INFO_FETCH_DATA).toString());
if (plugin.getWebServer().isAuthRequired() && CommandUtils.isPlayer(sender)) {
if (plugin.getInfoManager().isAuthRequired() && CommandUtils.isPlayer(sender)) {
plugin.getRunnableFactory().createNew(new AbsRunnable("WebUser exist check task") {
@Override
public void run() {
@ -98,16 +119,12 @@ public class AnalyzeCommand extends SubCommand {
return true;
}
private void updateCache(ISender sender, boolean refresh) {
if (refresh) {
int bootAnID = plugin.getBootAnalysisTaskID();
if (bootAnID != -1) {
plugin.getServer().getScheduler().cancelTask(bootAnID);
}
infoManager.addAnalysisNotification(sender);
infoManager.refreshAnalysis();
} else {
sendAnalysisMessage(Collections.singletonList(sender));
private void updateCache(ISender sender, UUID serverUUID) {
int bootAnID = plugin.getBootAnalysisTaskID();
if (bootAnID != -1) {
plugin.getServer().getScheduler().cancelTask(bootAnID);
}
infoManager.addAnalysisNotification(sender, serverUUID);
infoManager.refreshAnalysis(serverUUID);
}
}

View File

@ -6,6 +6,7 @@ package main.java.com.djrapitops.plan.systems.info;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.exceptions.ParseException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
@ -24,6 +25,8 @@ import main.java.com.djrapitops.plan.systems.webserver.response.InternalErrorRes
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.theme.Theme;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalysisReadyWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalyzeWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.RequestInspectPluginsTabBukkitWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.*;
import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebAPI;
@ -32,6 +35,7 @@ import main.java.com.djrapitops.plan.utilities.analysis.Analysis;
import main.java.com.djrapitops.plan.utilities.html.HtmlStructure;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
/**
@ -60,7 +64,7 @@ public class BukkitInformationManager extends InformationManager {
pluginsTabContents = new HashMap<>();
Optional<String> bungeeConnectionAddress = plugin.getServerInfoManager().getBungeeConnectionAddress();
if (bungeeConnectionAddress.isPresent()) {
if (bungeeConnectionAddress.isPresent() && Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.isFalse()) {
webServerAddress = bungeeConnectionAddress.get();
attemptConnection();
} else {
@ -70,9 +74,19 @@ public class BukkitInformationManager extends InformationManager {
}
@Override
public void refreshAnalysis() {
plugin.getDataCache().cacheSavedNames();
analysis.runAnalysis(this);
public void refreshAnalysis(UUID serverUUID) {
if (Plan.getServerUUID().equals(serverUUID)) {
plugin.getDataCache().cacheSavedNames();
analysis.runAnalysis(this);
} else if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(AnalyzeWebAPI.class).sendRequest(webServerAddress, serverUUID);
} catch (WebAPIException e) {
attemptConnection();
refreshAnalysis(serverUUID);
}
}
}
@Override
@ -81,7 +95,8 @@ public class BukkitInformationManager extends InformationManager {
try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid));
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
attemptConnection();
cachePlayer(uuid);
}
} else {
PageCache.loadPage("inspectPage: " + uuid, () -> new InspectPageResponse(this, uuid));
@ -103,7 +118,8 @@ public class BukkitInformationManager extends InformationManager {
try {
getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequest(webServerAddress, uuid);
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
attemptConnection();
cacheInspectPluginsTab(uuid, origin);
}
} else {
String serverName = plugin.getServerInfoManager().getServerName();
@ -120,7 +136,8 @@ public class BukkitInformationManager extends InformationManager {
try {
getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents);
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
attemptConnection();
cacheInspectPluginsTab(uuid, contents);
}
} else {
pluginsTabContents.put(uuid, contents);
@ -143,7 +160,8 @@ public class BukkitInformationManager extends InformationManager {
try {
return getWebAPI().getAPI(IsCachedWebAPI.class).isInspectCached(webServerAddress, uuid);
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
attemptConnection();
return isCached(uuid);
}
}
return super.isCached(uuid);
@ -155,7 +173,8 @@ public class BukkitInformationManager extends InformationManager {
try {
return getWebAPI().getAPI(IsCachedWebAPI.class).isAnalysisCached(webServerAddress, serverUUID);
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
attemptConnection();
return isAnalysisCached(serverUUID);
}
}
return PageCache.isCached("analysisPage:" + serverUUID);
@ -188,12 +207,20 @@ public class BukkitInformationManager extends InformationManager {
return dataCache;
}
public void cacheAnalysisdata(AnalysisData analysisData) {
public void cacheAnalysisData(AnalysisData analysisData) {
this.analysisData = analysisData;
refreshDate = MiscUtils.getTime();
cacheAnalysisHtml();
AnalyzeCommand.sendAnalysisMessage(analysisNotification);
analysisNotification.clear();
UUID serverUUID = Plan.getServerUUID();
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(AnalysisReadyWebAPI.class).sendRequest(webServerAddress, serverUUID);
return;
} catch (WebAPIException e) {
attemptConnection();
}
}
analysisReady(serverUUID);
}
private void cacheAnalysisHtml() {
@ -201,7 +228,8 @@ public class BukkitInformationManager extends InformationManager {
try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendAnalysisHtml(webServerAddress, getAnalysisHtml());
} catch (WebAPIException e) {
Log.toLog(this.getClass().getName(), e);
attemptConnection();
cacheAnalysisHtml();
}
} else {
PageCache.cachePage("analysisPage:" + Plan.getServerUUID(), () -> new AnalysisPageResponse(this));
@ -241,4 +269,14 @@ public class BukkitInformationManager extends InformationManager {
public String getWebServerAddress() {
return webServerAddress != null ? webServerAddress : plugin.getWebServer().getAccessAddress();
}
@Override
public void analysisReady(UUID serverUUID) {
try {
AnalyzeCommand.sendAnalysisMessage(analysisNotification.get(serverUUID), serverUUID);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
analysisNotification.getOrDefault(serverUUID, new HashSet<>()).clear();
}
}

View File

@ -19,6 +19,7 @@ import main.java.com.djrapitops.plan.systems.webserver.response.InternalErrorRes
import main.java.com.djrapitops.plan.systems.webserver.response.NotFoundResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalysisReadyWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalyzeWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.InspectWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.IsOnlineWebAPI;
@ -56,10 +57,6 @@ public class BungeeInformationManager extends InformationManager {
}
@Override
public void refreshAnalysis() {
// TODO Refresh network page
}
public void refreshAnalysis(UUID serverUUID) {
ServerInfo serverInfo = bukkitServers.get(serverUUID);
if (serverInfo == null) {
@ -76,7 +73,7 @@ public class BungeeInformationManager extends InformationManager {
AnalyzeWebAPI api = plugin.getWebServer().getWebAPI().getAPI(AnalyzeWebAPI.class);
try {
api.sendRequest(serverInfo.getWebAddress());
api.sendRequest(serverInfo.getWebAddress(), serverUUID);
} catch (WebAPIConnectionFailException e) {
attemptConnection();
} catch (WebAPIException e) {
@ -220,4 +217,16 @@ public class BungeeInformationManager extends InformationManager {
public Map<UUID, String> getNetworkPageContent() {
return networkPageContent;
}
@Override
public void analysisReady(UUID serverUUID) {
AnalysisReadyWebAPI api = getWebAPI().getAPI(AnalysisReadyWebAPI.class);
for (ServerInfo serverInfo : bukkitServers.values()) {
try {
api.sendRequest(serverInfo.getWebAddress(), serverUUID);
} catch (WebAPIException ignored) {
/*Ignored*/
}
}
}
}

View File

@ -10,9 +10,7 @@ import main.java.com.djrapitops.plan.systems.cache.SessionCache;
import main.java.com.djrapitops.plan.systems.info.parsing.UrlParser;
import main.java.com.djrapitops.plan.systems.webserver.PageCache;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.*;
/**
* Abstract layer for Bukkit and Bungee Information managers.
@ -24,10 +22,10 @@ import java.util.UUID;
public abstract class InformationManager {
boolean usingAnotherWebServer;
String webServerAddress;
Set<ISender> analysisNotification;
Map<UUID, Set<ISender>> analysisNotification;
public InformationManager() {
analysisNotification = new HashSet<>();
analysisNotification = new HashMap<>();
}
public abstract boolean attemptConnection();
@ -42,7 +40,7 @@ public abstract class InformationManager {
}
}
public abstract void refreshAnalysis();
public abstract void refreshAnalysis(UUID serverUUID);
public abstract DataCache getDataCache();
@ -70,8 +68,10 @@ public abstract class InformationManager {
*/
public abstract String getAnalysisHtml();
public void addAnalysisNotification(ISender sender) {
analysisNotification.add(sender);
public void addAnalysisNotification(ISender sender, UUID serverUUID) {
Set<ISender> notify = analysisNotification.getOrDefault(serverUUID, new HashSet<>());
notify.add(sender);
analysisNotification.put(serverUUID, notify);
}
public abstract String getPluginsTabContent(UUID uuid);
@ -81,4 +81,10 @@ public abstract class InformationManager {
}
public abstract String getWebServerAddress();
public boolean isAuthRequired() {
return getWebServerAddress().startsWith("https");
}
public abstract void analysisReady(UUID serverUUID);
}

View File

@ -0,0 +1,39 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import java.util.Map;
import java.util.UUID;
/**
* @author Rsl1122
*/
public class AnalysisReadyWebAPI extends WebAPI {
@Override
public Response onRequest(IPlan plugin, Map<String, String> variables) {
String serverUUIDS = variables.get("serverUUID");
if (serverUUIDS == null) {
return badRequest("serverUUID was not present");
}
UUID serverUUID = UUID.fromString(serverUUIDS);
plugin.getInfoManager().analysisReady(serverUUID);
return success();
}
@Override
public void sendRequest(String address) throws WebAPIException {
throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead.");
}
public void sendRequest(String address, UUID serverUUID) throws WebAPIException {
addVariable("serverUUID", serverUUID.toString());
super.sendRequest(address);
}
}

View File

@ -5,18 +5,35 @@
package main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import java.util.Map;
import java.util.UUID;
/**
* @author Fuzzlemann
* @author Rsl1122
*/
public class AnalyzeWebAPI extends WebAPI {
@Override
public Response onRequest(IPlan plugin, Map<String, String> variables) {
plugin.getInfoManager().refreshAnalysis();
String serverUUIDS = variables.get("serverUUID");
if (serverUUIDS == null) {
return badRequest("serverUUID was not present");
}
UUID serverUUID = UUID.fromString(serverUUIDS);
plugin.getInfoManager().refreshAnalysis(serverUUID);
return success();
}
@Override
public void sendRequest(String address) throws WebAPIException {
throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead.");
}
public void sendRequest(String address, UUID serverUUID) throws WebAPIException {
addVariable("serverUUID", serverUUID.toString());
super.sendRequest(address);
}
}

View File

@ -117,7 +117,7 @@ public class Analysis {
log(Locale.get(Msg.ANALYSIS_3RD_PARTY).toString());
Log.debug("Analysis", "Analyzing additional data sources (3rd party)");
analysisData.setAdditionalDataReplaceMap(analyzeAdditionalPluginData(analysisData.getPlayerCountPart().getUuids()));
((BukkitInformationManager) infoManager).cacheAnalysisdata(analysisData);
((BukkitInformationManager) infoManager).cacheAnalysisData(analysisData);
long time = Benchmark.stop("Analysis", "Analysis");
Log.logDebug("Analysis", time);