-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from dnd-side-project/dev
#147 ํ ์คํธ ๋ฐ ์ธ๋ฑ์ค ์์ฑ
- Loading branch information
Showing
23 changed files
with
430 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/dnd/namuiwiki/common/exception/AsyncExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.dnd.namuiwiki.common.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@Slf4j | ||
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler { | ||
|
||
@Override | ||
public void handleUncaughtException(Throwable ex, Method method, Object... params) { | ||
log.error("AsyncException message={}, declaringClass={}, methodName={}", ex.getMessage(), method.getDeclaringClass(), method.getName(), ex); | ||
|
||
for (Object param : params) { | ||
log.error("Parameter value - {}, declaringClass={}, methodName={}", param, method.getDeclaringClass(), method.getName()); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/dnd/namuiwiki/config/AsyncConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.dnd.namuiwiki.config; | ||
|
||
import com.dnd.namuiwiki.common.exception.AsyncExceptionHandler; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfiguration implements AsyncConfigurer { | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(2); | ||
executor.setMaxPoolSize(4); | ||
executor.setQueueCapacity(50); | ||
executor.setThreadNamePrefix("Async-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return asyncExceptionHandler(); | ||
} | ||
|
||
@Bean | ||
public AsyncExceptionHandler asyncExceptionHandler() { | ||
return new AsyncExceptionHandler(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/dnd/namuiwiki/config/CacheConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.dnd.namuiwiki.config; | ||
|
||
import org.springframework.cache.Cache; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.cache.concurrent.ConcurrentMapCache; | ||
import org.springframework.cache.support.SimpleCacheManager; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@EnableCaching | ||
@Configuration | ||
public class CacheConfiguration { | ||
|
||
@Bean | ||
CacheManager cacheManager() { | ||
SimpleCacheManager cacheManager = new SimpleCacheManager(); | ||
List<Cache> caches = new ArrayList<>(); | ||
caches.add(new ConcurrentMapCache("questions")); | ||
caches.add(new ConcurrentMapCache("question")); | ||
cacheManager.setCaches(caches); | ||
return cacheManager; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/dnd/namuiwiki/config/EventConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.dnd.namuiwiki.config; | ||
|
||
import com.dnd.namuiwiki.domain.statistic.StatisticsService; | ||
import com.dnd.namuiwiki.domain.survey.SurveyEventHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class EventConfiguration { | ||
private final StatisticsService statisticsService; | ||
|
||
@Bean | ||
public SurveyEventHandler surveyEventHandler() { | ||
return new SurveyEventHandler(statisticsService); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.dnd.namuiwiki.domain.dashboard; | ||
|
||
import com.dnd.namuiwiki.domain.survey.model.entity.Answer; | ||
import com.dnd.namuiwiki.domain.survey.type.Period; | ||
import com.dnd.namuiwiki.domain.survey.type.Relation; | ||
import com.dnd.namuiwiki.domain.user.entity.User; | ||
|
||
import java.util.List; | ||
|
||
public interface DashboardCustomRepository { | ||
void updateDashboard(User owner, Period period, Relation relation, List<Answer> answers); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardCustomRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.dnd.namuiwiki.domain.dashboard; | ||
|
||
import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard; | ||
import com.dnd.namuiwiki.domain.survey.model.entity.Answer; | ||
import com.dnd.namuiwiki.domain.survey.type.Period; | ||
import com.dnd.namuiwiki.domain.survey.type.Relation; | ||
import com.dnd.namuiwiki.domain.user.entity.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.mongodb.core.FindAndModifyOptions; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.data.mongodb.core.query.Update; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class DashboardCustomRepositoryImpl implements DashboardCustomRepository { | ||
private final MongoTemplate mongoTemplate; | ||
|
||
@Override | ||
public void updateDashboard(User owner, Period period, Relation relation, List<Answer> answers) { | ||
Query query = Query.query(Criteria.where("user").is(owner) | ||
.and("period").is(period) | ||
.and("relation").is(relation)); | ||
|
||
Update update = new Update(); | ||
|
||
FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true); | ||
|
||
for (Answer answer : answers) { | ||
update.inc(String.format("statistics.statistics.%s.totalCount", answer.getQuestion().getId())); | ||
|
||
if (answer.getType().isOption()) { | ||
update.inc(String.format("statistics.statistics.%s.legends.%s.count", | ||
answer.getQuestion().getId(), answer.getAnswer().toString())); | ||
} | ||
} | ||
|
||
mongoTemplate.findAndModify(query, update, options, Dashboard.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/dnd/namuiwiki/domain/question/QuestionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package com.dnd.namuiwiki.domain.question; | ||
|
||
import com.dnd.namuiwiki.domain.question.entity.Question; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface QuestionRepository extends MongoRepository<Question, String> { | ||
|
||
@NonNull | ||
@Cacheable("questions") | ||
List<Question> findAll(); | ||
|
||
@NonNull | ||
@Cacheable("question") | ||
Optional<Question> findById(@NonNull String id); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/dnd/namuiwiki/domain/survey/SurveyEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.dnd.namuiwiki.domain.survey; | ||
|
||
import com.dnd.namuiwiki.domain.statistic.StatisticsService; | ||
import com.dnd.namuiwiki.domain.survey.model.dto.SurveyCreatedEvent; | ||
import com.dnd.namuiwiki.domain.survey.model.entity.Survey; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class SurveyEventHandler { | ||
private final StatisticsService statisticsService; | ||
|
||
@Async | ||
@EventListener | ||
public void handleSurveySuccessEvent(SurveyCreatedEvent event) { | ||
Survey survey = event.getSurvey(); | ||
log.info("SurveyEventHandler.handleSurveySuccessEvent: surveyId={}", survey.getId()); | ||
|
||
statisticsService.updateStatistics(survey); | ||
} | ||
|
||
} |
Oops, something went wrong.