Skip to content

Commit

Permalink
feat: improve config parsing report feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrim4 committed Oct 7, 2024
1 parent bf7171d commit 9beb85d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface Config {

byte[] systemHash();

String report();

GeneralConfig general();

AdvancedConfig advanced();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
Base64.Encoder encoder = Base64.getUrlEncoder();

String latestLog = OFCLogger.getLatestVerboseLog();
root.addProperty("verbose_log", encoder.encodeToString(latestLog.getBytes(StandardCharsets.UTF_8)));
root.addProperty("verboseLog", encoder.encodeToString(latestLog.getBytes(StandardCharsets.UTF_8)));

try {
Path configPath = orebfuscator.getDataFolder().toPath().resolve("config.yml");
Expand All @@ -123,6 +123,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
e.printStackTrace();
}

String configReport = orebfuscator.getOrebfuscatorConfig().report();
configReport = configReport != null ? configReport : "";
root.addProperty("configReport", encoder.encodeToString(configReport.getBytes(StandardCharsets.UTF_8)));

Path path = orebfuscator.getDataFolder().toPath().resolve("dump-" + fileFormat.format(now) + ".json");
try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(path))) {
writer.setIndent(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;

import com.google.gson.JsonObject;

Expand All @@ -28,8 +28,9 @@ private static String formatBytes(long bytes) {
private final AtomicLong cacheHitCountDisk = new AtomicLong(0);
private final AtomicLong cacheMissCount = new AtomicLong(0);
private final AtomicLong cacheEstimatedSize = new AtomicLong(0);
private IntSupplier diskCacheQueueLength = () -> 0;
private IntSupplier obfuscationQueueLength = () -> 0;
private LongSupplier memoryCacheSize = () -> 0;
private LongSupplier diskCacheQueueLength = () -> 0;
private LongSupplier obfuscationQueueLength = () -> 0;

public void onCacheHitMemory() {
this.cacheHitCountMemory.incrementAndGet();
Expand All @@ -47,11 +48,15 @@ public void onCacheSizeChange(int delta) {
this.cacheEstimatedSize.addAndGet(delta);
}

public void setDiskCacheQueueLengthSupplier(IntSupplier supplier) {
public void setMemoryCacheSizeSupplier(LongSupplier supplier) {
this.memoryCacheSize = Objects.requireNonNull(supplier);
}

public void setDiskCacheQueueLengthSupplier(LongSupplier supplier) {
this.diskCacheQueueLength = Objects.requireNonNull(supplier);
}

public void setObfuscationQueueLengthSupplier(IntSupplier supplier) {
public void setObfuscationQueueLengthSupplier(LongSupplier supplier) {
this.obfuscationQueueLength = Objects.requireNonNull(supplier);
}

Expand All @@ -61,8 +66,9 @@ public String toString() {
long cacheHitCountDisk = this.cacheHitCountDisk.get();
long cacheMissCount = this.cacheMissCount.get();
long cacheEstimatedSize = this.cacheEstimatedSize.get();
long diskCacheQueueLength = this.diskCacheQueueLength.getAsInt();
long obfuscationQueueLength = this.obfuscationQueueLength.getAsInt();
long memoryCacheSize = this.memoryCacheSize.getAsLong();
long diskCacheQueueLength = this.diskCacheQueueLength.getAsLong();
long obfuscationQueueLength = this.obfuscationQueueLength.getAsLong();

double totalCacheRequest = (double) (cacheHitCountMemory + cacheHitCountDisk + cacheMissCount);

Expand All @@ -78,6 +84,8 @@ public String toString() {
builder.append(" - memoryCacheHitRate: ").append(formatPrecent(memoryCacheHitRate)).append('\n');
builder.append(" - diskCacheHitRate: ").append(formatPrecent(diskCacheHitRate)).append('\n');
builder.append(" - memoryCacheEstimatedSize: ").append(formatBytes(cacheEstimatedSize)).append('\n');
builder.append(" - memoryCacheBytesPerEntry: ").append(formatBytes(cacheEstimatedSize / memoryCacheSize)).append('\n');
builder.append(" - memoryCacheEntries: ").append(memoryCacheSize).append('\n');
builder.append(" - diskCacheQueueLength: ").append(diskCacheQueueLength).append('\n');
builder.append(" - obfuscationQueueLength: ").append(obfuscationQueueLength).append('\n');

Expand All @@ -91,8 +99,9 @@ public JsonObject toJson() {
object.addProperty("cacheHitCountDisk", this.cacheHitCountDisk.get());
object.addProperty("cacheMissCount", this.cacheMissCount.get());
object.addProperty("cacheEstimatedSize", this.cacheEstimatedSize.get());
object.addProperty("diskCacheQueueLength", this.diskCacheQueueLength.getAsInt());
object.addProperty("obfuscationQueueLength", this.obfuscationQueueLength.getAsInt());
object.addProperty("memoryCacheSize", this.memoryCacheSize.getAsLong());
object.addProperty("diskCacheQueueLength", this.diskCacheQueueLength.getAsLong());
object.addProperty("obfuscationQueueLength", this.obfuscationQueueLength.getAsLong());

return object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ObfuscationCache(Orebfuscator orebfuscator) {
.expireAfterAccess(this.cacheConfig.expireAfterAccess(), TimeUnit.MILLISECONDS)
.removalListener(this::onRemoval)
.build();
this.statistics.setMemoryCacheSizeSupplier(() -> this.cache.size());

if (this.cacheConfig.enableDiskCache()) {
this.serializer = new AsyncChunkSerializer(orebfuscator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class OrebfuscatorConfig implements Config {
private final Plugin plugin;

private byte[] systemHash;
private String configReport;

public OrebfuscatorConfig(Plugin plugin) {
this.plugin = plugin;
Expand All @@ -63,7 +64,7 @@ public void load() {

DefaultConfigParsingContext context = new DefaultConfigParsingContext();
this.deserialize(this.plugin.getConfig(), context);
context.report();
this.configReport = context.report();

if (context.hasErrors()) {
throw new IllegalArgumentException("Can't parse config due to errors, Orebfuscator will now disable itself!");
Expand Down Expand Up @@ -206,6 +207,11 @@ public byte[] systemHash() {
return systemHash;
}

@Override
public String report() {
return configReport;
}

@Override
public GeneralConfig general() {
return this.generalConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.imprex.orebfuscator.Orebfuscator;
import net.imprex.orebfuscator.UpdateSystem;
import net.imprex.orebfuscator.config.OrebfuscatorConfig;
import net.imprex.orebfuscator.util.ConsoleUtil;
import net.imprex.orebfuscator.util.PermissionUtil;

public class DeobfuscationListener implements Listener {
Expand Down Expand Up @@ -104,6 +105,10 @@ public void onJoin(PlayerJoinEvent event) {
}

if (PermissionUtil.canAccessAdminTools(player)) {
String configReport = this.config.report();
if (configReport != null) {
player.sendMessage("[§bOrebfuscator§f]§c " + ConsoleUtil.replaceAnsiColorWithChatColor(configReport));
}
this.updateSystem.checkForUpdates(player);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.logging.Level;

import org.bukkit.ChatColor;

public final class ConsoleUtil {

private static final int BOX_PADDING = 3;
Expand All @@ -13,6 +15,13 @@ public final class ConsoleUtil {
private ConsoleUtil() {
}

public static String replaceAnsiColorWithChatColor(String value) {
value = value.replaceAll("\u001B\\[m", ChatColor.RESET.toString());
value = value.replaceAll("\u001B\\[31;1m", ChatColor.RED.toString());
value = value.replaceAll("\u001B\\[33;1m", ChatColor.YELLOW.toString());
return value;
}

public static void printBox(Level level, String...lines) {
for (String line : createBox(lines)) {
OFCLogger.log(level, line);
Expand Down

0 comments on commit 9beb85d

Please sign in to comment.