Caches the page at inspection / analysis now

This commit is contained in:
Fuzzlemann 2017-07-30 11:45:14 +02:00
parent 941fab9e9d
commit c345ec7c5d
5 changed files with 35 additions and 20 deletions

View File

@ -176,12 +176,7 @@ public class DataCacheHandler extends SessionCache {
UserData uData = dataCache.get(uuid);
if (uData == null) {
if (cache) {
DBCallableProcessor cacher = new DBCallableProcessor() {
@Override
public void process(UserData data) {
cache(data);
}
};
DBCallableProcessor cacher = this::cache;
getTask.scheduleForGet(uuid, cacher, processor);
} else {
getTask.scheduleForGet(uuid, processor);

View File

@ -4,6 +4,7 @@ import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.ui.webserver.response.InspectPageResponse;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.analysis.ExportUtility;
@ -48,6 +49,7 @@ public class InspectCacheHandler {
public void process(UserData data) {
cache.put(uuid, new UserData(data));
cacheTimes.put(uuid, MiscUtils.getTime());
PageCacheHandler.cachePage("inspectPage: " + uuid.toString(), () -> new InspectPageResponse(Plan.getInstance().getUiServer().getDataReqHandler(), uuid));
try {
ExportUtility.writeInspectHtml(data, ExportUtility.getPlayersFolder(ExportUtility.getFolder()));
} catch (IOException ex) {

View File

@ -35,9 +35,9 @@ public class PageCacheHandler {
* <p>
* If the Response is created, it's automatically cached.
*
* @param identifier
* @param loader
* @return
* @param identifier The identifier of the page
* @param loader The {@link PageLoader} (How should it load the page if it's not cached)
* @return The Response that was cached or created by the {@link PageLoader loader}
*/
public static Response loadPage(String identifier, PageLoader loader) {
Response response = pageCache.asMap().get(identifier);
@ -53,6 +53,19 @@ public class PageCacheHandler {
return response;
}
/**
* Puts the page into the page cache.
* <p>
* If the cache already inherits that {@code identifier}, it's renewed.
*
* @param identifier The identifier of the page
* @param loader The {@link PageLoader} (How it should load the page)
*/
public static void cachePage(String identifier, PageLoader loader) {
Response response = loader.createResponse();
pageCache.put(identifier, response);
}
/**
* Clears the cache from all its contents.
*/

View File

@ -1,7 +1,6 @@
package main.java.com.djrapitops.plan.data.cache.queue;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo;
@ -89,12 +88,9 @@ class ProcessConsumer extends Consumer<HandlingInfo> {
return;
}
Log.debug(info.getUuid() + ": Processing type: " + info.getType().name());
DBCallableProcessor p = new DBCallableProcessor() {
@Override
public void process(UserData data) {
if (!info.process(data)) {
Log.error("Attempted to process data for wrong uuid: W:" + data.getUuid() + " | R:" + info.getUuid() + " Type:" + info.getType().name());
}
DBCallableProcessor p = data -> {
if (!info.process(data)) {
Log.error("Attempted to process data for wrong uuid: W:" + data.getUuid() + " | R:" + info.getUuid() + " Type:" + info.getType().name());
}
};
handler.getUserDataForProcessing(p, info.getUuid());

View File

@ -17,6 +17,8 @@ import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler;
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.ui.html.tables.PlayersTableCreator;
import main.java.com.djrapitops.plan.ui.webserver.response.AnalysisPageResponse;
import main.java.com.djrapitops.plan.ui.webserver.response.PlayersPageResponse;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
@ -48,7 +50,7 @@ public class Analysis {
* Analyzes the data of all offline players on the server.
* <p>
* First retrieves all offline players and checks those that are in the
* database. Then Runs a new Analysis Task Asynchronously. Saves AnalysisData
* database. Then runs a new Analysis Task asynchronously. Saves AnalysisData
* to the provided Cache. Saves all UserData to InspectCache for 15 minutes.
*
* @param analysisCache Cache that the data is saved to.
@ -58,8 +60,6 @@ public class Analysis {
return;
}
PageCacheHandler.clearCache();
plugin.processStatus().startExecution("Analysis");
log(Phrase.ANALYSIS_START.toString());
// Async task for Analysis
@ -68,6 +68,8 @@ public class Analysis {
public void run() {
taskId = this.getTaskId();
analyze(analysisCache, plugin.getDB());
PageCacheHandler.cachePage("analysisPage", () -> new AnalysisPageResponse(Plan.getInstance().getUiServer().getDataReqHandler()));
PageCacheHandler.cachePage("players", () -> new PlayersPageResponse(plugin));
taskId = -1;
this.cancel();
}
@ -83,8 +85,9 @@ public class Analysis {
* @return Whether or not analysis was successful.
*/
public boolean analyze(AnalysisCacheHandler analysisCache, Database db) {
log(Phrase.ANALYSIS_FETCH_DATA + "");
log(Phrase.ANALYSIS_FETCH_DATA.toString());
Benchmark.start("Analysis: Fetch Phase");
plugin.processStatus().setStatus("Analysis", "Analysis Fetch Phase");
try {
inspectCache.cacheAllUserData(db);
@ -92,18 +95,22 @@ public class Analysis {
Log.toLog(this.getClass().getName(), ex);
Log.error(Phrase.ERROR_ANALYSIS_FETCH_FAIL.toString());
}
List<UserData> rawData = inspectCache.getCachedUserData();
if (rawData.isEmpty()) {
Log.info(Phrase.ANALYSIS_FAIL_NO_DATA.toString());
return false;
}
List<TPS> tpsData = new ArrayList<>();
try {
tpsData = db.getTpsTable().getTPSData();
Log.debug("TPS Data Size: " + tpsData.size());
} catch (Exception ex) {
Log.toLog(this.getClass().getName(), ex);
}
return analyzeData(rawData, tpsData, analysisCache);
}
@ -154,7 +161,9 @@ public class Analysis {
if (Settings.ANALYSIS_LOG_FINISHED.isTrue()) {
Log.info(Phrase.ANALYSIS_COMPLETE.parse(String.valueOf(time), HtmlUtils.getServerAnalysisUrlWithProtocol()));
}
ExportUtility.export(plugin, analysisData, rawData);
PageCacheHandler.cachePage("analysisPage", () -> new AnalysisPageResponse(Plan.getInstance().getUiServer().getDataReqHandler()));
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
plugin.processStatus().setStatus("Analysis", "Error: " + e);