Skip to content

Commit

Permalink
release: 0.6.9 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Jul 10, 2024
2 parents 1f0a0ce + e48bc19 commit 278d7ab
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kotestExtensionSpringVersion=1.1.3
testContainerVersion=1.19.3

### Netx ###
netxVersion=0.4.5
netxVersion=0.4.6

### Sentry ###
sentryVersion=4.4.0
Expand Down
37 changes: 37 additions & 0 deletions src/main/kotlin/org/gitanimals/render/app/UserStatisticSchedule.kt
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"
}
}
14 changes: 14 additions & 0 deletions src/main/kotlin/org/gitanimals/render/core/clock.kt
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"))
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
}
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()
}
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,
)
11 changes: 11 additions & 0 deletions src/test/kotlin/org/gitanimals/render/TestRoot.kt
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
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 src/test/kotlin/org/gitanimals/render/supports/SagaCapture.kt
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
}
}

0 comments on commit 278d7ab

Please sign in to comment.