forked from chipmunkmc/chipmunkbot
Improve log querying?
This commit is contained in:
parent
ae54e3c675
commit
3501eafe68
1 changed files with 82 additions and 20 deletions
|
@ -20,14 +20,15 @@ import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
public class LogQueryCommand {
|
public class LogQueryCommand {
|
||||||
private Thread thread;
|
private LogQueryThread thread;
|
||||||
|
|
||||||
private static SimpleCommandExceptionType ALREADY_QUERYING_LOGS_EXCEPTION = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("Another log query is already running")));
|
private static SimpleCommandExceptionType ALREADY_QUERYING_LOGS_EXCEPTION = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("Another log query is already running")));
|
||||||
private static SimpleCommandExceptionType ALREADY_NOT_QUERYING_LOGS_EXCEPTION = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("No log query is currently running")));
|
private static SimpleCommandExceptionType NOT_QUERYING_LOGS_EXCEPTION = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("No log query is currently running")));
|
||||||
|
|
||||||
public static void register (CommandDispatcher dispatcher) {
|
public static void register (CommandDispatcher dispatcher) {
|
||||||
final LogQueryCommand instance = new LogQueryCommand();
|
final LogQueryCommand instance = new LogQueryCommand();
|
||||||
|
@ -36,7 +37,7 @@ public class LogQueryCommand {
|
||||||
literal("logquery")
|
literal("logquery")
|
||||||
.then(
|
.then(
|
||||||
literal("abort")
|
literal("abort")
|
||||||
// .executes(this::abortCommand)
|
.executes(instance::abortCommand)
|
||||||
)
|
)
|
||||||
.then(
|
.then(
|
||||||
literal("count")
|
literal("count")
|
||||||
|
@ -52,6 +53,10 @@ public class LogQueryCommand {
|
||||||
// .executes(instance::reportCommand)
|
// .executes(instance::reportCommand)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
.then(
|
||||||
|
literal("progress")
|
||||||
|
.executes(instance::progressCommand)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +72,14 @@ public class LogQueryCommand {
|
||||||
throw ALREADY_QUERYING_LOGS_EXCEPTION.create();
|
throw ALREADY_QUERYING_LOGS_EXCEPTION.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public int abortCommand (CommandContext<CommandSource> context) throws CommandSyntaxException {
|
public int abortCommand (CommandContext<CommandSource> context) throws CommandSyntaxException {
|
||||||
if (!querying()) throw ALREADY_NOT_QUERYING_LOGS_EXCEPTION.create();
|
if (!querying()) throw NOT_QUERYING_LOGS_EXCEPTION.create();
|
||||||
|
|
||||||
} */
|
thread.breakQuery();
|
||||||
|
context.getSource().sendOutput(Component.translatable("Stopped querying logs"));
|
||||||
|
|
||||||
|
return Command.SINGLE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
public int countCommand (CommandContext<CommandSource> context) throws CommandSyntaxException {
|
public int countCommand (CommandContext<CommandSource> context) throws CommandSyntaxException {
|
||||||
alreadyQueryingCheck();
|
alreadyQueryingCheck();
|
||||||
|
@ -87,24 +96,43 @@ public class LogQueryCommand {
|
||||||
return Command.SINGLE_SUCCESS;
|
return Command.SINGLE_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@AllArgsConstructor
|
public int progressCommand (CommandContext<CommandSource> context) {
|
||||||
public static class CountThread extends Thread {
|
final Component progress = Component.translatable(
|
||||||
|
"Currently reading %s (%s/%s)",
|
||||||
|
Component.text(thread.currentFilename, NamedTextColor.GREEN),
|
||||||
|
Component.text(thread.currentIndex + 1),
|
||||||
|
Component.text(thread.totalFiles)
|
||||||
|
);
|
||||||
|
|
||||||
|
context.getSource().sendOutput(progress, false);
|
||||||
|
return Command.SINGLE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static abstract class LogQueryThread extends Thread {
|
||||||
public Predicate<String> query;
|
public Predicate<String> query;
|
||||||
public File directory;
|
public File directory;
|
||||||
public CommandSource source;
|
|
||||||
|
public String currentFilename = null;
|
||||||
|
public int currentIndex = 0;
|
||||||
|
public int totalFiles;
|
||||||
|
|
||||||
|
private boolean breakFlag;
|
||||||
|
|
||||||
public void run () {
|
public void run () {
|
||||||
int instances = 0;
|
breakFlag = false;
|
||||||
final long startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
final String[] filenames = directory.list();
|
String[] filenames = directory.list();
|
||||||
|
if (filenames == null) return;
|
||||||
|
Arrays.sort(filenames);
|
||||||
|
totalFiles = filenames.length;
|
||||||
|
|
||||||
if (filenames == null) {
|
onStart();
|
||||||
source.sendOutput(Component.translatable("Unable to query the bot's logs, as the directory does not exist", NamedTextColor.RED));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String filename : filenames) {
|
for (String filename : filenames) {
|
||||||
|
currentFilename = filename;
|
||||||
|
|
||||||
|
if (breakFlag) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
InputStream is = new FileInputStream(new File(directory, filename));
|
InputStream is = new FileInputStream(new File(directory, filename));
|
||||||
if (filename.endsWith(".log.gz")) is = new GZIPInputStream(is);
|
if (filename.endsWith(".log.gz")) is = new GZIPInputStream(is);
|
||||||
|
@ -113,16 +141,50 @@ public class LogQueryCommand {
|
||||||
final Scanner scanner = new Scanner(is, StandardCharsets.UTF_8);
|
final Scanner scanner = new Scanner(is, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
while (scanner.hasNextLine()) {
|
while (scanner.hasNextLine()) {
|
||||||
if (!query.test(scanner.nextLine())) continue;
|
final String line = scanner.nextLine();
|
||||||
instances++;
|
if (!query.test(line)) continue;
|
||||||
|
onMatch(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner.close();
|
scanner.close();
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
currentIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void breakQuery () {
|
||||||
|
breakFlag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onStart () {}
|
||||||
|
public void onFinish () {}
|
||||||
|
abstract void onMatch (String line);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class CountThread extends LogQueryThread {
|
||||||
|
public int instances = 0;
|
||||||
|
public long startTime;
|
||||||
|
public CommandSource source;
|
||||||
|
|
||||||
|
public CountThread (Predicate<String> query, File directory, CommandSource source) {
|
||||||
|
this.query = query;
|
||||||
|
this.directory = directory;
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onStart () {
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onMatch (String line) {
|
||||||
|
instances++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onFinish () {
|
||||||
double seconds = (double) (System.currentTimeMillis() - startTime) / 1000d;
|
double seconds = (double) (System.currentTimeMillis() - startTime) / 1000d;
|
||||||
source.sendOutput(Component.translatable("Found %s instances of the specified query in %s seconds", Component.text(instances, NamedTextColor.GREEN), Component.text(seconds, NamedTextColor.GREEN)));
|
source.sendOutput(Component.translatable("Found %s instances of the specified query in %s seconds", Component.text(instances, NamedTextColor.GREEN), Component.text(seconds, NamedTextColor.GREEN)));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue