Skip to content

Commit

Permalink
feat: 스케줄러 시간 조절 (#498)
Browse files Browse the repository at this point in the history
* feat: 스케줄러 시간 조절

* feat: 스케줄러 추가

* feat: 테스트 계정 빼도록 수정
  • Loading branch information
injoon2019 authored Oct 3, 2024
1 parent 5fcd36a commit 2535cc4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.nexters.bottles.app.bottle.domain.enum.PingPongStatus
import com.nexters.bottles.app.bottle.repository.BottleRepository
import com.nexters.bottles.app.bottle.repository.LetterRepository
import com.nexters.bottles.app.user.domain.enum.Gender
import com.nexters.bottles.app.user.repository.UserProfileRepository
import com.nexters.bottles.app.user.repository.UserRepository
import mu.KotlinLogging
Expand Down Expand Up @@ -38,14 +39,12 @@ class StatisticsScheduler(
.baseUrl(slackUrl)
.build()

@Scheduled(cron = "0 0/5 * * * *")
@Scheduled(cron = "0 0 10 * * *")
fun sendDailyStatistics() {
log.info { "데일리 지표 스케줄러 돌기 시작" }
log.info { "slackUrl=$slackUrl" }
val yesterday = LocalDate.now().minusDays(1)

val allUsers = userRepository.findAll()
val currentUser = allUsers.filterNot { it.deleted }
val currentUser = allUsers.filterNot { it.deleted }.filter { it.id > 151 }
val yesterdayRegisterUser = allUsers.filter { it.createdAt.toLocalDate() == yesterday }
val yesterdayLeaveUser = allUsers.filter { it.deletedAt?.toLocalDate() == yesterday }
val yesterdayStartPingpong = bottleRepository
Expand All @@ -55,6 +54,15 @@ class StatisticsScheduler(
)
.filter { it.pingPongStatus == PingPongStatus.MATCHED }

val currentMaleUsers = allUsers.filter { it.gender == Gender.MALE }
val currentFemaleUsers = allUsers.filter { it.gender == Gender.FEMALE }

val currentIntroductionDoneMaleuser =
userProfileRepository.findAllByUserIdIn(currentMaleUsers.map { it.id }).filter { it.introduction.isNotEmpty() }

val currentIntroductionDoneFemaleuser =
userProfileRepository.findAllByUserIdIn(currentFemaleUsers.map { it.id }).filter { it.introduction.isNotEmpty() }

val request = mapOf(
"channel" to slackChannel,
"blocks" to listOf(
Expand All @@ -65,6 +73,8 @@ class StatisticsScheduler(
"text" to """
지표 물어다주는 새 :bird:
전체 유저: ${currentUser.count()}
자기소개 작성 남성 유저: ${currentIntroductionDoneMaleuser.count()}
자기소개 작성 여성 유저: ${currentIntroductionDoneFemaleuser.count()}
어제 가입 유저: ${yesterdayRegisterUser.count()}
어제 탈퇴 유저: ${yesterdayLeaveUser.count()}
어제 핑퐁 시작 유저: ${yesterdayStartPingpong.count()}
Expand All @@ -74,21 +84,16 @@ class StatisticsScheduler(
)
)

log.info { "Sending request to Slack: ${ObjectMapper().writeValueAsString(request)}" }

val response = webClient.post()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(request))
.retrieve()
.bodyToMono(String::class.java)
.block()

log.info { "response: $response" }
}

@Scheduled(cron = "* * 10 * * 1")
fun sendWeeklyStatistics() {
log.info { "위클리 지표 스케줄러 돌기 시작" }
val lastWeekMonday = LocalDate.now().minusDays(7)
val lastWeekSunday = LocalDate.now().minusDays(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ interface UserProfileRepository : JpaRepository<UserProfile, Long> {
fun findAllWithUser(): List<UserProfile>

fun findAllByCreatedAtGreaterThanAndCreatedAtLessThan(from: LocalDateTime, end: LocalDateTime): List<UserProfile>

fun findAllByUserIdIn(userIds: List<Long>): List<UserProfile>
}

0 comments on commit 2535cc4

Please sign in to comment.