prettify the verbose paste output

This commit is contained in:
Luck 2017-01-26 19:47:46 +00:00
parent f27034bb35
commit a617675771
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 67 additions and 23 deletions

View File

@ -32,6 +32,9 @@ import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.debug.DebugListener; import me.lucko.luckperms.common.debug.DebugListener;
import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.Predicates;
import io.github.mkremins.fanciful.ChatColor;
import io.github.mkremins.fanciful.FancyMessage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -90,12 +93,15 @@ public class VerboseCommand extends SingleCommand {
if (listener == null) { if (listener == null) {
Message.VERBOSE_OFF.send(sender); Message.VERBOSE_OFF.send(sender);
} else { } else {
Message.VERBOSE_RECORDING_UPLOAD_START.send(sender);
String url = listener.uploadPastedData(); String url = listener.uploadPastedData();
if (url == null) { if (url == null) {
url = "null"; url = "null";
} }
Message.VERBOSE_RECORDING_URL.send(sender, url); Message.VERBOSE_RECORDING_URL.send(sender);
sender.sendMessage(new FancyMessage(url).color(ChatColor.getByChar('b')).link(url));
return CommandResult.SUCCESS; return CommandResult.SUCCESS;
} }
} else { } else {

View File

@ -99,7 +99,7 @@ public enum Message {
VERBOSE_RECORDING_ON("&bVerbose recording set to &aTRUE &bfor all permissions.", true), VERBOSE_RECORDING_ON("&bVerbose recording set to &aTRUE &bfor all permissions.", true),
VERBOSE_RECORDING_ON_QUERY("&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}", true), VERBOSE_RECORDING_ON_QUERY("&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}", true),
VERBOSE_RECORDING_UPLOAD_START("&bVerbose recording was disabled. Uploading results...", true), VERBOSE_RECORDING_UPLOAD_START("&bVerbose recording was disabled. Uploading results...", true),
VERBOSE_RECORDING_URL("&aVerbose results URL: {0}", true), VERBOSE_RECORDING_URL("&aVerbose results URL:", true),
SEARCH_SEARCHING("&aSearching for users and groups with &b{0}&a...", true), SEARCH_SEARCHING("&aSearching for users and groups with &b{0}&a...", true),
SEARCH_RESULT("&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups.", true), SEARCH_RESULT("&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups.", true),

View File

@ -28,31 +28,44 @@ import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.utils.DateUtil;
import me.lucko.luckperms.common.utils.PasteUtils; import me.lucko.luckperms.common.utils.PasteUtils;
import me.lucko.luckperms.common.utils.Scripting; import me.lucko.luckperms.common.utils.Scripting;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
@RequiredArgsConstructor @RequiredArgsConstructor
public class DebugListener { public class DebugListener {
private static final int DATA_TRUNCATION = 700;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
private final long startTime = System.currentTimeMillis();
private final String pluginVersion; private final String pluginVersion;
private final Sender holder; private final Sender holder;
private final String filter; private final String filter;
private final boolean notify; private final boolean notify;
private List<CheckData> results = new ArrayList<>(); private final AtomicInteger counter = new AtomicInteger(0);
private final AtomicInteger matchedCounter = new AtomicInteger(0);
private final List<CheckData> results = new ArrayList<>();
public void acceptData(CheckData data) { public void acceptData(CheckData data) {
counter.incrementAndGet();
if (!matches(data, filter)) { if (!matches(data, filter)) {
return; return;
} }
matchedCounter.incrementAndGet();
if (results.size() < 500) { if (results.size() < DATA_TRUNCATION) {
results.add(data); results.add(data);
} }
@ -108,30 +121,52 @@ public class DebugListener {
} }
public String uploadPastedData() { public String uploadPastedData() {
long now = System.currentTimeMillis();
String startDate = DATE_FORMAT.format(new Date(startTime));
String endDate = DATE_FORMAT.format(new Date(now));
long secondsTaken = (now - startTime) / 1000L;
String duration = DateUtil.formatTime(secondsTaken);
String filter = this.filter;
if (filter == null || filter.equals("")){
filter = "any";
} else {
filter = "`" + filter + "`";
}
ImmutableList.Builder<String> output = ImmutableList.<String>builder() ImmutableList.Builder<String> output = ImmutableList.<String>builder()
.add("## Verbose Checking Output")
.add("#### This file was automatically generated by [LuckPerms](https://github.com/lucko/LuckPerms) " + pluginVersion) .add("#### This file was automatically generated by [LuckPerms](https://github.com/lucko/LuckPerms) " + pluginVersion)
.add("") .add("")
.add("Format: `<checked>` `<permission>` `<value>`") .add("### Metadata")
.add("") .add("| Key | Value |")
.add("Size: " + results.size()) .add("|-----|-------|")
.add("| Start Time | " + startDate + " |")
.add("| End Time | " + endDate + " |")
.add("| Duration | " + duration +" |")
.add("| Count | **" + matchedCounter.get() + "** / " + counter + " |")
.add("| User | " + holder.getName() + " |")
.add("| Filter | " + filter + " |")
.add(""); .add("");
if (results.size() >= 500) { if (matchedCounter.get() > results.size()) {
output.add("**WARN:** Result set exceeded max size of 500. Output was trimmed."); output.add("**WARN:** Result set exceeded max size of " + DATA_TRUNCATION + ". The output below was truncated to " + DATA_TRUNCATION + " entries.");
output.add(""); output.add("");
} }
output.add("### Output")
.add("Format: `<checked>` `<permission>` `<value>`")
.add("")
.add("___")
.add("");
List<String> ret = results.stream() List<String> ret = results.stream()
.map(c -> "`" + c.getChecked() + "` - " + c.getNode() + " - **" + c.getValue().toString() + "** ") .map(c -> "`" + c.getChecked() + "` - " + c.getNode() + " - **" + c.getValue().toString() + "** ")
.collect(Collectors.toList()); .collect(Collectors.toList());
output.add("___") output.addAll(ret);
.add("")
.addAll(ret)
.build();
results.clear(); results.clear();
return PasteUtils.paste(output.build().stream().collect(Collectors.joining("\n"))); return PasteUtils.paste("luckperms-verbose.md", "LuckPerms Verbose Checking Output", output.build().stream().collect(Collectors.joining("\n")));
} }
} }

View File

@ -136,6 +136,14 @@ public class DateUtil {
return DateUtil.formatDateDiff(now, c); return DateUtil.formatDateDiff(now, c);
} }
public static String formatTime(long seconds) {
Calendar c = new GregorianCalendar();
c.setTimeInMillis(0L);
Calendar c2 = new GregorianCalendar();
c2.setTimeInMillis(seconds * 1000L);
return DateUtil.formatDateDiff(c, c2);
}
private static String formatDateDiff(Calendar fromDate, Calendar toDate) { private static String formatDateDiff(Calendar fromDate, Calendar toDate) {
boolean future = false; boolean future = false;
if (toDate.equals(fromDate)) { if (toDate.equals(fromDate)) {

View File

@ -35,12 +35,7 @@ import java.net.URL;
public class PasteUtils { public class PasteUtils {
public static void main(String[] args) { public static String paste(String name, String desc, String contents) {
String s = "hey\nlol\nxd";
System.out.println(paste(s));
}
public static String paste(String contents) {
try { try {
HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
@ -50,10 +45,10 @@ public class PasteUtils {
try (OutputStream os = connection.getOutputStream()) { try (OutputStream os = connection.getOutputStream()) {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
new JsonWriter(sw).beginObject() new JsonWriter(sw).beginObject()
.name("description").value("LuckPerms Verbose Output") .name("description").value(desc)
.name("public").value(false) .name("public").value(false)
.name("files") .name("files")
.beginObject().name("lp-verbose.md") .beginObject().name(name)
.beginObject().name("content").value(contents) .beginObject().name("content").value(contents)
.endObject() .endObject()
.endObject() .endObject()

View File

@ -57,7 +57,7 @@ verbose-off: "&bVerbose checking output set to &cFALSE&b."
verbose-recording-on: "&bVerbose recording set to &aTRUE &bfor all permissions." verbose-recording-on: "&bVerbose recording set to &aTRUE &bfor all permissions."
verbose-recording-on-query: "&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}" verbose-recording-on-query: "&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}"
verbose-recording-upload-start: "&bVerbose recording was disabled. Uploading results..." verbose-recording-upload-start: "&bVerbose recording was disabled. Uploading results..."
verbose-recording-url: "&aVerbose results URL: {0}" verbose-recording-url: "&aVerbose results URL:"
search-searching: "&aSearching for users and groups with &b{0}&a..." search-searching: "&aSearching for users and groups with &b{0}&a..."
search-result: "&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups." search-result: "&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups."