Fix loading issue in stats page

This commit is contained in:
SebClem 2021-03-06 17:00:22 +01:00
parent eb0c1cf083
commit cdce2e836f

View File

@ -33,23 +33,12 @@ public class UserStatsUtils {
private static UserStatsUtils INSTANCE = new UserStatsUtils(); private static UserStatsUtils INSTANCE = new UserStatsUtils();
private final UserStatsRepository userStatsRepository;
public static UserStatsUtils getINSTANCE() { private final UserRepository userRepository;
if (INSTANCE == null) private final PasswordEncoder passwordEncoder;
INSTANCE = new UserStatsUtils(); private final Logger logger = LogManager.getLogger();
return INSTANCE;
}
public HashMap<String, VoicePresenceCounter> runningCounters = new HashMap<>(); public HashMap<String, VoicePresenceCounter> runningCounters = new HashMap<>();
private UserStatsRepository userStatsRepository;
private UserRepository userRepository;
private PasswordEncoder passwordEncoder;
private Logger logger = LogManager.getLogger();
private UserStatsUtils() { private UserStatsUtils() {
ApplicationContext context = SpringContext.getAppContext(); ApplicationContext context = SpringContext.getAppContext();
userStatsRepository = (UserStatsRepository) context.getBean("userStatsRepository"); userStatsRepository = (UserStatsRepository) context.getBean("userStatsRepository");
@ -58,6 +47,12 @@ public class UserStatsUtils {
} }
public static UserStatsUtils getINSTANCE() {
if (INSTANCE == null)
INSTANCE = new UserStatsUtils();
return INSTANCE;
}
public List<UserStats> getUserStats(UserEntity userEntity) { public List<UserStats> getUserStats(UserEntity userEntity) {
User jdaUser = CacheTools.getJdaUser(userEntity); User jdaUser = CacheTools.getJdaUser(userEntity);
//TODO clean database for deleted users //TODO clean database for deleted users
@ -193,18 +188,37 @@ public class UserStatsUtils {
List<UserStats> allStats = userStatsRepository.findByGuildId(guildId); List<UserStats> allStats = userStatsRepository.findByGuildId(guildId);
List<GuildStats> ranked = new ArrayList<>(); List<GuildStats> ranked = new ArrayList<>();
List<UserStats> needCache = new ArrayList<>();
Guild guild = MainBot.jda.getGuildById(guildId);
for (UserStats stats : allStats) { for (UserStats stats : allStats) {
if (CacheTools.getJdaUser(stats.getUser()) != null) { Member member = guild.getMemberById(stats.getUser().getJdaId());
String avatar = CacheTools.getJdaUser(stats.getUser()).getEffectiveAvatarUrl(); if (member == null) {
needCache.add(stats);
continue;
}
String avatar = member.getUser().getAvatarUrl();
GuildStats temp = new GuildStats(stats.getUser().getName(), 0, avatar, stats.getVocalTime(), stats.getMessageCount(), stats.getApiCommandCount());
if (stats.getUser().getId().equals(userEntity.getId())) {
selfGuildStats = temp;
}
ranked.add(temp);
}
if (needCache.size() != 0) {
logger.info("Cache mismatch, loading all guild");
MainBot.jda.getGuildById(guildId).loadMembers().get();
for (UserStats stats : needCache) {
Member member = guild.getMemberById(stats.getUser().getJdaId());
if (member == null) {
logger.warn("Can't find member '" + stats.getUser().getName() + "'after load, User leave the guild ?");
continue;
}
String avatar = member.getUser().getAvatarUrl();
GuildStats temp = new GuildStats(stats.getUser().getName(), 0, avatar, stats.getVocalTime(), stats.getMessageCount(), stats.getApiCommandCount()); GuildStats temp = new GuildStats(stats.getUser().getName(), 0, avatar, stats.getVocalTime(), stats.getMessageCount(), stats.getApiCommandCount());
if (stats.getUser().getId().equals(userEntity.getId())) { if (stats.getUser().getId().equals(userEntity.getId())) {
selfGuildStats = temp; selfGuildStats = temp;
} }
ranked.add(temp); ranked.add(temp);
} }
} }
ranked.sort((guildStats, t1) -> (int) (t1.total - guildStats.total)); ranked.sort((guildStats, t1) -> (int) (t1.total - guildStats.total));
@ -261,7 +275,7 @@ public class UserStatsUtils {
} }
public static class VoicePresenceCounter extends Thread { public static class VoicePresenceCounter extends Thread {
private Member member; private final Member member;
public VoicePresenceCounter(Member member) { public VoicePresenceCounter(Member member) {
this.member = member; this.member = member;