Server page exporting

This commit is contained in:
Rsl1122 2019-09-01 13:32:40 +03:00
parent 6df205074d
commit c1c2da8517
11 changed files with 421 additions and 123 deletions

View File

@ -97,7 +97,7 @@ public class AnalyzeCommand extends CommandNode {
try {
Server server = getServer(args);
sendWebUserNotificationIfNecessary(sender);
export.exportServer(server.getUuid());
export.exportServer(server); // TODO
sendLink(server, sender);
} catch (DBOpException e) {
sender.sendMessage("§cError occurred: " + e.toString());

View File

@ -0,0 +1,47 @@
/*
* 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 org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Export utility that takes care of path replacement for different resources referenced in other files.
*
* @author Rsl1122
*/
public class ExportPaths {
private List<String> replace;
private List<String> with;
public ExportPaths() {
replace = new ArrayList<>();
with = new ArrayList<>();
}
public String resolveExportPaths(String original) {
return StringUtils.replaceEach(original, replace.toArray(new String[0]), with.toArray(new String[0]));
}
public void put(String replace, String with) {
this.replace.add(replace);
this.with.add(with);
}
}

View File

@ -66,9 +66,9 @@ public class ExportSystem implements SubSystem {
}
if (config.isTrue(ExportSettings.JS_AND_CSS)) {
processing.submitNonCritical(htmlExport::exportJs);
processing.submitNonCritical(htmlExport::exportCss);
processing.submitNonCritical(htmlExport::exportPlugins);
// processing.submitNonCritical(htmlExport::exportJs);
// processing.submitNonCritical(htmlExport::exportCss);
// processing.submitNonCritical(htmlExport::exportPlugins);
}
if (config.isTrue(ExportSettings.PLAYERS_PAGE)) {
processing.submitNonCritical(htmlExport::exportPlayersPage);

View File

@ -0,0 +1,74 @@
/*
* 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.exceptions.ParseException;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.identification.Server;
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 javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Handles export for different pages.
*
* @author Rsl1122
*/
@Singleton
public class Exporter {
private final PlanFiles files;
private final PlanConfig config;
private final ServerPageExporter serverPageExporter;
private final ErrorHandler errorHandler;
@Inject
public Exporter(
PlanFiles files,
PlanConfig config,
ServerPageExporter serverPageExporter,
ErrorHandler errorHandler
) {
this.files = files;
this.config = config;
this.serverPageExporter = serverPageExporter;
this.errorHandler = errorHandler;
}
public Path getPageExportDirectory() {
Path exportDirectory = Paths.get(config.get(ExportSettings.HTML_EXPORT_PATH));
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
public void exportServerPage(Server server) {
try {
serverPageExporter.export(getPageExportDirectory(), server);
} catch (IOException | NotFoundException | ParseException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
}
}

View File

@ -0,0 +1,80 @@
/*
* 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.storage.file.Resource;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
/**
* Code that handles writing the actual files that are exported.
*
* @author Rsl1122
*/
abstract class FileExporter {
private static void copy(InputStream in, OutputStream out) throws IOException {
int read;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
}
void export(Path to, List<String> content) throws IOException {
Files.createDirectories(to.getParent());
Files.write(to, content, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
}
void export(Path to, String content) throws IOException {
Files.createDirectories(to.getParent());
Files.write(to, Arrays.asList(StringUtils.split(content, "\r\n")), StandardCharsets.UTF_8, StandardOpenOption.CREATE);
}
void export(Path to, Resource resource) throws IOException {
Files.createDirectories(to.getParent());
if (Files.exists(to)) Files.delete(to);
Files.createFile(to);
try (
InputStream in = resource.asInputStream();
OutputStream out = Files.newOutputStream(to)
) {
copy(in, out);
}
}
String toFileName(String resourceName) {
try {
return URLEncoder.encode(resourceName, "UTF-8").replace(".", "%2E");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Unexpected: UTF-8 encoding not supported", e);
}
}
}

View File

@ -28,7 +28,6 @@ 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.settings.theme.Theme;
import com.djrapitops.plan.settings.theme.ThemeVal;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.objects.BaseUserQueries;
@ -54,12 +53,14 @@ import java.util.*;
* @author Rsl1122
*/
@Singleton
@Deprecated
public class HtmlExport extends SpecificExport {
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 ErrorHandler errorHandler;
@ -69,6 +70,7 @@ public class HtmlExport extends SpecificExport {
PlanConfig config,
Theme theme,
DBSystem dbSystem,
Exporter exporter,
PageFactory pageFactory,
JSONFactory jsonFactory,
ServerInfo serverInfo,
@ -79,6 +81,7 @@ public class HtmlExport extends SpecificExport {
this.theme = theme;
this.files = files;
this.dbSystem = dbSystem;
this.exporter = exporter;
this.pageFactory = pageFactory;
this.errorHandler = errorHandler;
}
@ -88,21 +91,13 @@ public class HtmlExport extends SpecificExport {
return config.get(ExportSettings.HTML_EXPORT_PATH);
}
public void exportServer(UUID serverUUID) {
public void exportServer(Server server) {
Database database = dbSystem.getDatabase();
boolean hasProxy = database.query(ServerQueries.fetchProxyServerInformation()).isPresent();
if (serverInfo.getServer().isNotProxy() && hasProxy) {
return;
}
database.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID))
.map(Server::getName)
.ifPresent(serverName -> {
try {
exportAvailableServerPage(serverUUID, serverName);
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
});
// boolean hasProxy = database.query(ServerQueries.fetchProxyServerInformation()).isPresent();
// if (serverInfo.getServer().isNotProxy() && hasProxy) {
// return;
// }
exporter.exportServerPage(server);
}
public void exportPlayerPage(UUID playerUUID) {
@ -175,66 +170,6 @@ public class HtmlExport extends SpecificExport {
// TODO
}
public void exportAvailableServerPages() {
try {
Map<UUID, String> serverNames = dbSystem.getDatabase().query(ServerQueries.fetchServerNames());
for (Map.Entry<UUID, String> entry : serverNames.entrySet()) {
exportAvailableServerPage(entry.getKey(), entry.getValue());
}
} catch (IOException | DBOpException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
}
public void exportCss() {
String[] resources = new String[]{
"web/css/main.css",
"web/css/materialize.css",
"web/css/style.css",
"web/css/themes/all-themes.css"
};
copyFromJar(resources);
}
public void exportJs() {
String[] resources = new String[]{
"web/js/demo.js",
"web/js/admin.js",
"web/js/helpers.js",
"web/js/script.js",
"web/js/graphs.js"
};
copyFromJar(resources);
try {
String demo = files.getCustomizableResourceOrDefault("web/js/demo.js")
.asString()
.replace("${defaultTheme}", theme.getValue(ThemeVal.THEME_DEFAULT));
List<String> lines = Arrays.asList(demo.split("\n"));
File outputFolder = new File(getFolder(), "js");
Verify.isTrue(outputFolder.exists() && outputFolder.isDirectory() || outputFolder.mkdirs(),
() -> new FileNotFoundException("Output folder could not be created at " + outputFolder.getAbsolutePath()));
export(new File(outputFolder, "demo.js"), lines);
} catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e);
}
}
public void exportPlugins() {
String[] resources = new String[]{
"web/plugins/node-waves/waves.css",
"web/plugins/node-waves/waves.js",
"web/plugins/animate-css/animate.css",
"web/plugins/jquery-slimscroll/jquery.slimscroll.js",
"web/plugins/jquery/jquery.min.js",
"web/plugins/fullcalendar/fullcalendar.min.js",
"web/plugins/fullcalendar/fullcalendar.min.css",
"web/plugins/momentjs/moment.js",
};
copyFromJar(resources);
}
private void copyFromJar(String[] resources) {
for (String resource : resources) {
try {
@ -263,4 +198,10 @@ public class HtmlExport extends SpecificExport {
export(to, lines);
}
public void exportAvailableServerPages() {
for (Server server : dbSystem.getDatabase().query(ServerQueries.fetchPlanServerInformationCollection())) {
exportServer(server);
}
}
}

View File

@ -41,6 +41,7 @@ import java.util.UUID;
* @author Rsl1122
*/
@Singleton
@Deprecated
public class JSONExport extends SpecificExport {
private final PlanConfig config;

View File

@ -0,0 +1,194 @@
/*
* 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.delivery.rendering.pages.ServerPage;
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.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
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;
import java.util.UUID;
/**
* Handles exporting of /server page html, data and resources.
*
* @author Rsl1122
*/
@Singleton
public class ServerPageExporter extends FileExporter {
private final PlanFiles files;
private final PageFactory pageFactory;
private final RootJSONHandler jsonHandler;
private final ServerInfo serverInfo;
private final ExportPaths exportPaths;
@Inject
public ServerPageExporter(
PlanFiles files,
PageFactory pageFactory,
RootJSONHandler jsonHandler,
ServerInfo serverInfo // To know if current server is a Proxy
) {
this.files = files;
this.pageFactory = pageFactory;
this.jsonHandler = jsonHandler;
this.serverInfo = serverInfo;
exportPaths = new ExportPaths();
}
public void export(Path toDirectory, Server server) throws IOException, NotFoundException, ParseException {
exportRequiredResources(toDirectory);
exportJSON(toDirectory, server);
exportHtml(toDirectory, server);
}
private void exportHtml(Path toDirectory, Server server) throws IOException, NotFoundException, ParseException {
UUID serverUUID = server.getUuid();
Path to = toDirectory
.resolve(serverInfo.getServer().isProxy() ? "server/" + server.getName() : "server")
.resolve("index.html");
ServerPage serverPage = pageFactory.serverPage(serverUUID);
export(to, exportPaths.resolveExportPaths(serverPage.toHtml()));
}
private void exportJSON(Path toDirectory, Server server) throws IOException, NotFoundException {
String serverName = server.getName();
exportJSON(toDirectory, "serverOverview?server=" + serverName);
exportJSON(toDirectory, "onlineOverview?server=" + serverName);
exportJSON(toDirectory, "sessionsOverview?server=" + serverName);
exportJSON(toDirectory, "playerVersus?server=" + serverName);
exportJSON(toDirectory, "playerbaseOverview?server=" + serverName);
exportJSON(toDirectory, "performanceOverview?server=" + serverName);
exportJSON(toDirectory, "graph?type=performance&server=" + serverName);
exportJSON(toDirectory, "graph?type=aggregatedPing&server=" + serverName);
exportJSON(toDirectory, "graph?type=worldPie&server=" + serverName);
exportJSON(toDirectory, "graph?type=activity&server=" + serverName);
exportJSON(toDirectory, "graph?type=geolocation&server=" + serverName);
exportJSON(toDirectory, "graph?type=uniqueAndNew&server=" + serverName);
exportJSON(toDirectory, "graph?type=serverCalendar&server=" + serverName);
exportJSON(toDirectory, "graph?type=punchCard&server=" + serverName);
exportJSON(toDirectory, "players?server=" + serverName);
exportJSON(toDirectory, "kills?server=" + serverName);
exportJSON(toDirectory, "pingTable?server=" + serverName);
exportJSON(toDirectory, "sessions?server=" + serverName);
}
private void exportJSON(Path toDirectory, String resource) throws NotFoundException, IOException {
Response found = getJSONResponse(resource);
if (found instanceof ErrorResponse) {
throw new NotFoundException(resource + " was not properly exported: " + found.getContent());
}
String jsonResourceName = toFileName(toJSONResourceName(resource)) + ".json";
export(toDirectory.resolve("data").resolve(jsonResourceName), found.getContent());
exportPaths.put("../v1/" + resource, 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 {
// Style
exportResource(toDirectory, "css/sb-admin-2.css");
exportResource(toDirectory, "css/style.css");
exportImage(toDirectory, "img/Flaticon_circle.png");
// Plugins
exportResource(toDirectory, "vendor/jquery/jquery.min.js");
exportResource(toDirectory, "vendor/bootstrap/js/bootstrap.bundle.min.js");
exportResource(toDirectory, "vendor/jquery-easing/jquery.easing.min.js");
exportResource(toDirectory, "vendor/datatables/jquery.dataTables.min.js");
exportResource(toDirectory, "vendor/datatables/dataTables.bootstrap4.min.js");
exportResource(toDirectory, "vendor/highcharts/highstock.js");
exportResource(toDirectory, "vendor/highcharts/map.js");
exportResource(toDirectory, "vendor/highcharts/world.js");
exportResource(toDirectory, "vendor/highcharts/drilldown.js");
exportResource(toDirectory, "vendor/highcharts/highcharts-more.js");
exportResource(toDirectory, "vendor/highcharts/no-data-to-display.js");
exportResource(toDirectory, "vendor/fullcalendar/fullcalendar.min.css");
exportResource(toDirectory, "vendor/momentjs/moment.js");
exportResource(toDirectory, "vendor/fullcalendar/fullcalendar.min.js");
// Page level plugins
exportResource(toDirectory, "js/sb-admin-2.js");
exportResource(toDirectory, "js/xmlhttprequests.js");
exportResource(toDirectory, "js/color-selector.js");
// Page level scripts
exportResource(toDirectory, "js/sessionAccordion.js");
exportResource(toDirectory, "js/pingTable.js");
exportResource(toDirectory, "js/graphs.js");
exportResource(toDirectory, "js/server-values.js");
}
private void exportResource(Path toDirectory, String resourceName) throws IOException {
Resource resource = files.getCustomizableResourceOrDefault("web/" + resourceName);
Path to = toDirectory.resolve(resourceName);
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) {
// Server html is exported at /server/<name>/index.html or /server/index.html
return (serverInfo.getServer().isProxy() ? "../../" : "../") + toNonRelativePath(resourceName);
}
private String toNonRelativePath(String resourceName) {
return StringUtils.remove(resourceName, "../");
}
}

View File

@ -29,7 +29,6 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@ -40,6 +39,7 @@ import java.util.UUID;
*
* @author Rsl1122
*/
@Deprecated
public abstract class SpecificExport {
private final PlanFiles files;
@ -110,46 +110,4 @@ public abstract class SpecificExport {
String html = response.getContent();
exportPlayerPage(name, html);
}
void exportAvailableServerPage(UUID serverUUID, String serverName) throws IOException {
// TODO Force export in the future
Response response = null;
if (response == null) {
return;
}
String html = response.getContent()
.replace("href=\"plugins/", "href=\"../plugins/")
.replace("href=\"css/", "href=\"../css/")
.replace("src=\"plugins/", "src=\"../plugins/")
.replace("src=\"js/", "src=\"../js/")
.replace("../json/players?serverName=" + serverName, "./players_table.json");
File htmlLocation;
if (serverInfo.getServer().isProxy()) {
if (serverUUID.equals(serverInfo.getServerUUID())) {
htmlLocation = new File(getFolder(), "network");
} else {
htmlLocation = new File(getServerFolder(), URLEncoder.encode(serverName, "UTF-8").replace(".", "%2E"));
html = html.replace("../", "../../");
exportPlayersTableJSON(htmlLocation, serverUUID);
}
} else {
htmlLocation = getServerFolder();
exportPlayersTableJSON(htmlLocation, serverUUID);
}
htmlLocation.mkdirs();
File exportFile = new File(htmlLocation, "index.html");
List<String> lines = Arrays.asList(html.split("\n"));
export(exportFile, lines);
}
private void exportPlayersTableJSON(File htmlLocation, UUID serverUUID) throws IOException {
htmlLocation.mkdirs();
File exportFile = new File(htmlLocation, "players_table.json");
export(exportFile, Collections.singletonList(jsonFactory.serverPlayersTableJSON(serverUUID)));
}
}

View File

@ -53,6 +53,10 @@ public class PlanFiles implements SubSystem {
return dataFolder;
}
public Path getDataDirectory() {
return dataFolder.toPath();
}
public File getLogsFolder() {
File folder = getFileFromPluginFolder("logs");
folder.mkdirs();

View File

@ -34,7 +34,6 @@
<!-- Java Style -->
<module name="ArrayTypeStyle"/>
<module name="AvoidNestedBlocks"/>
<module name="CommentsIndentation"/>
<module name="DefaultComesLast"> <!-- Switches -->
<property name="skipIfLastAndSharedWithCase" value="true"/>
</module>