Skip to content
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

[COZY-445] refac: RoomStatUtil 분리 및 적용 #214

Merged
merged 9 commits into from
Dec 6, 2024
Merged

Conversation

veronees
Copy link
Member

@veronees veronees commented Dec 5, 2024

⚒️develop의 최신 커밋을 pull 받았나요?

#️⃣ 작업 내용

어떤 기능을 구현했나요?
기존 기능에서 어떤 점이 달라졌나요?
자세한 로직이 필요하다면 함께 적어주세요!
코드에 대한 설명이라면, 코맨트를 통해서 어떤 부분이 어떤 코드인지 설명해주세요!

  1. 방 일치율 계산 메서드, 칩 일치수 계산 메서드를 RoomStatUtil 클래스로 분리했습니다
  2. RoomQueryService, RoomRecommendService, FavoriteQueryService에 적용했습니다
  3. 성능 개선을 위해 페치조인 사용하도록 일부 수정했습니다

동작 확인

기능을 실행했을 때 정상 동작하는지 여부를 확인하고 스샷을 올려주세요

수정 전 방 추천 조회
image

수정 후 방 추천 조회 - 칩 일치수 반환 형태가 좀 수정되었습니다
image


찜한 방 조회
image


RoomQueryService에서 4개? RoomStatUtil로 변경되었는데 그 중 한개 테스트
image
일치율 잘 나옵니다

💬 리뷰 요구사항(선택)

리뷰어가 특별히 봐주었으면 하는 부분이 있다면 작성해주세요
고민사항도 적어주세요.

@eple0329 @suuu0719 두 분 코드 조금 수정해서 확인해주세요~

@veronees veronees added the enhancement New feature or request label Dec 5, 2024
@veronees veronees self-assigned this Dec 5, 2024
@veronees veronees changed the title [COZY-445] feat: RoomStatUtil 구현 및 적용 [COZY-445] refac: RoomStatUtil 구현 및 적용 Dec 5, 2024
@veronees veronees changed the title [COZY-445] refac: RoomStatUtil 구현 및 적용 [COZY-445] refac: RoomStatUtil 분리 및 적용 Dec 5, 2024
Copy link

github-actions bot commented Dec 5, 2024

리뷰해드려요~

FavoriteQueryService.java

  • 새로운 Hashtag 클래스 참조 추가
  • 새로운 RoomStatUtil 클래스 참조 추가
  • 멤버 스탯과 멤버 정보를 모두 가져오는 메소드 추가
  • 선호도 정보를 모두 가져오는 메소드 추가
  • 선호도 계산 메소드를 RoomStatUtil로 이동

MateRepository.java

  • 멤버 스탯과 멤버 정보를 모두 가져오는 메소드 추가

RoomRecommendConverter.java

  • 선호도 정보를 모두 가져오는 목록 추가

RoomRecommendationResponseDTO.java

  • 선호도 정보를 모두 가져오는 목록 추가

RoomQueryService.java

  • 선호도 계산 메소드를 RoomStatUtil로 이동

RoomRecommendService.java

  • 선호도 계산 메소드를 RoomStatUtil로 이동
  • 선호도 정보를 모두 가져오는 메소드 추가
  • 선호도 정보를 모두 가져오는 목록 추가

RoomStatUtil.java

  • 새로운 유틸리티 클래스 추가
  • 선호도 정보를 모두 가져오는 메소드 추가
  • 선호도 정보를 모두 가져오는 목록 추가
  • 선호도 계산 메소드 추가
  • 선호도 계산 메소드 추가

@@ -133,7 +133,7 @@ public List<FavoriteRoomResponseDTO> getFavoriteRoomList(Member member) {
// <방 id, 해당 방의 mate 리스트>
Map<Long, List<Mate>> roomIdMatesMap = responseRoomList.stream().collect(
Collectors.toMap(Room::getId,
room -> mateRepository.findFetchMemberByRoom(room, EntryStatus.JOINED)));
room -> mateRepository.findFetchMemberAndMemberStatByRoom(room, EntryStatus.JOINED)));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mate의 Member와 Member의 MemberStat에 대한 쿼리가 추가로 날아가서 페치 조인으로 변경했습니다

