From 9634484c7b3ecca70fbfe7367b89106d00986437 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 27 May 2022 19:58:40 +0200 Subject: [PATCH] Top helper messages based on message length migration script adding the new column (default 1), existing data is untouched Default count to 1 for backwards support using count 1 ensures that the new system naturally outputs the same as the old system for old months (since all messages then just count 1, as they did before) --- .../commands/tophelper/TopHelpersCommand.java | 23 +++++++++++-------- .../tophelper/TopHelpersMessageListener.java | 1 + .../V10__Alter_Top_Helper_Message_Length.sql | 1 + 3 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 application/src/main/resources/db/V10__Alter_Top_Helper_Message_Length.sql diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java index e6726cd3f8..479b1dae19 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java @@ -23,6 +23,7 @@ import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; +import java.math.BigDecimal; import java.time.*; import java.time.format.TextStyle; import java.util.*; @@ -38,7 +39,7 @@ /** * Command that displays the top helpers of a given time range. *

- * Top helpers are measured by their message count in help channels, as set by + * Top helpers are measured by their message length in help channels, as set by * {@link TopHelpersMessageListener}. */ public final class TopHelpersCommand extends SlashCommandAdapter { @@ -53,7 +54,7 @@ public final class TopHelpersCommand extends SlashCommandAdapter { /** * Creates a new instance. * - * @param database the database containing the message counts of top helpers + * @param database the database containing the message records of top helpers * @param config the config to use for this */ public TopHelpersCommand(@NotNull Database database, @NotNull Config config) { @@ -138,12 +139,13 @@ private boolean handleHasAuthorRole(@NotNull Member author, @NotNull IReplyCallb private @NotNull List computeTopHelpersDescending(long guildId, @NotNull TimeRange timeRange) { - return database.read(context -> context.select(HELP_CHANNEL_MESSAGES.AUTHOR_ID, DSL.count()) + return database.read(context -> context + .select(HELP_CHANNEL_MESSAGES.AUTHOR_ID, DSL.sum(HELP_CHANNEL_MESSAGES.MESSAGE_LENGTH)) .from(HELP_CHANNEL_MESSAGES) .where(HELP_CHANNEL_MESSAGES.GUILD_ID.eq(guildId) .and(HELP_CHANNEL_MESSAGES.SENT_AT.between(timeRange.start(), timeRange.end()))) .groupBy(HELP_CHANNEL_MESSAGES.AUTHOR_ID) - .orderBy(DSL.count().desc()) + .orderBy(DSL.two().desc()) .limit(TOP_HELPER_LIMIT) .fetch(Records.mapping(TopHelperResult::new))); } @@ -174,9 +176,9 @@ private static void handleTopHelpers(@NotNull Collection topHel @Nullable Member member) { String id = Long.toString(topHelper.authorId()); String name = member == null ? "UNKNOWN_USER" : member.getEffectiveName(); - String messageCount = Integer.toString(topHelper.messageCount()); + String messageLengths = Long.toString(topHelper.messageLengths().longValue()); - return List.of(id, name, messageCount); + return List.of(id, name, messageLengths); } private static @NotNull String dataTableToString(@NotNull Collection> dataTable, @@ -185,7 +187,7 @@ private static void handleTopHelpers(@NotNull Collection topHel List.of(new ColumnSetting("Id", HorizontalAlign.RIGHT), new ColumnSetting("Name", HorizontalAlign.RIGHT), new ColumnSetting( - "Message count (for %s)".formatted(timeRange.description()), + "Message lengths (for %s)".formatted(timeRange.description()), HorizontalAlign.RIGHT))); } @@ -206,14 +208,15 @@ private static void handleTopHelpers(@NotNull Collection topHel return AsciiTable.getTable(AsciiTable.BASIC_ASCII_NO_DATA_SEPARATORS, dataTable, columns); } - private record TimeRange(Instant start, Instant end, String description) { + private record TimeRange(@NotNull Instant start, @NotNull Instant end, + @NotNull String description) { } - private record TopHelperResult(long authorId, int messageCount) { + private record TopHelperResult(long authorId, @NotNull BigDecimal messageLengths) { } - private record ColumnSetting(String headerName, HorizontalAlign alignment) { + private record ColumnSetting(@NotNull String headerName, @NotNull HorizontalAlign alignment) { } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java index b7643df083..3e8483e568 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java @@ -44,6 +44,7 @@ private void addMessageRecord(@NotNull MessageReceivedEvent event) { .setChannelId(event.getChannel().getIdLong()) .setAuthorId(event.getAuthor().getIdLong()) .setSentAt(event.getMessage().getTimeCreated().toInstant()) + .setMessageLength((long) event.getMessage().getContentRaw().length()) .insert()); } } diff --git a/application/src/main/resources/db/V10__Alter_Top_Helper_Message_Length.sql b/application/src/main/resources/db/V10__Alter_Top_Helper_Message_Length.sql new file mode 100644 index 0000000000..d0938e6211 --- /dev/null +++ b/application/src/main/resources/db/V10__Alter_Top_Helper_Message_Length.sql @@ -0,0 +1 @@ +ALTER TABLE help_channel_messages ADD message_length BIGINT DEFAULT 1 NOT NULL; \ No newline at end of file