Skip to content

Commit

Permalink
fix: 레벨업 배치잡 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 24, 2024
1 parent b0b01b9 commit 8c77485
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions sql/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE `user_info`
(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'user id',
`nickname` varchar(64) NOT NULL COMMENT '닉네임',
`level` int DEFAULT 1 COMMENT '유저 레벨',
`created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '생성일',
`modified_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '수정일',
PRIMARY KEY (`id`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class HeroStatisticsJob(
systemActionLogRepository.count()
}
val countActiveUserTop3 = async(Dispatchers.IO) {
systemActionLogRepository.countActiveUserTop3(fromDate, toDate)
systemActionLogRepository.countActiveUser(fromDate, toDate)
}

/** 포즈 */
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/com/hero/alignlab/batch/user/job/UserLevelJob.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hero.alignlab.batch.user.job

import com.hero.alignlab.domain.log.infrastructure.SystemActionLogRepository
import com.hero.alignlab.domain.user.infrastructure.UserInfoRepository
import org.springframework.stereotype.Component
import java.time.LocalDateTime

@Component
class UserLevelJob(
private val systemActionLogRepository: SystemActionLogRepository,
private val userInfoRepository: UserInfoRepository,
) {
fun run() {
val toDate = LocalDateTime.now()
val fromDate = LocalDateTime.now().minusDays(1)

val activeUsers = systemActionLogRepository.countActiveUser(fromDate, toDate, 1000)
.filter { it.count > 100 }
.map { it.uid }

val users = userInfoRepository.findAllById(activeUsers)
.mapNotNull {
if (it.maxLevel) {
return@mapNotNull null
}

it.apply { this.level += 1 }
}

userInfoRepository.saveAll(users)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hero.alignlab.batch.user.scheduler

import com.hero.alignlab.batch.user.job.UserLevelJob
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class UserLevelScheduler(
private val userLevelJob: UserLevelJob,
) {
@Scheduled(cron = "0 0 * * * *")
fun run() {
CoroutineScope(Dispatchers.IO + Job()).launch {
userLevelJob.run()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ interface SystemActionLogRepository : JpaRepository<SystemActionLog, Long>, Syst

@Transactional(readOnly = true)
interface SystemActionLogQRepository {
fun countActiveUserTop3(fromCreatedAt: LocalDateTime, toCreatedAt: LocalDateTime): List<CountActiveUser>
fun countActiveUser(
fromCreatedAt: LocalDateTime,
toCreatedAt: LocalDateTime,
limit: Long = 3L
): List<CountActiveUser>
}

class SystemActionLogRepositoryImpl : SystemActionLogQRepository,
Expand All @@ -37,7 +41,11 @@ class SystemActionLogRepositoryImpl : SystemActionLogQRepository,

private val qSystemActionLog = QSystemActionLog.systemActionLog

override fun countActiveUserTop3(fromCreatedAt: LocalDateTime, toCreatedAt: LocalDateTime): List<CountActiveUser> {
override fun countActiveUser(
fromCreatedAt: LocalDateTime,
toCreatedAt: LocalDateTime,
limit: Long
): List<CountActiveUser> {
return JPAQuery<QSystemActionLog>(entityManager)
.select(
QCountActiveUser(
Expand All @@ -52,7 +60,7 @@ class SystemActionLogRepositoryImpl : SystemActionLogQRepository,
)
.groupBy(qSystemActionLog.uid)
.orderBy(qSystemActionLog.id.count().desc())
.limit(3)
.limit(limit)
.fetch()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class UserInfo(

@Column(name = "nickname")
var nickname: String,
) : BaseEntity()

@Column(name = "level")
var level: Int = 1,
) : BaseEntity() {
val maxLevel: Boolean
get() = level >= 5
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import com.hero.alignlab.domain.user.domain.UserInfo
data class UserInfoResponse(
val uid: Long,
val nickname: String,
val level: Int,
) {
companion object {
fun from(user: UserInfo): UserInfoResponse {
return UserInfoResponse(
uid = user.id,
nickname = user.nickname
nickname = user.nickname,
level = user.level,
)
}
}
Expand Down

0 comments on commit 8c77485

Please sign in to comment.