Skip to content

Commit acc30d7

Browse files
authored
Merge pull request #20 from Kimjipang/round10
refactor: product_metrics 테이블 컬럼 수정
2 parents 4071e77 + dc745c9 commit acc30d7

5 files changed

Lines changed: 26 additions & 17 deletions

File tree

apps/commerce-streamer/src/main/java/com/loopers/domain/ProductMetric.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,47 @@ public class ProductMetric extends BaseEntity {
2222
@Column(name = "sales_volume", nullable = false)
2323
private Long salesVolume;
2424

25+
@Column(name = "metric_date", nullable = false)
26+
private String metricDate;
27+
2528
@Enumerated(EnumType.STRING)
2629
@Column(name = "event_type", nullable = false)
2730
private ProductEventType eventType;
2831

29-
public ProductMetric(Long productId, Long viewCount, Long likeCount, Long salesVolume, ProductEventType eventType) {
32+
public ProductMetric(Long productId, Long viewCount, Long likeCount, Long salesVolume, String metricDate, ProductEventType eventType) {
3033
this.productId = productId;
3134
this.viewCount = viewCount;
3235
this.likeCount = likeCount;
3336
this.salesVolume = salesVolume;
37+
this.metricDate = metricDate;
3438
this.eventType = eventType;
3539
}
3640

3741
/*
3842
- [ ] 리팩토링 예정
3943
*/
40-
public static ProductMetric of(Long productId, ProductEventType eventType) {
44+
public static ProductMetric of(Long productId, String date, ProductEventType eventType) {
4145
if (eventType == null) {
4246
throw new IllegalArgumentException("정의되지 않은 event type입니다.");
4347
}
4448

4549
return switch (eventType) {
46-
case PRODUCT_VIEWED -> ofProductViewed(productId);
47-
case PRODUCT_LIKED -> ofProductLiked(productId);
48-
case PRODUCT_SALES -> ofProductSales(productId);
50+
case PRODUCT_VIEWED -> ofProductViewed(productId, date);
51+
case PRODUCT_LIKED -> ofProductLiked(productId, date);
52+
case PRODUCT_SALES -> ofProductSales(productId, date);
4953
};
5054
}
5155

52-
public static ProductMetric ofProductViewed(Long productId) {
53-
return new ProductMetric(productId, 1L, 0L, 0L, ProductEventType.PRODUCT_VIEWED);
56+
public static ProductMetric ofProductViewed(Long productId, String date) {
57+
return new ProductMetric(productId, 1L, 0L, 0L, date, ProductEventType.PRODUCT_VIEWED);
5458
}
5559

56-
public static ProductMetric ofProductLiked(Long productId) {
57-
return new ProductMetric(productId, 0L, 1L, 0L, ProductEventType.PRODUCT_LIKED);
60+
public static ProductMetric ofProductLiked(Long productId, String date) {
61+
return new ProductMetric(productId, 0L, 1L, 0L, date, ProductEventType.PRODUCT_LIKED);
5862
}
5963

60-
public static ProductMetric ofProductSales(Long productId) {
61-
return new ProductMetric(productId, 0L, 0L, 1L, ProductEventType.PRODUCT_SALES);
64+
public static ProductMetric ofProductSales(Long productId, String date) {
65+
return new ProductMetric(productId, 0L, 0L, 1L, date, ProductEventType.PRODUCT_SALES);
6266
}
6367

6468
public void increaseProductMetric(ProductEventType eventType) {

apps/commerce-streamer/src/main/java/com/loopers/domain/ProductMetricRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public interface ProductMetricRepository {
5-
ProductMetric findByProductId(Long productId);
5+
ProductMetric findByProductIdAndDate(Long productId, String date);
66

77
ProductMetric save(ProductMetric of);
88
}

apps/commerce-streamer/src/main/java/com/loopers/infrastructure/ProductMetricJpaRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66

77
public interface ProductMetricJpaRepository extends JpaRepository<ProductMetric, Long> {
88
ProductMetric findByProductId(Long productId);
9+
10+
ProductMetric findByProductIdAndMetricDate(Long productId, String date);
911
}

apps/commerce-streamer/src/main/java/com/loopers/infrastructure/ProductMetricRepositoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class ProductMetricRepositoryImpl implements ProductMetricRepository {
1212
private final ProductMetricJpaRepository productMetricJpaRepository;
1313

1414
@Override
15-
public ProductMetric findByProductId(Long productId) {
16-
return productMetricJpaRepository.findByProductId(productId);
15+
public ProductMetric findByProductIdAndDate(Long productId, String date) {
16+
return productMetricJpaRepository.findByProductIdAndMetricDate(productId, date);
1717
}
1818

1919
@Override

apps/commerce-streamer/src/main/java/com/loopers/interfaces/consumer/KafkaOutboxConsumer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,19 @@ public void productViewedListener(
4646

4747
Map<Long, Double> scoreDelta = new HashMap<>();
4848

49+
String date = LocalDate.now(KST).format(YYYYMMDD);
50+
4951
for (var record : messages) {
5052
OutboxEvent value = objectMapper.readValue(record.value(), OutboxEvent.class);
5153
Long productId = value.aggregateId();
5254
ProductEventType eventType = ProductEventType.valueOf(value.eventType());
5355

5456
scoreDelta.merge(productId, weight(eventType), Double::sum);
5557

56-
ProductMetric productMetric = productMetricRepository.findByProductId(productId);
58+
ProductMetric productMetric = productMetricRepository.findByProductIdAndDate(productId, date);
5759

5860
if (productMetric == null) {
59-
ProductMetric newProductMetric = ProductMetric.of(productId, eventType);
61+
ProductMetric newProductMetric = ProductMetric.of(productId, date, eventType);
6062

6163
productMetricRepository.save(newProductMetric);
6264
}
@@ -65,7 +67,7 @@ public void productViewedListener(
6567
}
6668
}
6769

68-
String key = "ranking:all:" + LocalDate.now(KST).format(YYYYMMDD);
70+
String key = "ranking:all:" + date;
6971
ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
7072

7173
for (var e : scoreDelta.entrySet()) {
@@ -85,3 +87,4 @@ public void productViewedListener(
8587
};
8688
}
8789
}
90+

0 commit comments

Comments
 (0)