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

Deploy: v2.025 #72

Merged
merged 14 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -53,4 +53,13 @@ public SseEmitter connectSseForConvertedCanvas(
return canvasSseEmitterService.connect(profileId);
}

@GetMapping("")
public ResponseEntity<Response> convertSketchToCanvasTest(
@RequestParam MultipartFile sketchFile
)throws IOException{
String sketchUrl = s3ImageUploadService.uploadFile(sketchFile, "rendingPage");
Response response = canvasConvertService.convertSketchToCanvasDemo(sketchUrl);
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.siliconvalley.domain.rabbitMQ.service.ConvertRequestSender;
import com.siliconvalley.domain.sse.application.CanvasSseEmitterFInder;
import com.siliconvalley.domain.sse.application.ConvertResultSender;
import com.siliconvalley.global.common.code.CommonCode;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -43,6 +44,10 @@ public Response convertSketchToCanvas(Long profileId, Long subjectId, String ske
return convertRequestSender.sendSketchConversionRequest(sketch, canvas.getId(), profileId, subject);
}

public Response convertSketchToCanvasDemo(String sketch){
return Response.of(CommonCode.GOOD_REQUEST, null);
}

public Response updateSketchAndCanvas(Long profileId, Long canvasId, String sketch){
Canvas canvas = canvasFindDao.findById(canvasId);
return canvasUpdateService.updateSketchAndCanvas(canvas, sketch, profileId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.siliconvalley.domain.canvas.service;

import com.siliconvalley.domain.canvas.domain.Canvas;
import com.siliconvalley.domain.canvas.dto.CanvasConvertResponse;
import com.siliconvalley.domain.image.service.S3ImageUploadService;
import com.siliconvalley.domain.post.service.RankCachingService;
import com.siliconvalley.domain.rabbitMQ.code.RabbitMQCode;
Expand All @@ -27,7 +28,6 @@ public Response updateSketchAndCanvas(Canvas canvas, String newSketch, Long prof
s3ImageUploadService.deleteImage(canvas.getSketch());
canvas.updateSketch(newSketch);
convertRequestSender.sendSketchConversionRequest(newSketch, canvas.getId(), profileId, canvas.getSubject());
return Response.of(RabbitMQCode.CONVERSION_RESPONSE_SUCCESS, rankCachingService.getTopPostThisWeek(canvas.getSubject().getId()));
return Response.of(RabbitMQCode.CONVERSION_RESPONSE_SUCCESS, new CanvasConvertResponse(canvas.getId(), rankCachingService.getTopPostThisWeek(canvas.getSubject().getId())));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.siliconvalley.domain.google.controller;

import com.siliconvalley.domain.google.service.VisionDetectingService;
import com.siliconvalley.global.common.code.CommonCode;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/test/google")
@RequiredArgsConstructor
public class DetectingTestController {

private final VisionDetectingService visionDetectingService;

@GetMapping("{canvasId}")
public ResponseEntity<Response> test(
@PathVariable Long canvasId
){
return ResponseEntity.ok(visionDetectingService.testLabelDetecting(canvasId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.siliconvalley.domain.google.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LabelResultDto {

private String label;
private Float confidence;;

public LabelResultDto(String label, Float confidence){
this.label = label;
this.confidence = confidence;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageAnnotatorSettings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -24,14 +22,17 @@ public class GoogleVisionApiService {
@Value("${google.service.account.key}")
private String serviceAccountKey;

public Map<String, Float> detectObjects(String filePath) {
public Map<String, Float> detectLabels(String filePath) {
List<AnnotateImageRequest> requests = new ArrayList<>();
Map<String, Float> results = new HashMap<>();

try {
ImageSource imageSource = ImageSource.newBuilder().setImageUri(filePath).build();
Image img = Image.newBuilder().setSource(imageSource).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.OBJECT_LOCALIZATION).build();

// LABEL_DETECTION 유형으로 변경
Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();

AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
Expand All @@ -56,9 +57,10 @@ public Map<String, Float> detectObjects(String filePath) {
continue;
}

for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
log.info("Detected object: " + entity.getName() + " with confidence: " + entity.getScore());
results.put(entity.getName().toLowerCase(), entity.getScore());
// EntityAnnotation 리스트로 변경
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
log.info("Detected label: " + annotation.getDescription() + " with confidence: " + annotation.getScore());
results.put(annotation.getDescription().toLowerCase(), annotation.getScore());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import com.siliconvalley.domain.canvas.dao.CanvasFindDao;
import com.siliconvalley.domain.canvas.domain.Canvas;
import com.siliconvalley.domain.google.dto.LabelResultDto;
import com.siliconvalley.domain.stage.domain.Score;
import com.siliconvalley.global.common.code.CommonCode;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
Expand All @@ -21,12 +26,22 @@ public class VisionDetectingService {

public Score calculateCanvasScore(Long canvasId) {
Canvas canvas = canvasFindDao.findById(canvasId);
Map<String, Float> detectResult = visionService.detectObjects(canvas.getCanvas());

if (detectResult.size() == 0) {return Score.LOW;}
Float detectionScore = detectResult.get(canvas.getSubject().getPix2Pix().getModelName());
Map<String, Float> detectResult = visionService.detectLabels(canvas.getCanvas());
if (detectResult.size() == 0 || canvas.getSubject().getPix2Pix().getVisionName().equals(null)) {return Score.LOW;}
Float detectionScore = detectResult.get(canvas.getSubject().getPix2Pix().getVisionName());

return Score.determineScore(detectionScore);
}

public Response testLabelDetecting(Long canvasId){
Canvas canvas = canvasFindDao.findById(canvasId);
List<LabelResultDto> results = new ArrayList<>();
for (Map.Entry<String ,Float> entry : visionService.detectLabels(canvas.getCanvas()).entrySet()){
results.add(new LabelResultDto(entry.getKey(), entry.getValue()));
}

return Response.of(CommonCode.GOOD_REQUEST, results);

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.siliconvalley.domain.item.subject.dao;

import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.item.subject.dto.SubjectCachingDto;
import com.siliconvalley.domain.item.subject.exception.SubjectNotFoundException;
import com.siliconvalley.domain.pix2pix.dto.Pix2PixAndSubjectDto;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
Expand All @@ -29,9 +29,9 @@ public Subject findById(Long subjectId) {
}

@Cacheable(key = "'subject:' + #subjectId", value = "subjectCache")
public Pix2PixAndSubjectDto findSubjectForCachingById(Long subjectId){
public SubjectCachingDto findSubjectForCachingById(Long subjectId){
Optional<Subject> subject = subjectRepository.findById(subjectId);
subject.orElseThrow(() -> new SubjectNotFoundException(subjectId));
return new Pix2PixAndSubjectDto(subject.get());
return new SubjectCachingDto(subject.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.siliconvalley.domain.item.subject.dto;

import com.siliconvalley.domain.item.subject.domain.Subject;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SubjectCachingDto {

private Long subjectId;
private String subjectName;
private String subjectImage;

@Builder
public SubjectCachingDto(Subject subject){
this.subjectId = subject.getId();
this.subjectName = subject.getSubjectName();
this.subjectImage = subject.getSubjectImage();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Pix2PixFindDao {

private final Pix2PixRepository pix2PixRepository;

@Cacheable(key = "'subject:'+ #subject", value = "subjectCache")
@Cacheable(key = "'subject:'+ #subject", value = "subjectAndModelCache")
public UseModelDto findBySubjectId(Long subjectId){
Pix2Pix pix2Pix = pix2PixRepository.findBySubjectId(subjectId);
return new UseModelDto(pix2Pix);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/siliconvalley/domain/post/dao/PostFindDao.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.siliconvalley.domain.post.dao;

import com.siliconvalley.domain.item.subject.dao.SubjectFindDao;
import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.item.subject.dto.SubjectCachingDto;
import com.siliconvalley.domain.post.code.PostCode;
import com.siliconvalley.domain.post.domain.Post;
import com.siliconvalley.domain.post.dto.PostListResponse;
import com.siliconvalley.domain.post.dto.PostDetailResponse;
import com.siliconvalley.domain.post.dto.PostSubjectResponse;
import com.siliconvalley.domain.post.exception.PostNotFoundException;
import com.siliconvalley.global.common.dto.Response;
Expand All @@ -15,9 +14,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
Expand All @@ -26,6 +24,7 @@ public class PostFindDao {

private final PostRepository postRepository;
private final PostCustomRepository postCustomRepository;
private final SubjectFindDao subjectFindDao;

public Post findById(Long targetId){
final Optional<Post> post = postRepository.findById(targetId);
Expand All @@ -40,7 +39,8 @@ public Response findAll(Pageable pageable){

public Response getPostsBySubjectName(Long subjectId, Pageable pageable) {
Page<PostListResponse> postListResponses = postCustomRepository.findBySubjectId(subjectId, pageable);
return Response.of(PostCode.POST_RETRIEVE_SUCCESS, postListResponses);
SubjectCachingDto dto = subjectFindDao.findSubjectForCachingById(subjectId);
return Response.of(PostCode.POST_RETRIEVE_SUCCESS, new PostSubjectResponse(dto, postListResponses));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.siliconvalley.domain.post.dto;

import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.item.subject.dto.SubjectCachingDto;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -18,10 +19,10 @@ public class PostSubjectResponse {
private int currentPage;
private int totalPages;

public PostSubjectResponse(Subject subject, Page<PostListResponse> postPage){
this.subjectId = subject.getId();
this.subjectName = subject.getSubjectName();
this.subjectImage = subject.getSubjectImage();
public PostSubjectResponse(SubjectCachingDto dto, Page<PostListResponse> postPage){
this.subjectId = dto.getSubjectId();
this.subjectName = dto.getSubjectName();
this.subjectImage = dto.getSubjectImage();
this.postLists = postPage.getContent();
this.currentPage = postPage.getNumber();
this.totalPages = postPage.getTotalPages();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void cachingRankToRedis(RankingCachingDto rankingCachingDto){
public String getTopPostThisWeek(Long subjectId){
RankingCachingDto rankingCachingDto = redisTemplate.opsForList().index(generateRedisKey(subjectId), -1);
if (rankingCachingDto.getRankerList().isEmpty()){
return "ranking is empty";
return null;
}
return rankingCachingDto.getRankerList().get(0).getCanvasUrl();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ public ResponseEntity getStageWithRecord(
return ResponseEntity.status(HttpStatus.OK).body(stageFindDao.getStageWithRecord(profileId, stageId));
}

@PostMapping("/{profileId}/stages/{stageId}/record")

@PostMapping("/{profileId}/stages/{stageId}/records")
public ResponseEntity evaluateCanvasAndcreateRecord(
@PathVariable(name = "profileId") Long profileId,
@PathVariable(name = "stageId") Long stageId,
Expand All @@ -254,7 +255,7 @@ public ResponseEntity evaluateCanvasAndcreateRecord(
}

@PatchMapping("/{profileId}/stages/{stageId}/records/{recordId}")
public ResponseEntity evaluateCanvasAndupdateRecord(
public ResponseEntity evaluateCanvasAndUpdateRecord(
@PathVariable(name = "recordId") Long recordId,
@RequestBody RecordUpdateRequest dto
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum RabbitMQCode implements ResponseCode {
CONVERSION_REQUEST_FAILURE(400, "변환 요청에 실패하였습니다.", HttpStatus.BAD_REQUEST),
CONVERSION_RESPONSE_SUCCESS(200, "변환 응답을 성공적으로 수신하였습니다.", HttpStatus.OK),
CONVERSION_RESPONSE_FAILURE(500, "변환 응답 수신에 실패하였습니다.", HttpStatus.INTERNAL_SERVER_ERROR),
DEMO_CONVERSION_SUCCESS(200, "체험 변환 요청에 성공하였습니다.", HttpStatus.OK)
;

private final int code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.siliconvalley.domain.rabbitMQ.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DemoConversionRequest {
private String sketchUrl;
private String modelName;

public DemoConversionRequest(String sketchUrl, String modelName){
this.sketchUrl = sketchUrl;
this.modelName = modelName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.siliconvalley.domain.rabbitMQ.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DemoConversionResponse {
private String canvasUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.post.service.RankCachingService;
import com.siliconvalley.domain.rabbitMQ.code.RabbitMQCode;
import com.siliconvalley.domain.rabbitMQ.dto.DemoConversionRequest;
import com.siliconvalley.domain.rabbitMQ.dto.SketchConversionRequest;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,4 +29,10 @@ public Response sendSketchConversionRequest(String sketchUrl, Long canvasId, Lon
rabbitTemplate.convertAndSend(exchange, "sketch_conversion_request_queue" , request);
return Response.of(RabbitMQCode.CONVERSION_REQUEST_SUCCESS, new CanvasConvertResponse(canvasId, rankCachingService.getTopPostThisWeek(subject.getId())));
}

public Response sendDemoConversionRequest(String sketchUrl){
DemoConversionRequest request = new DemoConversionRequest(sketchUrl, "panda");
rabbitTemplate.convertAndSend(exchange, "demo_conversion_request_queue", request);
return Response.of(RabbitMQCode.CONVERSION_REQUEST_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Response evaluateCanvasAndcreateRecord(Long profileId, Long stageId,Recor
Profile profile = profileFindDao.findById(profileId);
Stage stage = stageFindDao.findById(stageId);

if (recordFindDao.findByProfileId(profileId).isPresent()) {
if (recordFindDao.findByProfileIdAndStageId(profileId, stageId).isPresent()) {
throw new RecordAlreadyExist(stage.getStageNum() + "번 스테이지 기록");
}

Expand Down
Loading
Loading