11package com .ject .vs .chat .adapter .web ;
22
3+ import com .ject .vs .analytics .AnalyticsEvent ;
4+ import com .ject .vs .analytics .AnalyticsEventLogger ;
35import com .ject .vs .chat .adapter .web .dto .*;
46import com .ject .vs .chat .port .in .*;
57import com .ject .vs .chat .port .in .dto .*;
1012import org .springframework .security .core .annotation .AuthenticationPrincipal ;
1113import org .springframework .web .bind .annotation .*;
1214
15+ import java .util .List ;
16+
1317@ RestController
1418@ RequestMapping ("/api/chats" )
1519@ RequiredArgsConstructor
1620public class ChatController implements ChatDocs {
1721
1822 private final ChatQueryUseCase chatQueryUseCase ;
1923 private final ChatCommandUseCase chatCommandUseCase ;
24+ private final AnalyticsEventLogger analytics ;
2025
2126 @ GetMapping
2227 @ Override
2328 public ChatListResponse getChatList (@ AuthenticationPrincipal Long userId ,
2429 @ RequestParam VoteStatus status ) {
25- return new ChatListResponse (
26- chatQueryUseCase .getChatList (userId , status ).stream ()
27- .map (ChatListItemResponse ::from )
28- .toList ()
29- );
30+ List <ChatListItemResponse > chats = chatQueryUseCase .getChatList (userId , status ).stream ()
31+ .map (ChatListItemResponse ::from )
32+ .toList ();
33+
34+ // unread_total_count: 각 채팅방의 안 읽은 메시지 수를 서버에서 합산(SUM)
35+ long unreadTotalCount = chats .stream ()
36+ .mapToLong (ChatListItemResponse ::unreadCount )
37+ .sum ();
38+
39+ analytics .log (AnalyticsEvent .of ("chat_list_viewed" )
40+ .put ("status" , status )
41+ .put ("chat_count" , chats .size ())
42+ .put ("unread_total_count" , unreadTotalCount ));
43+
44+ return new ChatListResponse (chats );
3045 }
3146
3247 @ GetMapping ("/{voteId}" )
3348 @ Override
3449 public ChatRoomResponse getChatRoom (@ AuthenticationPrincipal Long userId ,
3550 @ PathVariable Long voteId ) {
36- return ChatRoomResponse .from (chatQueryUseCase .getChatRoom (voteId ));
51+ ChatRoomResponse response = ChatRoomResponse .from (chatQueryUseCase .getChatRoom (voteId ));
52+
53+ analytics .log (AnalyticsEvent .of ("chat_room_entered" )
54+ .put ("vote_id" , response .voteId ())
55+ .put ("vote_status" , response .status ())
56+ .put ("participant_count" , response .participantCount ())
57+ .put ("end_at" , response .endAt ()));
58+
59+ return response ;
3760 }
3861
3962 @ GetMapping ("/{voteId}/gauge" )
@@ -48,7 +71,15 @@ public MessagePageResponse getMessages(@PathVariable Long voteId,
4871 @ AuthenticationPrincipal Long userId ,
4972 @ RequestParam (required = false ) Long cursor ,
5073 @ RequestParam (defaultValue = "30" ) int size ) {
51- return MessagePageResponse .from (chatQueryUseCase .getMessages (voteId , userId , cursor , size ));
74+ MessagePageResponse response = MessagePageResponse .from (chatQueryUseCase .getMessages (voteId , userId , cursor , size ));
75+
76+ analytics .log (AnalyticsEvent .of ("chat_messages_viewed" )
77+ .put ("vote_id" , voteId )
78+ .put ("message_count" , response .messages ().size ())
79+ .put ("next_cursor" , response .nextCursor ())
80+ .put ("has_next" , response .hasNext ()));
81+
82+ return response ;
5283 }
5384
5485 @ PostMapping ("/{voteId}/messages" )
@@ -58,7 +89,17 @@ public MessageResponse sendMessage(@PathVariable Long voteId,
5889 @ AuthenticationPrincipal Long userId ,
5990 @ RequestBody @ Valid SendMessageRequest request ) {
6091 SendMessageCommand command = new SendMessageCommand (voteId , userId , request .content ());
61- return MessageResponse .from (chatCommandUseCase .sendMessage (command ));
92+ MessageResult result = chatCommandUseCase .sendMessage (command );
93+
94+ analytics .log (AnalyticsEvent .of ("chat_message_sent" )
95+ .put ("vote_id" , voteId )
96+ .put ("message_id" , result .messageId ())
97+ .put ("message_length" , request .content () != null ? request .content ().length () : 0 )
98+ .put ("sender_vote_option" , result .senderVoteOption ())
99+ .put ("is_mine" , result .isMine ())
100+ .put ("is_first_message" , result .isFirstMessage ()));
101+
102+ return MessageResponse .from (result );
62103 }
63104
64105 @ PostMapping ("/{voteId}/read" )
@@ -68,5 +109,9 @@ public void markAsRead(@PathVariable Long voteId,
68109 @ AuthenticationPrincipal Long userId ,
69110 @ RequestBody @ Valid MarkAsReadRequest request ) {
70111 chatCommandUseCase .markAsRead (new MarkAsReadCommand (voteId , userId , request .lastReadMessageId ()));
112+
113+ analytics .log (AnalyticsEvent .of ("chat_read_updated" )
114+ .put ("vote_id" , voteId )
115+ .put ("last_read_message_id" , request .lastReadMessageId ()));
71116 }
72117}
0 commit comments