From c9a811c758875363e9d4045575c9b1fc42c45c76 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 29 Oct 2023 23:18:35 +0200 Subject: [PATCH] Add configs to DebugReport --- .../manager/ConnectionConfigManager.java | 2 +- .../manager/MainConfigManager.java | 2 +- .../manager/MessagesConfigManager.java | 4 ++ .../manager/MessagesConfigSingleManager.java | 2 +- .../manager/abstraction/ConfigManager.java | 3 + .../abstraction/ConfigurateConfigManager.java | 13 +++-- .../manager/loader/ConfigLoaderProvider.java | 5 +- .../discordsrv/common/debug/DebugReport.java | 55 ++++++++++++++++--- 8 files changed, 68 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/ConnectionConfigManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/ConnectionConfigManager.java index 7c21fb01..7d567e2b 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/ConnectionConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/ConnectionConfigManager.java @@ -45,7 +45,7 @@ public abstract class ConnectionConfigManager } @Override - protected String fileName() { + public String fileName() { return ConnectionConfig.FILE_NAME; } } diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/MainConfigManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/MainConfigManager.java index d9e07673..86923fd9 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/MainConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/MainConfigManager.java @@ -45,7 +45,7 @@ public abstract class MainConfigManager } @Override - protected String fileName() { + public String fileName() { return MainConfig.FILE_NAME; } } diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigManager.java index d890b676..ade13809 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigManager.java @@ -25,6 +25,10 @@ public abstract class MessagesConfigManager { public abstract C createConfiguration(); + public Map> getAllManagers() { + return Collections.unmodifiableMap(configs); + } + public MessagesConfigSingleManager getManager(Locale locale) { synchronized (configs) { return configs.get(locale); diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigSingleManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigSingleManager.java index bf1ad923..76380133 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigSingleManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/MessagesConfigSingleManager.java @@ -24,7 +24,7 @@ public class MessagesConfigSingleManager } @Override - protected String fileName() { + public String fileName() { if (multi) { return aggregateManager.directory().resolve(locale.getISO3Language() + ".yaml").toString(); } diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigManager.java index 69bbd326..c69f83b1 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigManager.java @@ -19,6 +19,8 @@ package com.discordsrv.common.config.configurate.manager.abstraction; import com.discordsrv.common.exception.ConfigException; +import org.spongepowered.configurate.CommentedConfigurationNode; +import org.spongepowered.configurate.loader.AbstractConfigurationLoader; public interface ConfigManager { @@ -27,5 +29,6 @@ public interface ConfigManager { void load() throws ConfigException; void reload() throws ConfigException; + void save(AbstractConfigurationLoader loader) throws ConfigException; void save() throws ConfigException; } diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java index 99c460cc..11f180ed 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/abstraction/ConfigurateConfigManager.java @@ -94,7 +94,7 @@ public abstract class ConfigurateConfigManager loader) throws ConfigException { try { - LT loader = loader(); CommentedConfigurationNode node = loader.createNode(); save(configuration, (Class) configuration.getClass(), node); loader.save(node); @@ -403,6 +402,12 @@ public abstract class ConfigurateConfigManager clazz, CommentedConfigurationNode node) throws SerializationException { objectMapper().get(clazz).save(config, node); } diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/manager/loader/ConfigLoaderProvider.java b/common/src/main/java/com/discordsrv/common/config/configurate/manager/loader/ConfigLoaderProvider.java index d20d46fc..385545ad 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/manager/loader/ConfigLoaderProvider.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/manager/loader/ConfigLoaderProvider.java @@ -35,11 +35,10 @@ public interface ConfigLoaderProvider createBuilder(); @ApiStatus.NonExtendable - default LT createLoader(Path configFile, ConfigurationOptions options) { + default AbstractConfigurationLoader.Builder createLoader(Path configFile, ConfigurationOptions options) { return createBuilder() .path(configFile) .defaultOptions(options) - .headerMode(headerMode()) - .build(); + .headerMode(headerMode()); } } diff --git a/common/src/main/java/com/discordsrv/common/debug/DebugReport.java b/common/src/main/java/com/discordsrv/common/debug/DebugReport.java index 0c785503..65a94748 100644 --- a/common/src/main/java/com/discordsrv/common/debug/DebugReport.java +++ b/common/src/main/java/com/discordsrv/common/debug/DebugReport.java @@ -19,9 +19,13 @@ package com.discordsrv.common.debug; import com.discordsrv.common.DiscordSRV; +import com.discordsrv.common.config.configurate.manager.MessagesConfigSingleManager; +import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager; +import com.discordsrv.common.config.messages.MessagesConfig; import com.discordsrv.common.debug.file.DebugFile; import com.discordsrv.common.debug.file.KeyValueDebugFile; import com.discordsrv.common.debug.file.TextDebugFile; +import com.discordsrv.common.exception.ConfigException; import com.discordsrv.common.paste.Paste; import com.discordsrv.common.paste.PasteService; import com.discordsrv.common.plugin.Plugin; @@ -30,8 +34,12 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import net.dv8tion.jda.api.JDA; import org.apache.commons.lang3.exception.ExceptionUtils; +import org.spongepowered.configurate.CommentedConfigurationNode; +import org.spongepowered.configurate.loader.AbstractConfigurationLoader; +import java.io.BufferedWriter; import java.io.IOException; +import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.nio.file.FileStore; import java.nio.file.Files; @@ -44,7 +52,7 @@ import java.util.zip.ZipOutputStream; public class DebugReport { - private static final int BIG_FILE_SPLIT_SIZE = 20000; + private static final int BIG_FILE_SPLIT_SIZE = 50000; private final List files = new ArrayList<>(); private final DiscordSRV discordSRV; @@ -56,10 +64,16 @@ public class DebugReport { public void generate() { discordSRV.eventBus().publish(new DebugGenerateEvent(this)); - addFile(environment()); - addFile(plugins()); + addFile(environment()); // 100 + addFile(plugins()); // 90 for (Path debugLog : discordSRV.logger().getDebugLogs()) { - addFile(readFile(1, debugLog)); + addFile(readFile(80, debugLog)); + } + addFile(config(79, discordSRV.configManager())); + addFile(rawConfig(79, discordSRV.configManager())); + for (MessagesConfigSingleManager manager : discordSRV.messagesConfigManager().getAllManagers().values()) { + addFile(config(78, manager)); + addFile(rawConfig(78, manager)); } } @@ -160,7 +174,7 @@ public class DebugReport { } catch (IOException ignored) {} values.put("docker", docker); - return new KeyValueDebugFile(10, "environment.json", values); + return new KeyValueDebugFile(100, "environment.json", values); } private DebugFile plugins() { @@ -169,14 +183,35 @@ public class DebugReport { .sorted(Comparator.comparing(plugin -> plugin.name().toLowerCase(Locale.ROOT))) .collect(Collectors.toList()); + int order = 90; + String fileName = "plugins.json"; try { String json = discordSRV.json().writeValueAsString(plugins); - return new TextDebugFile(5, "plugins.json", json); + return new TextDebugFile(order, fileName, json); } catch (JsonProcessingException e) { - return null; + return exception(order, fileName, e); } } + private DebugFile config(int order, ConfigurateConfigManager manager) { + String fileName = "parsed_" + manager.fileName(); + try (StringWriter writer = new StringWriter()) { + AbstractConfigurationLoader loader = manager + .createLoader(manager.filePath(), manager.nodeOptions(true)) + .sink(() -> new BufferedWriter(writer)) + .build(); + manager.save(loader); + + return new TextDebugFile(order, fileName, writer.toString()); + } catch (IOException | ConfigException e) { + return exception(order, fileName, e); + } + } + + private DebugFile rawConfig(int order, ConfigurateConfigManager manager) { + return readFile(order, manager.filePath()); + } + private DebugFile readFile(int order, Path file) { String fileName = file.getFileName().toString(); if (!Files.exists(file)) { @@ -187,7 +222,11 @@ public class DebugReport { List lines = Files.readAllLines(file, StandardCharsets.UTF_8); return new TextDebugFile(order, fileName, String.join("\n", lines)); } catch (IOException e) { - return new TextDebugFile(order, fileName, ExceptionUtils.getStackTrace(e)); + return exception(order, fileName, e); } } + + private DebugFile exception(int order, String fileName, Throwable throwable) { + return new TextDebugFile(order, fileName, ExceptionUtils.getStackTrace(throwable)); + } }