-
Notifications
You must be signed in to change notification settings - Fork 3
[BE] batch -> scheduler #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kdh-92
wants to merge
6
commits into
main
Choose a base branch
from
be-feat/add-batch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
07d8a4a
batch 설정 추가
kdh-92 85919b4
멀티 스키마 연결 & 테스트
kdh-92 9f9299b
배치 스키마에 배치 메타데이터 추가 처리
kdh-92 7cfa06f
batch 제거 후 scheduler로 주간, 월간 세팅 (cron, use 용 yml 추가)
kdh-92 337f4e9
batch 제거 & 통계 주간, 월간에 필요한 분기 처리 추가
kdh-92 a153473
주간 / 월간 통계 테이블 & Entry 생성 -> 계산 및 저장 로직 생성
kdh-92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...tiggle-root/tiggle/src/main/kotlin/com/side/tiggle/domain/scheduler/model/MonthlyStats.kt
This file contains hidden or 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,29 @@ | ||
| package com.side.tiggle.domain.scheduler.model | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnore | ||
| import com.side.tiggle.domain.category.model.Category | ||
| import com.side.tiggle.domain.member.model.Member | ||
| import java.time.LocalDate | ||
| import javax.persistence.* | ||
|
|
||
| @Entity | ||
| @Table(name = "monthly_stats") | ||
| class MonthlyStats( | ||
| @JsonIgnore | ||
| @JoinColumn(name = "member_id", nullable = false) | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| val member: Member, | ||
| val monthlyStart: LocalDate, | ||
| val monthlyEnd: LocalDate, | ||
| val totalAmount: Int, | ||
| val highestAmount: Int, | ||
| val lowestAmount: Int, | ||
| @JsonIgnore | ||
| @JoinColumn(name = "most_frequent_category_id", nullable = false) | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| val category: Category | ||
| ){ | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| val id: Long? = null | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../tiggle-root/tiggle/src/main/kotlin/com/side/tiggle/domain/scheduler/model/WeeklyStats.kt
This file contains hidden or 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,26 @@ | ||
| package com.side.tiggle.domain.scheduler.model | ||
|
|
||
| import com.side.tiggle.domain.category.model.Category | ||
| import com.side.tiggle.domain.member.model.Member | ||
| import java.time.LocalDate | ||
| import javax.persistence.* | ||
|
|
||
| @Entity | ||
| @Table(name = "weekly_stats") | ||
| class WeeklyStats( | ||
| @JoinColumn(name = "member_id", nullable = false) | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| val member: Member, | ||
| val weeklyStart: LocalDate, | ||
| val weeklyEnd: LocalDate, | ||
| val totalAmount: Int, | ||
| val highestAmount: Int, | ||
| val lowestAmount: Int, | ||
| @JoinColumn(name = "most_frequent_category_id", nullable = false) | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| val category: Category | ||
| ){ | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| val id: Long? = null | ||
| } |
7 changes: 7 additions & 0 deletions
7
...gle/src/main/kotlin/com/side/tiggle/domain/scheduler/repository/MonthlyStatsRepository.kt
This file contains hidden or 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,7 @@ | ||
| package com.side.tiggle.domain.scheduler.repository | ||
|
|
||
| import com.side.tiggle.domain.scheduler.model.MonthlyStats | ||
| import org.springframework.data.jpa.repository.JpaRepository | ||
|
|
||
| interface MonthlyStatsRepository: JpaRepository<MonthlyStats, Long> { | ||
| } |
7 changes: 7 additions & 0 deletions
7
...ggle/src/main/kotlin/com/side/tiggle/domain/scheduler/repository/WeeklyStatsRepository.kt
This file contains hidden or 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,7 @@ | ||
| package com.side.tiggle.domain.scheduler.repository | ||
|
|
||
| import com.side.tiggle.domain.scheduler.model.WeeklyStats | ||
| import org.springframework.data.jpa.repository.JpaRepository | ||
|
|
||
| interface WeeklyStatsRepository: JpaRepository<WeeklyStats, Long> { | ||
| } |
51 changes: 51 additions & 0 deletions
51
...tiggle-root/tiggle/src/main/kotlin/com/side/tiggle/domain/scheduler/service/JobService.kt
This file contains hidden or 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 com.side.tiggle.domain.scheduler.service | ||
|
|
||
| import com.side.tiggle.domain.transaction.repository.TransactionRepository | ||
| import org.springframework.stereotype.Service | ||
| import java.time.DayOfWeek | ||
| import java.time.LocalDate | ||
| import java.time.YearMonth | ||
|
|
||
| @Service | ||
| class JobService( | ||
| private val txRepository: TransactionRepository, | ||
| private val statsService: StatsService | ||
| ) { | ||
|
|
||
| fun runNowJob() { | ||
| // 현재 작업 로직 | ||
| val (weeklyStart, weeklyEnd) = getDate("weekly", LocalDate.now()) | ||
| val weeklyMemberTxsMap = txRepository.findByDateBetweenOrderByDateAsc(weeklyStart, weeklyEnd).groupBy { it.member.id } | ||
| println("시작일 : ${weeklyStart}, 종료일 : ${weeklyEnd}") | ||
| statsService.calculateAndSaveStats(weeklyMemberTxsMap, weeklyStart, weeklyEnd) | ||
| } | ||
|
|
||
| fun generateWeeklySummary() { | ||
| val (weeklyStart, weeklyEnd) = getDate("weekly", LocalDate.now()) | ||
| val weeklyMemberTxsMap = txRepository.findByDateBetweenOrderByDateAsc(weeklyStart, weeklyEnd).groupBy { it.member.id } | ||
| } | ||
|
|
||
| fun generateMonthlySummary() { | ||
|
|
||
| println("Montly summary is running...") | ||
|
|
||
| val (monthlyStart, monthlyEnd) = getDate("monthly", LocalDate.now()) | ||
| val monthlyMemberTxsMap = txRepository.findByDateBetweenOrderByDateAsc(monthlyStart, monthlyEnd).groupBy { it.member.id } | ||
| } | ||
|
|
||
| fun getDate(type: String, today: LocalDate): Pair<LocalDate, LocalDate> { | ||
| return when (type) { | ||
| "weekly" -> { | ||
| val lastMonday = today.minusWeeks(1).with(DayOfWeek.MONDAY) | ||
| val lastSunday = today.minusWeeks(1).with(DayOfWeek.SUNDAY) | ||
| Pair(lastMonday, lastSunday) | ||
| } | ||
| "monthly" -> { | ||
| val firstDayOfLastMonth = today.minusMonths(1).withDayOfMonth(1) | ||
| val lastDayOfLastMonth = YearMonth.from(today.minusMonths(1)).atEndOfMonth() | ||
| Pair(firstDayOfLastMonth, lastDayOfLastMonth) | ||
| } | ||
| else -> throw IllegalStateException("Unknown type $type") | ||
| } | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
...ggle-root/tiggle/src/main/kotlin/com/side/tiggle/domain/scheduler/service/StatsService.kt
This file contains hidden or 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,70 @@ | ||
| package com.side.tiggle.domain.scheduler.service | ||
|
|
||
| import com.side.tiggle.domain.category.repository.CategoryRepository | ||
| import com.side.tiggle.domain.member.repository.MemberRepository | ||
| import com.side.tiggle.domain.scheduler.model.MonthlyStats | ||
| import com.side.tiggle.domain.scheduler.model.WeeklyStats | ||
| import com.side.tiggle.domain.scheduler.repository.MonthlyStatsRepository | ||
| import com.side.tiggle.domain.scheduler.repository.WeeklyStatsRepository | ||
| import com.side.tiggle.domain.transaction.model.Transaction | ||
| import com.side.tiggle.global.exception.NotFoundException | ||
| import org.springframework.stereotype.Service | ||
| import java.time.LocalDate | ||
| import java.time.temporal.ChronoUnit | ||
|
|
||
| @Service | ||
| class StatsService( | ||
| private val weeklyStatsRepository: WeeklyStatsRepository, | ||
| private val monthlyStatsRepository: MonthlyStatsRepository, | ||
| private val memberRepository: MemberRepository, | ||
| private val categoryRepository: CategoryRepository | ||
| ) { | ||
|
|
||
| fun calculateAndSaveStats(memberTxs: Map<Long, List<Transaction>>, periodStart: LocalDate, periodEnd: LocalDate) { | ||
| memberTxs.mapValues { (memberId, txs) -> | ||
| val totalAmount = txs.sumOf { it.amount } | ||
| val highestAmount = txs.maxOf { it.amount } | ||
| val lowestAmount = txs.minOf { it.amount } | ||
| val mostFrequentCategory = txs | ||
| .groupBy { it.category.id } | ||
| .mapValues { entry -> | ||
| entry.value.size to entry.value.sumOf { it.amount } | ||
| } | ||
| .entries | ||
| .sortedWith( | ||
| compareByDescending<Map.Entry<Long?, Pair<Int, Int>>> { it.value.first } | ||
| .thenByDescending { it.value.second } | ||
| ) | ||
| .firstOrNull()?.key ?: -1 | ||
|
|
||
| val member = memberRepository.findById(memberId).orElseThrow { NotFoundException() } | ||
| val category = categoryRepository.findById(mostFrequentCategory).orElseThrow { NotFoundException() } | ||
|
|
||
| val daysBetween = ChronoUnit.DAYS.between(periodStart, periodEnd) | ||
| println("total : $totalAmount \n highest : $highestAmount \n lowest : $lowestAmount") | ||
| if (daysBetween <= 7) { | ||
| val weeklyStats = WeeklyStats( | ||
| member = member, | ||
| weeklyStart = periodStart, | ||
| weeklyEnd = periodEnd, | ||
| totalAmount = totalAmount, | ||
| highestAmount = highestAmount, | ||
| lowestAmount = lowestAmount, | ||
| category = category | ||
| ) | ||
| weeklyStatsRepository.save(weeklyStats) | ||
| } else { | ||
| val monthlyStats = MonthlyStats( | ||
| member = member, | ||
| monthlyStart = periodStart, | ||
| monthlyEnd = periodEnd, | ||
| totalAmount = totalAmount, | ||
| highestAmount = highestAmount, | ||
| lowestAmount = lowestAmount, | ||
| category = category | ||
| ) | ||
| monthlyStatsRepository.save(monthlyStats) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
...iggle-root/tiggle/src/main/kotlin/com/side/tiggle/global/config/SchedulerConfiguration.kt
This file contains hidden or 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,48 @@ | ||
| package com.side.tiggle.global.config | ||
|
|
||
| import com.side.tiggle.domain.scheduler.service.JobService | ||
| import org.springframework.beans.factory.annotation.Value | ||
| import org.springframework.context.annotation.Configuration | ||
| import org.springframework.scheduling.annotation.EnableScheduling | ||
| import org.springframework.scheduling.annotation.SchedulingConfigurer | ||
| import org.springframework.scheduling.config.ScheduledTaskRegistrar | ||
|
|
||
| @Configuration | ||
| @EnableScheduling | ||
| class SchedulerConfig( | ||
| private val jobService: JobService, | ||
| @Value("\${week-schedule.cron}") private val weekCron: String, | ||
| @Value("\${week-schedule.use}") private val useWeekSchedule: Boolean, | ||
| @Value("\${monthly-schedule.cron}") private val monthlyCron: String, | ||
| @Value("\${monthly-schedule.use}") private val useMonthlySchedule: Boolean, | ||
| @Value("\${now-schedule.cron}") private val nowCron: String, | ||
| @Value("\${now-schedule.use}") private val useNowSchedule: Boolean | ||
| ) : SchedulingConfigurer { | ||
|
|
||
| override fun configureTasks(taskRegistrar: ScheduledTaskRegistrar) { | ||
| if (useWeekSchedule) { | ||
| taskRegistrar.addCronTask({ runJob("weekly") }, weekCron) | ||
| } | ||
| if (useMonthlySchedule) { | ||
| taskRegistrar.addCronTask({ runJob("monthly") }, monthlyCron) | ||
| } | ||
| if (useNowSchedule) { | ||
| taskRegistrar.addCronTask({ runJob("now") }, nowCron) | ||
| } | ||
| } | ||
|
|
||
| private fun runJob(jobType: String) { | ||
| try { | ||
| when (jobType) { | ||
| "weekly" -> jobService.generateWeeklySummary() | ||
| "monthly" -> jobService.generateMonthlySummary() | ||
| "now" -> jobService.runNowJob() | ||
| else -> println("Unknown job type: $jobType") | ||
| } | ||
| } catch (e: InterruptedException) { | ||
| println("* Thread가 강제 종료되었습니다. Message: ${e.message}") | ||
| } catch (e: Exception) { | ||
| println("* Batch 시스템이 예기치 않게 종료되었습니다. Message: ${e.message}") | ||
| } | ||
| } | ||
| } | ||
83 changes: 83 additions & 0 deletions
83
backend/tiggle-root/tiggle/src/main/kotlin/com/side/tiggle/global/scheduler/JobService.kt
This file contains hidden or 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,83 @@ | ||
| package com.side.tiggle.global.scheduler | ||
|
|
||
| import com.side.tiggle.domain.transaction.model.Transaction | ||
| import com.side.tiggle.domain.transaction.repository.TransactionRepository | ||
| import org.springframework.stereotype.Service | ||
| import java.time.DayOfWeek | ||
| import java.time.LocalDate | ||
| import java.time.YearMonth | ||
|
|
||
| @Service | ||
| class JobService( | ||
| private val txRepository: TransactionRepository | ||
| ) { | ||
|
|
||
| fun runNowJob() { | ||
| // 현재 작업 로직 | ||
| val (weeklyStart, weeklyEnd) = getDate("weekly", LocalDate.now()) | ||
| val weeklyMemberTxsMap = txRepository.findByDateBetweenOrderByDateAsc(weeklyStart, weeklyEnd).groupBy { it.member.id } | ||
|
|
||
| println(calculateMemberStats(weeklyMemberTxsMap)) | ||
| } | ||
|
|
||
| fun generateWeeklySummary() { | ||
| val (weeklyStart, weeklyEnd) = getDate("weekly", LocalDate.now()) | ||
| val weeklyMemberTxsMap = txRepository.findByDateBetweenOrderByDateAsc(weeklyStart, weeklyEnd).groupBy { it.member.id } | ||
| } | ||
|
|
||
| fun generateMonthlySummary() { | ||
|
|
||
| println("Montly summary is running...") | ||
|
|
||
| val (monthlyStart, monthlyEnd) = getDate("monthly", LocalDate.now()) | ||
| val monthlyMemberTxsMap = txRepository.findByDateBetweenOrderByDateAsc(monthlyStart, monthlyEnd).groupBy { it.member.id } | ||
| } | ||
|
|
||
| fun getDate(type: String, today: LocalDate): Pair<LocalDate, LocalDate> { | ||
| return when (type) { | ||
| "weekly" -> { | ||
| val lastMonday = today.minusWeeks(1).with(DayOfWeek.MONDAY) | ||
| val lastSunday = today.minusDays(1).with(DayOfWeek.SUNDAY) | ||
| Pair(lastMonday, lastSunday) | ||
| } | ||
| "monthly" -> { | ||
| val firstDayOfLastMonth = today.minusMonths(1).withDayOfMonth(1) | ||
| val lastDayOfLastMonth = YearMonth.from(today.minusMonths(1)).atEndOfMonth() | ||
| Pair(firstDayOfLastMonth, lastDayOfLastMonth) | ||
| } | ||
| else -> throw IllegalStateException("Unknown type $type") | ||
| } | ||
| } | ||
|
|
||
| fun calculateMemberStats(memberTxs: Map<Long, List<Transaction>>) : Map<Long, MemberStats> { | ||
| return memberTxs.mapValues { entry -> | ||
| val txs = entry.value | ||
| val totalAmount = txs.sumOf { it.amount } | ||
| val highestAmount = txs.maxOf { it.amount } | ||
| val lowestAmount = txs.minOf { it.amount } | ||
| val mostFrequentCategory = txs | ||
| .groupingBy { it.category.id } | ||
| .eachCount() | ||
| .maxByOrNull { it.value }?.key ?: -1 | ||
|
|
||
| MemberStats( | ||
| totalAmount = totalAmount, | ||
| highestAmount = highestAmount, | ||
| lowestAmount = lowestAmount, | ||
| mostFrequentCategory = mostFrequentCategory | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 총 금액 | ||
| // 최고 금액 | ||
| // 최저 금액 | ||
| // 최다 카테고리 항목 | ||
| // 이전 주(달)과 차액 | ||
| data class MemberStats( | ||
| val totalAmount: Int, | ||
| val highestAmount: Int, | ||
| val lowestAmount: Int, | ||
| val mostFrequentCategory: Long | ||
| ) |
11 changes: 11 additions & 0 deletions
11
backend/tiggle-root/tiggle/src/main/resources/application-scheduler.yml
This file contains hidden or 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 @@ | ||
| week-schedule: | ||
| cron: "0 0 0 * * MON" | ||
| use: true | ||
|
|
||
| monthly-schedule: | ||
| cron: "0 0 0 1 * *" | ||
| use: true | ||
|
|
||
| now-schedule: | ||
| cron: "0 0/1 * * * ?" # 매분 동작 | ||
| use: true |
This file contains hidden or 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
13 changes: 0 additions & 13 deletions
13
backend/tiggle-root/tiggle/src/test/java/com/side/tiggle/TiggleApplicationTests.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.