diff options
author | Luck <git@lucko.me> | 2019-05-13 10:57:06 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-05-13 10:57:06 +0100 |
commit | 32f6355c8c7fc1611140bfcce6afe8b25cea5697 (patch) | |
tree | d2f87081d98f312ff46ba734c104ff730c848d4f /spark-common/src | |
parent | cf3ce4d9d7c443e0051eb7cff7dc71cf2a0eb851 (diff) | |
download | spark-32f6355c8c7fc1611140bfcce6afe8b25cea5697.tar.gz spark-32f6355c8c7fc1611140bfcce6afe8b25cea5697.tar.bz2 spark-32f6355c8c7fc1611140bfcce6afe8b25cea5697.zip |
Activity log improvements
Diffstat (limited to 'spark-common/src')
5 files changed, 74 insertions, 24 deletions
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 4b5e942..9228938 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,6 +36,7 @@ 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 { @@ -45,7 +46,7 @@ public class ActivityLog { private final Path file; - private final List<Activity> log = new LinkedList<>(); + private final LinkedList<Activity> log = new LinkedList<>(); private final Object[] mutex = new Object[0]; public ActivityLog(Path file) { @@ -54,7 +55,7 @@ public class ActivityLog { public void addToLog(Activity activity) { synchronized (this.mutex) { - this.log.add(activity); + this.log.addFirst(activity); } save(); } @@ -132,15 +133,28 @@ public class ActivityLog { public static final class Activity { private final String user; + private final UUID uuid; private final long time; private final String type; - private final String url; - public Activity(String user, long time, String type, String url) { + private final String dataType; + 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); + } + + public static Activity fileActivity(CommandSender user, long time, String type, String filePath) { + return new Activity(user.getName(), user.getUniqueId(), time, type, "file", filePath); + } + + private Activity(String user, UUID uuid, long time, String type, String dataType, String dataValue) { this.user = user; + this.uuid = uuid; this.time = time; this.type = type; - this.url = url; + this.dataType = dataType; + this.dataValue = dataValue; } public String getUser() { @@ -155,26 +169,39 @@ public class ActivityLog { return this.type; } - public String getUrl() { - return this.url; + public String getDataType() { + return this.dataType; + } + + public String getDataValue() { + return this.dataValue; } public boolean shouldExpire() { - return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(7); + if (dataType.equals("url")) { + return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(7); + } else { + return false; + } } 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("time", new JsonPrimitive(this.time)); object.add("type", new JsonPrimitive(this.type)); JsonObject data = new JsonObject(); - data.add("url", new JsonPrimitive(this.url)); + data.add("type", new JsonPrimitive(this.dataType)); + data.add("value", new JsonPrimitive(this.dataValue)); object.add("data", data); return object; @@ -183,12 +210,23 @@ public class ActivityLog { public static Activity deserialize(JsonElement element) { JsonObject object = element.getAsJsonObject(); - String user = object.get("user").getAsJsonObject().get("name").getAsJsonPrimitive().getAsString(); + 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; + } + long time = object.get("time").getAsJsonPrimitive().getAsLong(); String type = object.get("type").getAsJsonPrimitive().getAsString(); - String url = object.get("data").getAsJsonObject().get("url").getAsJsonPrimitive().getAsString(); - return new Activity(user, time, type, url); + JsonObject dataObject = object.get("data").getAsJsonObject(); + String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString(); + String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString(); + + return new Activity(user, uuid, 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 9d2ecc0..4c684ba 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 @@ -22,10 +22,14 @@ package me.lucko.spark.common; import net.kyori.text.Component; +import java.util.UUID; + public interface CommandSender { String getName(); + UUID getUniqueId(); + void sendMessage(Component message); boolean hasPermission(String permission); 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 36ba47c..e2bd80d 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 @@ -67,14 +67,18 @@ public class ActivityLogModule implements CommandModule { .append(TextComponent.of(activity.getUser(), TextColor.WHITE)) .build() ); + + TextComponent.Builder valueComponent = TextComponent.builder(activity.getDataValue()) + .color(TextColor.WHITE) + .decoration(TextDecoration.UNDERLINED, true); + + if (activity.getDataType().equals("url")) { + valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue())); + } + resp.replyPrefixed(TextComponent.builder(" ") - .append(TextComponent.of("Link: ", TextColor.GRAY)) - .append(TextComponent.builder(activity.getUrl()) - .color(TextColor.WHITE) - .decoration(TextDecoration.UNDERLINED, true) - .clickEvent(ClickEvent.openUrl(activity.getUrl())) - .build() - ) + .append(TextComponent.of(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", TextColor.GRAY)) + .append(valueComponent) .build() ); resp.reply(TextComponent.space()); diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java index e3f1cdb..42c03d0 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java @@ -20,7 +20,7 @@ package me.lucko.spark.common.command.modules; -import me.lucko.spark.common.ActivityLog; +import me.lucko.spark.common.ActivityLog.Activity; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; @@ -77,7 +77,7 @@ public class MemoryModule implements CommandModule { .build() ); - platform.getActivityLog().addToLog(new ActivityLog.Activity(sender.getName(), System.currentTimeMillis(), "Heap dump summary", url)); + platform.getActivityLog().addToLog(Activity.urlActivity(sender, System.currentTimeMillis(), "Heap dump summary", url)); } catch (IOException e) { resp.broadcastPrefixed(TextComponent.of("An error occurred whilst uploading the data.", TextColor.RED)); e.printStackTrace(); @@ -119,7 +119,11 @@ public class MemoryModule implements CommandModule { return; } - resp.broadcastPrefixed(TextComponent.of("Heap dump written to: " + file.toString(), TextColor.GOLD)); + resp.broadcastPrefixed(TextComponent.builder("Heap dump written to: ", TextColor.GOLD) + .append(TextComponent.of(file.toString(), TextColor.DARK_GRAY)) + .build() + ); + platform.getActivityLog().addToLog(Activity.fileActivity(sender, System.currentTimeMillis(), "Heap dump", file.toString())); }); }) .tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--run-gc-before", "--include-non-live")) diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index 11d23da..4ea6144 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -20,7 +20,7 @@ package me.lucko.spark.common.command.modules; -import me.lucko.spark.common.ActivityLog; +import me.lucko.spark.common.ActivityLog.Activity; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; @@ -255,7 +255,7 @@ public class SamplerModule implements CommandModule { .build() ); - platform.getActivityLog().addToLog(new ActivityLog.Activity(resp.sender().getName(), System.currentTimeMillis(), "Sampler", url)); + platform.getActivityLog().addToLog(Activity.urlActivity(resp.sender(), System.currentTimeMillis(), "Sampler", url)); } catch (IOException e) { resp.broadcastPrefixed(TextComponent.of("An error occurred whilst uploading the results.", TextColor.RED)); e.printStackTrace(); |