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

✨ [Feat] 유저의 입력 값 바탕 제일 높은 가중치의 개발자 3명 반환하는 API #151

Merged
merged 13 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package ssuPlector.repository.developer;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import ssuPlector.domain.Developer;
import ssuPlector.domain.category.Part;
import ssuPlector.dto.request.DeveloperDTO.DeveloperMatchingDTO;

public interface DeveloperRepositoryCustom {

Page<Developer> findDevelopers(String sortType, Part part, Pageable pageable);

List<Developer> searchDeveloper(String developerName);

Map<Long, Double> matchDeveloper(String developerInfo, DeveloperMatchingDTO requestDTO);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ssuPlector.repository.developer;

import static ssuPlector.domain.QDeveloper.developer;
import static ssuPlector.domain.QProjectDeveloper.projectDeveloper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand All @@ -18,7 +21,10 @@

import lombok.RequiredArgsConstructor;
import ssuPlector.domain.Developer;
import ssuPlector.domain.category.DevLanguage;
import ssuPlector.domain.category.Part;
import ssuPlector.domain.category.TechStack;
import ssuPlector.dto.request.DeveloperDTO.DeveloperMatchingDTO;
import ssuPlector.global.exception.GlobalException;
import ssuPlector.global.response.code.GlobalErrorCode;

Expand Down Expand Up @@ -77,4 +83,62 @@ BooleanExpression part2Eq(Part part) {
public List<Developer> searchDeveloper(String name) {
return queryFactory.selectFrom(developer).where(developer.name.contains(name)).fetch();
}

BooleanExpression searchDeveloperStudentNumber(String min, String max) {
if (min == null) min = "0";
if (max == null) max = "100";
return developer.studentNumber.between(min, max);
}

BooleanExpression searchDeveloperProjectExperience(Boolean experience) {
if (experience == null || !experience) {
return developer.isNotNull();
}
return queryFactory
.selectFrom(projectDeveloper)
.where(projectDeveloper.developer.eq(developer))
.exists();
}

@Override
public Map<Long, Double> matchDeveloper(String developerInfo, DeveloperMatchingDTO requestDTO) {
// part, 개발 경험, 학번
List<Developer> developers =
queryFactory
.selectFrom(developer)
.where(
part1Eq(requestDTO.getPart())
Copy link
Member

Choose a reason for hiding this comment

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

part1만 조회하는 이유가 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

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

아니요.. 그냥 제가 까먹은겁니다 ... 추가하도록 할게요!!

.and(
searchDeveloperStudentNumber(
requestDTO.getStudentNumberMin(),
requestDTO.getStudentNumberMax()))
.and(
searchDeveloperProjectExperience(
requestDTO.getProjectExperience())))
.fetch();

Map<Long, Double> weight = new HashMap<>();

// 사용언어, 기술스택
for (Developer developer : developers) {
double tmpWeight = 0.0;

List<DevLanguage> devLanguageList = developer.getLanguageList();
List<DevLanguage> intersectionLang =
devLanguageList.stream()
.filter(requestDTO.getLanguageList()::contains)
.toList();

tmpWeight += intersectionLang.size() * 0.5;

List<TechStack> techStackList = developer.getTechStackList();
List<TechStack> intersectionTech =
techStackList.stream().filter(requestDTO.getTechStackList()::contains).toList();

tmpWeight += intersectionTech.size() * 0.3;

weight.put(developer.getId(), tmpWeight);
}
return weight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface DeveloperService {
Long createDummyDeveloper(DummyDeveloperRequestDTO requestDTO, MultipartFile image);

List<DeveloperSearchDTO> searchDeveloper(String developerName);

List<DeveloperSearchDTO> matchDeveloper(String developerInfo, DeveloperMatchingDTO requestDTO);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package ssuPlector.service.developer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import ssuPlector.aws.s3.AmazonS3Manager;
import ssuPlector.converter.DeveloperConverter;
Expand All @@ -23,6 +38,7 @@
import ssuPlector.domain.category.TechStack;
import ssuPlector.dto.request.DeveloperDTO;
import ssuPlector.dto.request.DeveloperDTO.DeveloperListRequestDTO;
import ssuPlector.dto.request.DeveloperDTO.DeveloperMatchingDTO;
import ssuPlector.dto.request.DeveloperDTO.DeveloperRequestDTO;
import ssuPlector.dto.request.DeveloperDTO.DeveloperUpdateRequestDTO;
import ssuPlector.dto.response.DeveloperDTO.DeveloperSearchDTO;
Expand All @@ -42,6 +58,9 @@ public class DeveloperServiceImpl implements DeveloperService {
private final AmazonS3Manager s3Manager;
private final UuidRepository uuidRepository;

@Value("${sp.ai.url}")
private String aiUrl;

@Override
@Transactional
public Long createDeveloper(String email, DeveloperRequestDTO requestDTO) {
Expand Down Expand Up @@ -137,4 +156,73 @@ public List<DeveloperSearchDTO> searchDeveloper(String developerName) {
List<Developer> developers = developerRepository.searchDeveloper(developerName);
return developers.stream().map(DeveloperConverter::toDeveloperSearchDTO).toList();
}

@Override
public List<DeveloperSearchDTO> matchDeveloper(
String developerInfo, DeveloperMatchingDTO requestDTO) {
// request
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);
String requestBody =
String.format(
"{\"part\":\"%s\", \"request\":\"%s\"}",
requestDTO.getPart(), developerInfo);

HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

ResponseEntity<String> response =
restTemplate.exchange(aiUrl, HttpMethod.POST, requestEntity, String.class);

// response
ObjectMapper objectMapper = new ObjectMapper();
List<Long> developerIds = new ArrayList<>();
try {
JsonNode developersNode = objectMapper.readTree(response.getBody()).path("developers");
Iterator<JsonNode> elements = developersNode.elements();
while (elements.hasNext()) {
Long developerId = elements.next().path("developer_id").asLong();
developerIds.add(developerId);
}
} catch (JsonProcessingException e) {
throw new GlobalException(GlobalErrorCode.INVALID_REQUEST_INFO);
}

Map<Long, Double> weight = developerRepository.matchDeveloper(developerInfo, requestDTO);

double w = 0.0;
for (Long developerId : developerIds) {
Developer developer =
developerRepository
.findById(developerId)
.orElseThrow(
() -> new GlobalException(GlobalErrorCode.DEVELOPER_NOT_FOUND));

weight.put(developer.getId(), weight.get(developer.getId()) + w);
w -= 0.3;
Copy link
Member

Choose a reason for hiding this comment

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

JSON 데이터를 읽어오는 과정이 복잡하긴 하지만 각 개발자들의 매칭 정도를 가중치로 표현해서 정렬하는 좋은 방식인 것 같습니다. 오류 부분을 수정하고 나서 머지하면 될 것 같습니다. 수고하셨습니다

}

// sort
List<Pair<Long, Double>> sortedDeveloper =
weight.entrySet().stream()
.map(entry -> Pair.of(entry.getKey(), entry.getValue()))
.sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue()))
.toList();

List<Developer> developerList =
sortedDeveloper.stream()
.limit(3)
.map(
m -> {
Optional<Developer> optionalDeveloper =
developerRepository.findById(m.getLeft());
return optionalDeveloper.orElseThrow(
() ->
new GlobalException(
GlobalErrorCode.DEVELOPER_NOT_FOUND));
})
.toList();
return developerList.stream().map(DeveloperConverter::toDeveloperSearchDTO).toList();
}
}