-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
594f2c0
commit f34e144
Showing
6 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/com/hero/alignlab/batch/grouprank/job/GroupRankRefreshJob.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/hero/alignlab/batch/grouprank/scheduler/GroupRankScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters