-
Notifications
You must be signed in to change notification settings - Fork 0
[HCR-198] Redis Consumer -> Postgres Consumer 적재 변경 #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
51850d6
[HSC-198] feat: 로그 dto 수정
rettooo cec0005
[HSC-198] feat: postgres, jpa
rettooo d80f3cf
[HSC-198] test: 테스트 컨테이너 추가
rettooo 66c1998
[HSC-198] feat: docker,yaml 설정
rettooo 8e6165e
[HSC-198] chore: 삭제
rettooo 5584567
[HSC-198] feat: ProductViewHistory 엔티티
rettooo 9b4e09b
[HSC-198] feat: Repository
rettooo 612e836
[HSC-198] feat: Consumer, Service
rettooo 2fed122
[HSC-198] test: 테스트
rettooo 8c05200
[HSC-198] feat: build gradle 수정
rettooo 8485314
[HSC-198] refactor: event_properties 전용 dto
rettooo 82346f7
[HSC-198] test: 수정
rettooo a2d914f
[HSC-198] feat: 테스트 수정
rettooo 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
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
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
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/holliverse/logserver/entity/ProductViewHistory.java
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,39 @@ | ||
| package com.holliverse.logserver.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.EmbeddedId; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
| import java.time.OffsetDateTime; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Table(name = "product_view_history") | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ProductViewHistory { | ||
|
|
||
| @EmbeddedId | ||
| private ProductViewHistoryId id; | ||
|
|
||
| @Column(name = "product_name", nullable = false, length = 100) | ||
| private String productName; | ||
|
|
||
| @Column(name = "product_type", nullable = false, length = 50) | ||
| private String productType; | ||
|
|
||
| // List<String>을 JSON 문자열로 직렬화해 저장. DB DDL에서 jsonb 타입으로 선언됨. | ||
| @Column(columnDefinition = "jsonb") | ||
| private String tags; | ||
|
|
||
| @Column(name = "viewed_at", nullable = false, columnDefinition = "TIMESTAMPTZ") | ||
| private OffsetDateTime viewedAt; | ||
|
|
||
| @Column(name = "last_event_id", nullable = false) | ||
| private Long lastEventId; | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/holliverse/logserver/entity/ProductViewHistoryId.java
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,24 @@ | ||
| package com.holliverse.logserver.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embeddable; | ||
| import java.io.Serializable; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Embeddable | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @EqualsAndHashCode | ||
| // (member_id, product_id) 복합 PK @Embeddable | ||
| public class ProductViewHistoryId implements Serializable { | ||
|
|
||
| @Column(name = "member_id", nullable = false) | ||
| private Long memberId; | ||
|
|
||
| @Column(name = "product_id", nullable = false) | ||
| private Long productId; | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
src/main/java/com/holliverse/logserver/repository/ProductViewHistoryRepository.java
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,64 @@ | ||
| package com.holliverse.logserver.repository; | ||
|
|
||
| import com.holliverse.logserver.entity.ProductViewHistory; | ||
| import com.holliverse.logserver.entity.ProductViewHistoryId; | ||
| import java.time.OffsetDateTime; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Modifying; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| public interface ProductViewHistoryRepository | ||
| extends JpaRepository<ProductViewHistory, ProductViewHistoryId> { | ||
|
|
||
| /** | ||
| * 복합 PK (member_id, product_id) 기준 UPSERT. | ||
| * 동일 사용자가 동일 상품을 재조회하면 나머지 컬럼 전체를 최신값으로 덮어씀. | ||
| * tags는 Java String → CAST AS jsonb 로 PostgreSQL 레벨에서 변환됨. | ||
| */ | ||
| @Modifying | ||
| @Query(value = """ | ||
| INSERT INTO product_view_history | ||
| (member_id, product_id, product_name, product_type, tags, viewed_at, last_event_id) | ||
| VALUES | ||
| (:memberId, :productId, :productName, :productType, | ||
| CAST(:tags AS jsonb), :viewedAt, :lastEventId) | ||
| ON CONFLICT (member_id, product_id) | ||
| DO UPDATE SET | ||
| product_name = EXCLUDED.product_name, | ||
| product_type = EXCLUDED.product_type, | ||
| tags = EXCLUDED.tags, | ||
| viewed_at = EXCLUDED.viewed_at, | ||
| last_event_id = EXCLUDED.last_event_id | ||
| """, nativeQuery = true) | ||
| void upsert( | ||
| @Param("memberId") Long memberId, | ||
| @Param("productId") Long productId, | ||
| @Param("productName") String productName, | ||
| @Param("productType") String productType, | ||
| @Param("tags") String tags, | ||
| @Param("viewedAt") OffsetDateTime viewedAt, | ||
| @Param("lastEventId") Long lastEventId | ||
| ); | ||
|
|
||
| /** | ||
| * 유저당 최신 N개(viewed_at DESC)를 초과하는 오래된 레코드 삭제. | ||
| * 복합 PK 구조라 id 컬럼이 없으므로 product_id NOT IN 서브쿼리로 대상을 특정함. | ||
| */ | ||
| @Modifying | ||
| @Query(value = """ | ||
| DELETE FROM product_view_history | ||
| WHERE member_id = :memberId | ||
| AND product_id NOT IN ( | ||
| SELECT product_id | ||
| FROM product_view_history | ||
| WHERE member_id = :memberId | ||
| ORDER BY viewed_at DESC | ||
| LIMIT :maxCount | ||
| ) | ||
| """, nativeQuery = true) | ||
| void trimOldRecords( | ||
| @Param("memberId") Long memberId, | ||
| @Param("maxCount") int maxCount | ||
| ); | ||
| } |
78 changes: 78 additions & 0 deletions
78
src/main/java/com/holliverse/logserver/service/PostgresLogService.java
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,78 @@ | ||
| package com.holliverse.logserver.service; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.holliverse.logserver.dto.LogEvent; | ||
| import com.holliverse.logserver.repository.ProductViewHistoryRepository; | ||
| import java.time.OffsetDateTime; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PostgresLogService { | ||
|
|
||
| private static final int MAX_RECENT_VIEWS = 30; | ||
|
|
||
| private final ProductViewHistoryRepository repository; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| /** | ||
| * click_product_detail 이벤트 1건을 product_view_history 테이블에 UPSERT 후 | ||
| * 해당 유저의 레코드가 MAX_RECENT_VIEWS 를 초과하면 오래된 항목 자동 삭제. | ||
| */ | ||
| @Transactional | ||
| public void process(LogEvent event) throws JsonProcessingException { | ||
| Long memberId = event.getMemberId(); | ||
| ClickProductDetailProperties props = objectMapper.convertValue( | ||
| event.getEventProperties(), | ||
| ClickProductDetailProperties.class | ||
| ); | ||
|
|
||
| Long productId = props.getProductId(); | ||
| if (productId == null) { | ||
| throw new IllegalArgumentException("product_id 변환 실패: null"); | ||
| } | ||
| String productName = props.getProductName(); | ||
| String productType = props.getProductType(); | ||
| List<String> tagList = props.getTags() != null ? props.getTags() : Collections.emptyList(); | ||
| String tags = objectMapper.writeValueAsString(tagList); | ||
|
|
||
| OffsetDateTime viewedAt = OffsetDateTime.parse(event.getTimestamp()); | ||
| Long lastEventId = event.getEventId(); | ||
|
|
||
| repository.upsert(memberId, productId, productName, productType, tags, viewedAt, lastEventId); | ||
| repository.trimOldRecords(memberId, MAX_RECENT_VIEWS); | ||
|
|
||
| log.debug("[PostgresLog] UPSERT 완료 memberId={} productId={}", memberId, productId); | ||
| } | ||
|
|
||
| /** | ||
| * click_product_detail 이벤트의 event_properties 전용 DTO. | ||
| * ObjectMapper.convertValue()로 변환하여 Map 직접 캐스팅 및 ClassCastException을 방지. | ||
| * JSON 키(snake_case)는 @JsonProperty로 Java CamelCase 필드에 매핑. | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| private static class ClickProductDetailProperties { | ||
|
|
||
| @JsonProperty("product_id") | ||
| private Long productId; | ||
|
|
||
| @JsonProperty("product_name") | ||
| private String productName; | ||
|
|
||
| @JsonProperty("product_type") | ||
| private String productType; | ||
|
|
||
| private List<String> tags; | ||
| } | ||
| } |
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.