CacheInspectPluginsTabRequest

This commit is contained in:
Rsl1122 2018-01-23 18:16:09 +02:00
parent 137b0f3100
commit f50d8dc90c
7 changed files with 228 additions and 80 deletions

View File

@ -1,11 +1,11 @@
package com.djrapitops.plan.data.plugin; package com.djrapitops.plan.data.plugin;
import com.djrapitops.plan.Plan; import com.djrapitops.plan.Plan;
import com.djrapitops.plan.data.element.InspectContainer;
import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import java.util.ArrayList; import java.util.*;
import java.util.List;
/** /**
* Class responsible for hooking to other plugins and managing the %plugins% * Class responsible for hooking to other plugins and managing the %plugins%
@ -35,6 +35,10 @@ public class HookHandler {
} }
} }
public static HookHandler getInstance() {
return null;// TODO
}
/** /**
* Adds a new PluginData source to the list. * Adds a new PluginData source to the list.
* <p> * <p>
@ -72,4 +76,23 @@ public class HookHandler {
public List<PluginData> getAdditionalDataSources() { public List<PluginData> getAdditionalDataSources() {
return additionalDataSources; return additionalDataSources;
} }
public Map<PluginData, InspectContainer> getInspectContainersFor(UUID uuid) {
List<PluginData> plugins = getAdditionalDataSources();
Map<PluginData, InspectContainer> containers = new HashMap<>();
for (PluginData pluginData : plugins) {
InspectContainer inspectContainer = new InspectContainer();
try {
InspectContainer container = pluginData.getPlayerData(uuid, inspectContainer);
if (container != null && !container.isEmpty()) {
containers.put(pluginData, container);
}
} catch (Exception e) {
String sourcePlugin = pluginData.getSourcePlugin();
Log.error("PluginData caused exception: " + sourcePlugin);
Log.toLog(this.getClass().getName() + " " + sourcePlugin, e);
}
}
return containers;
}
} }

View File

@ -11,7 +11,7 @@ import java.util.UUID;
/** /**
* Operations for transferring data via Database to another server. * Operations for transferring data via Database to another server.
* * <p>
* Receiving server has to be using the same database. * Receiving server has to be using the same database.
* *
* @author Rsl1122 * @author Rsl1122
@ -26,6 +26,8 @@ public interface TransferOperations {
void storeNetworkPageContent(UUID serverUUID, String encodedHtml) throws DBException; void storeNetworkPageContent(UUID serverUUID, String encodedHtml) throws DBException;
void storePlayerPluginsTab(UUID player, UUID serverUUID, String encodedHtml) throws DBException;
// Get // Get
Map<UUID, String> getEncodedPlayerHtml() throws DBException; Map<UUID, String> getEncodedPlayerHtml() throws DBException;
@ -35,4 +37,6 @@ public interface TransferOperations {
Map<UUID, String> getEncodedServerHtml() throws DBException; Map<UUID, String> getEncodedServerHtml() throws DBException;
UUID getServerPlayerIsOnline(UUID playerUUID) throws DBException; UUID getServerPlayerIsOnline(UUID playerUUID) throws DBException;
Map<UUID, String> getEncodedPlayerPluginsTabs(UUID playerUUID) throws DBException;
} }

View File

@ -0,0 +1,90 @@
/*
* 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 com.djrapitops.plan.system.info.request;
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
import com.djrapitops.plan.api.exceptions.connection.TransferDatabaseException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.pages.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.cache.PageId;
import com.djrapitops.plan.system.webserver.response.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.response.pages.parts.InspectPagePluginsContent;
import com.djrapitops.plan.utilities.Base64Util;
import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.utilities.Verify;
import java.util.Map;
import java.util.UUID;
/**
* InfoRequest used to place HTML of player's Plugins Tab to ResponseCache.
*
* @author Rsl1122
*/
public class CacheInspectPluginsTabRequest extends InfoRequestWithVariables implements CacheRequest {
private static final String SPLIT = ";;SPLIT;;";
private final UUID player;
private final String navAndHtml;
private CacheInspectPluginsTabRequest() {
player = null;
navAndHtml = null;
}
public CacheInspectPluginsTabRequest(UUID player, String nav, String html) {
Verify.nullCheck(player, nav);
variables.put("player", player.toString());
this.player = player;
this.navAndHtml = nav + SPLIT + html;
}
public static CacheInspectPluginsTabRequest createHandler() {
return new CacheInspectPluginsTabRequest();
}
@Override
public void placeDataToDatabase() throws WebException {
Verify.nullCheck(player, navAndHtml);
String encodedHtml = Base64Util.encode(navAndHtml);
try {
Database.getActive().transfer().storePlayerPluginsTab(player, ServerInfo.getServerUUID(), encodedHtml);
} catch (DBException e) {
throw new TransferDatabaseException(e);
}
}
@Override
public Response handleRequest(Map<String, String> variables) throws WebException {
// Available variables: sender, player
String player = variables.get("player");
NullCheck.check(player, new BadRequestException("Player UUID 'player' variable not supplied."));
UUID uuid = UUID.fromString(player);
try {
Map<UUID, String> pages = Database.getActive().transfer().getEncodedPlayerPluginsTabs(uuid);
InspectPagePluginsContent pluginsTab = (InspectPagePluginsContent)
ResponseCache.loadResponse(PageId.PLAYER_PLUGINS_TAB.of(uuid), InspectPagePluginsContent::new);
for (Map.Entry<UUID, String> entry : pages.entrySet()) {
UUID serverUUID = entry.getKey();
String[] html = Base64Util.decode(entry.getValue()).split(SPLIT);
pluginsTab.addTab(serverUUID, html[0], html[1]);
}
} catch (DBException e) {
throw new TransferDatabaseException(e);
}
return DefaultResponses.SUCCESS.get();
}
}

