Skip to content

Commit

Permalink
release v1.1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Oct 2, 2024
2 parents 156adc8 + 261e951 commit c057ab3
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ class AuthFacade(
}
}

suspend fun getUidFromToken(token: AuthUserToken): Long {
return jwtTokenService.decodeToken(token).id
suspend fun getUidFromTokenOrNull(token: AuthUserToken): Long? {
return try {
jwtTokenService.decodeToken(token).id
} catch (e: Exception) {
null
}
}

suspend fun getUidFromTokenOrThrow(token: AuthUserToken): Long {
return getUidFromTokenOrNull(token) ?: throw InvalidTokenException(ErrorCode.INVALID_TOKEN)
}

private fun raiseIf(userStatus: UserStatus): UserStatusTypeModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SystemActionLogEventListener(
mdcCoroutineScope(Dispatchers.IO + Job() + coroutineExceptionHandler.handler, event.traceId).launch {
val uid = event.token
?.let { token -> AuthUserToken.from(token) }
?.let { token -> authFacade.getUidFromToken(token) }
?.let { token -> authFacade.getUidFromTokenOrNull(token) }

SystemActionLog(
uid = uid,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.oksusu.susu.api.exception.advice

import com.oksusu.susu.api.common.dto.ErrorResponse
import com.oksusu.susu.common.exception.InvalidTokenException
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class AuthExceptionHandler {
private val logger = KotlinLogging.logger { }

@ExceptionHandler(InvalidTokenException::class)
protected fun handleInvalidTokenException(e: InvalidTokenException): ResponseEntity<ErrorResponse> {
logger.warn { "InvalidTokenException ${e.message}" }
val response = ErrorResponse(
errorCode = e.errorCode.name,
reason = e.message ?: e.errorCode.description,
)
return ResponseEntity(response, e.errorCode.status)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class SusuExceptionHandler {
private val logger = KotlinLogging.logger { }

@ExceptionHandler(SusuException::class)
protected fun handleBusinessException(e: SusuException): ResponseEntity<ErrorResponse> {
logger.warn { "BusinessException ${e.message}" }
protected fun handleSusuException(e: SusuException): ResponseEntity<ErrorResponse> {
logger.warn { "SusuException ${e.message}" }
val response = ErrorResponse(
errorCode = e.errorCode.name,
reason = e.message ?: e.errorCode.description,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.oksusu.susu.batch.summary.job

import arrow.fx.coroutines.parZip
import com.oksusu.susu.client.discord.DiscordClient
import com.oksusu.susu.client.discord.model.DiscordMessageModel
import com.oksusu.susu.common.extension.format
Expand Down Expand Up @@ -33,8 +34,18 @@ class SusuStatisticsDailySummaryJob(
suspend fun runDailySummaryJob() {
val now = LocalDateTime.now()
val beforeOneDay = now.minusDays(1)
val beforeTwoDay = beforeOneDay.minusDays(1)

parZipWithMDC(
parZip(
{ dailySummary(beforeOneDay, now) },
{ dailySummary(beforeTwoDay, beforeOneDay) }
) { todayMessage, yesterdayMessage ->
discordClient.sendSummary(message(todayMessage, yesterdayMessage))
}
}

private suspend fun dailySummary(beforeOneDay: LocalDateTime, now: LocalDateTime): DailySummaryMessage {
return parZipWithMDC(
{ withContext(Dispatchers.IO) { systemActionLogRepository.countByCreatedAtBetween(beforeOneDay, now) } },
{ withContext(Dispatchers.IO) { userRepository.countByCreatedAtBetween(beforeOneDay, now) } },
{ withContext(Dispatchers.IO) { envelopeRepository.countByCreatedAtBetween(beforeOneDay, now) } },
Expand Down Expand Up @@ -64,10 +75,10 @@ class SusuStatisticsDailySummaryJob(
dailyReportHistoryCount = dailyReportHistoryCount,
totalActiveUserCount = totalActiveUserCount
)
}.run { discordClient.sendSummary(this.message()) }
}
}

private data class DailySummaryMessage(
data class DailySummaryMessage(
val now: LocalDateTime,
val dailySystemActionLogCount: Long,
val dailyUserCount: Long,
Expand All @@ -77,21 +88,21 @@ class SusuStatisticsDailySummaryJob(
val dailyUserWithdrawCount: Long,
val dailyReportHistoryCount: Long,
val totalActiveUserCount: Long,
) {
fun message(): DiscordMessageModel {
return DiscordMessageModel(
"""
**[ 일단위 통계 알림 ${now.format("yyyyMMdd HH:mm:ss")} ]**
- 전날 종합 api 호출수 : $dailySystemActionLogCount
- 총합 실제 유저수 : $totalActiveUserCount
- 전날 신규 가입 유저수 : $dailyUserCount
- 전날 유저 탈퇴수 : $dailyUserWithdrawCount
- 전날 신규 봉투 생성수 : $dailyEnvelopeCount
- 전날 신규 장부 생성수 : $dailyLedgerCount
- 전날 신규 친구 생성수 : $dailyFriendCount
- 전날 종합 신고수 : $dailyReportHistoryCount
)

fun message(today: DailySummaryMessage, yesterday: DailySummaryMessage): DiscordMessageModel {
return DiscordMessageModel(
"""
**[ 일단위 통계 알림 ${today.now.format("yyyyMMdd HH:mm:ss")} ]**
- 전날 종합 api 호출수 : ${today.dailySystemActionLogCount} [이틀 대비 ${today.dailySystemActionLogCount - yesterday.dailySystemActionLogCount}]
- 총합 실제 유저수 : ${today.totalActiveUserCount} [이틀 대비 ${today.totalActiveUserCount - yesterday.totalActiveUserCount}]
- 전날 신규 가입 유저수 : ${today.dailyUserCount} [이틀 대비 ${today.dailyUserCount - yesterday.dailyUserCount}]
- 전날 유저 탈퇴수 : ${today.dailyUserWithdrawCount} [이틀 대비 ${today.dailyUserWithdrawCount - yesterday.dailyUserWithdrawCount}]
- 전날 신규 봉투 생성수 : ${today.dailyEnvelopeCount} [이틀 대비 ${today.dailyEnvelopeCount - yesterday.dailyEnvelopeCount}]
- 전날 신규 장부 생성수 : ${today.dailyLedgerCount} [이틀 대비 ${today.dailyLedgerCount - yesterday.dailyLedgerCount}]
- 전날 신규 친구 생성수 : ${today.dailyFriendCount} [이틀 대비 ${today.dailyFriendCount - yesterday.dailyFriendCount}]
- 전날 종합 신고수 : ${today.dailyReportHistoryCount} [이틀 대비 ${today.dailyReportHistoryCount - yesterday.dailyReportHistoryCount}]
""".trimIndent()
)
}
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.oksusu.susu.batch.summary.job

import arrow.fx.coroutines.parZip
import com.oksusu.susu.client.discord.DiscordClient
import com.oksusu.susu.client.discord.model.DiscordMessageModel
import com.oksusu.susu.common.extension.format
Expand Down Expand Up @@ -31,8 +32,21 @@ class SusuStatisticsHourSummaryJob(
suspend fun runHourSummaryJob() {
val now = LocalDateTime.now()
val beforeOneHour = now.minusHours(1)
val beforeTwoHour = beforeOneHour.minusHours(2)

parZipWithMDC(
parZip(
{ dailySummary(beforeOneHour, now) },
{ dailySummary(beforeTwoHour, beforeOneHour) }
) { beforeOneMessage, beforeTwoMessage ->
discordClient.sendSummary(message(beforeOneMessage, beforeTwoMessage))
}
}

private suspend fun dailySummary(
beforeOneHour: LocalDateTime,
now: LocalDateTime,
): HourSummaryMessage {
return parZipWithMDC(
{ withContext(Dispatchers.IO) { systemActionLogRepository.countByCreatedAtBetween(beforeOneHour, now) } },
{ withContext(Dispatchers.IO) { ledgerRepository.countByCreatedAtBetween(beforeOneHour, now) } },
{ withContext(Dispatchers.IO) { envelopeRepository.countByCreatedAtBetween(beforeOneHour, now) } },
Expand All @@ -57,10 +71,10 @@ class SusuStatisticsHourSummaryJob(
userCount = userCount,
userWithdrawCount = userWithdrawCount
)
}.run { discordClient.sendSummary(this.message()) }
}
}

private data class HourSummaryMessage(
data class HourSummaryMessage(
val beforeOneHour: LocalDateTime,
val now: LocalDateTime,
val systemActionLogCount: Long,
Expand All @@ -69,20 +83,20 @@ class SusuStatisticsHourSummaryJob(
val friendCount: Long,
val userCount: Long,
val userWithdrawCount: Long,
) {
fun message(): DiscordMessageModel {
return DiscordMessageModel(
"""
**[ 시간단위 통계 알림 ${now.format("yyyy-MM-dd HH:mm:ss")} ]**
- ${beforeOneHour.format("HH:mm:ss")} ~ ${now.format("HH:mm:ss")}
- api 호출수 : $systemActionLogCount
- 장부 생성수 : $ledgerCount
- 봉투 생성수 : $envelopeCount
- 친구 생성수 : $friendCount
- 유저 생성수 : $userCount
- 유저 탈퇴수 : $userWithdrawCount
)

fun message(beforeOneMessage: HourSummaryMessage, beforeTwoMessage: HourSummaryMessage): DiscordMessageModel {
return DiscordMessageModel(
"""
**[ 시간단위 통계 알림 ${beforeOneMessage.now.format("yyyy-MM-dd HH:mm:ss")} ]**
- ${beforeOneMessage.beforeOneHour.format("HH:mm:ss")} ~ ${beforeOneMessage.now.format("HH:mm:ss")}
- api 호출수 : ${beforeOneMessage.systemActionLogCount} [전시간 대비 ${beforeOneMessage.systemActionLogCount - beforeTwoMessage.systemActionLogCount}]
- 장부 생성수 : ${beforeOneMessage.ledgerCount} [전시간 대비 ${beforeOneMessage.ledgerCount - beforeTwoMessage.ledgerCount}]
- 봉투 생성수 : ${beforeOneMessage.envelopeCount} [전시간 대비 ${beforeOneMessage.envelopeCount - beforeTwoMessage.envelopeCount}]
- 친구 생성수 : ${beforeOneMessage.friendCount} [전시간 대비 ${beforeOneMessage.friendCount - beforeTwoMessage.friendCount}]
- 유저 생성수 : ${beforeOneMessage.userCount} [전시간 대비 ${beforeOneMessage.userCount - beforeTwoMessage.userCount}]
- 유저 탈퇴수 : ${beforeOneMessage.userWithdrawCount} [전시간 대비 ${beforeOneMessage.userWithdrawCount - beforeTwoMessage.userWithdrawCount}]
""".trimIndent()
)
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.oksusu.susu.common.exception

class InvalidTokenException(
val errorCode: ErrorCode,
message: String? = null,
val extra: Map<String, Any>? = null,
) : RuntimeException(message ?: errorCode.description)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ open class SusuException(

class NotFoundException(errorCode: ErrorCode) : SusuException(errorCode)

class InvalidTokenException(errorCode: ErrorCode) : SusuException(errorCode)

class InvalidRequestException(errorCode: ErrorCode) : SusuException(errorCode)

class FailToCreateException(errorCode: ErrorCode) : SusuException(errorCode)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=1.1.21
version=1.1.22
kotlin.code.style=official

0 comments on commit c057ab3

Please sign in to comment.