From 6602483b18ff556147094c2e6b816b0898ed698e Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 13 May 2019 11:28:35 +0100 Subject: Include sender metadata too --- .../java/me/lucko/spark/common/ActivityLog.java | 34 ++++----------- .../java/me/lucko/spark/common/CommandSender.java | 51 ++++++++++++++++++++++ .../common/command/modules/ActivityLogModule.java | 2 +- .../spark/common/heapdump/HeapDumpSummary.java | 14 ++++-- .../me/lucko/spark/common/sampler/Sampler.java | 20 ++++++--- .../lucko/spark/common/sampler/ThreadDumper.java | 7 ++- 6 files changed, 87 insertions(+), 41 deletions(-) (limited to 'spark-common/src') diff --git a/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java b/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java index 9228938..7327e2b 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java +++ b/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java @@ -36,7 +36,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.LinkedList; import java.util.List; -import java.util.UUID; import java.util.concurrent.TimeUnit; public class ActivityLog { @@ -132,8 +131,7 @@ public class ActivityLog { } public static final class Activity { - private final String user; - private final UUID uuid; + private final CommandSender.Data user; private final long time; private final String type; @@ -141,23 +139,22 @@ public class ActivityLog { private final String dataValue; public static Activity urlActivity(CommandSender user, long time, String type, String url) { - return new Activity(user.getName(), user.getUniqueId(), time, type, "url", url); + return new Activity(user.toData(), time, type, "url", url); } public static Activity fileActivity(CommandSender user, long time, String type, String filePath) { - return new Activity(user.getName(), user.getUniqueId(), time, type, "file", filePath); + return new Activity(user.toData(), time, type, "file", filePath); } - private Activity(String user, UUID uuid, long time, String type, String dataType, String dataValue) { + private Activity(CommandSender.Data user, long time, String type, String dataType, String dataValue) { this.user = user; - this.uuid = uuid; this.time = time; this.type = type; this.dataType = dataType; this.dataValue = dataValue; } - public String getUser() { + public CommandSender.Data getUser() { return this.user; } @@ -188,14 +185,7 @@ public class ActivityLog { public JsonObject serialize() { JsonObject object = new JsonObject(); - JsonObject user = new JsonObject(); - user.add("type", new JsonPrimitive(this.uuid != null ? "player" : "other")); - user.add("name", new JsonPrimitive(this.user)); - if (this.uuid != null) { - user.add("uuid", new JsonPrimitive(this.uuid.toString())); - } - object.add("user", user); - + object.add("user", this.user.serialize()); object.add("time", new JsonPrimitive(this.time)); object.add("type", new JsonPrimitive(this.type)); @@ -210,15 +200,7 @@ public class ActivityLog { public static Activity deserialize(JsonElement element) { JsonObject object = element.getAsJsonObject(); - JsonObject userObject = object.get("user").getAsJsonObject(); - String user = userObject.get("name").getAsJsonPrimitive().getAsString(); - UUID uuid; - if (userObject.has("uuid")) { - uuid = UUID.fromString(userObject.get("uuid").getAsJsonPrimitive().getAsString()); - } else { - uuid = null; - } - + CommandSender.Data user = CommandSender.Data.deserialize(object.get("user")); long time = object.get("time").getAsJsonPrimitive().getAsLong(); String type = object.get("type").getAsJsonPrimitive().getAsString(); @@ -226,7 +208,7 @@ public class ActivityLog { String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString(); String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString(); - return new Activity(user, uuid, time, type, dataType, dataValue); + return new Activity(user, time, type, dataType, dataValue); } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java b/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java index 4c684ba..aca5fba 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java +++ b/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java @@ -20,6 +20,9 @@ package me.lucko.spark.common; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; import net.kyori.text.Component; import java.util.UUID; @@ -34,4 +37,52 @@ public interface CommandSender { boolean hasPermission(String permission); + default Data toData() { + return new Data(getName(), getUniqueId()); + } + + final class Data { + private final String name; + private final UUID uniqueId; + + public Data(String name, UUID uniqueId) { + this.name = name; + this.uniqueId = uniqueId; + } + + public String getName() { + return this.name; + } + + public UUID getUniqueId() { + return this.uniqueId; + } + + public boolean isPlayer() { + return this.uniqueId != null; + } + + public JsonObject serialize() { + JsonObject user = new JsonObject(); + user.add("type", new JsonPrimitive(isPlayer() ? "player" : "other")); + user.add("name", new JsonPrimitive(this.name)); + if (this.uniqueId != null) { + user.add("uniqueId", new JsonPrimitive(this.uniqueId.toString())); + } + return user; + } + + public static CommandSender.Data deserialize(JsonElement element) { + JsonObject userObject = element.getAsJsonObject(); + String user = userObject.get("name").getAsJsonPrimitive().getAsString(); + UUID uuid; + if (userObject.has("uniqueId")) { + uuid = UUID.fromString(userObject.get("uniqueId").getAsJsonPrimitive().getAsString()); + } else { + uuid = null; + } + return new CommandSender.Data(user, uuid); + } + } + } diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java index e2bd80d..c51fe2c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java @@ -64,7 +64,7 @@ public class ActivityLogModule implements CommandModule { ); resp.replyPrefixed(TextComponent.builder(" ") .append(TextComponent.of("Created by: ", TextColor.GRAY)) - .append(TextComponent.of(activity.getUser(), TextColor.WHITE)) + .append(TextComponent.of(activity.getUser().getName(), TextColor.WHITE)) .build() ); diff --git a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java index c95e9ab..6021d92 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java +++ b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java @@ -20,7 +20,9 @@ package me.lucko.spark.common.heapdump; +import com.google.gson.Gson; import com.google.gson.stream.JsonWriter; +import me.lucko.spark.common.CommandSender; import me.lucko.spark.common.util.TypeDescriptors; import javax.management.JMX; @@ -106,9 +108,15 @@ public final class HeapDumpSummary { this.entries = entries; } - private void writeOutput(JsonWriter writer) throws IOException { + private void writeOutput(JsonWriter writer, CommandSender creator) throws IOException { writer.beginObject(); writer.name("type").value("heap"); + + writer.name("metadata").beginObject(); + writer.name("user"); + new Gson().toJson(creator.toData().serialize(), writer); + writer.endObject(); + writer.name("entries").beginArray(); for (Entry entry : this.entries) { writer.beginObject(); @@ -122,11 +130,11 @@ public final class HeapDumpSummary { writer.endObject(); } - public byte[] formCompressedDataPayload() { + public byte[] formCompressedDataPayload(CommandSender creator) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(byteOut), StandardCharsets.UTF_8)) { try (JsonWriter jsonWriter = new JsonWriter(writer)) { - writeOutput(jsonWriter); + writeOutput(jsonWriter, creator); } } catch (IOException e) { throw new RuntimeException(e); diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java index f68a611..a02eec5 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java @@ -22,7 +22,9 @@ package me.lucko.spark.common.sampler; import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.google.gson.Gson; import com.google.gson.stream.JsonWriter; +import me.lucko.spark.common.CommandSender; import me.lucko.spark.common.sampler.aggregator.DataAggregator; import me.lucko.spark.common.sampler.aggregator.SimpleDataAggregator; import me.lucko.spark.common.sampler.aggregator.TickedDataAggregator; @@ -164,25 +166,29 @@ public class Sampler implements Runnable { } private void writeMetadata(JsonWriter writer) throws IOException { - writer.name("startTime").value(startTime); - writer.name("interval").value(interval); + writer.name("startTime").value(this.startTime); + writer.name("interval").value(this.interval); writer.name("threadDumper").beginObject(); - threadDumper.writeMetadata(writer); + this.threadDumper.writeMetadata(writer); writer.endObject(); writer.name("dataAggregator").beginObject(); - dataAggregator.writeMetadata(writer); + this.dataAggregator.writeMetadata(writer); writer.endObject(); } - private void writeOutput(JsonWriter writer) throws IOException { + private void writeOutput(JsonWriter writer, CommandSender creator) throws IOException { writer.beginObject(); writer.name("type").value("sampler"); writer.name("metadata").beginObject(); writeMetadata(writer); + + writer.name("user"); + new Gson().toJson(creator.toData().serialize(), writer); + writer.endObject(); writer.name("threads").beginArray(); @@ -203,11 +209,11 @@ public class Sampler implements Runnable { writer.endObject(); } - public byte[] formCompressedDataPayload() { + public byte[] formCompressedDataPayload(CommandSender creator) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(byteOut), StandardCharsets.UTF_8)) { try (JsonWriter jsonWriter = new JsonWriter(writer)) { - writeOutput(jsonWriter); + writeOutput(jsonWriter, creator); } } catch (IOException e) { throw new RuntimeException(e); diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java index 2ceec04..fee130e 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/ThreadDumper.java @@ -76,7 +76,6 @@ public interface ThreadDumper { * Implementation of {@link ThreadDumper} that generates data for a specific set of threads. */ final class Specific implements ThreadDumper { - private final ThreadFinder threadFinder = new ThreadFinder(); private final long[] ids; public Specific(long[] ids) { @@ -85,7 +84,7 @@ public interface ThreadDumper { public Specific(Set names) { Set namesLower = names.stream().map(String::toLowerCase).collect(Collectors.toSet()); - this.ids = this.threadFinder.getThreads() + this.ids = new ThreadFinder().getThreads() .filter(t -> namesLower.contains(t.getName().toLowerCase())) .mapToLong(Thread::getId) .toArray(); @@ -100,7 +99,7 @@ public interface ThreadDumper { public void writeMetadata(JsonWriter writer) throws IOException { writer.name("type").value("specific"); writer.name("ids").beginArray(); - for (long id : ids) { + for (long id : this.ids) { writer.value(id); } writer.endArray(); @@ -155,7 +154,7 @@ public interface ThreadDumper { public void writeMetadata(JsonWriter writer) throws IOException { writer.name("type").value("regex"); writer.name("patterns").beginArray(); - for (Pattern pattern : namePatterns) { + for (Pattern pattern : this.namePatterns) { writer.value(pattern.pattern()); } writer.endArray(); -- cgit