Skip to content

Commit

Permalink
Merge pull request #2476 from NationalSecurityAgency/t#2457/bulk-achi…
Browse files Browse the repository at this point in the history
…evement-load

T#2457/bulk achievement load
  • Loading branch information
sudo-may authored Dec 4, 2023
2 parents eec5b23 + 2b90373 commit 58340c8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
5 changes: 2 additions & 3 deletions service/src/main/java/skills/skillLoading/SkillsLoader.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,6 @@ class SkillsLoader {
})
}

Date achievedOn = achievedLevelRepository.getAchievedDateByUserIdAndProjectIdAndSkillId(userId, skillDef.projectId, skillDef.skillId)
Date expirationDate
Date mostRecentlyPerformedOn
Date lastExpirationDate
Expand All @@ -1244,7 +1243,7 @@ class SkillsLoader {
if (skillDefAndUserPoints.attributes && skillDefAndUserPoints.attributes.type == SkillAttributesDef.SkillAttributesType.AchievementExpiration) {
ExpirationAttrs expirationAttrs = skillAttributeService.convertAttrs(skillDefAndUserPoints.attributes, ExpirationAttrs)
expirationDate = expirationAttrs.nextExpirationDate
if(!achievedOn) {
if(!skillDefAndUserPoints.achievedOn) {
lastExpirationDate = skillDefAndUserPoints.expiredOn
}
isMotivationalSkill = expirationAttrs?.expirationType == ExpirationAttrs.DAILY
Expand Down Expand Up @@ -1273,7 +1272,7 @@ class SkillsLoader {
pointIncrementInterval: skillDef.pointIncrementInterval,
maxOccurrencesWithinIncrementInterval: skillDef.numMaxOccurrencesIncrementInterval,
totalPoints: skillDef.totalPoints,
achievedOn: achievedOn,
achievedOn: skillDefAndUserPoints.achievedOn,
dependencyInfo: skillDefAndUserPoints.dependencyInfo,
badgeDependencyInfo: badgeDependencySummary,
selfReporting: loadSelfReportingFromApproval(skillDefAndUserPoints),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import skills.storage.repos.QuizToSkillDefRepo
import skills.storage.repos.SkillApprovalRepo
import skills.storage.repos.SkillDefRepo
import skills.storage.repos.SkillDefWithExtraRepo
import skills.storage.repos.UserAchievedLevelRepo
import skills.storage.repos.UserPerformedSkillRepo
import skills.storage.repos.UserPointsRepo

Expand Down Expand Up @@ -70,6 +71,9 @@ class SubjectDataLoader {
@Autowired
ExpiredUserAchievementRepo expiredUserAchievementRepo

@Autowired
UserAchievedLevelRepo achievedLevelRepository

static class SkillsAndPoints {
SkillDef skillDef
int points
Expand All @@ -90,6 +94,7 @@ class SubjectDataLoader {
List<SkillTag> tags = []
SkillAttributesDef attributes
Date expiredOn
Date achievedOn
}

static class SkillsData {
Expand Down Expand Up @@ -145,6 +150,7 @@ class SubjectDataLoader {
skillsAndPoints = handleBadges(projectId, skillsAndPoints)
skillsAndPoints = handleSkillTags(projectId, skillsAndPoints)
skillsAndPoints = handleSkillQuizInfo(projectId, skillsAndPoints)
skillsAndPoints = handleAchievements(projectId, userId, skillsAndPoints)
skillsAndPoints = handleSkillExpirations(projectId, userId, skillsAndPoints)

new SkillsData(childrenWithPoints: skillsAndPoints)
Expand Down Expand Up @@ -178,9 +184,26 @@ class SubjectDataLoader {
return skillsAndPoints;
}

private List<SkillsAndPoints> handleSkillExpirations(String projectId, String userId, List<SkillsAndPoints> skillsAndPoints) {
private List<SkillsAndPoints> handleAchievements(String projectId, String userId, List<SkillsAndPoints> skillsAndPoints) {
if(projectId) {
List<String> skillIds = collectSkillIds(skillsAndPoints)
List<UserAchievement> achievedSkills = achievedLevelRepository.getAchievedDateByUserIdAndProjectIdAndSkillBatch(userId, projectId, skillIds)
if (achievedSkills) {
skillsAndPoints.each { it ->
List<UserAchievement> achievements = achievedSkills.findAll{skill -> skill.skillId == it.skillDef.skillId}
if(achievements) {
achievements?.sort { skill -> skill.achievedOn }
it.achievedOn = achievements.first()?.achievedOn
}
}
}
}
return skillsAndPoints
}

private List<SkillsAndPoints> handleSkillExpirations(String projectId, String userId, List<SkillsAndPoints> skillsAndPoints) {
if(projectId) {
List<String> skillIds = collectUnachievedSkillIds(skillsAndPoints)
def expiredSkills = expiredUserAchievementRepo.findMostRecentExpirationForAllSkills(projectId, userId, skillIds)
if (expiredSkills) {
skillsAndPoints.each { it ->
Expand Down Expand Up @@ -254,6 +277,22 @@ class SubjectDataLoader {
return skillIds
}

private List<String> collectUnachievedSkillIds(List<SkillsAndPoints> skillsAndPoints) {
List<String> skillIds = []
skillsAndPoints.forEach { it ->
if(!it.achievedOn) {
if (it.skillDef.type == SkillDef.ContainerType.SkillsGroup) {
if (it.children) {
skillIds.addAll(it.children.findAll{ child -> !child.achievedOn }.collect { child -> child.skillDef.skillId })
}
} else if (it.skillDef.type == SkillDef.ContainerType.Skill) {
skillIds.add(it.skillDef.skillId)
}
}
}
return skillIds
}

private List<SkillsAndPoints> handleGroupDescriptions(String projectId, List<SkillsAndPoints> skillsAndPoints, List<SkillRelDef.RelationshipType> relationshipTypes) {
if (relationshipTypes.containsAll([SkillRelDef.RelationshipType.SkillsGroupRequirement, SkillRelDef.RelationshipType.GroupSkillToSubject])) {
List<SkillsAndPoints> groups = skillsAndPoints.findAll({ it.skillDef.type == SkillDef.ContainerType.SkillsGroup })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ interface UserAchievedLevelRepo extends CrudRepository<UserAchievement, Integer>
@Query('''select ua.achievedOn from UserAchievement ua where ua.userId= ?1 and ua.projectId=?2 and ua.skillId=?3''')
Date getAchievedDateByUserIdAndProjectIdAndSkillId(String userId, String projectId, String skillId)

@Nullable
@Query('''select ua from UserAchievement ua where ua.userId = ?1 and ua.projectId = ?2 and ua.skillId in ?3''')
List<UserAchievement> getAchievedDateByUserIdAndProjectIdAndSkillBatch(String userId, String projectId, List<String> skillId)

@Query('''select ua from UserAchievement ua where ua.userId = ?1 and ua.projectId in ?2''')
List<UserAchievement> findAllByUserAndProjectIds(String userId, Collection<String> projectId)

Expand Down

0 comments on commit 58340c8

Please sign in to comment.