Export of /players page

This commit is contained in:
Rsl1122 2019-09-01 18:28:59 +03:00
parent 7208c1a8a7
commit b67534af20
8 changed files with 189 additions and 303 deletions

View File

@ -17,7 +17,6 @@
package com.djrapitops.plan.commands.subcommands.manage;
import com.djrapitops.plan.delivery.export.Exporter;
import com.djrapitops.plan.delivery.export.HtmlExport;
import com.djrapitops.plan.exceptions.ExportException;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.Permissions;
@ -57,7 +56,6 @@ public class ManageExportCommand extends CommandNode {
private final PlanConfig config;
private final DBSystem dbSystem;
private final Exporter exporter;
private final HtmlExport htmlExport;
private final Processing processing;
@Inject
@ -67,8 +65,7 @@ public class ManageExportCommand extends CommandNode {
PlanConfig config,
DBSystem dbSystem,
Exporter exporter,
Processing processing,
HtmlExport htmlExport
Processing processing
) {
super("export", Permissions.MANAGE.getPermission(), CommandType.CONSOLE);
@ -77,7 +74,6 @@ public class ManageExportCommand extends CommandNode {
this.config = config;
this.dbSystem = dbSystem;
this.exporter = exporter;
this.htmlExport = htmlExport;
this.processing = processing;
setArguments("<export_kind>/list");
@ -116,7 +112,7 @@ public class ManageExportCommand extends CommandNode {
private void exportPlayers(Sender sender) {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
if (config.get(ExportSettings.PLAYERS_PAGE)) {
processing.submitNonCritical(htmlExport::exportPlayersPage);
processing.submitNonCritical(exporter::exportPlayersPage);
}
Boolean exportPlayerJSON = config.get(ExportSettings.PLAYER_JSON);
Boolean exportPlayerHTML = config.get(ExportSettings.PLAYER_PAGES);

View File

@ -86,6 +86,10 @@ public class ExportScheduler {
.runTaskTimerAsynchronously(0L, period)
);
taskSystem.registerTask("Players page export",
new ExportTask(exporter, Exporter::exportPlayersPage, logger, errorHandler)
).runTaskTimerAsynchronously(0L, period);
int offsetMultiplier = proxy.isPresent() ? 1 : 0; // Delay first server export if on a network.
for (Server server : servers) {
taskSystem.registerTask("Server export",

View File

@ -18,9 +18,6 @@ package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.ExportSettings;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
@ -36,28 +33,19 @@ import javax.inject.Singleton;
@Singleton
public class ExportSystem implements SubSystem {
private final PlanConfig config;
private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final Processing processing;
private final ExportScheduler exportScheduler;
private final HtmlExport htmlExport;
@Inject
public ExportSystem(
PlanConfig config,
DBSystem dbSystem,
ServerInfo serverInfo,
Processing processing,
ExportScheduler exportScheduler,
HtmlExport htmlExport
ExportScheduler exportScheduler
) {
this.config = config;
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.processing = processing;
this.exportScheduler = exportScheduler;
this.htmlExport = htmlExport;
}
@Override
@ -69,10 +57,6 @@ public class ExportSystem implements SubSystem {
}
exportScheduler.scheduleExport();
if (config.isTrue(ExportSettings.PLAYERS_PAGE)) {
processing.submitNonCritical(htmlExport::exportPlayersPage);
}
}
@Override

View File

@ -45,6 +45,7 @@ public class Exporter {
private final PlanConfig config;
private final PlayerJSONExporter playerJSONExporter;
private final PlayerPageExporter playerPageExporter;
private final PlayersPageExporter playersPageExporter;
private final ServerPageExporter serverPageExporter;
private final NetworkPageExporter networkPageExporter;
@ -56,6 +57,7 @@ public class Exporter {
PlanConfig config,
PlayerJSONExporter playerJSONExporter,
PlayerPageExporter playerPageExporter,
PlayersPageExporter playersPageExporter,
ServerPageExporter serverPageExporter,
NetworkPageExporter networkPageExporter
) {
@ -63,6 +65,7 @@ public class Exporter {
this.config = config;
this.playerJSONExporter = playerJSONExporter;
this.playerPageExporter = playerPageExporter;
this.playersPageExporter = playersPageExporter;
this.serverPageExporter = serverPageExporter;
this.networkPageExporter = networkPageExporter;
@ -128,6 +131,18 @@ public class Exporter {
}
}
public boolean exportPlayersPage() throws ExportException {
Path toDirectory = getPageExportDirectory();
if (!config.get(ExportSettings.PLAYERS_PAGE)) return false;
try {
playersPageExporter.export(toDirectory);
return true;
} catch (IOException | NotFoundException | ParseException e) {
throw new ExportException("Failed to export players page, " + e.getMessage(), e);
}
}
/**
* Export Raw Data JSON of a player.
*

View File

@ -1,90 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.exceptions.ParseException;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.ExportSettings;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import com.djrapitops.plugin.utilities.Verify;
import org.apache.commons.lang3.StringUtils;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* Class responsible for Html Export task.
*
* @author Rsl1122
*/
@Singleton
@Deprecated
public class HtmlExport extends SpecificExport {
private final PlanConfig config;
private final PageFactory pageFactory;
private final ErrorHandler errorHandler;
@Inject
public HtmlExport(
PlanFiles files,
PlanConfig config,
PageFactory pageFactory,
ServerInfo serverInfo,
ErrorHandler errorHandler
) {
super(files, serverInfo);
this.config = config;
this.pageFactory = pageFactory;
this.errorHandler = errorHandler;
}
@Override
protected String getPath() {
return config.get(ExportSettings.HTML_EXPORT_PATH);
}
public void exportPlayersPage() {
try {
String html = pageFactory.playersPage().toHtml()
.replace("href=\"plugins/", "href=\"../plugins/")
.replace("href=\"css/", "href=\"../css/")
.replace("src=\"plugins/", "src=\"../plugins/")
.replace("src=\"js/", "src=\"../js/");
List<String> lines = Arrays.asList(StringUtils.split(html, "\n"));
File htmlLocation = new File(getFolder(), "players");
Verify.isTrue(htmlLocation.exists() && htmlLocation.isDirectory() || htmlLocation.mkdirs(),
() -> new FileNotFoundException("Output folder could not be created at" + htmlLocation.getAbsolutePath()));
File exportFile = new File(htmlLocation, "index.html");
export(exportFile, lines);
} catch (IOException | DBOpException | ParseException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
}
}

View File

@ -0,0 +1,167 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.delivery.rendering.pages.Page;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.delivery.webserver.RequestTarget;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler;
import com.djrapitops.plan.delivery.webserver.response.Response;
import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse;
import com.djrapitops.plan.exceptions.ParseException;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.exceptions.connection.WebException;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.storage.file.Resource;
import org.apache.commons.lang3.StringUtils;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
/**
* Handles exporting of /players page html, data and resources.
*
* @author Rsl1122
*/
@Singleton
public class PlayersPageExporter extends FileExporter {
private final PlanFiles files;
private final PageFactory pageFactory;
private final RootJSONHandler jsonHandler;
private final Locale locale;
private final Theme theme;
private final ExportPaths exportPaths;
@Inject
public PlayersPageExporter(
PlanFiles files,
PageFactory pageFactory,
RootJSONHandler jsonHandler,
Locale locale,
Theme theme
) {
this.files = files;
this.pageFactory = pageFactory;
this.jsonHandler = jsonHandler;
this.locale = locale;
this.theme = theme;
exportPaths = new ExportPaths();
}
public void export(Path toDirectory) throws IOException, NotFoundException, ParseException {
exportRequiredResources(toDirectory);
exportJSON(toDirectory);
exportHtml(toDirectory);
}
private void exportHtml(Path toDirectory) throws IOException, ParseException {
Path to = toDirectory
.resolve("players")
.resolve("index.html");
Page page = pageFactory.playersPage();
export(to, exportPaths.resolveExportPaths(locale.replaceMatchingLanguage(page.toHtml())));
}
private void exportJSON(Path toDirectory) throws NotFoundException, IOException {
Response found = getJSONResponse("players");
if (found instanceof ErrorResponse) {
throw new NotFoundException("players page was not properly exported: " + found.getContent());
}
String jsonResourceName = toFileName(toJSONResourceName("players")) + ".json";
export(toDirectory.resolve("data").resolve(jsonResourceName), found.getContent());
exportPaths.put("../v1/players", toRelativePathFromRoot("data/" + jsonResourceName));
}
private String toJSONResourceName(String resource) {
return StringUtils.replaceEach(resource, new String[]{"?", "&", "type=", "server="}, new String[]{"-", "_", "", ""});
}
private Response getJSONResponse(String resource) {
try {
return jsonHandler.getResponse(null, new RequestTarget(URI.create(resource)));
} catch (WebException e) {
// The rest of the exceptions should not be thrown
throw new IllegalStateException("Unexpected exception thrown: " + e.toString(), e);
}
}
private void exportRequiredResources(Path toDirectory) throws IOException {
exportImage(toDirectory, "img/Flaticon_circle.png");
// Style
exportResources(toDirectory,
"css/sb-admin-2.css",
"css/style.css",
"vendor/jquery/jquery.min.js",
"vendor/bootstrap/js/bootstrap.bundle.min.js",
"vendor/jquery-easing/jquery.easing.min.js",
"vendor/datatables/jquery.dataTables.min.js",
"vendor/datatables/dataTables.bootstrap4.min.js",
"js/sb-admin-2.js",
"js/xmlhttprequests.js",
"js/color-selector.js"
);
}
private void exportResources(Path toDirectory, String... resourceNames) throws IOException {
for (String resourceName : resourceNames) {
exportResource(toDirectory, resourceName);
}
}
private void exportResource(Path toDirectory, String resourceName) throws IOException {
Resource resource = files.getCustomizableResourceOrDefault("web/" + resourceName);
Path to = toDirectory.resolve(resourceName);
if (resourceName.endsWith(".css")) {
export(to, theme.replaceThemeColors(resource.asString()));
} else {
export(to, resource.asLines());
}
exportPaths.put(resourceName, toRelativePathFromRoot(resourceName));
}
private void exportImage(Path toDirectory, String resourceName) throws IOException {
Resource resource = files.getCustomizableResourceOrDefault("web/" + resourceName);
Path to = toDirectory.resolve(resourceName);
export(to, resource);
exportPaths.put(resourceName, toRelativePathFromRoot(resourceName));
}
private String toRelativePathFromRoot(String resourceName) {
// Players html is exported at /players/index.html or /server/index.html
return "../" + toNonRelativePath(resourceName);
}
private String toNonRelativePath(String resourceName) {
return StringUtils.remove(resourceName, "../");
}
}

View File

@ -1,76 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.storage.file.PlanFiles;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
/**
* Abstract Html Export Task.
*
* // TODO Export should check config settings
*
* @author Rsl1122
*/
@Deprecated
public abstract class SpecificExport {
private final PlanFiles files;
protected final ServerInfo serverInfo;
SpecificExport(
PlanFiles files,
ServerInfo serverInfo
) {
this.files = files;
// Hacky, TODO export needs a rework
this.serverInfo = serverInfo;
}
protected File getFolder() {
File folder;
String path = getPath();
boolean isAbsolute = Paths.get(path).isAbsolute();
if (isAbsolute) {
folder = new File(path);
} else {
File dataFolder = files.getDataFolder();
folder = new File(dataFolder, path);
}
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdirs();
}
return folder;
}
protected abstract String getPath();
protected void export(File to, List<String> lines) throws IOException {
Files.write(to.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
}
}

View File

@ -1,114 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.gathering.domain.Session;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.ExportSettings;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.transactions.events.PlayerRegisterTransaction;
import com.djrapitops.plan.storage.database.transactions.events.SessionEndTransaction;
import com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction;
import com.jayway.awaitility.Awaitility;
import org.junit.Assume;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import utilities.TestConstants;
import utilities.mocks.PluginMockComponent;
import java.io.File;
import java.nio.file.Path;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test against Server JSON Export errors.
*
* @author Rsl1122
*/
@RunWith(JUnitPlatform.class)
class AnalysisExportTest {
private PlanSystem system;
@BeforeEach
void setupPlanSystem(@TempDir Path dir) throws Exception {
PluginMockComponent component = new PluginMockComponent(dir);
system = component.getPlanSystem();
PlanConfig config = system.getConfigSystem().getConfig();
config.set(ExportSettings.JSON_EXPORT_PATH, "Test");
config.set(ExportSettings.SERVER_JSON, true);
system.enable();
Database database = system.getDatabaseSystem().getDatabase();
storeSomeData(database);
}
private void storeSomeData(Database database) {
UUID uuid = TestConstants.PLAYER_ONE_UUID;
database.executeTransaction(new PlayerRegisterTransaction(uuid, () -> 1000L, "name"));
Session session = new Session(uuid, TestConstants.SERVER_UUID, 1000L, "world", "SURVIVAL");
session.endSession(11000L);
database.executeTransaction(new WorldNameStoreTransaction(TestConstants.SERVER_UUID, "world"));
database.executeTransaction(new SessionEndTransaction(session));
}
@AfterEach
void tearDownSystem() {
system.disable();
}
@Test
void serverJSONIsExported() {
Assume.assumeFalse(true);// TODO Fix test, Changes to server page handling
File exportFolder = system.getPlanFiles().getFileFromPluginFolder("Test");
Awaitility.await().atMost(5, TimeUnit.SECONDS)
.until(() -> exportFolder.listFiles() != null);
File[] folders = exportFolder.listFiles();
assertNotNull(folders);
boolean found = false;
for (File folder : folders) {
if (folder.isFile()) {
continue;
}
if (folder.getName().equals("server")) {
for (File file : folder.listFiles()) {
if (file.getName().contains("Plan.json")) {
found = true;
}
}
}
}
assertTrue(found);
}
}