Skip to content

Commit e27d520

Browse files
committed
feat: 월별/주간별 랭킹 프로세서 추가
1 parent 170808d commit e27d520

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.loopers.batch.job.ranking.processor;
2+
3+
import com.loopers.batch.domain.ranking.MonthlyRanking;
4+
import com.loopers.dto.RankedProduct;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.batch.core.configuration.annotation.StepScope;
7+
import org.springframework.batch.item.ItemProcessor;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.time.YearMonth;
12+
import java.time.format.DateTimeFormatter;
13+
import java.util.concurrent.atomic.AtomicInteger;
14+
15+
@Slf4j
16+
@StepScope
17+
@Component
18+
public class MonthlyRankingProcessor implements ItemProcessor<RankedProduct, MonthlyRanking> {
19+
20+
@Value("#{jobParameters['yearMonth']}")
21+
private String yearMonthStr;
22+
23+
private static final DateTimeFormatter YEAR_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
24+
25+
private final AtomicInteger rankCounter = new AtomicInteger(0);
26+
27+
@Override
28+
public MonthlyRanking process(RankedProduct item) {
29+
YearMonth yearMonth = parseYearMonth();
30+
31+
int rank = rankCounter.incrementAndGet();
32+
33+
return MonthlyRanking.create(
34+
rank,
35+
item.productId(),
36+
item.score(),
37+
yearMonth
38+
);
39+
}
40+
41+
private YearMonth parseYearMonth() {
42+
if (yearMonthStr != null && !yearMonthStr.isBlank()) {
43+
return YearMonth.parse(yearMonthStr, YEAR_MONTH_FORMATTER);
44+
}
45+
return YearMonth.now();
46+
}
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.loopers.batch.job.ranking.processor;
2+
3+
import com.loopers.batch.domain.ranking.WeeklyRanking;
4+
import com.loopers.dto.RankedProduct;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.batch.core.configuration.annotation.StepScope;
7+
import org.springframework.batch.item.ItemProcessor;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.time.DayOfWeek;
12+
import java.time.LocalDate;
13+
import java.time.format.DateTimeFormatter;
14+
import java.util.concurrent.atomic.AtomicInteger;
15+
16+
@Slf4j
17+
@StepScope
18+
@Component
19+
public class WeeklyRankingProcessor implements ItemProcessor<RankedProduct, WeeklyRanking> {
20+
21+
@Value("#{jobParameters['startDate']}")
22+
private String startDateStr;
23+
24+
@Value("#{jobParameters['endDate']}")
25+
private String endDateStr;
26+
27+
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
28+
29+
private final AtomicInteger rankCounter = new AtomicInteger(0);
30+
31+
@Override
32+
public WeeklyRanking process(RankedProduct item) {
33+
LocalDate weekStart = parseStartDate();
34+
LocalDate weekEnd = parseEndDate();
35+
36+
int rank = rankCounter.incrementAndGet();
37+
38+
return WeeklyRanking.create(
39+
rank,
40+
item.productId(),
41+
item.score(),
42+
weekStart,
43+
weekEnd
44+
);
45+
}
46+
47+
private LocalDate parseStartDate() {
48+
if (startDateStr != null && !startDateStr.isBlank()) {
49+
return LocalDate.parse(startDateStr, DATE_FORMATTER);
50+
}
51+
return LocalDate.now().with(DayOfWeek.MONDAY);
52+
}
53+
54+
private LocalDate parseEndDate() {
55+
if (endDateStr != null && !endDateStr.isBlank()) {
56+
return LocalDate.parse(endDateStr, DATE_FORMATTER);
57+
}
58+
return LocalDate.now().with(DayOfWeek.SUNDAY);
59+
}
60+
}

0 commit comments

Comments
 (0)