Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 969849b

Browse files
authoredMay 29, 2022
Top helper messages based on message length (#446)
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)
1 parent ff007a2 commit 969849b

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed
 

‎application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.togetherjava.tjbot.config.Config;
2424
import org.togetherjava.tjbot.db.Database;
2525

26+
import java.math.BigDecimal;
2627
import java.time.*;
2728
import java.time.format.TextStyle;
2829
import java.util.*;
@@ -38,7 +39,7 @@
3839
/**
3940
* Command that displays the top helpers of a given time range.
4041
* <p>
41-
* Top helpers are measured by their message count in help channels, as set by
42+
* Top helpers are measured by their message length in help channels, as set by
4243
* {@link TopHelpersMessageListener}.
4344
*/
4445
public final class TopHelpersCommand extends SlashCommandAdapter {
@@ -53,7 +54,7 @@ public final class TopHelpersCommand extends SlashCommandAdapter {
5354
/**
5455
* Creates a new instance.
5556
*
56-
* @param database the database containing the message counts of top helpers
57+
* @param database the database containing the message records of top helpers
5758
* @param config the config to use for this
5859
*/
5960
public TopHelpersCommand(@NotNull Database database, @NotNull Config config) {
@@ -138,12 +139,13 @@ private boolean handleHasAuthorRole(@NotNull Member author, @NotNull IReplyCallb
138139

139140
private @NotNull List<TopHelperResult> computeTopHelpersDescending(long guildId,
140141
@NotNull TimeRange timeRange) {
141-
return database.read(context -> context.select(HELP_CHANNEL_MESSAGES.AUTHOR_ID, DSL.count())
142+
return database.read(context -> context
143+
.select(HELP_CHANNEL_MESSAGES.AUTHOR_ID, DSL.sum(HELP_CHANNEL_MESSAGES.MESSAGE_LENGTH))
142144
.from(HELP_CHANNEL_MESSAGES)
143145
.where(HELP_CHANNEL_MESSAGES.GUILD_ID.eq(guildId)
144146
.and(HELP_CHANNEL_MESSAGES.SENT_AT.between(timeRange.start(), timeRange.end())))
145147
.groupBy(HELP_CHANNEL_MESSAGES.AUTHOR_ID)
146-
.orderBy(DSL.count().desc())
148+
.orderBy(DSL.two().desc())
147149
.limit(TOP_HELPER_LIMIT)
148150
.fetch(Records.mapping(TopHelperResult::new)));
149151
}
@@ -174,9 +176,9 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
174176
@Nullable Member member) {
175177
String id = Long.toString(topHelper.authorId());
176178
String name = member == null ? "UNKNOWN_USER" : member.getEffectiveName();
177-
String messageCount = Integer.toString(topHelper.messageCount());
179+
String messageLengths = Long.toString(topHelper.messageLengths().longValue());
178180

179-
return List.of(id, name, messageCount);
181+
return List.of(id, name, messageLengths);
180182
}
181183

182184
private static @NotNull String dataTableToString(@NotNull Collection<List<String>> dataTable,
@@ -185,7 +187,7 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
185187
List.of(new ColumnSetting("Id", HorizontalAlign.RIGHT),
186188
new ColumnSetting("Name", HorizontalAlign.RIGHT),
187189
new ColumnSetting(
188-
"Message count (for %s)".formatted(timeRange.description()),
190+
"Message lengths (for %s)".formatted(timeRange.description()),
189191
HorizontalAlign.RIGHT)));
190192
}
191193

@@ -206,14 +208,15 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
206208
return AsciiTable.getTable(AsciiTable.BASIC_ASCII_NO_DATA_SEPARATORS, dataTable, columns);
207209
}
208210

209-
private record TimeRange(Instant start, Instant end, String description) {
211+
private record TimeRange(@NotNull Instant start, @NotNull Instant end,
212+
@NotNull String description) {
210213
}
211214

212215

213-
private record TopHelperResult(long authorId, int messageCount) {
216+
private record TopHelperResult(long authorId, @NotNull BigDecimal messageLengths) {
214217
}
215218

216219

217-
private record ColumnSetting(String headerName, HorizontalAlign alignment) {
220+
private record ColumnSetting(@NotNull String headerName, @NotNull HorizontalAlign alignment) {
218221
}
219222
}

‎application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private void addMessageRecord(@NotNull MessageReceivedEvent event) {
4444
.setChannelId(event.getChannel().getIdLong())
4545
.setAuthorId(event.getAuthor().getIdLong())
4646
.setSentAt(event.getMessage().getTimeCreated().toInstant())
47+
.setMessageLength((long) event.getMessage().getContentRaw().length())
4748
.insert());
4849
}
4950
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE help_channel_messages ADD message_length BIGINT DEFAULT 1 NOT NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.