Skip to content

Commit

Permalink
feat: 전날 종합 pose 데이터 업데이트
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Aug 6, 2024
1 parent 113e0c4 commit 1772240
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
package com.hero.alignlab.batch.job

import com.hero.alignlab.common.extension.executesOrNull
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.pose.application.PoseCountService
import com.hero.alignlab.domain.pose.application.PoseSnapshotService
import org.joda.time.LocalDate
import com.hero.alignlab.domain.pose.domain.vo.PoseTotalCount
import com.hero.alignlab.domain.user.application.UserInfoService
import org.springframework.stereotype.Component
import java.time.LocalDate

@Component
class PoseCountUpdateJob(
private val poseCountService: PoseCountService,
private val poseSnapshotService: PoseSnapshotService,
private val userInfoService: UserInfoService,
private val txTemplates: TransactionTemplates,
) {
/**
* **전날 종합 데이터 업데이트**
* - 전날 데이터의 싱크가 틀린 경우가 발생했을 때를 대비하여, 새벽에 이전 데이터 정합도를 다시 한번 체크한다.
*/
suspend fun run() {
val targetDate = LocalDate.now().minusDays(1)

val uids = userInfoService.findAllUids()

uids
.chunked(20)
.forEach { targetUids ->
val totalCountByUid = poseSnapshotService.countByTypeAndDate(targetUids, targetDate)
.groupBy { totalCount -> totalCount.uid }

val poseCounts = poseCountService.findAllByUidIn(targetUids)
.mapNotNull { poseCount ->
val totalCount = totalCountByUid[poseCount.uid] ?: return@mapNotNull null

poseCount.apply {
this.totalCount = PoseTotalCount(
count = totalCount.associate { it.type to it.count.toInt() }.toMutableMap()
)
}
}

txTemplates.writer.executesOrNull {
poseCountService.saveAllSync(poseCounts)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class PoseCountService(
return poseCountRepository.save(poseCount)
}

@Transactional
fun saveAllSync(poseCounts: List<PoseCount>): List<PoseCount> {
return poseCountRepository.saveAll(poseCounts)
}

suspend fun findByUidAndDateOrNull(uid: Long, date: LocalDate): PoseCount? {
return withContext(Dispatchers.IO) {
poseCountRepository.findByUidAndDate(uid, date)
Expand Down Expand Up @@ -71,4 +76,10 @@ class PoseCountService(
}
)
}

suspend fun findAllByUidIn(uids: List<Long>): List<PoseCount> {
return withContext(Dispatchers.IO) {
poseCountRepository.findAllByUidIn(uids)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package com.hero.alignlab.domain.pose.application

import com.hero.alignlab.domain.pose.domain.PoseSnapshot
import com.hero.alignlab.domain.pose.infrastructure.PoseSnapshotRepository
import com.hero.alignlab.domain.pose.infrastructure.model.PoseTypeCountModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate

@Service
class PoseSnapshotService(
Expand All @@ -13,4 +17,10 @@ class PoseSnapshotService(
fun saveSync(poseSnapshot: PoseSnapshot): PoseSnapshot {
return poseSnapshotRepository.save(poseSnapshot)
}

suspend fun countByTypeAndDate(uids: List<Long>, date: LocalDate): List<PoseTypeCountModel> {
return withContext(Dispatchers.IO) {
poseSnapshotRepository.countByTypeAndDate(uids, date)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PoseCount(
/** 집계 데이터 */
@Column(name = "total_count")
@Convert(converter = PoseTotalCountConverter::class)
val totalCount: PoseTotalCount,
var totalCount: PoseTotalCount,

/** 기준 날짜 */
@Column(name = "`date`")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import java.time.LocalDate
@Repository
interface PoseCountRepository : JpaRepository<PoseCount, Long>, PostCountQRepository {
fun findByUidAndDate(uid: Long, date: LocalDate): PoseCount?

fun findAllByUidIn(uids: List<Long>): List<PoseCount>
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
package com.hero.alignlab.domain.pose.infrastructure

import com.hero.alignlab.domain.pose.domain.PoseSnapshot
import com.hero.alignlab.domain.pose.domain.QPoseSnapshot
import com.hero.alignlab.domain.pose.infrastructure.model.PoseTypeCountModel
import com.hero.alignlab.domain.pose.infrastructure.model.QPoseTypeCountModel
import com.querydsl.jpa.impl.JPAQuery
import jakarta.persistence.EntityManager
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate

@Transactional(readOnly = true)
@Repository
interface PoseSnapshotRepository : JpaRepository<PoseSnapshot, Long>
interface PoseSnapshotRepository : JpaRepository<PoseSnapshot, Long>, PoseSnapshotQRepository

@Transactional(readOnly = true)
interface PoseSnapshotQRepository {
fun countByTypeAndDate(uids: List<Long>, date: LocalDate): List<PoseTypeCountModel>
}

class PoseSnapshotRepositoryImpl : PoseSnapshotQRepository, QuerydslRepositorySupport(PoseSnapshot::class.java) {
@Autowired
@Qualifier("heroEntityManager")
override fun setEntityManager(entityManager: EntityManager) {
super.setEntityManager(entityManager)
}

private val qPoseSnapshot = QPoseSnapshot.poseSnapshot

override fun countByTypeAndDate(uids: List<Long>, date: LocalDate): List<PoseTypeCountModel> {
return JPAQuery<QPoseSnapshot>(entityManager)
.select(
QPoseTypeCountModel(
qPoseSnapshot.uid,
qPoseSnapshot.type,
qPoseSnapshot.id.count()
)
)
.from(qPoseSnapshot)
.where(
qPoseSnapshot.uid.`in`(uids)
.and(qPoseSnapshot.createdAt.year().eq(date.year))
.and(qPoseSnapshot.createdAt.month().eq(date.monthValue))
.and(qPoseSnapshot.createdAt.dayOfMonth().eq(date.dayOfMonth))
)
.groupBy(qPoseSnapshot.type)
.fetch()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.hero.alignlab.domain.pose.infrastructure.model

import com.hero.alignlab.domain.pose.domain.vo.PoseType
import com.querydsl.core.annotations.QueryProjection

data class PoseTypeCountModel @QueryProjection constructor(
val uid: Long,
val type: PoseType,
val count: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ class UserInfoService(
fun deleteBySync(id: Long) {
userInfoRepository.deleteById(id)
}

suspend fun findAllUids(): List<Long> {
return withContext(Dispatchers.IO) {
userInfoRepository.findAllUids()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface UserInfoQRepository {
fun findByCredential(username: String, password: EncryptData): UserInfo?

fun findByOAuth(provider: OAuthProvider, oauthId: String): UserInfo?

fun findAllUids(): List<Long>
}

class UserInfoQRepositoryImpl : UserInfoQRepository, QuerydslRepositorySupport(UserInfo::class.java) {
Expand Down Expand Up @@ -55,4 +57,11 @@ class UserInfoQRepositoryImpl : UserInfoQRepository, QuerydslRepositorySupport(U
qOAuthUserInfo.oauthId.eq(oauthId)
).fetchFirst()
}

override fun findAllUids(): List<Long> {
return JPAQuery<UserInfo>(entityManager)
.select(qUserInfo.id)
.from(qUserInfo)
.fetch()
}
}

0 comments on commit 1772240

Please sign in to comment.