[AB-210] improving performance of star stats

This commit is contained in:
Sheldan
2021-03-24 20:11:32 +01:00
parent ddb256cf75
commit 134f25955c
16 changed files with 88 additions and 15 deletions

View File

@@ -15,6 +15,8 @@ public interface StarboardPostReactionRepository extends JpaRepository<Starboard
void deleteByStarboardPost(StarboardPost post); void deleteByStarboardPost(StarboardPost post);
long countByStarboardPost(StarboardPost post);
@Query(value = "SELECT r.reactor_user_in_server_id as userId, COUNT(*) AS starCount \n" + @Query(value = "SELECT r.reactor_user_in_server_id as userId, COUNT(*) AS starCount \n" +
"FROM starboard_post_reaction r \n" + "FROM starboard_post_reaction r \n" +
"INNER JOIN starboard_post p ON p.id = r.post_id\n" + "INNER JOIN starboard_post p ON p.id = r.post_id\n" +

View File

@@ -18,6 +18,8 @@ public interface StarboardPostRepository extends JpaRepository<StarboardPost, Lo
List<StarboardPost> findByServer_Id(Long serverId); List<StarboardPost> findByServer_Id(Long serverId);
Long countByServer_Id(Long serverId);
@Query(value = "SELECT p.id, COUNT(*) AS starCount \n" + @Query(value = "SELECT p.id, COUNT(*) AS starCount \n" +
" FROM starboard_post p \n" + " FROM starboard_post p \n" +
" INNER JOIN starboard_post_reaction r ON p.id = r.post_id\n" + " INNER JOIN starboard_post_reaction r ON p.id = r.post_id\n" +
@@ -30,6 +32,16 @@ public interface StarboardPostRepository extends JpaRepository<StarboardPost, Lo
" LIMIT :count", nativeQuery = true) " LIMIT :count", nativeQuery = true)
List<Long> getTopStarboardPostsForUser(Long serverId, Long userId, Integer count); List<Long> getTopStarboardPostsForUser(Long serverId, Long userId, Integer count);
@Query(value = "SELECT p.id, COUNT(*) AS starCount \n" +
" FROM starboard_post p \n" +
" INNER JOIN starboard_post_reaction r ON p.id = r.post_id\n" +
" WHERE p.server_id = :serverId\n" +
" AND p.ignored = false\n" +
" GROUP BY p.id \n" +
" ORDER BY starCount DESC \n" +
" LIMIT :count", nativeQuery = true)
List<Long> getTopStarboardPostsForServer(Long serverId, Integer count);
@Query(value = "SELECT COUNT(*) AS starCount\n" + @Query(value = "SELECT COUNT(*) AS starCount\n" +
"FROM starboard_post_reaction r \n" + "FROM starboard_post_reaction r \n" +
" INNER JOIN starboard_post p ON p.id = r.post_id \n" + " INNER JOIN starboard_post p ON p.id = r.post_id \n" +

View File

@@ -196,8 +196,8 @@ public class StarboardServiceBean implements StarboardService {
allFutures.addAll(topStarReceiverFutures); allFutures.addAll(topStarReceiverFutures);
return FutureUtils.toSingleFuture(allFutures).thenApply(aVoid -> { return FutureUtils.toSingleFuture(allFutures).thenApply(aVoid -> {
List<StarboardPost> starboardPosts = starboardPostManagementService.retrieveTopPosts(serverId, count); List<StarboardPost> starboardPosts = starboardPostManagementService.retrieveTopPosts(serverId, count);
List<StarStatsPost> starStatsPosts = starboardPosts.stream().map(this::fromStarboardPost).collect(Collectors.toList()); List<StarStatsPost> starStatsPosts = starboardPosts.stream().map(this::fromStarboardPost).sorted(Comparator.comparingLong(StarStatsPost::getStarCount).reversed()).collect(Collectors.toList());
Integer postCount = starboardPostManagementService.getPostCount(serverId); Long postCount = starboardPostManagementService.getPostCount(serverId);
Integer reactionCount = starboardPostReactorManagementService.getStarCount(serverId); Integer reactionCount = starboardPostReactorManagementService.getStarCount(serverId);
List<String> emotes = new ArrayList<>(); List<String> emotes = new ArrayList<>();
for (int i = 1; i < count + 1; i++) { for (int i = 1; i < count + 1; i++) {
@@ -224,7 +224,7 @@ public class StarboardServiceBean implements StarboardService {
Long receivedStars = starboardPostManagementService.retrieveReceivedStarsOfUserInServer(member.getGuild().getIdLong(), member.getIdLong()); Long receivedStars = starboardPostManagementService.retrieveReceivedStarsOfUserInServer(member.getGuild().getIdLong(), member.getIdLong());
Long givenStars = starboardPostManagementService.retrieveGivenStarsOfUserInServer(member.getGuild().getIdLong(), member.getIdLong()); Long givenStars = starboardPostManagementService.retrieveGivenStarsOfUserInServer(member.getGuild().getIdLong(), member.getIdLong());
List<StarboardPost> topPosts = starboardPostManagementService.retrieveTopPostsForUserInServer(member.getGuild().getIdLong(), member.getIdLong(), count); List<StarboardPost> topPosts = starboardPostManagementService.retrieveTopPostsForUserInServer(member.getGuild().getIdLong(), member.getIdLong(), count);
List<StarStatsPost> starStatsPosts = topPosts.stream().map(this::fromStarboardPost).sorted(Comparator.comparingInt(StarStatsPost::getStarCount).reversed()).collect(Collectors.toList()); List<StarStatsPost> starStatsPosts = topPosts.stream().map(this::fromStarboardPost).sorted(Comparator.comparingLong(StarStatsPost::getStarCount).reversed()).collect(Collectors.toList());
List<String> emotes = new ArrayList<>(); List<String> emotes = new ArrayList<>();
for (int i = 1; i < count + 1; i++) { for (int i = 1; i < count + 1; i++) {
emotes.add(getStarboardRankingEmote(member.getGuild().getIdLong(), i)); emotes.add(getStarboardRankingEmote(member.getGuild().getIdLong(), i));
@@ -246,7 +246,7 @@ public class StarboardServiceBean implements StarboardService {
.serverId(starboardPost.getServer().getId()) .serverId(starboardPost.getServer().getId())
.channelId(channel.getId()) .channelId(channel.getId())
.messageId(starboardPost.getPostMessageId()) .messageId(starboardPost.getPostMessageId())
.starCount(starboardPost.getReactions().size()) .starCount(starboardPostReactorManagementService.getReactorCountOfPost(starboardPost))
.build(); .build();
} }

View File

@@ -58,10 +58,8 @@ public class StarboardPostManagementServiceBean implements StarboardPostManageme
@Override @Override
public List<StarboardPost> retrieveTopPosts(Long serverId, Integer count) { public List<StarboardPost> retrieveTopPosts(Long serverId, Integer count) {
List<StarboardPost> posts = retrieveAllPosts(serverId); List<Long> topPostIds = repository.getTopStarboardPostsForServer(serverId, count);
posts.sort(Comparator.comparingInt(o -> o.getReactions().size())); return repository.findAllById(topPostIds);
Collections.reverse(posts);
return posts.subList(0, Math.min(count, posts.size()));
} }
@Override @Override
@@ -86,8 +84,8 @@ public class StarboardPostManagementServiceBean implements StarboardPostManageme
} }
@Override @Override
public Integer getPostCount(Long serverId) { public Long getPostCount(Long serverId) {
return retrieveAllPosts(serverId).size(); return repository.countByServer_Id(serverId);
} }
@Override @Override

View File

@@ -65,4 +65,9 @@ public class StarboardPostReactorManagementServiceBean implements StarboardPostR
return converter.convertToStarStatsUser(starReceivers, serverId); return converter.convertToStarStatsUser(starReceivers, serverId);
} }
@Override
public Long getReactorCountOfPost(StarboardPost starboardPost) {
return repository.countByStarboardPost(starboardPost);
}
} }

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../dbchangelog-3.8.xsd" >
<include file="starboard-tables/tables.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,15 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<changeSet author="Sheldan" id="additional_starboard_post-index">
<createIndex indexName="idx_starboard_author" tableName="starboard_post">
<column name="author_user_in_server_id"/>
</createIndex>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,18 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<changeSet author="Sheldan" id="additional_starboard_reaction-index">
<createIndex indexName="idx_starboard_reaction_user" tableName="starboard_post_reaction">
<column name="reactor_user_in_server_id"/>
</createIndex>
<createIndex indexName="idx_starboard_reaction_post" tableName="starboard_post_reaction">
<column name="post_id"/>
</createIndex>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,11 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<include file="starboard_post.xml" relativeToChangelogFile="true"/>
<include file="starboard_post_reaction.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -7,4 +7,5 @@
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog-3.8.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog-3.8.xsd" > http://www.liquibase.org/xml/ns/pro dbchangelog-3.8.xsd" >
<include file="1.0-starboard/collection.xml" relativeToChangelogFile="true"/> <include file="1.0-starboard/collection.xml" relativeToChangelogFile="true"/>
<include file="1.2.3-starboard/collection.xml" relativeToChangelogFile="true"/>
</databaseChangeLog> </databaseChangeLog>

View File

@@ -298,7 +298,7 @@ public class StarboardServiceBeanTest {
List<CompletableFuture<StarStatsUser>> topGiver = Arrays.asList(statsUser, statsUser2); List<CompletableFuture<StarStatsUser>> topGiver = Arrays.asList(statsUser, statsUser2);
when(starboardPostReactorManagementService.retrieveTopStarGiver(SERVER_ID, limit)).thenReturn(topGiver); when(starboardPostReactorManagementService.retrieveTopStarGiver(SERVER_ID, limit)).thenReturn(topGiver);
when(starboardPostReactorManagementService.retrieveTopStarReceiver(SERVER_ID, limit)).thenReturn(topGiver); when(starboardPostReactorManagementService.retrieveTopStarReceiver(SERVER_ID, limit)).thenReturn(topGiver);
when(starboardPostManagementService.getPostCount(SERVER_ID)).thenReturn(50); when(starboardPostManagementService.getPostCount(SERVER_ID)).thenReturn(50L);
when(starboardPostReactorManagementService.getStarCount(SERVER_ID)).thenReturn(500); when(starboardPostReactorManagementService.getStarCount(SERVER_ID)).thenReturn(500);
when(emoteService.getUsableEmoteOrDefault(SERVER_ID, "starboardBadge1")).thenReturn("1"); when(emoteService.getUsableEmoteOrDefault(SERVER_ID, "starboardBadge1")).thenReturn("1");
when(emoteService.getUsableEmoteOrDefault(SERVER_ID, "starboardBadge2")).thenReturn("2"); when(emoteService.getUsableEmoteOrDefault(SERVER_ID, "starboardBadge2")).thenReturn("2");

View File

@@ -198,7 +198,7 @@ public class StarboardPostManagementServiceBeanTest {
StarboardPost starboardPost2 = Mockito.mock(StarboardPost.class); StarboardPost starboardPost2 = Mockito.mock(StarboardPost.class);
List<StarboardPost> posts = Arrays.asList(starboardPost1, starboardPost2); List<StarboardPost> posts = Arrays.asList(starboardPost1, starboardPost2);
when(repository.findByServer_Id(SERVER_ID)).thenReturn(posts); when(repository.findByServer_Id(SERVER_ID)).thenReturn(posts);
Integer retrievedPostCount = testUnit.getPostCount(SERVER_ID); Long retrievedPostCount = testUnit.getPostCount(SERVER_ID);
Assert.assertEquals(posts.size(), retrievedPostCount.intValue()); Assert.assertEquals(posts.size(), retrievedPostCount.intValue());
} }
} }

View File

@@ -15,5 +15,5 @@ public class GuildStarStatsModel {
private List<StarStatsUser> starGiver; private List<StarStatsUser> starGiver;
private Integer totalStars; private Integer totalStars;
private List<String> badgeEmotes; private List<String> badgeEmotes;
private Integer starredMessages; private Long starredMessages;
} }

View File

@@ -12,7 +12,7 @@ public class StarStatsPost {
private Long serverId; private Long serverId;
private Long channelId; private Long channelId;
private Long messageId; private Long messageId;
private Integer starCount; private Long starCount;
public String getMessageUrl() { public String getMessageUrl() {
return MessageUtils.buildMessageUrl(serverId ,channelId, messageId); return MessageUtils.buildMessageUrl(serverId ,channelId, messageId);

View File

@@ -17,7 +17,7 @@ public interface StarboardPostManagementService {
Long retrieveGivenStarsOfUserInServer(Long serverId, Long userId); Long retrieveGivenStarsOfUserInServer(Long serverId, Long userId);
Long retrieveReceivedStarsOfUserInServer(Long serverId, Long userId); Long retrieveReceivedStarsOfUserInServer(Long serverId, Long userId);
List<StarboardPost> retrieveAllPosts(Long serverId); List<StarboardPost> retrieveAllPosts(Long serverId);
Integer getPostCount(Long serverId); Long getPostCount(Long serverId);
Optional<StarboardPost> findByMessageId(Long messageId); Optional<StarboardPost> findByMessageId(Long messageId);
Optional<StarboardPost> findByStarboardPostId(Long postId); Optional<StarboardPost> findByStarboardPostId(Long postId);
Optional<StarboardPost> findByStarboardPostMessageId(Long postId); Optional<StarboardPost> findByStarboardPostMessageId(Long postId);

View File

@@ -15,4 +15,5 @@ public interface StarboardPostReactorManagementService {
Integer getStarCount(Long serverId); Integer getStarCount(Long serverId);
List<CompletableFuture<StarStatsUser>> retrieveTopStarGiver(Long serverId, Integer count); List<CompletableFuture<StarStatsUser>> retrieveTopStarGiver(Long serverId, Integer count);
List<CompletableFuture<StarStatsUser>> retrieveTopStarReceiver(Long serverId, Integer count); List<CompletableFuture<StarStatsUser>> retrieveTopStarReceiver(Long serverId, Integer count);
Long getReactorCountOfPost(StarboardPost starboardPost);
} }