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/subscribe #32

Merged
merged 3 commits into from
Nov 23, 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
@@ -0,0 +1,26 @@
package gwangjang.server.domain.community.adapter.in.message;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@Configuration
@RequiredArgsConstructor
public class MemberChannels {

@Bean
public Supplier<Message<String>> sendToMember() { // 메시지를 주기적으로 생성하고 보내는 데 사용
return () -> {
// 메시지 생성 로직
String jsonMessage = "{\"key\":\"value\"}";

// dto to string
return MessageBuilder.withPayload(jsonMessage).build();
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gwangjang.server.domain.community.adapter.in.message.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import gwangjang.server.domain.community.application.dto.res.MemberDto;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Deserializer;

import java.util.Map;

@RequiredArgsConstructor
public class CommunityCustomDeserializer implements Deserializer<MemberDto> {

private final ObjectMapper objectMapper;

@Override
public void configure(Map<String, ?> configs, boolean isKey) {
Deserializer.super.configure(configs, isKey);
}

@Override
public MemberDto deserialize(String topic, byte[] data) {
if (data == null) {
return null;
}

try {
return objectMapper.readValue(data, MemberDto.class);
} catch (Exception e) {
throw new RuntimeException("Deserialization error: " + e.getMessage(), e);
}
}

@Override
public MemberDto deserialize(String topic, Headers headers, byte[] data) {
return Deserializer.super.deserialize(topic, headers, data);
}

@Override
public void close() {
Deserializer.super.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//package gwangjang.server.domain.adapter.out.client;
//
//import org.springframework.cloud.openfeign.FeignClient;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//
//import java.util.concurrent.TimeoutException;
//
//@FeignClient("memberService")
//public interface FindMemberFeignClient {
//
//// @CircuitBreaker(name="memberService")
//// @RequestMapping(method= RequestMethod.GET,value="/writer/{writerId}",consumes = "application/json")
//// Long findWriterById(@PathVariable("memberId")Long writerId) throws TimeoutException;
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//package gwangjang.server.domain.adapter.out.client;
//
//import gwangjang.server.domain.domain.repository.CommunityRepository;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.kafka.annotation.KafkaListener;
//import org.springframework.stereotype.Service;
//
//@Service
//@Slf4j
//public class KafkaConsumer {
//
// CommunityRepository communityRepository;
//
// @Autowired
// public KafkaConsumer(CommunityRepository communityRepository) {
// this.communityRepository = communityRepository;
// }
//
// @KafkaListener(topics = "gugbab-services-kafka-users-star-cnt-topic", groupId = "serviesConsumerGroupId")
// public void updateUserStarCnt(String kafkaMessage) {
//
// log.info("kafka Message : " + kafkaMessage);
//
//
// }
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//package gwangjang.server.domain.adapter.out.client;
//
//
//import org.apache.kafka.clients.consumer.ConsumerConfig;
//import org.apache.kafka.common.serialization.StringDeserializer;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//import org.springframework.kafka.annotation.EnableKafka;
//import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
//import org.springframework.kafka.core.ConsumerFactory;
//import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
//
//import java.util.HashMap;
//import java.util.Map;
//
//@EnableKafka
//@Configuration
//public class KafkaConsumerConfig {
//
// Environment env;
//
// @Autowired
// public KafkaConsumerConfig(Environment env) {
// this.env = env;
// }
//
//
// @Bean
// // Topic에 접속에 필요한 정보
// public ConsumerFactory<String, String> consumerFactory() {
// Map<String, Object> properties = new HashMap<>();
// properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, env.getProperty("gugbab.public.url.local.kafka_server"));
// properties.put(ConsumerConfig.GROUP_ID_CONFIG, "serviesConsumerGroupId");
// properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
// properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
// return new DefaultKafkaConsumerFactory<>(properties);
// }
//
// @Bean
// // Topic Listener
// public ConcurrentKafkaListenerContainerFactory<String, String> KafkaListenerContatinerFactory() {
// ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory = new ConcurrentKafkaListenerContainerFactory<>();
// kafkaListenerContainerFactory.setConsumerFactory(consumerFactory());
//
// return kafkaListenerContainerFactory;
// }
//
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gwangjang.server.domain.community.adapter.out.message;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.MessageChannel;

import java.util.function.Consumer;

@Configuration
@RequiredArgsConstructor
public class CommunityChannels {

// @Bean
// public Function<String, String> uppercase() { // 메시지를 받아 처리한 후 결과를 반환
// return message -> {
// // 메시지 처리 로직
// return message.toUpperCase();
// };
//}
@Bean
public Consumer<String> receiveFromMember() { //메시지를 받아 처리하는 데 사용되지만 반환 값이 없
return message -> {
// 메시지 처리 로직 ( 역직렬화 )
System.out.println("Received: " + message);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gwangjang.server.domain.community.adapter.out.source;

public class KafkaSourceBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gwangjang.server.domain.Issue.application.dto.res;

import gwangjang.server.domain.Issue.application.dto.res.TrendRes;
import lombok.*;

import java.util.List;

@Getter
@Builder
@Setter
@NoArgsConstructor
public class NaverTrendDto {
private List<TrendRes.Trend> data;
private String date;

public NaverTrendDto(List<TrendRes.Trend> data, String date) {
this.data = data;
this.date = date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public class KeywordSubscribeUseCase {

public List<MainBubbleRes> getBubbleData() {
List<MainBubbleRes> mainBubbleRes = new ArrayList<>();
List<MainBubbleData> mainBubbleData1 = new ArrayList<>();
mainBubbleData1.add(new MainBubbleData("후쿠시마 오염수", 356L));
mainBubbleData1.add(new MainBubbleData("일회용품 사용 규제 시행", 123L));
mainBubbleRes.add(new MainBubbleRes("환경", mainBubbleData1));

List<MainBubbleData> mainBubbleData2 = new ArrayList<>();
mainBubbleData2.add(new MainBubbleData("SPC 불매 운동", 335L));
Expand All @@ -32,6 +28,15 @@ public List<MainBubbleRes> getBubbleData() {
mainBubbleData3.add(new MainBubbleData("국민연금 개혁", 323L));
mainBubbleRes.add(new MainBubbleRes("주거/사회안전망", mainBubbleData3));



List<MainBubbleData> mainBubbleData1 = new ArrayList<>();
mainBubbleData1.add(new MainBubbleData("후쿠시마 오염", 356L));
mainBubbleData1.add(new MainBubbleData("일회용품 사용 규제 시행", 123L));
mainBubbleRes.add(new MainBubbleRes("환경", mainBubbleData1));



List<MainBubbleData> mainBubbleData4 = new ArrayList<>();
mainBubbleData4.add(new MainBubbleData("의대 정원 확대", 156L));
mainBubbleData4.add(new MainBubbleData("서이초 교사 사건", 400L));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gwangjang.server.domain.Issue.application.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import gwangjang.server.domain.Issue.application.dto.res.NaverTrendDto;
import gwangjang.server.domain.Issue.application.dto.res.TrendRes;
import gwangjang.server.domain.Issue.application.dto.res.TrendRes.DataPoint;
import jakarta.transaction.Transactional;
Expand All @@ -17,28 +18,48 @@ public class NaverTrendByIssueUseCase {

private final NaverTrendUtil naverTrendUtil;

public List<TrendRes.Trend> getNaverTrend(String issue) {
public NaverTrendDto getNaverTrend(String issue) {

if(issue.length() > 13){
issue = issue.substring(0, 7);
System.out.println(issue);
}

String replace = issue.replace(" ", "");
List<TrendRes.Trend> trendList = new ArrayList<>();

TrendRes trendRes = naverTrendUtil.main(replace);

trendRes.getResults().get(0).getData().stream().forEach(

dataPoint ->{
dataPoint.updatePeriod();
trendList.add(new TrendRes.Trend(dataPoint.getPeriod(), dataPoint.getRatio()));
}
);
String [] month = new String[] {"5월 1주차", "5월 2주차", "5월 3주차", "5월 4주차", "5월 5주차", "6월 1주차",
"6월 2주차", "6월 3주차", "6월 4주차", "7월 1주차", "7월 2주차", "7월 3주차", "7월 4주차","7월 5주차",
"8월 1주차", "8월 2주차", "8월 3주차", "8월 4주차", "8월 5주차", "9월 1주차", "9월 2주차", "9월 3주차",
"9월 4주차","10월 1주차","10월 2주차","10월 3주차","10월 4주차","10월 5주차","11월 1주차","11월 2주차","11월 3주차" } ;

List<TrendRes.Trend> trendList = new ArrayList<>();

List<DataPoint> data = trendRes.getResults().get(0).getData();

data.stream().forEach(
dataPoint -> dataPoint.updatePeriod()
);

int max = 0;
String maxDate = null;
int j = 0;
for (int i = 0; i < month.length; i++) {
if (j < data.size() && data.get(j).getPeriod().equals(month[i])) {
if (max < data.get(j).getRatio()) {
max = data.get(j).getRatio();
maxDate = data.get(j).getPeriod();
}
trendList.add(new TrendRes.Trend(data.get(j).getPeriod(), data.get(j).getRatio()));
j++;
} else {
trendList.add(new TrendRes.Trend(month[i], 0));
}
}

return trendList;
return new NaverTrendDto(trendList,maxDate);


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import gwangjang.server.domain.Issue.application.dto.res.MainBubbleRes;
import gwangjang.server.domain.Issue.application.dto.res.NaverTrendDto;
import gwangjang.server.domain.Issue.application.dto.res.TrendIssueGraphRes;
import gwangjang.server.domain.Issue.application.dto.res.TrendRes;
import gwangjang.server.domain.Issue.application.service.KeywordSubscribeUseCase;
Expand Down Expand Up @@ -34,7 +35,7 @@ public ResponseEntity<SuccessResponse<List<MainBubbleRes>>> getIssueAndKeywordBy

@GetMapping("/trend/{issue}")
// public ResponseEntity<SuccessResponse<List<TrendIssueGraphRes>>> getTrendDataByIssue() {
public ResponseEntity<SuccessResponse<List<TrendRes.Trend>>> getTrendDataByIssue(@PathVariable("issue") String issue) {
public ResponseEntity<SuccessResponse<NaverTrendDto>> getTrendDataByIssue(@PathVariable("issue") String issue) {
return ResponseEntity.ok(SuccessResponse.create(GET_MAIN_BUBBLE_CHART.getMessage(), this.naverTrendByIssueUseCase.getNaverTrend(issue)));
}

Expand Down
Loading