Skip to content

Commit

Permalink
feat: 버블차트 API
Browse files Browse the repository at this point in the history
  • Loading branch information
eojinny committed Nov 22, 2023
1 parent 40c4c0e commit 1a4001e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gwangjang.server.domain.contents.application.dto.res;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@Builder
public class BubbleFrontRes {
private Long x;
private Long y;
private Long z;
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
@Transactional
Expand All @@ -28,6 +31,14 @@ public List<ContentsDataRes> getContentsByIssue(String issue) {
}

public List<BubbleChartRes> getBubbleChart(String issueTitle) {
return contentsQueryService.getBubbleChart(issueTitle);
List<BubbleChartRes> list = contentsQueryService.getBubbleChart(issueTitle);
// List<BubbleChartRes> selectedList = new ArrayList<>();
// for (int i = 39; i < list.size(); i += 40) {
// selectedList.add(list.get(i));
// }
for(int i=0; i<list.size(); i++){
list.get(i).setRank((40L*(i+1)));
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -24,33 +25,34 @@ public class ContentsQueryService {
public List<ContentsDataRes> getContentsByIssue(String issue) {
return contentsRepository.getContentsByIssueId(issue);
}
public List<BubbleChartRes> getBubbleChart(String issue) {

public List<BubbleChartRes> getBubbleChart(String issueTitle) {
List<TotalReq> keywordList = findKeywordFeignClient.getAll().getBody().getData();
Map<String, Long> keywordIdMap = keywordList.stream()
.collect(Collectors.toMap(keyword -> keyword.getIssueTitle() + " " + keyword.getKeyword(), TotalReq::getKeywordId));

return contentsRepository.findMaxOccurrencesByIssueAndKeyword().stream()
List<BubbleChartRes> bubbleChartList = contentsRepository.findMaxOccurrencesByIssueAndKeyword().stream()
.filter(objects -> {
String issueTitle = (String) objects[0];
return issueTitle.contains(issue);
String fetchedIssueTitle = (String) objects[0];
return fetchedIssueTitle.contains(issueTitle);
})
.map(objects -> {
String issueTitle = (String) objects[0];
String fetchedIssueTitle = (String) objects[0];
String keyword = (String) objects[1];
String date = (String) objects[2];
String date = ((String) objects[2]).substring(5, 7); // Keep only year and month part
Long count = (Long) objects[3];

// Construct the key to look up keywordId in the map
String key = issueTitle + keyword;
String key = fetchedIssueTitle + keyword;
Long keywordId = keywordIdMap.getOrDefault(key, null);

return new BubbleChartRes(issueTitle, keyword, date, keywordId != null ? keywordId : count);
return new BubbleChartRes(fetchedIssueTitle, keyword, date, keywordId != null ? keywordId : count);
})
.collect(Collectors.toList());
}




// Sort the list based on rank (assuming 'rank' is a field in BubbleChartRes)
bubbleChartList.sort(Comparator.comparing(BubbleChartRes::getRank));

return bubbleChartList;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gwangjang.server.domain.contents.presentation;

import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes;
import gwangjang.server.domain.contents.application.dto.res.BubbleFrontRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsRes;
import gwangjang.server.domain.contents.application.service.ContentsSubscribeUseCase;
Expand All @@ -19,7 +20,9 @@

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static gwangjang.server.domain.contents.presentation.constant.ContentsResponse.GET_MY_CONTENTS;
import static gwangjang.server.domain.contents.presentation.constant.ContentsResponseMessage.GET_CONTENTS_SUCCESS;
Expand Down Expand Up @@ -76,8 +79,21 @@ public ResponseEntity<SuccessResponse<List<ContentsDataRes>>> getMySubscribe(@Re
return ResponseEntity.ok(SuccessResponse.create(GET_MY_CONTENTS.getMessage(), this.contentsSubscribeUseCase.getContentsByIssue(issue)));
}
@GetMapping("/bubbleChart/{issue}")
public ResponseEntity<SuccessResponse<List<BubbleChartRes>>> getBubbleChart(@PathVariable("issue")String issue) {
return ResponseEntity.ok(SuccessResponse.create(GET_CONTENTS_SUCCESS.getMessage(), this.contentsSubscribeUseCase.getBubbleChart(issue)));
public ResponseEntity<SuccessResponse<List<BubbleFrontRes>>> getBubbleChart(@PathVariable("issue") String issue) {
List<BubbleChartRes> bubbleChartList = this.contentsSubscribeUseCase.getBubbleChart(issue);
List<BubbleFrontRes> result = new ArrayList<>();
Random rand = new Random();

for (BubbleChartRes bubbleChart : bubbleChartList) {
Long x = Long.parseLong(bubbleChart.getDate());
Long y = (long) (rand.nextInt(10) + 1); // Random value between 1 and 10
Long z = bubbleChart.getRank();
String name = bubbleChart.getKeyword();

result.add(new BubbleFrontRes(x, y, z, name));
}

return ResponseEntity.ok(SuccessResponse.create(GET_CONTENTS_SUCCESS.getMessage(), result));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
@RequiredArgsConstructor
public enum ContentsResponseMessage {

GET_CONTENTS_SUCCESS("컨텐츠 조회 완료");

GET_CONTENTS_SUCCESS("컨텐츠 조회 완료"),
GET_BUBBLE_SUCCESS("버블 조회 완료");
private final String message;

}

0 comments on commit 1a4001e

Please sign in to comment.