-
Notifications
You must be signed in to change notification settings - Fork 38
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
Showing
9 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/org/gitanimals/render/app/UserStatisticSchedule.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,37 @@ | ||
package org.gitanimals.render.app | ||
|
||
import org.gitanimals.render.core.instant | ||
import org.gitanimals.render.core.toKr | ||
import org.gitanimals.render.domain.UserStatisticService | ||
import org.gitanimals.render.domain.event.UserYesterdayReport | ||
import org.rooftop.netx.api.SagaManager | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class UserStatisticSchedule( | ||
private val sagaManager: SagaManager, | ||
private val userStatisticService: UserStatisticService, | ||
) { | ||
|
||
@Scheduled(cron = EVERY_9AM) | ||
fun sendYesterdayNewUserReport() { | ||
val yesterday = instant().toKr().minusDays(1) | ||
val yesterdayUserCount = userStatisticService.getYesterdayUserCount() | ||
val totalUserCount = userStatisticService.getTotalUserCount() | ||
|
||
val userYesterdayReport = UserYesterdayReport( | ||
date = yesterday, | ||
yesterdayNewUserCount = yesterdayUserCount, | ||
totalUserCount = totalUserCount, | ||
serverName = SERVER_NAME, | ||
) | ||
|
||
sagaManager.startSync(userYesterdayReport) | ||
} | ||
|
||
private companion object { | ||
private const val EVERY_9AM = "0 0 9 * * ?" | ||
private const val SERVER_NAME = "RENDER" | ||
} | ||
} |
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,14 @@ | ||
package org.gitanimals.render.core | ||
|
||
import java.time.Clock | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
|
||
var clock: Clock = Clock.systemUTC() | ||
|
||
fun instant() = Instant.now(clock) | ||
|
||
fun Instant.toZonedDateTime() = ZonedDateTime.ofInstant(this, clock.zone) | ||
|
||
fun Instant.toKr() = ZonedDateTime.ofInstant(this, ZoneId.of("Asia/Seoul")) |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/org/gitanimals/render/domain/UserStatisticRepository.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,15 @@ | ||
package org.gitanimals.render.domain | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.data.repository.query.Param | ||
import java.time.Instant | ||
|
||
interface UserStatisticRepository : JpaRepository<User, Long> { | ||
|
||
@Query("select count(u.id) from user u where u.createdAt between :startDay and :endDay") | ||
fun getDailyUserCount( | ||
@Param("startDay") startDay: Instant, | ||
@Param("endDay") endDay: Instant, | ||
): Int | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/org/gitanimals/render/domain/UserStatisticService.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,20 @@ | ||
package org.gitanimals.render.domain | ||
|
||
import org.springframework.stereotype.Service | ||
import java.time.LocalDate | ||
import java.time.ZoneOffset | ||
|
||
@Service | ||
class UserStatisticService( | ||
private val userStatisticRepository: UserStatisticRepository, | ||
) { | ||
|
||
fun getYesterdayUserCount(): Int { | ||
val current = LocalDate.now() | ||
val startDay = current.atTime(0, 0, 0).toInstant(ZoneOffset.UTC) | ||
val endDay = current.atTime(23, 59, 59).toInstant(ZoneOffset.UTC) | ||
return userStatisticRepository.getDailyUserCount(startDay, endDay) | ||
} | ||
|
||
fun getTotalUserCount(): Long = userStatisticRepository.count() | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/org/gitanimals/render/domain/event/UserYesterdayReport.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 org.gitanimals.render.domain.event | ||
|
||
import java.time.ZonedDateTime | ||
|
||
data class UserYesterdayReport( | ||
val date: ZonedDateTime, | ||
val yesterdayNewUserCount: Int, | ||
val totalUserCount: Long, | ||
val serverName: String, | ||
) |
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,11 @@ | ||
package org.gitanimals.render | ||
|
||
import org.rooftop.netx.meta.EnableSaga | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing | ||
import org.springframework.retry.annotation.EnableRetry | ||
|
||
@EnableSaga | ||
@EnableRetry | ||
@SpringBootApplication | ||
class TestRoot |
36 changes: 36 additions & 0 deletions
36
src/test/kotlin/org/gitanimals/render/app/UserStatisticScheduleTest.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,36 @@ | ||
package org.gitanimals.render.app | ||
|
||
import io.kotest.assertions.nondeterministic.eventually | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import org.gitanimals.render.TestRoot | ||
import org.gitanimals.render.supports.RedisContainer | ||
import org.gitanimals.render.supports.SagaCapture | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.TestPropertySource | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@SpringBootTest( | ||
classes = [ | ||
TestRoot::class, | ||
RedisContainer::class, | ||
SagaCapture::class, | ||
] | ||
) | ||
@TestPropertySource("classpath:application.properties") | ||
internal class UserStatisticScheduleTest( | ||
private val userStatisticSchedule: UserStatisticSchedule, | ||
private val sagaCapture: SagaCapture, | ||
) : DescribeSpec({ | ||
|
||
describe("sendYesterdayNewUserReport ๋ฉ์๋๋") { | ||
context("ํธ์ถ๋๋ฉด,") { | ||
it("UserYesterdayReport ๋ฅผ ๋ด์ ์ด๋ฒคํธ๋ฅผ ๋ฐํํ๋ค.") { | ||
userStatisticSchedule.sendYesterdayNewUserReport() | ||
|
||
eventually(5.seconds) { | ||
sagaCapture.startCountShouldBe(1) | ||
} | ||
} | ||
} | ||
} | ||
}) |
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/org/gitanimals/render/supports/SagaCapture.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,51 @@ | ||
package org.gitanimals.render.supports | ||
|
||
import io.kotest.matchers.equals.shouldBeEqual | ||
import org.rooftop.netx.api.* | ||
import org.rooftop.netx.meta.SagaHandler | ||
|
||
@SagaHandler | ||
class SagaCapture { | ||
|
||
val storage = mutableMapOf<String, Int>() | ||
|
||
fun clear() { | ||
storage.clear() | ||
} | ||
|
||
fun startCountShouldBe(count: Int) { | ||
(storage["start"] ?: 0) shouldBeEqual count | ||
} | ||
|
||
fun joinCountShouldBe(count: Int) { | ||
(storage["join"] ?: 0) shouldBeEqual count | ||
} | ||
|
||
fun commitCountShouldBe(count: Int) { | ||
(storage["commit"] ?: 0) shouldBeEqual count | ||
} | ||
|
||
fun rollbackCountShouldBe(count: Int) { | ||
(storage["rollback"] ?: 0) shouldBeEqual count | ||
} | ||
|
||
@SagaStartListener(successWith = SuccessWith.END) | ||
fun captureStart(startEvent: SagaStartEvent) { | ||
storage["start"] = (storage["start"] ?: 0) + 1 | ||
} | ||
|
||
@SagaJoinListener(successWith = SuccessWith.END) | ||
fun captureJoin(joinEvent: SagaJoinEvent) { | ||
storage["join"] = (storage["join"] ?: 0) + 1 | ||
} | ||
|
||
@SagaCommitListener | ||
fun captureCommit(commitEvent: SagaCommitEvent) { | ||
storage["commit"] = (storage["commit"] ?: 0) + 1 | ||
} | ||
|
||
@SagaRollbackListener | ||
fun captureRollback(rollbackEvent: SagaRollbackEvent) { | ||
storage["rollback"] = (storage["rollback"] ?: 0) + 1 | ||
} | ||
} |