GenerateAnalysisPageRequest now runs analysis properly. In addition:

- Laid groundwork for running analysis of offline servers
- Changed InfoRequest request address to /info/ from /api/ to reduce confusion.
This commit is contained in:
Rsl1122 2018-01-24 11:48:23 +02:00
parent 4e959c325b
commit 4d76671bc8
10 changed files with 91 additions and 119 deletions

View File

@ -89,8 +89,9 @@ public class Plan extends BukkitPlugin implements PlanPlugin {
return (Plan) StaticHolder.getInstance(Plan.class); return (Plan) StaticHolder.getInstance(Plan.class);
} }
@Deprecated
public static UUID getServerUUID() { public static UUID getServerUUID() {
return getInstance().getServerUuid(); return ServerInfo.getServerUUID();
} }
public UUID getServerUuid() { public UUID getServerUuid() {

View File

@ -13,4 +13,8 @@ public class InternalErrorException extends WebFailException {
public InternalErrorException() { public InternalErrorException() {
super("Internal Error occurred on receiving server"); super("Internal Error occurred on receiving server");
} }
public InternalErrorException(String message, Throwable cause) {
super(message, cause);
}
} }

View File

@ -67,7 +67,7 @@ public class ConnectionOut {
public void sendRequest() throws WebException { public void sendRequest() throws WebException {
String address = toServer.getWebAddress(); String address = toServer.getWebAddress();
try { try {
URL url = new URL(address + "/api/" + this.getClass().getSimpleName().toLowerCase()); URL url = new URL(address + "/info/" + this.getClass().getSimpleName().toLowerCase());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (address.startsWith("https")) { if (address.startsWith("https")) {
HttpsURLConnection httpsConn = (HttpsURLConnection) connection; HttpsURLConnection httpsConn = (HttpsURLConnection) connection;

View File

@ -5,12 +5,20 @@
package com.djrapitops.plan.system.info.request; package com.djrapitops.plan.system.info.request;
import com.djrapitops.plan.api.exceptions.connection.BadRequestException; import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
import com.djrapitops.plan.api.exceptions.connection.InternalErrorException;
import com.djrapitops.plan.api.exceptions.connection.WebException; import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.data.AnalysisData;
import com.djrapitops.plan.system.cache.DataCache;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.InfoSystem; import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.pages.DefaultResponses; import com.djrapitops.plan.system.webserver.pages.DefaultResponses;
import com.djrapitops.plan.system.webserver.pages.parsing.AnalysisPage;
import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.pages.AnalysisPageResponse;
import com.djrapitops.plan.utilities.NullCheck; import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plan.utilities.analysis.Analysis;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import java.util.Map; import java.util.Map;
@ -51,16 +59,26 @@ public class GenerateAnalysisPageRequest extends InfoRequestWithVariables implem
if (!ServerInfo.getServerUUID().equals(serverUUID)) { if (!ServerInfo.getServerUUID().equals(serverUUID)) {
throw new BadRequestException("Requested Analysis page from wrong server."); throw new BadRequestException("Requested Analysis page from wrong server.");
} }
String html = getHtml();
InfoSystem.getInstance().sendRequest(new CacheAnalysisPageRequest(serverUUID, html)); InfoSystem infoSystem = InfoSystem.getInstance();
infoSystem.sendRequest(new CacheAnalysisPageRequest(serverUUID, AnalysisPageResponse.getRefreshingHtml()));
infoSystem.sendRequest(new CacheAnalysisPageRequest(serverUUID, analyseAndGetHtml()));
return DefaultResponses.SUCCESS.get(); return DefaultResponses.SUCCESS.get();
} }
public String getHtml() { public String analyseAndGetHtml() throws InternalErrorException {
// TODO Perform Analysis & get HTML try {
return null; UUID serverUUID = ServerInfo.getServerUUID();
Database db = Database.getActive();
DataCache dataCache = DataCache.getInstance();
AnalysisData analysisData = Analysis.runAnalysisFor(serverUUID, db, dataCache);
return new AnalysisPage(analysisData).toHtml();
} catch (Exception e) {
Log.toLog(Analysis.class, e);
throw new InternalErrorException("Analysis failed due to exception", e);
}
} }
public static GenerateAnalysisPageRequest createHandler() { public static GenerateAnalysisPageRequest createHandler() {

View File

@ -10,7 +10,7 @@ import com.djrapitops.plan.system.webserver.response.Response;
import java.util.Map; import java.util.Map;
/** /**
* //TODO Class Javadoc Comment * Represents a request that Plan servers can send each other.
* *
* @author Rsl1122 * @author Rsl1122
*/ */

View File

@ -50,7 +50,7 @@ public class ResponseHandler extends TreePageHandler {
} }
public void registerWebAPIPages() { public void registerWebAPIPages() {
registerPage("api", new InfoRequestPageHandler()); registerPage("info", new InfoRequestPageHandler());
// TODO Remove redundant comment after implementing replacements // TODO Remove redundant comment after implementing replacements
// private void registerWebAPIs() { // private void registerWebAPIs() {
@ -93,6 +93,12 @@ public class ResponseHandler extends TreePageHandler {
return new BadRequestResponse(e.getMessage()); return new BadRequestResponse(e.getMessage());
} catch (UnauthorizedServerException e) { } catch (UnauthorizedServerException e) {
return new UnauthorizedServerResponse(e.getMessage()); return new UnauthorizedServerResponse(e.getMessage());
} catch (InternalErrorException e) {
if (e.getCause() != null) {
return new InternalErrorResponse(request.getTarget(), e.getCause());
} else {
return new InternalErrorResponse(request.getTarget(), e);
}
} catch (Exception e) { } catch (Exception e) {
Log.toLog(this.getClass().getName(), e); Log.toLog(this.getClass().getName(), e);
return new InternalErrorResponse(request.getTarget(), e); return new InternalErrorResponse(request.getTarget(), e);

View File

@ -4,14 +4,10 @@
*/ */
package com.djrapitops.plan.system.webserver.pages.parsing; package com.djrapitops.plan.system.webserver.pages.parsing;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.PlanBungee;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.api.exceptions.ParseException; import com.djrapitops.plan.api.exceptions.ParseException;
import com.djrapitops.plan.data.AnalysisData; import com.djrapitops.plan.data.AnalysisData;
import com.djrapitops.plan.utilities.file.FileUtil; import com.djrapitops.plan.utilities.file.FileUtil;
import com.djrapitops.plan.utilities.html.HtmlUtils; import com.djrapitops.plan.utilities.html.HtmlUtils;
import com.djrapitops.plugin.api.Check;
import java.io.IOException; import java.io.IOException;
@ -23,11 +19,9 @@ import java.io.IOException;
public class AnalysisPage extends Page { public class AnalysisPage extends Page {
private final AnalysisData data; private final AnalysisData data;
private final PlanPlugin plugin;
public AnalysisPage(AnalysisData analysisData, PlanPlugin plugin) { public AnalysisPage(AnalysisData analysisData) {
this.data = analysisData; this.data = analysisData;
this.plugin = plugin;
} }
@Override @Override
@ -40,11 +34,4 @@ public class AnalysisPage extends Page {
throw new ParseException(e); throw new ParseException(e);
} }
} }
private int getPlayersOnline() {
if (Check.isBukkitAvailable()) {
return ((Plan) plugin).getServer().getOnlinePlayers().size();
}
return ((PlanBungee) plugin).getProxy().getOnlineCount();
}
} }

View File

@ -50,4 +50,13 @@ public class AnalysisPageResponse extends Response {
super.setHeader("HTTP/1.1 200 OK"); super.setHeader("HTTP/1.1 200 OK");
super.setContent(html); super.setContent(html);
} }
public static String getRefreshingHtml() {
ErrorResponse refreshPage = new ErrorResponse();
refreshPage.setTitle("Analysis is being refreshed..");
refreshPage.setParagraph("<meta http-equiv=\"refresh\" content=\"25\" /><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> Analysis is being run, refresh the page after a few seconds.. (F5)");
refreshPage.replacePlaceholders();
return refreshPage.getContent();
}
} }

View File

@ -70,7 +70,7 @@ public class BukkitInformationManager extends InformationManager {
@Override @Override
public void refreshAnalysis(UUID serverUUID) { public void refreshAnalysis(UUID serverUUID) {
if (Plan.getServerUUID().equals(serverUUID)) { if (Plan.getServerUUID().equals(serverUUID)) {
analysis.runAnalysis(this); analysis.runAnalysis();
} else if (usingAnotherWebServer) { } else if (usingAnotherWebServer) {
try { try {
getWebAPI().getAPI(AnalyzeWebAPI.class).sendRequest(webServerAddress, serverUUID); getWebAPI().getAPI(AnalyzeWebAPI.class).sendRequest(webServerAddress, serverUUID);
@ -97,7 +97,7 @@ public class BukkitInformationManager extends InformationManager {
@Override @Override
public String getAnalysisHtml() { public String getAnalysisHtml() {
if (analysisData == null) { if (analysisData == null) {
analysis.runAnalysis(this); analysis.runAnalysis();
ErrorResponse analysisRefreshPage = new ErrorResponse(); ErrorResponse analysisRefreshPage = new ErrorResponse();
analysisRefreshPage.setTitle("Analysis is being refreshed.."); analysisRefreshPage.setTitle("Analysis is being refreshed..");
analysisRefreshPage.setParagraph("<meta http-equiv=\"refresh\" content=\"25\" /><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> Analysis is being run, refresh the page after a few seconds.. (F5)"); analysisRefreshPage.setParagraph("<meta http-equiv=\"refresh\" content=\"25\" /><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> Analysis is being run, refresh the page after a few seconds.. (F5)");
@ -134,6 +134,7 @@ public class BukkitInformationManager extends InformationManager {
cacheAnalysisHtml(getAnalysisHtml()); cacheAnalysisHtml(getAnalysisHtml());
} }
@Deprecated
public void cacheAnalysisHtml(String html) { public void cacheAnalysisHtml(String html) {
if (usingAnotherWebServer) { if (usingAnotherWebServer) {
try { try {

View File

@ -1,29 +1,26 @@
package com.djrapitops.plan.utilities.analysis; package com.djrapitops.plan.utilities.analysis;
import com.djrapitops.plan.Plan; import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.data.AnalysisData; import com.djrapitops.plan.data.AnalysisData;
import com.djrapitops.plan.data.PlayerProfile; import com.djrapitops.plan.data.PlayerProfile;
import com.djrapitops.plan.data.ServerProfile; import com.djrapitops.plan.data.ServerProfile;
import com.djrapitops.plan.data.element.AnalysisContainer; import com.djrapitops.plan.data.element.AnalysisContainer;
import com.djrapitops.plan.data.plugin.BanData; import com.djrapitops.plan.data.plugin.BanData;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.data.plugin.PluginData; import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.Msg; import com.djrapitops.plan.settings.locale.Msg;
import com.djrapitops.plan.system.cache.DataCache; import com.djrapitops.plan.system.cache.DataCache;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.tasks.BukkitTaskSystem; import com.djrapitops.plan.system.tasks.BukkitTaskSystem;
import com.djrapitops.plan.system.tasks.TaskSystem; import com.djrapitops.plan.system.tasks.TaskSystem;
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plan.system.webserver.response.errors.InternalErrorResponse;
import com.djrapitops.plan.systems.info.BukkitInformationManager;
import com.djrapitops.plan.systems.info.InformationManager;
import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.api.Benchmark; import com.djrapitops.plugin.api.Benchmark;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -33,22 +30,27 @@ import java.util.stream.Collectors;
*/ */
public class Analysis { public class Analysis {
private static ServerProfile serverProfile; private static Long refreshDate;
private final Plan plugin; private final UUID serverUUID;
private int taskId = -1; private final Database database;
public static Optional<Long> getRefreshDate() { private static ServerProfile serverProfile;
// TODO private final DataCache dataCache;
return Optional.empty(); private boolean analysingThisServer;
private Analysis(UUID serverUUID, Database database, DataCache dataCache) {
this.serverUUID = serverUUID;
analysingThisServer = ServerInfo.getServerUUID().equals(serverUUID);
this.database = database;
this.dataCache = dataCache;
} }
/** public static Optional<Long> getRefreshDate() {
* Class Constructor. return Optional.ofNullable(refreshDate);
* }
* @param plugin Current instance of Plan
*/ public static AnalysisData runAnalysisFor(UUID serverUUID, Database database, DataCache dataCache) throws Exception {
public Analysis(Plan plugin) { return new Analysis(serverUUID, database, dataCache).runAnalysis();
this.plugin = plugin;
} }
/** /**
@ -60,68 +62,19 @@ public class Analysis {
return serverProfile; return serverProfile;
} }
/** private AnalysisData runAnalysis() throws Exception {
* Analyzes the data of all offline players on the server.
*
* @param infoManager InformationManager of the plugin.
*/
public void runAnalysis(InformationManager infoManager) {
if (isAnalysisBeingRun()) {
return;
}
((BukkitTaskSystem) TaskSystem.getInstance()).cancelBootAnalysis(); ((BukkitTaskSystem) TaskSystem.getInstance()).cancelBootAnalysis();
Benchmark.start("Analysis"); Benchmark.start("Analysis");
log(Locale.get(Msg.ANALYSIS_START).toString()); log(Locale.get(Msg.ANALYSIS_START).toString());
// Async task for Analysis return analyze();
RunnableFactory.createNew(new AbsRunnable("AnalysisTask") {
@Override
public void run() {
try {
ErrorResponse analysisRefreshPage = new ErrorResponse();
analysisRefreshPage.setTitle("Analysis is being refreshed..");
analysisRefreshPage.setParagraph("<meta http-equiv=\"refresh\" content=\"25\" /><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> 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());
} catch (Exception e) {
Log.toLog(this.getClass().getName() + ":" + this.getTaskName(), e);
} finally {
taskId = -1;
this.cancel();
}
}
}).runTaskAsynchronously();
} }
/** private AnalysisData analyze() throws Exception {
* Caches analyzed data of db to the provided cache analysisCache.
*
* @param infoManager InformationManager of the plugin.
* method.
* @param db Database which data will be analyzed.
* @return Whether or not analysis was successful.
*/
public boolean analyze(InformationManager infoManager, Database db) {
log(Locale.get(Msg.ANALYSIS_FETCH).toString()); log(Locale.get(Msg.ANALYSIS_FETCH).toString());
Benchmark.start("Fetch Phase"); Benchmark.start("Fetch Phase");
Log.logDebug("Database", "Analysis Fetch"); Log.logDebug("Database", "Analysis Fetch");
Log.logDebug("Analysis", "Analysis Fetch Phase"); Log.logDebug("Analysis", "Analysis Fetch Phase");
return analyzeData(infoManager, db);
}
/**
* Analyze data in the db about this server.
*
* @param infoManager InformationManager of the plugin.
* @return Success?
*/
public boolean analyzeData(InformationManager infoManager, Database db) {
try { try {
Benchmark.start("Create Empty dataset"); Benchmark.start("Create Empty dataset");
@ -129,8 +82,10 @@ public class Analysis {
Benchmark.stop("Analysis", "Create Empty dataset"); Benchmark.stop("Analysis", "Create Empty dataset");
Benchmark.start("Fetch Phase"); Benchmark.start("Fetch Phase");
ServerProfile profile = db.fetch().getServerProfile(Plan.getServerUUID()); ServerProfile profile = database.fetch().getServerProfile(serverUUID);
profile.addActiveSessions(new HashMap<>(SessionCache.getActiveSessions())); if (analysingThisServer) {
profile.addActiveSessions(new HashMap<>(SessionCache.getActiveSessions()));
}
serverProfile = profile; serverProfile = profile;
updatePlayerNameCache(profile); updatePlayerNameCache(profile);
@ -150,31 +105,27 @@ public class Analysis {
log(Locale.get(Msg.ANALYSIS_3RD_PARTY).toString()); log(Locale.get(Msg.ANALYSIS_3RD_PARTY).toString());
Log.logDebug("Analysis", "Analyzing additional data sources (3rd party)"); Log.logDebug("Analysis", "Analyzing additional data sources (3rd party)");
analysisData.parsePluginsSection(analyzeAdditionalPluginData(profile.getUuids())); analysisData.parsePluginsSection(analyzeAdditionalPluginData(profile.getUuids()));
((BukkitInformationManager) infoManager).cacheAnalysisData(analysisData); return analysisData;
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
((BukkitInformationManager) plugin.getInfoManager()).cacheAnalysisHtml(new InternalErrorResponse(e, "Analysis").getContent());
Log.logDebug("Analysis", "Error: " + e);
return false;
} finally { } finally {
refreshDate = MiscUtils.getTime();
long time = Benchmark.stop("Analysis", "Analysis"); long time = Benchmark.stop("Analysis", "Analysis");
Log.logDebug("Analysis"); Log.logDebug("Analysis");
Log.info(Locale.get(Msg.ANALYSIS_FINISHED).parse(time, "")); Log.info(Locale.get(Msg.ANALYSIS_FINISHED).parse(time, ""));
serverProfile = null; serverProfile = null;
} }
return true;
} }
private void updatePlayerNameCache(ServerProfile profile) { private void updatePlayerNameCache(ServerProfile profile) {
DataCache dataCache = DataCache.getInstance();
for (PlayerProfile player : profile.getPlayers()) { for (PlayerProfile player : profile.getPlayers()) {
dataCache.updateNames(player.getUuid(), player.getName(), null); dataCache.updateNames(player.getUuid(), player.getName(), null);
} }
} }
private void setBannedByPlugins(ServerProfile profile) { private void setBannedByPlugins(ServerProfile profile) {
UUID serverUUID = Plan.getServerUUID(); if (!analysingThisServer) {
List<BanData> banPlugins = plugin.getHookHandler().getAdditionalDataSources().stream() return;
}
List<BanData> banPlugins = HookHandler.getInstance().getAdditionalDataSources().stream()
.filter(p -> p instanceof BanData) .filter(p -> p instanceof BanData)
.map(p -> (BanData) p) .map(p -> (BanData) p)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -200,13 +151,17 @@ public class Analysis {
} }
private Map<PluginData, AnalysisContainer> analyzeAdditionalPluginData(Set<UUID> uuids) { private Map<PluginData, AnalysisContainer> analyzeAdditionalPluginData(Set<UUID> uuids) {
if (!analysingThisServer) {
return new HashMap<>();
}
Map<PluginData, AnalysisContainer> containers = new HashMap<>(); Map<PluginData, AnalysisContainer> containers = new HashMap<>();
Benchmark.start("Analysis", "3rd party Analysis"); Benchmark.start("Analysis", "3rd party Analysis");
List<PluginData> sources = plugin.getHookHandler().getAdditionalDataSources(); List<PluginData> sources = HookHandler.getInstance().getAdditionalDataSources();
Log.logDebug("Analysis", "Additional Sources: " + sources.size()); Log.logDebug("Analysis", "Additional Sources: " + sources.size());
sources.parallelStream().forEach(source -> { sources.parallelStream().forEach(source -> {
PlanPlugin plugin = PlanPlugin.getInstance();
StaticHolder.saveInstance(this.getClass(), plugin.getClass()); StaticHolder.saveInstance(this.getClass(), plugin.getClass());
try { try {
Benchmark.start("Analysis", "Source " + source.getSourcePlugin()); Benchmark.start("Analysis", "Source " + source.getSourcePlugin());
@ -226,13 +181,4 @@ public class Analysis {
Benchmark.stop("Analysis", "3rd party Analysis"); Benchmark.stop("Analysis", "3rd party Analysis");
return containers; return containers;
} }
/**
* Condition whether or not analysis is being run.
*
* @return true / false (state)
*/
public boolean isAnalysisBeingRun() {
return taskId != -1;
}
} }