Skip to content

Top helpers based on message length #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -38,7 +39,7 @@
/**
* Command that displays the top helpers of a given time range.
* <p>
* 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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -138,12 +139,13 @@ private boolean handleHasAuthorRole(@NotNull Member author, @NotNull IReplyCallb

private @NotNull List<TopHelperResult> 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)));
}
Expand Down Expand Up @@ -174,9 +176,9 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> 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<List<String>> dataTable,
Expand All @@ -185,7 +187,7 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> 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)));
}

Expand All @@ -206,14 +208,15 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> 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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE help_channel_messages ADD message_length BIGINT DEFAULT 1 NOT NULL;