[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);
long countByStarboardPost(StarboardPost post);
@Query(value = "SELECT r.reactor_user_in_server_id as userId, COUNT(*) AS starCount \n" +
"FROM starboard_post_reaction r \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);
Long countByServer_Id(Long serverId);
@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" +
@@ -30,6 +32,16 @@ public interface StarboardPostRepository extends JpaRepository<StarboardPost, Lo
" LIMIT :count", nativeQuery = true)
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" +
"FROM starboard_post_reaction r \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);
return FutureUtils.toSingleFuture(allFutures).thenApply(aVoid -> {
List<StarboardPost> starboardPosts = starboardPostManagementService.retrieveTopPosts(serverId, count);
List<StarStatsPost> starStatsPosts = starboardPosts.stream().map(this::fromStarboardPost).collect(Collectors.toList());
Integer postCount = starboardPostManagementService.getPostCount(serverId);
List<StarStatsPost> starStatsPosts = starboardPosts.stream().map(this::fromStarboardPost).sorted(Comparator.comparingLong(StarStatsPost::getStarCount).reversed()).collect(Collectors.toList());
Long postCount = starboardPostManagementService.getPostCount(serverId);
Integer reactionCount = starboardPostReactorManagementService.getStarCount(serverId);
List<String> emotes = new ArrayList<>();
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 givenStars = starboardPostManagementService.retrieveGivenStarsOfUserInServer(member.getGuild().getIdLong(), member.getIdLong());
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<>();
for (int i = 1; i < count + 1; i++) {
emotes.add(getStarboardRankingEmote(member.getGuild().getIdLong(), i));
@@ -246,7 +246,7 @@ public class StarboardServiceBean implements StarboardService {
.serverId(starboardPost.getServer().getId())
.channelId(channel.getId())
.messageId(starboardPost.getPostMessageId())
.starCount(starboardPost.getReactions().size())
.starCount(starboardPostReactorManagementService.getReactorCountOfPost(starboardPost))
.build();
}

View File

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

View File

@@ -65,4 +65,9 @@ public class StarboardPostReactorManagementServiceBean implements StarboardPostR
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/pro dbchangelog-3.8.xsd" >
<include file="1.0-starboard/collection.xml" relativeToChangelogFile="true"/>
<include file="1.2.3-starboard/collection.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -298,7 +298,7 @@ public class StarboardServiceBeanTest {
List<CompletableFuture<StarStatsUser>> topGiver = Arrays.asList(statsUser, statsUser2);
when(starboardPostReactorManagementService.retrieveTopStarGiver(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(emoteService.getUsableEmoteOrDefault(SERVER_ID, "starboardBadge1")).thenReturn("1");
when(emoteService.getUsableEmoteOrDefault(SERVER_ID, "starboardBadge2")).thenReturn("2");

View File

@@ -198,7 +198,7 @@ public class StarboardPostManagementServiceBeanTest {
StarboardPost starboardPost2 = Mockito.mock(StarboardPost.class);
List<StarboardPost> posts = Arrays.asList(starboardPost1, starboardPost2);
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());
}
}

View File

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

View File

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

View File

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

View File

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