Skip to content

Commit

Permalink
fix: 스코어 연산 처리 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 4, 2024
1 parent 3e574d8 commit 594f2c0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
import java.time.LocalDateTime

@Service
class PoseSnapshotService(
Expand All @@ -27,6 +28,20 @@ class PoseSnapshotService(
}
}

suspend fun countByUidsAndCreatedAtBetween(
uids: List<Long>,
fromCreatedAt: LocalDateTime,
toCreatedAt: LocalDateTime,
): List<PoseTypeCountModel> {
return withContext(Dispatchers.IO) {
poseSnapshotRepository.countByUidAndCreatedAt(
uids = uids,
fromCreatedAt = fromCreatedAt,
toCreatedAt = toCreatedAt,
)
}
}

suspend fun deleteAll() {
txTemplates.writer.coExecuteOrNull {
poseSnapshotRepository.deleteAllInBatch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ interface PoseSnapshotRepository : JpaRepository<PoseSnapshot, Long>, PoseSnapsh
fun countByCreatedAtBetween(startAt: LocalDateTime, endAt: LocalDateTime): Long
}

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

fun countByUidAndCreatedAt(
uids: List<Long>,
fromCreatedAt: LocalDateTime,
toCreatedAt: LocalDateTime,
): List<PoseTypeCountModel>
}

class PoseSnapshotQRepositoryImpl : PoseSnapshotQRepository, QuerydslRepositorySupport(PoseSnapshot::class.java) {
Expand Down Expand Up @@ -54,4 +59,26 @@ class PoseSnapshotQRepositoryImpl : PoseSnapshotQRepository, QuerydslRepositoryS
.groupBy(qPoseSnapshot.uid, qPoseSnapshot.type)
.fetch()
}

override fun countByUidAndCreatedAt(
uids: List<Long>,
fromCreatedAt: LocalDateTime,
toCreatedAt: LocalDateTime
): List<PoseTypeCountModel> {
return JPAQuery<QPoseSnapshot>(entityManager)
.select(
QPoseTypeCountModel(
qPoseSnapshot.uid,
qPoseSnapshot.type,
qPoseSnapshot.id.count()
)
)
.from(qPoseSnapshot)
.where(
qPoseSnapshot.uid.`in`(uids),
qPoseSnapshot.createdAt.between(fromCreatedAt, toCreatedAt)
)
.groupBy(qPoseSnapshot.uid, qPoseSnapshot.type)
.fetch()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.hero.alignlab.domain.group.application.GroupUserScoreService
import com.hero.alignlab.domain.group.application.GroupUserService
import com.hero.alignlab.domain.pose.application.PoseCountService
import com.hero.alignlab.domain.pose.application.PoseKeyPointSnapshotService
import com.hero.alignlab.domain.pose.application.PoseSnapshotService
import com.hero.alignlab.domain.pose.domain.PoseCount
import com.hero.alignlab.domain.pose.domain.PoseKeyPointSnapshot
import com.hero.alignlab.domain.pose.domain.vo.PoseType.Companion.BAD_POSE
Expand All @@ -19,6 +20,7 @@ import org.springframework.transaction.event.TransactionalEventListener

@Component
class PoseSnapshotListener(
private val poseSnapshotService: PoseSnapshotService,
private val poseKeyPointSnapshotService: PoseKeyPointSnapshotService,
private val poseCountService: PoseCountService,
private val groupUserScoreService: GroupUserScoreService,
Expand Down Expand Up @@ -58,10 +60,14 @@ class PoseSnapshotListener(

/** group score 처리 */
groupUserService.findByUidOrNull(event.poseSnapshot.uid)?.run {
val score = updatedPoseCount.totalCount.count
.filter { (key, _) -> key in BAD_POSE }
.values
.sum()
val to = event.poseSnapshot.createdAt
val from = to.minusHours(1)

val score = poseSnapshotService.countByUidsAndCreatedAtBetween(
uids = listOf(event.poseSnapshot.uid),
fromCreatedAt = from,
toCreatedAt = to
).filter { model -> model.type in BAD_POSE }.sumOf { model -> model.count }.toInt()

groupUserScoreService.createOrUpdateGroupUserScore(this, score)
}
Expand Down

0 comments on commit 594f2c0

Please sign in to comment.