View File

@ -0,0 +1,72 @@
/*
* 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 com.djrapitops.plan.system.info.request;
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.webserver.pages.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.cache.PageId;
import com.djrapitops.plan.system.webserver.response.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.response.pages.parts.InspectPagePluginsContent;
import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.utilities.Verify;
import java.util.Map;
import java.util.UUID;
/**
* InfoRequest for Generating Inspect page plugins tab on receiving WebServer.
*
* @author Rsl1122
*/
public class GenerateInspectPluginsTabRequest extends InfoRequestWithVariables implements GenerateRequest {
private final UUID playerUUID;
private GenerateInspectPluginsTabRequest() {
playerUUID = null;
}
public GenerateInspectPluginsTabRequest(UUID uuid) {
Verify.nullCheck(uuid);
playerUUID = uuid;
variables.put("player", uuid.toString());
}
public static GenerateInspectPluginsTabRequest createHandler() {
return new GenerateInspectPluginsTabRequest();
}
@Override
public void placeDataToDatabase() {
// No data required in a Generate request
}
@Override
public Response handleRequest(Map<String, String> variables) throws WebException {
// Available variables: sender, player
String player = variables.get("player");
NullCheck.check(player, new BadRequestException("Player UUID 'player' variable not supplied."));
UUID uuid = UUID.fromString(player);
String[] navAndhtml = getNavAndHtml(uuid);
InfoSystem.getInstance().sendRequest(new CacheInspectPluginsTabRequest(uuid, navAndhtml[0], navAndhtml[1]));
return DefaultResponses.SUCCESS.get();
}
private String[] getNavAndHtml(UUID uuid) {
return ((InspectPagePluginsContent) ResponseCache.loadResponse(PageId.PLAYER_PLUGINS_TAB.of(uuid),
InspectPagePluginsContent::new)).getContents();
}
public UUID getPlayerUUID() {
return playerUUID;
}
}

View File

@ -4,8 +4,14 @@
*/ */
package com.djrapitops.plan.system.webserver.response.pages.parts; package com.djrapitops.plan.system.webserver.response.pages.parts;
import com.djrapitops.plan.data.element.InspectContainer;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.utilities.comparators.PluginDataNameComparator;
import com.djrapitops.plan.utilities.html.HtmlStructure; import com.djrapitops.plan.utilities.html.HtmlStructure;
import com.djrapitops.plan.utilities.html.structure.AnalysisPluginsTabContentCreator;
import java.util.*; import java.util.*;
@ -26,11 +32,37 @@ public class InspectPagePluginsContent extends Response {
} }
public InspectPagePluginsContent(UUID serverUUID, String nav, String html) { public InspectPagePluginsContent(UUID serverUUID, String nav, String html) {
pluginsTab = new HashMap<>(); this();
addTab(serverUUID, nav, html); addTab(serverUUID, nav, html);
} }
public static InspectPagePluginsContent generateForThisServer(UUID uuid) {
HookHandler hookHandler = HookHandler.getInstance();
Map<PluginData, InspectContainer> containers = hookHandler.getInspectContainersFor(uuid);
String serverName = ServerInfo.getServerName();
String actualServerName = serverName.equals("Plan") ? "Server " + ServerInfo.getServerID() : serverName;
if (containers.isEmpty()) {
new InspectPagePluginsContent(uuid, "<li><a>" + actualServerName + "(No Data)</a></li>", "");
}
String nav = "<li><a class=\"nav-button\" href=\"javascript:void(0)\">" + actualServerName + "</a></li>";
StringBuilder tab = new StringBuilder();
tab.append("<div class=\"tab\"><div class=\"row clearfix\">");
List<PluginData> order = new ArrayList<>(containers.keySet());
order.sort(new PluginDataNameComparator());
for (PluginData pluginData : order) {
InspectContainer container = containers.get(pluginData);
AnalysisPluginsTabContentCreator.appendThird(pluginData, container, tab);
}
tab.append("</div></div>");
return new InspectPagePluginsContent(ServerInfo.getServerUUID(), nav, tab.toString());
}
public void addTab(UUID serverUUID, String nav, String html) { public void addTab(UUID serverUUID, String nav, String html) {
pluginsTab.put(serverUUID, new String[]{nav, html}); pluginsTab.put(serverUUID, new String[]{nav, html});
} }

