Skip to content

Commit

Permalink
#2425 Switch to getting skills for subject in a batch
Browse files Browse the repository at this point in the history
dwalizer committed Oct 23, 2023
1 parent aa32915 commit d6401b9
Showing 5 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ class UserAchievementExpirationService {
}
} else if (expirationAttrs.expirationType == ExpirationAttrs.DAILY) {
LocalDateTime achievedOnOlderThan = now.minusDays(expirationAttrs.every)
expireAchievementsForSkillAchievedBefore(skillAttributesDef, achievedOnOlderThan?.toDate())
expireAchievementsForSkillAchievedBefore(skillAttributesDef.skillRefId, achievedOnOlderThan?.toDate())
} else if (expirationAttrs.expirationType != ExpirationAttrs.NEVER) {
log.error("Unexpected expirationType [${expirationAttrs?.expirationType}] - ${expirationAttrs}")
}
@@ -89,16 +89,15 @@ class UserAchievementExpirationService {
skillEventAdminService.deleteAllSkillEventsForSkill(skillRefId)
}

private void expireAchievementsForSkillAchievedBefore(SkillAttributesDef skillAttributesDef, Date expirationDate) {
private void expireAchievementsForSkillAchievedBefore(Integer skillRefId, Date expirationDate) {
// find any UserAchievement's for this skill where the most recent associated UserPerformedSkill.performedOn is older than the expiration date
List<UserAchievement> expiredUserAchievements = expiredUserAchievementRepo.findUserAchievementsBySkillRefIdWithMostRecentUserPerformedSkillBefore(skillAttributesDef.skillRefId, expirationDate)
List<UserAchievement> expiredUserAchievements = expiredUserAchievementRepo.findUserAchievementsBySkillRefIdWithMostRecentUserPerformedSkillBefore(skillRefId, expirationDate)

expiredUserAchievements.each { ua ->
// move this user_achievement record to the expired_user_achievements table
expiredUserAchievementRepo.expireAchievementById(ua.id)
// remove all skill events for this skill and user
skillEventAdminService.deleteAllSkillEventsForSkillAndUser(skillAttributesDef.skillRefId, ua.userId)
skillAttributesDefRepo.save(skillAttributesDef)
skillEventAdminService.deleteAllSkillEventsForSkillAndUser(skillRefId, ua.userId)
}
}

Original file line number Diff line number Diff line change
@@ -1245,10 +1245,7 @@ class SkillsLoader {
ExpirationAttrs expirationAttrs = skillAttributeService.convertAttrs(skillDefAndUserPoints.attributes, ExpirationAttrs)
expirationDate = expirationAttrs.nextExpirationDate
if(!achievedOn) {
def expiredSkill = expiredUserAchievementRepo.findMostRecentExpirationForSkill(skillDef.projectId, userId, skillDef.skillId)
if (expiredSkill) {
lastExpirationDate = expiredSkill.expiredOn
}
lastExpirationDate = skillDefAndUserPoints.expiredOn
}
isMotivationalSkill = expirationAttrs?.expirationType == ExpirationAttrs.DAILY
if (isMotivationalSkill) {
20 changes: 20 additions & 0 deletions service/src/main/java/skills/skillLoading/SubjectDataLoader.groovy
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ import skills.services.settings.SettingsService
import skills.skillLoading.model.SkillDependencySummary
import skills.skillLoading.model.SkillTag
import skills.storage.model.*
import skills.storage.repos.ExpiredUserAchievementRepo
import skills.storage.repos.QuizToSkillDefRepo
import skills.storage.repos.SkillApprovalRepo
import skills.storage.repos.SkillDefRepo
@@ -66,6 +67,9 @@ class SubjectDataLoader {
@Autowired
SkillApprovalRepo skillApprovalRepo

@Autowired
ExpiredUserAchievementRepo expiredUserAchievementRepo

static class SkillsAndPoints {
SkillDef skillDef
int points
@@ -85,6 +89,7 @@ class SubjectDataLoader {
List<SimpleBadgeRes> badges = []
List<SkillTag> tags = []
SkillAttributesDef attributes
Date expiredOn
}

static class SkillsData {
@@ -140,6 +145,7 @@ class SubjectDataLoader {
skillsAndPoints = handleBadges(projectId, skillsAndPoints)
skillsAndPoints = handleSkillTags(projectId, skillsAndPoints)
skillsAndPoints = handleSkillQuizInfo(projectId, skillsAndPoints)
skillsAndPoints = handleSkillExpirations(projectId, userId, skillsAndPoints)

new SkillsData(childrenWithPoints: skillsAndPoints)
}
@@ -172,6 +178,20 @@ class SubjectDataLoader {
return skillsAndPoints;
}

private List<SkillsAndPoints> handleSkillExpirations(String projectId, String userId, List<SkillsAndPoints> skillsAndPoints) {
if(projectId) {
List<String> skillIds = collectSkillIds(skillsAndPoints)
def expiredSkills = expiredUserAchievementRepo.findMostRecentExpirationForAllSkills(projectId, userId, skillIds)
if (expiredSkills) {
skillsAndPoints.each { it ->
def expirationInfo = expiredSkills.find{ skill -> skill.skillId == it.skillDef.skillId }
it.expiredOn = expirationInfo?.expiredOn
}
}
}
return skillsAndPoints
}

@Profile
private List<SkillsAndPoints> handleSkillTags(String projectId, List<SkillsAndPoints> skillsAndPoints) {
if(projectId) {
Original file line number Diff line number Diff line change
@@ -87,4 +87,12 @@ interface ExpiredUserAchievementRepo extends CrudRepository<ExpiredUserAchieveme
''')
@Nullable
ExpiredUserAchievement findMostRecentExpirationForSkill(@Param("projectId") String projectId, @Param("userId") String userId, @Param("skillId") String skillId)

@Query(value = '''
SELECT eua
FROM ExpiredUserAchievement eua
WHERE eua.projectId = :projectId AND eua.skillId IN (:skills) AND eua.userId = :userId
''')
@Nullable
List<ExpiredUserAchievement> findMostRecentExpirationForAllSkills(@Param("projectId") String projectId, @Param("userId") String userId, @Param("skills") List<String> skills)
}
Original file line number Diff line number Diff line change
@@ -1917,7 +1917,6 @@ class PostAchievementSkillExpirationSpecs extends DefaultIntSpec {
skillsService.updateBadge(badge, badge.badgeId)

String userId = "user1"
Long timestamp = (new Date()-8).time
Date yesterday = (new Date()-1)
Date twoDaysAgo = (new Date()-2)
Date timestamp8Days = new Date()-8
@@ -1938,4 +1937,40 @@ class PostAchievementSkillExpirationSpecs extends DefaultIntSpec {
then:
skill4Information.lastExpirationDate == null
}

def "get expired skill summaries for subject"() {
def proj = SkillsFactory.createProject()
def subj = SkillsFactory.createSubject()
def skills = SkillsFactory.createSkills(10, )

skillsService.createProject(proj)
skillsService.createSubject(subj)
skillsService.createSkills(skills)

Map badge = [projectId: proj.projectId, badgeId: 'badge1', name: 'Test Badge 1']
skillsService.addBadge(badge)
skillsService.assignSkillToBadge(projectId: proj.projectId, badgeId: badge.badgeId, skillId: skills[0].skillId)
badge.enabled = 'true'
skillsService.updateBadge(badge, badge.badgeId)

String userId = "user1"
String secondUserId = "user2"
Date timestamp8Days = new Date()-8
Date timestamp9Days = new Date()-8
Long secondTimestamp = new Date().time

setup:
skills.forEach { it ->
skillsService.addSkill([projectId: proj.projectId, skillId: it.skillId], userId, timestamp8Days)
skillsService.addSkill([projectId: proj.projectId, skillId: it.skillId], userId, timestamp9Days)
skillsService.addSkill([projectId: proj.projectId, skillId: it.skillId], secondUserId, new Date(secondTimestamp))
}

when:
expireSkills(proj.projectId, skills)
def subjectInfo = skillsService.getSkillSummary(userId, proj.projectId, subj.subjectId)

then:
subjectInfo.skills.lastExpiredDate != null
}
}

0 comments on commit d6401b9

Please sign in to comment.