Skip to content

Commit

Permalink
chore: 자식 태그 추가시 sse 전송 (#94)
Browse files Browse the repository at this point in the history
* chore: 자식 태그 추가시 sse 전송

* refactor: 메모 업데이트시 백그라운드 작업 비동기 처리

* chore: 어노테이션 추가
  • Loading branch information
BaeJinho4028 authored Nov 21, 2024
1 parent 0eaa854 commit 303aaf9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.example.oatnote.domain.memotag.service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.redisson.api.RAtomicLong;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.example.oatnote.domain.memotag.service.client.AiMemoTagClient;
import com.example.oatnote.domain.memotag.service.client.dto.AiCreateEmbeddingResponse;
import com.example.oatnote.domain.memotag.service.client.dto.AiCreateMetadataResponse;
import com.example.oatnote.domain.memotag.service.client.dto.AiCreateStructureRequest;
import com.example.oatnote.domain.memotag.service.client.dto.AiCreateStructureResponse;
import com.example.oatnote.domain.memotag.service.client.dto.innerDto.RawTag;
import com.example.oatnote.domain.memotag.service.memo.MemoService;
import com.example.oatnote.domain.memotag.service.memo.model.Memo;
import com.example.oatnote.domain.memotag.service.producer.SseMessageProducer;
import com.example.oatnote.domain.memotag.service.relation.MemoTagRelationService;
import com.example.oatnote.domain.memotag.service.relation.model.MemoTagRelation;
import com.example.oatnote.domain.memotag.service.tag.TagService;
Expand Down Expand Up @@ -132,4 +132,39 @@ void processMemoTag(AiCreateStructureResponse aiCreateStructureResponse, String
memoService.createMemos(memos, userId);
memoTagRelationService.createRelations(memoTagRelations, userId);
}

@Async("AsyncMemoTagExecutor")
public void updateEmbeddingAndMetadata(
Memo memo,
String content,
List<String> imageUrls,
List<String> voiceUrls,
String userId
) {
AiCreateEmbeddingResponse aiCreateEmbeddingResponse = null;

boolean isContentChanged = !content.equals(memo.getContent());
if (isContentChanged) {
aiCreateEmbeddingResponse = aiMemoTagClient.createEmbedding(content);
}

AiCreateMetadataResponse aiCreateMetadataResponse = aiMemoTagClient.createMetadata(
content,
imageUrls,
voiceUrls
);
List<Double> embedding = Objects.nonNull(aiCreateEmbeddingResponse)
? aiCreateEmbeddingResponse.embedding() : memo.getEmbedding();
String metadata = Objects.nonNull(aiCreateMetadataResponse)
? aiCreateMetadataResponse.metadata() : memo.getMetadata();
List<Double> embeddingMetadata = Objects.nonNull(aiCreateMetadataResponse)
? aiCreateMetadataResponse.embeddingMetadata() : memo.getEmbeddingMetadata();

memo.update(
embedding,
embeddingMetadata,
metadata
);
memoService.updateMemo(memo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.redisson.api.RedissonClient;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -55,7 +54,6 @@
import com.example.oatnote.domain.memotag.service.memo.MemoService;
import com.example.oatnote.domain.memotag.service.memo.model.Memo;
import com.example.oatnote.domain.memotag.service.producer.FileMessageProducer;
import com.example.oatnote.domain.memotag.service.producer.SseMessageProducer;
import com.example.oatnote.domain.memotag.service.relation.MemoTagRelationService;
import com.example.oatnote.domain.memotag.service.relation.model.MemoTagRelation;
import com.example.oatnote.domain.memotag.service.searchhistory.SearchHistoryService;
Expand Down Expand Up @@ -121,6 +119,7 @@ public void createMemos(CreateMemosRequest request, String userId) {
asyncMemoTagService.createStructure(request.fileUrl(), userId);
}

@ProcessingMemoCount(action = ActionType.JUST_PUBLISH)
public CreateChildTagResponse createChildTag(String tagId, CreateChildTagRequest request, String userId) {
tagService.validateTagExist(request.name(), userId);
tagId = Objects.requireNonNullElse(tagId, userId);
Expand Down Expand Up @@ -295,41 +294,19 @@ public SearchMemosUsingDbResponse searchMemosUsingDb(String searchHistoryId, Str

@ProcessingMemoCount(action = ActionType.JUST_PUBLISH)
public UpdateMemoResponse updateMemo(String memoId, UpdateMemoRequest request, String userId) {
String updatedContent = request.content();
List<String> updatedImageUrls = request.imageUrls();
List<String> updatedVoiceUrls = request.voiceUrls();
String content = request.content();
List<String> imageUrls = request.imageUrls();
List<String> voiceUrls = request.voiceUrls();

Memo memo = memoService.getMemo(memoId, userId);

AiCreateEmbeddingResponse aiCreateEmbeddingResponse = null;

boolean isContentChanged = !updatedContent.equals(memo.getContent());
if (isContentChanged) {
aiCreateEmbeddingResponse = aiMemoTagClient.createEmbedding(updatedContent);
}

AiCreateMetadataResponse aiCreateMetadataResponse = aiMemoTagClient.createMetadata(
updatedContent,
updatedImageUrls,
updatedVoiceUrls
);

List<Double> embedding = Objects.nonNull(aiCreateEmbeddingResponse)
? aiCreateEmbeddingResponse.embedding() : memo.getEmbedding();
String metadata = Objects.nonNull(aiCreateMetadataResponse)
? aiCreateMetadataResponse.metadata() : memo.getMetadata();
List<Double> embeddingMetadata = Objects.nonNull(aiCreateMetadataResponse)
? aiCreateMetadataResponse.embeddingMetadata() : memo.getEmbeddingMetadata();

processDeletedFiles(memo, updatedImageUrls, updatedVoiceUrls, userId);
processDeletedFiles(memo, imageUrls, voiceUrls, userId);
asyncMemoTagService.updateEmbeddingAndMetadata(memo, content, imageUrls, voiceUrls, userId);

memo.update(
updatedContent,
updatedImageUrls,
updatedVoiceUrls,
metadata,
embedding,
embeddingMetadata
content,
imageUrls,
voiceUrls
);
Memo updatedMemo = memoService.updateMemo(memo);
MemoResponse memoResponse = getMemoResponses(List.of(updatedMemo.getId()), userId).get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,23 @@ public void update(
}

public void update(
String content,
List<String> imageUrls,
List<String> voiceUrls,
String metadata,
String updatedContent,
List<String> updatedImageUrls,
List<String> updatedVoiceUrls
) {
this.content = updatedContent;
this.imageUrls = updatedImageUrls;
this.voiceUrls = updatedVoiceUrls;
this.updatedAt = LocalDateTime.now();
}

public void update(
List<Double> embedding,
List<Double> embeddingMetadata
List<Double> embeddingMetadata,
String metadata
) {
this.content = content;
this.imageUrls = imageUrls;
this.voiceUrls = voiceUrls;
this.metadata = metadata;
this.embedding = embedding;
this.embeddingMetadata = embeddingMetadata;
this.updatedAt = LocalDateTime.now();
}
}

0 comments on commit 303aaf9

Please sign in to comment.