View File

@ -12,9 +12,6 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.connection.WebFailException; import com.djrapitops.plan.api.exceptions.connection.WebFailException;
import com.djrapitops.plan.command.commands.AnalyzeCommand; import com.djrapitops.plan.command.commands.AnalyzeCommand;
import com.djrapitops.plan.data.AnalysisData; import com.djrapitops.plan.data.AnalysisData;
import com.djrapitops.plan.data.element.InspectContainer;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.settings.theme.Theme; import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.system.cache.DataCache; import com.djrapitops.plan.system.cache.DataCache;
import com.djrapitops.plan.system.processing.processors.Processor; import com.djrapitops.plan.system.processing.processors.Processor;
@ -31,6 +28,7 @@ import com.djrapitops.plan.system.webserver.response.errors.InternalErrorRespons
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse; import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
import com.djrapitops.plan.system.webserver.response.pages.AnalysisPageResponse; import com.djrapitops.plan.system.webserver.response.pages.AnalysisPageResponse;
import com.djrapitops.plan.system.webserver.response.pages.InspectPageResponse; import com.djrapitops.plan.system.webserver.response.pages.InspectPageResponse;
import com.djrapitops.plan.system.webserver.response.pages.parts.InspectPagePluginsContent;
import com.djrapitops.plan.system.webserver.webapi.WebAPIManager; import com.djrapitops.plan.system.webserver.webapi.WebAPIManager;
import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalysisReadyWebAPI; import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalysisReadyWebAPI;
import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalyzeWebAPI; import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalyzeWebAPI;
@ -40,7 +38,6 @@ import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plan.utilities.analysis.Analysis; import com.djrapitops.plan.utilities.analysis.Analysis;
import com.djrapitops.plan.utilities.file.export.HtmlExport; import com.djrapitops.plan.utilities.file.export.HtmlExport;
import com.djrapitops.plan.utilities.html.HtmlStructure; import com.djrapitops.plan.utilities.html.HtmlStructure;
import com.djrapitops.plan.utilities.html.structure.InspectPluginsTabContentCreator;
import com.djrapitops.plugin.api.utility.log.ErrorLogger; import com.djrapitops.plugin.api.utility.log.ErrorLogger;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
@ -162,24 +159,7 @@ public class BukkitInformationManager extends InformationManager {
cacheInspectPluginsTab(uuid, origin); cacheInspectPluginsTab(uuid, origin);
} }
} else { } else {
HookHandler hookHandler = plugin.getHookHandler(); cacheInspectPluginsTab(uuid, InspectPagePluginsContent.generateForThisServer(uuid).getContents());
List<PluginData> plugins = hookHandler.getAdditionalDataSources();
Map<PluginData, InspectContainer> containers = new HashMap<>();
for (PluginData pluginData : plugins) {
InspectContainer inspectContainer = new InspectContainer();
try {
InspectContainer container = pluginData.getPlayerData(uuid, inspectContainer);
if (container != null && !container.isEmpty()) {
containers.put(pluginData, container);
}
} catch (Exception e) {
String sourcePlugin = pluginData.getSourcePlugin();
Log.error("PluginData caused exception: " + sourcePlugin);
Log.toLog(this.getClass().getName() + " " + sourcePlugin, e);
}
}
cacheInspectPluginsTab(uuid, InspectPluginsTabContentCreator.createContent(containers));
} }
} }

View File

@ -1,53 +0,0 @@
/*
* 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 com.djrapitops.plan.utilities.html.structure;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.data.element.InspectContainer;
import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.system.info.server.BukkitServerInfo;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.utilities.comparators.PluginDataNameComparator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* HTML utility class for parsing HTML for Inspect page plugins tabs.
*
* @author Rsl1122
*/
public class InspectPluginsTabContentCreator {
public static String[] createContent(Map<PluginData, InspectContainer> containers) {
BukkitServerInfo serverInfoManager = Plan.getInstance().getServerInfoManager();
String serverName = ServerInfo.getServerName();
String actualServerName = serverName.equals("Plan") ? "Server " + ServerInfo.getServerID() : serverName;
if (containers.isEmpty()) {
return new String[]{"<li><a>" + actualServerName + "(No Data)</a></li>",
""};
}
String nav = "<li><a class=\"nav-button\" href=\"javascript:void(0)\">" + actualServerName + "</a></li>";
StringBuilder tab = new StringBuilder();
tab.append("<div class=\"tab\"><div class=\"row clearfix\">");
List<PluginData> order = new ArrayList<>(containers.keySet());
order.sort(new PluginDataNameComparator());
for (PluginData pluginData : order) {
InspectContainer container = containers.get(pluginData);
AnalysisPluginsTabContentCreator.appendThird(pluginData, container, tab);
}
tab.append("</div></div>");
return new String[]{nav, tab.toString()};
}
}