Export of Player JSON

This commit is contained in:
Rsl1122 2019-09-01 18:16:38 +03:00
parent 8a5656e9dc
commit 7208c1a8a7
6 changed files with 97 additions and 146 deletions

View File

@ -18,9 +18,7 @@ package com.djrapitops.plan.commands.subcommands.manage;
import com.djrapitops.plan.delivery.export.Exporter; import com.djrapitops.plan.delivery.export.Exporter;
import com.djrapitops.plan.delivery.export.HtmlExport; import com.djrapitops.plan.delivery.export.HtmlExport;
import com.djrapitops.plan.delivery.export.JSONExport;
import com.djrapitops.plan.exceptions.ExportException; import com.djrapitops.plan.exceptions.ExportException;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.processing.Processing; import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.Permissions; import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.settings.config.PlanConfig;
@ -58,10 +56,8 @@ public class ManageExportCommand extends CommandNode {
private final ColorScheme colorScheme; private final ColorScheme colorScheme;
private final PlanConfig config; private final PlanConfig config;
private final DBSystem dbSystem; private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final Exporter exporter; private final Exporter exporter;
private final HtmlExport htmlExport; private final HtmlExport htmlExport;
private final JSONExport jsonExport;
private final Processing processing; private final Processing processing;
@Inject @Inject
@ -70,11 +66,9 @@ public class ManageExportCommand extends CommandNode {
ColorScheme colorScheme, ColorScheme colorScheme,
PlanConfig config, PlanConfig config,
DBSystem dbSystem, DBSystem dbSystem,
ServerInfo serverInfo,
Exporter exporter, Exporter exporter,
Processing processing, Processing processing,
HtmlExport htmlExport, HtmlExport htmlExport
JSONExport jsonExport
) { ) {
super("export", Permissions.MANAGE.getPermission(), CommandType.CONSOLE); super("export", Permissions.MANAGE.getPermission(), CommandType.CONSOLE);
@ -82,10 +76,8 @@ public class ManageExportCommand extends CommandNode {
this.colorScheme = colorScheme; this.colorScheme = colorScheme;
this.config = config; this.config = config;
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.exporter = exporter; this.exporter = exporter;
this.htmlExport = htmlExport; this.htmlExport = htmlExport;
this.jsonExport = jsonExport;
this.processing = processing; this.processing = processing;
setArguments("<export_kind>/list"); setArguments("<export_kind>/list");
@ -101,7 +93,7 @@ public class ManageExportCommand extends CommandNode {
String exportArg = args[0]; String exportArg = args[0];
if ("list".equals(exportArg)) { if ("list".equals(exportArg)) {
sender.sendMessage("> " + colorScheme.getMainColor() + "players, server_json"); sender.sendMessage("> " + colorScheme.getMainColor() + "players");
return; return;
} }
@ -115,22 +107,10 @@ public class ManageExportCommand extends CommandNode {
} }
private Consumer<Sender> getExportFunction(String exportArg) { private Consumer<Sender> getExportFunction(String exportArg) {
switch (exportArg) { if ("players".equals(exportArg)) {
case "players": return this::exportPlayers;
return this::exportPlayers;
case "server_json":
return this::exportServerJSON;
default:
return sender -> sender.sendMessage(locale.getString(ManageLang.FAIL_EXPORTER_NOT_FOUND, exportArg));
} }
} return sender -> sender.sendMessage(locale.getString(ManageLang.FAIL_EXPORTER_NOT_FOUND, exportArg));
private void exportServerJSON(Sender sender) {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
processing.submitNonCritical(() -> {
jsonExport.exportServerJSON(serverInfo.getServerUUID());
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
});
} }
private void exportPlayers(Sender sender) { private void exportPlayers(Sender sender) {
@ -140,6 +120,12 @@ public class ManageExportCommand extends CommandNode {
} }
Boolean exportPlayerJSON = config.get(ExportSettings.PLAYER_JSON); Boolean exportPlayerJSON = config.get(ExportSettings.PLAYER_JSON);
Boolean exportPlayerHTML = config.get(ExportSettings.PLAYER_PAGES); Boolean exportPlayerHTML = config.get(ExportSettings.PLAYER_PAGES);
if (!exportPlayerJSON && !exportPlayerHTML) {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL));
sender.sendMessage("§c'" + ExportSettings.PLAYER_JSON.getPath() + "' & '" + ExportSettings.PLAYER_PAGES.getPath() + "': false");
return;
}
processing.submitNonCritical(() -> { processing.submitNonCritical(() -> {
Map<UUID, String> players = dbSystem.getDatabase().query(UserIdentifierQueries.fetchAllPlayerNames()); Map<UUID, String> players = dbSystem.getDatabase().query(UserIdentifierQueries.fetchAllPlayerNames());
int size = players.size(); int size = players.size();
@ -148,7 +134,7 @@ public class ManageExportCommand extends CommandNode {
int i = 1; int i = 1;
for (Map.Entry<UUID, String> entry : players.entrySet()) { for (Map.Entry<UUID, String> entry : players.entrySet()) {
try { try {
if (exportPlayerJSON) jsonExport.exportPlayerJSON(entry.getKey()); if (exportPlayerJSON) exporter.exportPlayerJSON(entry.getKey(), entry.getValue());
if (exportPlayerHTML) exporter.exportPlayerPage(entry.getKey(), entry.getValue()); if (exportPlayerHTML) exporter.exportPlayerPage(entry.getKey(), entry.getValue());
} catch (ExportException e) { } catch (ExportException e) {
failed++; failed++;

View File

@ -43,6 +43,7 @@ public class Exporter {
private final PlanFiles files; private final PlanFiles files;
private final PlanConfig config; private final PlanConfig config;
private final PlayerJSONExporter playerJSONExporter;
private final PlayerPageExporter playerPageExporter; private final PlayerPageExporter playerPageExporter;
private final ServerPageExporter serverPageExporter; private final ServerPageExporter serverPageExporter;
private final NetworkPageExporter networkPageExporter; private final NetworkPageExporter networkPageExporter;
@ -53,12 +54,14 @@ public class Exporter {
public Exporter( public Exporter(
PlanFiles files, PlanFiles files,
PlanConfig config, PlanConfig config,
PlayerJSONExporter playerJSONExporter,
PlayerPageExporter playerPageExporter, PlayerPageExporter playerPageExporter,
ServerPageExporter serverPageExporter, ServerPageExporter serverPageExporter,
NetworkPageExporter networkPageExporter NetworkPageExporter networkPageExporter
) { ) {
this.files = files; this.files = files;
this.config = config; this.config = config;
this.playerJSONExporter = playerJSONExporter;
this.playerPageExporter = playerPageExporter; this.playerPageExporter = playerPageExporter;
this.serverPageExporter = serverPageExporter; this.serverPageExporter = serverPageExporter;
this.networkPageExporter = networkPageExporter; this.networkPageExporter = networkPageExporter;
@ -73,6 +76,13 @@ public class Exporter {
: files.getDataDirectory().resolve(exportDirectory); : files.getDataDirectory().resolve(exportDirectory);
} }
private Path getJSONExportDirectory() {
Path exportDirectory = Paths.get(config.get(ExportSettings.JSON_EXPORT_PATH));
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
/** /**
* Export a page of a server. * Export a page of a server.
* *
@ -117,4 +127,24 @@ public class Exporter {
throw new ExportException("Failed to export player: " + playerName + ", " + e.getMessage(), e); throw new ExportException("Failed to export player: " + playerName + ", " + e.getMessage(), e);
} }
} }
/**
* Export Raw Data JSON of a player.
*
* @param playerUUID UUID of the player.
* @param playerName Name of the player.
* @return false if the json was not exported due to config settings.
* @throws ExportException If the export failed due to IOException.
*/
public boolean exportPlayerJSON(UUID playerUUID, String playerName) throws ExportException {
Path toDirectory = getJSONExportDirectory();
if (!config.get(ExportSettings.PLAYER_JSON)) return false;
try {
playerJSONExporter.export(toDirectory, playerUUID, playerName);
return true;
} catch (IOException e) {
throw new ExportException("Failed to export player: " + playerName + ", " + e.getMessage(), e);
}
}
} }

View File

@ -16,15 +16,12 @@
*/ */
package com.djrapitops.plan.delivery.export; package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory; import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.exceptions.ParseException; import com.djrapitops.plan.exceptions.ParseException;
import com.djrapitops.plan.exceptions.database.DBOpException; import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.ExportSettings; import com.djrapitops.plan.settings.config.paths.ExportSettings;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler; import com.djrapitops.plugin.logging.error.ErrorHandler;
@ -49,10 +46,6 @@ import java.util.List;
public class HtmlExport extends SpecificExport { public class HtmlExport extends SpecificExport {
private final PlanConfig config; private final PlanConfig config;
private final Theme theme;
private final PlanFiles files;
private final DBSystem dbSystem;
private final Exporter exporter;
private final PageFactory pageFactory; private final PageFactory pageFactory;
private final ErrorHandler errorHandler; private final ErrorHandler errorHandler;
@ -60,20 +53,12 @@ public class HtmlExport extends SpecificExport {
public HtmlExport( public HtmlExport(
PlanFiles files, PlanFiles files,
PlanConfig config, PlanConfig config,
Theme theme,
DBSystem dbSystem,
Exporter exporter,
PageFactory pageFactory, PageFactory pageFactory,
JSONFactory jsonFactory,
ServerInfo serverInfo, ServerInfo serverInfo,
ErrorHandler errorHandler ErrorHandler errorHandler
) { ) {
super(files, jsonFactory, serverInfo); super(files, serverInfo);
this.config = config; this.config = config;
this.theme = theme;
this.files = files;
this.dbSystem = dbSystem;
this.exporter = exporter;
this.pageFactory = pageFactory; this.pageFactory = pageFactory;
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
} }

View File

@ -1,94 +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.json.JSONFactory;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
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.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.UUID;
/**
* Class in charge of exporting json files.
*
* @author Rsl1122
*/
@Singleton
@Deprecated
public class JSONExport extends SpecificExport {
private final PlanConfig config;
private final DBSystem dbSystem;
private final ResponseFactory responseFactory;
private final ErrorHandler errorHandler;
@Inject
public JSONExport(
PlanFiles files,
PlanConfig config,
DBSystem dbSystem,
ServerInfo serverInfo,
JSONFactory jsonFactory,
ResponseFactory responseFactory,
ErrorHandler errorHandler
) {
super(files, jsonFactory, serverInfo);
this.config = config;
this.dbSystem = dbSystem;
this.responseFactory = responseFactory;
this.errorHandler = errorHandler;
}
@Override
protected String getPath() {
return config.get(ExportSettings.JSON_EXPORT_PATH);
}
public void exportPlayerJSON(UUID playerUUID) {
String json = responseFactory.rawPlayerPageResponse(playerUUID).getContent();
dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID))
.ifPresent(playerName -> {
try {
File htmlLocation = getPlayerFolder();
htmlLocation.mkdirs();
File exportFile = new File(htmlLocation, URLEncoder.encode(playerName, "UTF-8") + ".json");
export(exportFile, Collections.singletonList(json));
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
});
}
public void exportServerJSON(UUID serverUUID) {
// TODO Export JSON Parser results
}
}

View File

@ -0,0 +1,52 @@
/*
* 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.webserver.response.ResponseFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.nio.file.Path;
import java.util.UUID;
/**
* Handles exporting of player json.
*
* @author Rsl1122
*/
@Singleton
public class PlayerJSONExporter extends FileExporter {
private final ResponseFactory responseFactory;
@Inject
public PlayerJSONExporter(
ResponseFactory responseFactory
) {
this.responseFactory = responseFactory;
}
public void export(Path toDirectory, UUID playerUUID, String playerName) throws IOException {
Path to = toDirectory.resolve("player/" + toFileName(playerName) + ".json");
exportJSON(to, playerUUID);
}
private void exportJSON(Path to, UUID playerUUID) throws IOException {
export(to, responseFactory.rawPlayerPageResponse(playerUUID).getContent());
}
}

View File

@ -16,7 +16,6 @@
*/ */
package com.djrapitops.plan.delivery.export; package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.storage.file.PlanFiles;
@ -39,15 +38,14 @@ import java.util.List;
public abstract class SpecificExport { public abstract class SpecificExport {
private final PlanFiles files; private final PlanFiles files;
private final JSONFactory jsonFactory; // Hacky, TODO export needs a rework
protected final ServerInfo serverInfo; protected final ServerInfo serverInfo;
SpecificExport( SpecificExport(
PlanFiles files, PlanFiles files,
JSONFactory jsonFactory, ServerInfo serverInfo ServerInfo serverInfo
) { ) {
this.files = files; this.files = files;
this.jsonFactory = jsonFactory; // Hacky, TODO export needs a rework
this.serverInfo = serverInfo; this.serverInfo = serverInfo;
} }
@ -75,10 +73,4 @@ public abstract class SpecificExport {
Files.write(to.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE); Files.write(to.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
} }
File getPlayerFolder() {
File player = new File(getFolder(), "player");
player.mkdirs();
return player;
}
} }