Skip to content

Commit

Permalink
feat: 스코어 배치 잡 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 4, 2024
1 parent 594f2c0 commit f34e144
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.hero.alignlab.batch.grouprank.job

import com.hero.alignlab.common.extension.coExecuteOrNull
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.group.application.GroupUserScoreService
import com.hero.alignlab.domain.group.application.GroupUserService
import com.hero.alignlab.domain.pose.application.PoseSnapshotService
import com.hero.alignlab.ws.handler.ReactiveGroupUserWebSocketHandler
import org.springframework.stereotype.Component
import java.time.LocalDateTime

@Component
class GroupRankRefreshJob(
private val groupUserService: GroupUserService,
private val poseSnapshotService: PoseSnapshotService,
private val groupUserScoreService: GroupUserScoreService,
private val txTemplates: TransactionTemplates,
private val wsHandler: ReactiveGroupUserWebSocketHandler,
) {
suspend fun run() {
val to = LocalDateTime.now()
val from = to.minusHours(1)

val groupUsers = groupUserService.findAll()

val uids = groupUsers.map { it.uid }

val counts = poseSnapshotService.countByUidsAndCreatedAtBetween(
uids = uids,
fromCreatedAt = from,
toCreatedAt = to
).associateBy { it.uid }

groupUserScoreService.findAllByUids(uids)
.groupBy { it.groupId }
.forEach { (key, value) ->
val groupUserScore = value.mapNotNull {
val score = counts[it.uid]?.count?.toInt() ?: return@mapNotNull null

it.apply {
it.score = score
}
}

txTemplates.writer.coExecuteOrNull {
groupUserScoreService.saveAllSync(groupUserScore)
}

wsHandler.launchSendEvent(key)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.hero.alignlab.batch.grouprank.scheduler

import com.hero.alignlab.batch.grouprank.job.GroupRankRefreshJob
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 GroupRankScheduler(
private val groupRankRefreshJob: GroupRankRefreshJob,
) {
/** 5분에 한번 스케줄러 동작 */
@Scheduled(fixedRate = 300000)
fun runRefreshGroupRank() {
CoroutineScope(Dispatchers.IO + Job()).launch {
groupRankRefreshJob.run()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ class GroupUserScoreService(
saveSync(createOrUpdateGroupUserScore)
}
}

suspend fun findAllByUids(uids: List<Long>): List<GroupUserScore> {
return withContext(Dispatchers.IO) {
groupUserScoreRepository.findAllByUidIn(uids)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,10 @@ class GroupUserService(
groupUserRepository.countAllByGroupId(groupId)
}
}

suspend fun findAll(): List<GroupUser> {
return withContext(Dispatchers.IO) {
groupUserRepository.findAll()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ interface GroupUserScoreRepository : JpaRepository<GroupUserScore, Long> {
fun findAllByGroupUserIdIn(groupUserIds: List<Long>): List<GroupUserScore>

fun findAllByGroupIdAndUidIn(groupId: Long, uids: List<Long>): List<GroupUserScore>

fun findAllByUidIn(uids: List<Long>): List<GroupUserScore>
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class ReactiveGroupUserWebSocketHandler(
}
}

fun launchSendEvent(groupId: Long) {
groupUserByMap[groupId]?.let { groupUsers ->
launchSendEvent(groupId, groupUsers)
}
}

/** 발송되는 순서가 중요하지 않다. */
private fun launchSendEvent(
groupId: Long,
Expand Down

0 comments on commit f34e144

Please sign in to comment.