Inspect Page Plugins Tab Structure parsing

This commit is contained in:
Rsl1122 2017-11-27 13:55:02 +02:00
parent b012b09469
commit ae6f3f8de7
11 changed files with 165 additions and 54 deletions

View File

@ -12,6 +12,7 @@ import main.java.com.djrapitops.plan.api.exceptions.*;
import main.java.com.djrapitops.plan.command.commands.AnalyzeCommand;
import main.java.com.djrapitops.plan.data.AnalysisData;
import main.java.com.djrapitops.plan.data.additional.HookHandler;
import main.java.com.djrapitops.plan.data.additional.InspectContainer;
import main.java.com.djrapitops.plan.data.additional.PluginData;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.info.parsing.AnalysisPageParser;
@ -30,6 +31,7 @@ import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebA
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.analysis.Analysis;
import main.java.com.djrapitops.plan.utilities.html.HtmlStructure;
import main.java.com.djrapitops.plan.utilities.html.structure.InspectPluginsTabContentCreator;
import java.io.IOException;
import java.sql.SQLException;
@ -52,7 +54,7 @@ public class BukkitInformationManager extends InformationManager {
private String analysisPluginsTab;
private Long refreshDate;
private final Map<UUID, String> pluginsTabContents;
private final Map<UUID, String[]> pluginsTabContents;
public BukkitInformationManager(Plan plugin) {
this.plugin = plugin;
@ -93,6 +95,10 @@ public class BukkitInformationManager extends InformationManager {
@Override
public void cachePlayer(UUID uuid) {
if (uuid == null) {
Log.debug("BukkitInformationManager.cachePlayer: UUID was null");
return;
}
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid));
@ -131,14 +137,26 @@ public class BukkitInformationManager extends InformationManager {
String serverName = plugin.getServerInfoManager().getServerName();
HookHandler hookHandler = plugin.getHookHandler();
List<PluginData> plugins = hookHandler.getAdditionalDataSources();
// TODO Inspect Plugins Tab
// Map<String, Serializable> replaceMap = hookHandler.getAdditionalInspectReplaceRules(uuid);
// String contents = HtmlStructure.createInspectPluginsTabContent(serverName, plugins, replaceMap);
cacheInspectPluginsTab(uuid, "");
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);
}
}
public void cacheInspectPluginsTab(UUID uuid, String contents) {
cacheInspectPluginsTab(uuid, InspectPluginsTabContentCreator.createContent(containers));
}
}
public void cacheInspectPluginsTab(UUID uuid, String[] contents) {
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents);
@ -158,8 +176,8 @@ public class BukkitInformationManager extends InformationManager {
}
@Override
public String getPluginsTabContent(UUID uuid) {
String calculating = HtmlStructure.createInspectPageTabContentCalculating();
public String[] getPluginsTabContent(UUID uuid) {
String[] calculating = HtmlStructure.createInspectPageTabContentCalculating();
return pluginsTabContents.getOrDefault(uuid, calculating);
}

View File

@ -43,7 +43,7 @@ public class BungeeInformationManager extends InformationManager {
private Map<UUID, ServerInfo> bukkitServers;
private final Map<UUID, String> networkPageContent;
private final Map<UUID, Map<UUID, String>> pluginsTabContent;
private final Map<UUID, Map<UUID, String[]>> pluginsTabContent;
private final BungeeServerInfoManager serverInfoManager;
public BungeeInformationManager(PlanBungee plugin) throws SQLException {
@ -246,10 +246,10 @@ public class BungeeInformationManager extends InformationManager {
*/
@Override
public String getPlayerHtml(UUID uuid) {
Response response = PageCache.loadPage("inspectPage:" + uuid,
Response response = PageCache.copyPage("inspectPage:" + uuid,
() -> new NotFoundResponse("No Bukkit Servers were online to process this request"));
if (response instanceof InspectPageResponse) {
((InspectPageResponse) response).setInspectPagePluginsTab(pluginsTabContent.get(uuid));
((InspectPageResponse) response).setInspectPagePluginsTab(getPluginsTabContent(uuid));
}
return response.getContent();
}
@ -275,17 +275,28 @@ public class BungeeInformationManager extends InformationManager {
* @return Html string.
*/
@Override
public String getPluginsTabContent(UUID uuid) {
Map<UUID, String> pluginsTab = pluginsTabContent.get(uuid);
public String[] getPluginsTabContent(UUID uuid) {
Map<UUID, String[]> pluginsTab = pluginsTabContent.get(uuid);
if (pluginsTab == null) {
return HtmlStructure.createInspectPageTabContentCalculating();
}
StringBuilder builder = new StringBuilder();
for (String tab : pluginsTab.values()) {
builder.append(tab);
List<String[]> order = new ArrayList<>(pluginsTab.values());
// Sort serverNames alphabetically
order.sort(new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareTo(o2[1]);
}
return builder.toString();
});
StringBuilder nav = new StringBuilder();
StringBuilder tabs = new StringBuilder();
for (String[] tab : order) {
nav.append(tab[0]);
tabs.append(tab[1]);
}
return new String[]{nav.toString(), tabs.toString()};
}
/**
@ -295,8 +306,8 @@ public class BungeeInformationManager extends InformationManager {
* @param uuid UUID of the player
* @param html Plugins tab html for the player on the server
*/
public void cachePluginsTabContent(UUID serverUUID, UUID uuid, String html) {
Map<UUID, String> perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>());
public void cachePluginsTabContent(UUID serverUUID, UUID uuid, String[] html) {
Map<UUID, String[]> perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>());
perServerPluginsTab.put(serverUUID, html);
pluginsTabContent.put(uuid, perServerPluginsTab);
Response inspectResponse = PageCache.loadPage("inspectPage: " + uuid);

View File

@ -70,7 +70,7 @@ public abstract class InformationManager {
analysisNotification.put(serverUUID, notify);
}
public abstract String getPluginsTabContent(UUID uuid);
public abstract String[] getPluginsTabContent(UUID uuid);
public boolean isUsingAnotherWebServer() {
return usingAnotherWebServer;

View File

@ -52,6 +52,9 @@ public class InspectPageParser extends PageParser {
public String parse() throws ParseException {
try {
if (uuid == null) {
throw new IllegalStateException("UUID was null!");
}
Log.logDebug("Database", "Inspect Parse Fetch");
Benchmark.start("Inspect Parse, Fetch");
Database db = plugin.getDB();
@ -65,6 +68,9 @@ public class InspectPageParser extends PageParser {
addValue("timeZone", MiscUtils.getTimeZoneOffsetHours());
PlayerProfile profile = db.getPlayerProfile(uuid);
if (profile == null) {
throw new IllegalStateException("Player profile was null!");
}
String online = "Offline";
Optional<Session> activeSession = plugin.getInfoManager().getDataCache().getCachedSession(uuid);

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.systems.webserver;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import main.java.com.djrapitops.plan.systems.webserver.response.InspectPageResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import java.util.Map;
@ -67,6 +68,22 @@ public class PageCache {
return pageCache.getIfPresent(identifier);
}
/**
* Returns a copy some responses
*
* Currently supported copyable responses: InspectPageResponse
*
* @param identifier The identifier of the page
* @return Copied Response of loadPage, so that cache contents are not changed.
*/
public static Response copyPage(String identifier, PageLoader loader) {
Response response = loadPage(identifier, loader);
if (response instanceof InspectPageResponse) {
return InspectPageResponse.copyOf((InspectPageResponse) response);
}
return response;
}
/**
* Puts the page into the page cache.
* <p>

View File

@ -2,7 +2,9 @@ package main.java.com.djrapitops.plan.systems.webserver.response;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.webserver.theme.Theme;
import org.apache.commons.lang3.text.StrSubstitutor;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -12,30 +14,37 @@ import java.util.UUID;
*/
public class InspectPageResponse extends Response {
private String inspectPagePluginsTab;
private final UUID uuid;
public InspectPageResponse(InformationManager infoManager, UUID uuid) {
this.uuid = uuid;
super.setHeader("HTTP/1.1 200 OK");
super.setContent(infoManager.getPlayerHtml(uuid));
setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid));
}
public InspectPageResponse(InformationManager infoManager, UUID uuid, String html) {
this.uuid = uuid;
super.setHeader("HTTP/1.1 200 OK");
super.setContent(Theme.replaceColors(html));
setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid));
}
public void setInspectPagePluginsTab(String inspectPagePluginsTab) {
if (this.inspectPagePluginsTab != null) {
setContent(getContent().replace(this.inspectPagePluginsTab, inspectPagePluginsTab));
} else {
setContent(getContent().replace("${tabContentPlugins}", inspectPagePluginsTab));
}
this.inspectPagePluginsTab = inspectPagePluginsTab;
private InspectPageResponse(InspectPageResponse response) {
this.uuid = response.uuid;
super.setHeader(response.getHeader());
super.setContent(response.getContent());
}
public void setInspectPagePluginsTab(Map<UUID, String> uuidStringMap) {
// TODO
public void setInspectPagePluginsTab(String[] inspectPagePluginsTab) {
Map<String, String> replaceMap = new HashMap<>();
replaceMap.put("navPluginsTabs", inspectPagePluginsTab[0]);
replaceMap.put("pluginsTabs", inspectPagePluginsTab[1]);
setContent(StrSubstitutor.replace(getContent(), replaceMap));
}
public static InspectPageResponse copyOf(InspectPageResponse response) {
return new InspectPageResponse(response);
}
}

View File

@ -31,6 +31,10 @@ public abstract class Response {
this.type = ResponseType.HTML.get();
}
protected String getHeader() {
return header;
}
public String getResponse() {
return header + "\r\n"
+ "Content-Type: " + type + ";\r\n"

View File

@ -33,9 +33,17 @@ public class PostInspectPluginsTabWebAPI extends WebAPI {
UUID uuid = UUID.fromString(uuidS);
UUID serverUUID = UUID.fromString(variables.get("sender"));
String nav = variables.get("nav");
if (nav == null) {
return badRequest("nav not included");
}
String html = variables.get("html");
if (html == null) {
return badRequest("html not included");
}
String[] content = new String[]{nav, html};
((BungeeInformationManager) plugin.getInfoManager()).cachePluginsTabContent(serverUUID, uuid, html);
((BungeeInformationManager) plugin.getInfoManager()).cachePluginsTabContent(serverUUID, uuid, content);
return success();
}
@ -45,9 +53,10 @@ public class PostInspectPluginsTabWebAPI extends WebAPI {
throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead.");
}
public void sendPluginsTab(String address, UUID uuid, String html) throws WebAPIException {
public void sendPluginsTab(String address, UUID uuid, String[] html) throws WebAPIException {
addVariable("uuid", uuid.toString());
addVariable("html", html);
addVariable("nav", html[0]);
addVariable("html", html[1]);
super.sendRequest(address);
}
}

View File

@ -13,7 +13,6 @@ import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager;
import main.java.com.djrapitops.plan.utilities.FormatUtils;
import main.java.com.djrapitops.plan.utilities.analysis.AnalysisUtils;
import main.java.com.djrapitops.plan.utilities.html.graphs.PlayerActivityGraphCreator;
import main.java.com.djrapitops.plan.utilities.html.structure.SessionTabStructureCreator;
import main.java.com.djrapitops.plan.utilities.html.tables.SessionsTableCreator;
@ -72,26 +71,17 @@ public class HtmlStructure {
}
}
private static String[] getSessionsAsTable(Map<String, List<Session>> sessions, List<Session> allSessions, UUID uuid) {
Map<Integer, UUID> uuidByID = new HashMap<>();
for (List<Session> sessionList : sessions.values()) {
for (Session session : sessionList) {
uuidByID.put(session.getSessionID(), uuid);
}
}
return new String[]{/*Html.TABLE_SESSIONS.parse(SessionsTableCreator.createTable(uuidByID, allSessions)[0]),*/"", ""};
}
public static String createInspectPageTabContentCalculating() {
return "<div class=\"row\">" +
"<div class=\"column\">" +
"<div class=\"box-header\">" +
"<h2><i class=\"fa fa-cube\" aria-hidden=\"true\"></i> No Plugins</h2></div>" +
"<div class=\"box plugin\">" +
"<p><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> Plugins tab is still being calculated, please refresh the page after a while (F5)</p>" +
public static String[] createInspectPageTabContentCalculating() {
String tab = "<div class=\"tab\">" +
"<div class=\"row clearfix\">" +
"<div class=\"col-lg-12 col-md-12 col-sm-12 col-xs-12\">" +
"<div class=\"card\">" +
"<div class=\"header\"><h2><i class=\"fa fa-users\"></i> Plugin Data</h2></div>" +
"<div class=\"body\">" +
"<p><i class=\"fa fa-spin fa-refresh\"></i> Calculating Plugins tab, refresh (F5) shortly..</p>" +
"</div></div>" +
"</div>";
"</div></div></div>";
return new String[]{"<li><a>Calculating... Refresh shortly</a></li>", tab};
}
public static String createNetworkPageContent(Map<UUID, String> networkPageContents) {

View File

@ -5,6 +5,7 @@
package main.java.com.djrapitops.plan.utilities.html.structure;
import main.java.com.djrapitops.plan.data.additional.AnalysisContainer;
import main.java.com.djrapitops.plan.data.additional.InspectContainer;
import main.java.com.djrapitops.plan.data.additional.PluginData;
import main.java.com.djrapitops.plan.utilities.analysis.Analysis;
import main.java.com.djrapitops.plan.utilities.comparators.PluginDataNameComparator;
@ -85,7 +86,7 @@ public class AnalysisPluginsTabContentCreator {
};
}
private static void appendThird(PluginData pluginData, AnalysisContainer container, StringBuilder generalTab) {
public static void appendThird(PluginData pluginData, InspectContainer container, StringBuilder generalTab) {
generalTab.append("<div class=\"col-xs-12 col-sm-12 col-md-4 col-lg-4\">" +
"<div class=\"card\">" +
"<div class=\"header\">" +

View File

@ -0,0 +1,46 @@
/*
* 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.utilities.html.structure;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.additional.InspectContainer;
import main.java.com.djrapitops.plan.data.additional.PluginData;
import main.java.com.djrapitops.plan.systems.info.server.BukkitServerInfoManager;
import main.java.com.djrapitops.plan.utilities.comparators.PluginDataNameComparator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class InspectPluginsTabContentCreator {
public static String[] createContent(Map<PluginData, InspectContainer> containers) {
BukkitServerInfoManager serverInfoManager = Plan.getInstance().getServerInfoManager();
String serverName = serverInfoManager.getServerName();
String actualServerName = serverName.equals("Plan") ? "Server " + serverInfoManager.getServerID() : serverName;
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()};
}
}