-
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
113e0c4
commit 1772240
Showing
9 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
32 changes: 31 additions & 1 deletion
32
src/main/kotlin/com/hero/alignlab/batch/job/PoseCountUpdateJob.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 |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |
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
46 changes: 45 additions & 1 deletion
46
src/main/kotlin/com/hero/alignlab/domain/pose/infrastructure/PoseSnapshotRepository.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 |
---|---|---|
@@ -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() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/hero/alignlab/domain/pose/infrastructure/model/PoseTypeCountModel.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,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, | ||
) |
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