@startuml analytics-배포완료구독 !theme mono title Analytics Service - DistributionCompleted 이벤트 구독 처리 내부 시퀀스\n(Kafka Event 구독) participant "Kafka Consumer" as Consumer participant "DistributionCompletedListener" as Listener participant "AnalyticsService" as Service participant "AnalyticsRepository" as Repository participant "CacheService" as Cache participant "Redis" as Redis database "Analytics DB" as DB note over Consumer **Kafka Consumer 설정** - Topic: DistributionCompleted - Consumer Group: analytics-service - Partition Key: eventId - At-Least-Once Delivery 보장 end note Kafka -> Consumer: DistributionCompleted 이벤트 수신\n{\n eventId: "uuid",\n distributedChannels: [\n {\n channel: "우리동네TV",\n status: "SUCCESS",\n expectedViews: 5000\n },\n {\n channel: "지니TV",\n status: "SUCCESS",\n expectedViews: 10000\n },\n {\n channel: "Instagram",\n status: "SUCCESS",\n expectedViews: 2000\n }\n ],\n completedAt: "2025-10-22T12:00:00Z"\n} activate Consumer Consumer -> Listener: onDistributionCompleted(event) activate Listener note right of Listener **멱등성 체크** - Redis Set에 이벤트 ID 존재 여부 확인 - 중복 처리 방지 - Key: distribution_completed:{eventId} end note Listener -> Redis: SISMEMBER distribution_completed {eventId} activate Redis alt 이벤트 미처리 (멱등성 보장) Redis --> Listener: false (미처리) deactivate Redis Listener -> Service: updateDistributionStats(event) activate Service note right of Service **배포 채널 통계 저장** - 채널별 배포 상태 기록 - 예상 노출 수 집계 - 배포 완료 시각 기록 end note Service -> Service: parseChannelStats(event) note right of Service **채널 데이터 파싱** - distributedChannels 배열 순회 - 각 채널별 통계 추출 - 총 예상 노출 수 계산 end note loop 각 채널별로 Service -> Repository: saveChannelStats(\n eventId, channel, stats\n) activate Repository Repository -> DB: 채널별 통계 저장\n(이벤트ID, 채널명, 상태,\n예상노출수, 배포일시 저장,\n중복 시 업데이트) activate DB DB --> Repository: 1 row inserted/updated deactivate DB Repository --> Service: ChannelStatsEntity deactivate Repository end note right of Service **배포 통계 저장 완료** - 채널별 배포 상태 기록 - 예상 노출 수 저장 - 향후 외부 API 조회 시 기준 데이터로 활용 end note Service -> Repository: updateTotalViews(eventId, totalViews) activate Repository Repository -> DB: 총 노출 수 업데이트\n(총 예상 노출 수를 설정하고,\n수정일시를 현재 시각으로 업데이트) activate DB DB --> Repository: 1 row updated deactivate DB Repository --> Service: UpdateResult (success) deactivate Repository note right of Service **이벤트 통계 업데이트** - 총 예상 노출 수 업데이트 - 다음 대시보드 조회 시 반영 end note Service -> Cache: delete("analytics:dashboard:{eventId}") activate Cache note right of Cache **캐시 무효화** - 기존 캐시 삭제 - 다음 조회 시 최신 배포 통계 반영 - 채널별 성과 차트 갱신 end note Cache -> Redis: DEL analytics:dashboard:{eventId} activate Redis Redis --> Cache: OK deactivate Redis Cache --> Service: OK deactivate Cache Service -> Redis: SADD distribution_completed {eventId} activate Redis note right of Redis **멱등성 처리 완료 기록** - Redis Set에 eventId 추가 - TTL 설정 (7일) end note Redis --> Service: OK deactivate Redis Service --> Listener: 배포 통계 업데이트 완료 deactivate Service Listener -> Consumer: ACK (처리 완료) deactivate Listener else 이벤트 이미 처리됨 (중복) Redis --> Listener: true (이미 처리) deactivate Redis note right of Listener **중복 이벤트 스킵** - At-Least-Once Delivery로 인한 중복 - 멱등성 보장으로 중복 처리 방지 end note Listener -> Consumer: ACK (스킵) deactivate Listener end Consumer --> Kafka: Commit Offset deactivate Consumer note over Consumer, DB **처리 시간** - 이벤트 수신 → 통계 업데이트 완료: 약 0.3초 - 채널별 DB INSERT (3개): 0.15초 - event_stats UPDATE: 0.05초 - Redis 캐시 무효화: 0.01초 - 멱등성 체크: 0.01초 **배포 통계 효과** - 배포 완료 즉시 통계 반영 - 채널별 성과 추적 가능 - 다음 대시보드 조회 시 최신 배포 정보 제공 end note @enduml