Comment on lines +152 to +155
? RoomStatUtil.getPreferenceStatsMatchCounts(member, criteriaPreferenceList,
mates, memberStat)
: RoomStatUtil.getPreferenceStatsMatchCountsWithoutMemberStat(
criteriaPreferenceList);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

유틸 클래스 사용으로 수정했습니다

Comment on lines -167 to +172
List<String> roomHashTags = roomHashtagRepository.findHashtagsByRoomId(
room.getId());
List<RoomHashtag> roomHashtags = room.getRoomHashtags();
List<String> roomHashTags = roomHashtags.stream()
.map(RoomHashtag::getHashtag)
.map(Hashtag::getHashtag)
.toList();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방 조회시 RoomHashTag도 같이 가져오는걸 확인해서 쿼리 안날리도록 수정했습니다

@@ -71,7 +72,7 @@ public RoomDetailResponseDTO getRoomById(Long roomId, Long memberId) {
Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(memberId,
joinedMates.stream().map(mate -> mate.getMember().getId()).collect(Collectors.toList()));

Integer roomEquality = getCalculateRoomEquality(equalityMap);
Integer roomEquality = RoomStatUtil.getCalculateRoomEquality(equalityMap);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomStatUtil 적용 1

@@ -209,7 +202,7 @@ public List<RoomDetailResponseDTO> getRoomList(Long memberId, EntryStatus entryS
List<Mate> joinedMates = mateRepository.findAllByRoomIdAndEntryStatus(room.getId(), EntryStatus.JOINED);
Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(memberId,
joinedMates.stream().map(mate -> mate.getMember().getId()).collect(Collectors.toList()));
Integer roomEquality = getCalculateRoomEquality(equalityMap);
Integer roomEquality = RoomStatUtil.getCalculateRoomEquality(equalityMap);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomStatUtil 적용 2

@@ -307,7 +300,7 @@ public List<RoomSearchResponseDTO> searchRooms(String keyword, Long memberId) {
);
return RoomConverter.toRoomSearchResponseDTO(
room,
getCalculateRoomEquality(equalityMap)
RoomStatUtil.getCalculateRoomEquality(equalityMap)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomStatUtil 적용 3

@@ -330,7 +323,7 @@ public List<RoomSearchResponseDTO> searchRooms(String keyword, Long memberId) {
);
return RoomConverter.toRoomSearchResponseDTO(
room,
getCalculateRoomEquality(equalityMap)
RoomStatUtil.getCalculateRoomEquality(equalityMap)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomStatUtil 적용 4

Comment on lines +69 to +70
Map<Long, List<Mate>> roomMateMap = mateRepository.findAllFetchMemberAndMemberStatByEntryStatus(
EntryStatus.JOINED)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 페치조인 적용했어요

Comment on lines +123 to +124
List<PreferenceMatchCountDTO> preferenceStatsMatchCounts = RoomStatUtil.getPreferenceStatsMatchCounts(
member, preferenceList, roomMateMap.get(room.getId()), member.getMemberStat());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomStatUtil 사용으로 변경했습니다

Comment on lines +171 to +172
List<PreferenceMatchCountDTO> preferenceMatchCountDTOList = RoomStatUtil.getPreferenceStatsMatchCountsWithoutMemberStat(
memberPreferenceList);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomStatUtil 사용으로 변경했습니다2

Copy link

github-actions bot commented Dec 5, 2024

리뷰해드려요~

FavoriteQueryService.java - Review 1

  • 새로운 RoomStatUtil 클래스를 사용하여 함수 내부에서 계산되는 것을 제거했습니다.
  • 변경 전:
private List<PreferenceMatchCountDTO> getPreferenceStatsMatchCounts(Member member,
        List<String> criteriaPreferenceList, List<Mate> mates, MemberStat memberStat) {
    // 선호 스탯 일치 횟수 계산
    List<PreferenceMatchCountDTO> preferenceMatchCountDTOList = criteriaPreferenceList.stream()
        .map(preference -> {
            long equalCount = mates.stream()
                .map(mate -> mate.getMember().getMemberStat())
                .filter(mateStat -> mateStat != null && !mateStat.getMember().getId().equals(
                    member.getId())
                    && Objects.equals(MemberStatUtil.getMemberStatField(mateStat, preference),
                    MemberStatUtil.getMemberStatField(memberStat, preference)))
                .count();

            return PreferenceMatchCountDTO.builder()
                .preferenceName(preference)
                .count((int) equalCount)
                .build();
        }).toList();

    return preferenceMatchCountDTOList;
}
  • 변경 후:
private List<PreferenceMatchCountDTO> getPreferenceStatsMatchCounts(Member member,
        List<String> criteriaPreferenceList, List<Mate> mates, MemberStat memberStat) {
    return RoomStatUtil.getPreferenceStatsMatchCounts(member, criteriaPreferenceList, mates, memberStat);
}

MateRepository.java - Review 2

  • 새로운 메서드 findFetchMemberAndMemberStatByRoom 추가
  • 새로운 메서드 findAllFetchMemberAndMemberStatByEntryStatus 추가

RoomRecommendConverter.java - Review 3

  • 새로운 필드 preferenceMatchCountList 추가

RoomRecommendationResponseDTO.java - Review 4

  • 새로운 필드 preferenceMatchCountList 추가

RoomQueryService.java - Review 5

  • 새로운 RoomStatUtil 클래스를 사용하여 함수 내부에서 계산되는 것을 제거했습니다.
  • 변경 전:
private Integer getCalculateRoomEquality(Map<Long, Integer> equalityMap){
    List<Integer> roomEquality = equalityMap.values().stream()
        .toList();
    int sum = roomEquality.stream().mapToInt(Integer::intValue).sum();
    return roomEquality.isEmpty() ? null : sum / roomEquality.size();
}
  • 변경 후:
private Integer getCalculateRoomEquality(Map<Long, Integer> equalityMap){
    return RoomStatUtil.getCalculateRoomEquality(equalityMap);
}

RoomRecommendService.java - Review 6

  • 새로운 RoomStatUtil 클래스를 사용하여 함수 내부에서 계산되는 것을 제거했습니다.
  • 변경 전:
private Map<Long, Integer> calculateRoomEqualityMap(List<Room> roomList, Member member,
        Map<Long, List<Mate>> roomMateMap) {
    // 코드 생략
}
  • 변경 후:
private HashMap<Long, Integer> calculateRoomEqualityMap(List<Room> roomList, Member member,
        Map<Long, List<Mate>> roomMateMap) {
    // 코드 생략
}

RoomStatUtil.java - Review 7

  • 새로운 클래스 추가
  • 새로운 메서드 getPreferenceStatsMatchCounts 추가
  • 새로운 메서드 getPreferenceStatsMatchCountsWithoutMemberStat 추가
  • 새로운 메서드 getCalculateRoomEquality 추가

추가 개선 사항:

  • RoomStatUtil 클래스를 사용하여 코드를 간소화하고 재사용 가능한 코드를 만들었습니다.
  • 메서드 내부에서 계산되는 것을 제거하여 코드의 가독성을 향상시켰습니다.

Copy link

github-actions bot commented Dec 5, 2024

리뷰해드려요~

  1. src/main/java/com/cozymate/cozymate_server/domain/favorite/service/FavoriteQueryService.java
  • Removed the import for com.cozymate.cozymate\_server.domain.roomhashtag.repository.RoomHashtagRepository and added an import for com.cozymate.cozymate\_server.domain.roomhashtag.RoomHashtag.
  • Changed the method findHashtagsByRoomId to getRoomHashtags and modified the return type to List<RoomHashtag>.
  • Changed the method findFetchMemberByRoom to findFetchMemberAndMemberStatByRoom and added an import for com.cozymate.cozymate\_server.domain.memberstat.MemberStat.
  • Modified the method getFavoriteRoomList to use the new method findFetchMemberAndMemberStatByRoom.
  1. src/main/java/com/cozymate/cozymate_server/domain/mate/repository/MateRepository.java
  • Added two new methods findFetchMemberAndMemberStatByRoom and findAllFetchMemberAndMemberStatByEntryStatus.
  1. src/main/java/com/cozymate/cozymate_server/domain/room/converter/RoomRecommendConverter.java
  • Added an import for com.cozymate.cozymate\_server.domain.favorite.dto.response.PreferenceMatchCountDTO.
  • Changed the parameter type of equalMemberStatNum to List<PreferenceMatchCountDTO> in the methods toRoomRecommendationResponse and toRoomRecommendationResponseWhenNoMemberStat.
  1. src/main/java/com/cozymate/cozymate_server/domain/room/dto/response/RoomRecommendationResponseDTO.java
  • Added an import for com.cozymate.cozymate\_server.domain.favorite.dto.response.PreferenceMatchCountDTO.
  • Changed the type of equalMemberStatNum to List<PreferenceMatchCountDTO>.
  1. src/main/java/com/cozymate/cozymate_server/domain/room/service/RoomQueryService.java
  • Added an import for com.cozymate.cozymate\_server.global.utils.RoomStatUtil.
  • Changed the method getCalculateRoomEquality to use RoomStatUtil.getCalculateRoomEquality.
  1. src/main/java/com/cozymate/cozymate_server/domain/room/service/RoomRecommendService.java
  • Added an import for com.cozymate.cozymate\_server.global.utils.RoomStatUtil.
  • Changed the method calculateRoomEqualityMap to use RoomStatUtil.getCalculateRoomEquality.
  • Changed the method createRoomRecommendationResponse to use RoomStatUtil.getPreferenceStatsMatchCounts.
  • Changed the method getRoomRecommendationResponseListWhenNoMemberStat to use RoomStatUtil.getPreferenceStatsMatchCountsWithoutMemberStat.
  1. src/main/java/com/cozymate/cozymate_server/global/utils/RoomStatUtil.java
  • Created a new class RoomStatUtil with the following methods:
  • getPreferenceStatsMatchCounts
  • getPreferenceStatsMatchCountsWithoutMemberStat
  • getCalculateRoomEquality

These changes aim to improve the performance of the application by reducing the number of database queries and improving the readability of the code. The new methods getPreferenceStatsMatchCounts and getPreferenceStatsMatchCountsWithoutMemberStat in the RoomStatUtil class calculate the preference match counts and equality in a more efficient way. The new method findFetchMemberAndMemberStatByRoom in the MateRepository class fetches the member and member stat information in a single query, reducing the number of database queries. The changes in the RoomRecommendConverter and RoomRecommendationResponseDTO classes update the response structure to include the preference match counts instead of the equality map. The changes in the RoomQueryService, RoomRecommendService, and RoomStatUtil classes update the methods to use the new classes and methods.

Copy link

github-actions bot commented Dec 5, 2024

리뷰해드려요~

FavoriteQueryService.java - Review

  1. Removed @Slf4j annotation.
  2. Removed RoomHashtagRepository dependency.
  3. Added RoomHashtag import.
  4. Added RoomStatUtil import.
  5. Changed mateRepository.findFetchMemberByRoom to mateRepository.findFetchMemberAndMemberStatByRoom.
  6. Changed mateRepository.findAllFetchMemberByEntryStatus to mateRepository.findAllFetchMemberAndMemberStatByEntryStatus.
  7. Changed getPreferenceStatsMatchCounts method to use RoomStatUtil.
  8. Changed getCalculateRoomEquality method to use RoomStatUtil.

MateRepository.java - Review

  1. Added findFetchMemberAndMemberStatByRoom and findAllFetchMemberAndMemberStatByEntryStatus methods.

RoomRecommendConverter.java - Review

  1. Added PreferenceMatchCountDTO import.
  2. Changed equalMemberStatNum to preferenceMatchCountList.

RoomRecommendationResponseDTO.java - Review

  1. Changed equalMemberStatNum to preferenceMatchCountList.

RoomQueryService.java - Review

  1. Added RoomStatUtil import.
  2. Changed getCalculateRoomEquality method to use RoomStatUtil.

RoomRecommendService.java - Review

  1. Removed MemberStatUtil import.
  2. Removed MemberStatEqualityRepository dependency.
  3. Added MemberStatEqualityQueryService dependency.
  4. Changed mateRepository.findAllByEntryStatus to mateRepository.findAllFetchMemberAndMemberStatByEntryStatus.
  5. Changed calculateRoomEqualityMap method to use RoomStatUtil.
  6. Changed createRoomRecommendationResponse method to use RoomStatUtil.
  7. Changed getRoomRecommendationResponseListWhenNoMemberStat method to use RoomStatUtil.

RoomStatUtil.java - Review

  1. Created a new class for room statistic utility methods.
  2. Added PreferenceMatchCountDTO import.
  3. Added MemberStatUtil import.
  4. Added getPreferenceStatsMatchCounts method.
  5. Added getPreferenceStatsMatchCountsWithoutMemberStat method.
  6. Added getCalculateRoomEquality method.

These changes aim to refactor the codebase by moving duplicated logic to a utility class and simplifying the code by removing unnecessary imports and dependencies.

Comment on lines +96 to +107
List<Mate> mates = roomMateMap.get(room.getId());

Map<Long, Integer> equalityMap = memberStatEqualityQueryService.getEquality(
member.getId(),
mates.stream()
.map(mate -> mate.getMember().getId())
.toList()
);

Integer roomEquality = RoomStatUtil.getCalculateRoomEquality(equalityMap);

roomEqualityMap.put(room.getId(), roomEquality);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방 일치율 계산하는 부분입니다

Copy link
Contributor

@jpark0506 jpark0506 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM~ join fetch 적용도 좋은 것 같슴당

Copy link
Member

@eple0329 eple0329 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM입니다.
열심히 작성한 방 추천 코드는 다시 만들어졌군요... ㅠㅜ

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global Util말고 room util로 넣어주세요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 파일은 Global보다 room에 직접적인 연관이 있는 유틸이니 Room 패키지에 util로 들어가는게 맞다고 봅니당
global은 어떤 도메인과 연관을 가지지 않는 클래스를 놓는거라고 생각해서요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 수정했습니다

Copy link

github-actions bot commented Dec 6, 2024

리뷰해드려요~

FavoriteQueryService.java - Review 1

  • The RoomHashtagRepository dependency has been removed.
  • The RoomHashtag class is imported.
  • The RoomHashtag instance is obtained from the Room instance.
  • The RoomHashtag instance is utilized to obtain the list of hashtags.

MateRepository.java - Review 1

  • Two new methods have been added:
  1. findFetchMemberAndMemberStatByRoom: This method returns a list of Mate instances with their corresponding Member and MemberStat instances fetched.
  2. findAllFetchMemberAndMemberStatByEntryStatus: This method returns a list of Mate instances with their corresponding Member and MemberStat instances fetched, filtered by the given EntryStatus.

RoomRecommendConverter.java - Review 1

  • The PreferenceMatchCountDTO class is imported.
  • The preferenceMatchCountList parameter has been added to the toRoomRecommendationResponse and toRoomRecommendationResponseWhenNoMemberStat methods.

RoomRecommendationResponseDTO.java - Review 1

  • The List<PreferenceMatchCountDTO> field has been added.

RoomQueryService.java - Review 1

  • The RoomStatUtil class is imported.
  • The getCalculateRoomEquality method has been updated to utilize the RoomStatUtil class.

RoomRecommendService.java - Review 1

  • The RoomStatUtil class is imported.
  • The calculateRoomEqualityMap method has been updated to utilize the RoomStatUtil class.
  • The createRoomRecommendationResponse method has been updated to utilize the RoomStatUtil class.

RoomStatUtil.java - Review 1

  • This class has been created to contain methods related to room statistics.
  • The getPreferenceStatsMatchCounts method has been added to calculate the preference match counts.
  • The getPreferenceStatsMatchCountsWithoutMemberStat method has been added to calculate the preference match counts without a member stat.
  • The getCalculateRoomEquality method has been added to calculate the room equality.

In conclusion, the changes are focused on refactoring and optimizing the code related to room statistics and preferences. The RoomStatUtil class has been introduced to centralize the logic related to room statistics. The MateRepository class has been updated to include methods that fetch Member and MemberStat instances along with Mate instances. The RoomRecommendConverter and RoomRecommendationResponseDTO classes have been updated to include the PreferenceMatchCountDTO list. The RoomQueryService and RoomRecommendService classes have been updated to utilize the RoomStatUtil class.

@veronees veronees merged commit 8225f14 into develop Dec 6, 2024
1 check passed
@eple0329 eple0329 deleted the feature/COZY-445 branch December 6, 2024 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants