Skip to content

Commit

Permalink
Merge pull request #201 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
eun-seong authored Sep 10, 2024
2 parents d0ac00c + bad814f commit e0fb724
Show file tree
Hide file tree
Showing 40 changed files with 351 additions and 536 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ ext {
set('jwtVersion', '0.12.5')
set('springdocOpenapiUIVersion', '2.3.0')
set('mockitoVersion', '5.10.0')
}

ext {
set('jwtVersion', '0.12.5')
set('springdocOpenapiUIVersion', '2.3.0')
set('springRetryVersion', '2.0.7')
}

group = 'com.dnd'
Expand All @@ -33,6 +31,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

/* spring retry */
implementation("org.springframework.retry:spring-retry:${springRetryVersion}")
implementation("org.springframework:spring-aspects")

/* micrometer */
implementation 'io.micrometer:micrometer-registry-prometheus'

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/dnd/namuiwiki/common/util/ListUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dnd.namuiwiki.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ListUtils {
public static <T> List<T> convertList(Object obj) {
if (obj.getClass().isArray()) {
return Arrays.asList((T[]) obj);
} else if (obj instanceof Collection) {
return new ArrayList<>((Collection<T>) obj);
} else {
throw new ClassCastException("Object is not a List.");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dnd.namuiwiki.config;

import com.dnd.namuiwiki.domain.dashboard.DashboardService;
import com.dnd.namuiwiki.domain.statistic.StatisticsService;
import com.dnd.namuiwiki.domain.survey.SurveyEventHandler;
import lombok.RequiredArgsConstructor;
Expand All @@ -10,10 +11,11 @@
@RequiredArgsConstructor
public class EventConfiguration {
private final StatisticsService statisticsService;
private final DashboardService dashboardService;

@Bean
public SurveyEventHandler surveyEventHandler() {
return new SurveyEventHandler(statisticsService);
return new SurveyEventHandler(statisticsService, dashboardService);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dnd.namuiwiki.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

@Configuration
@EnableRetry(proxyTargetClass=true)
public class RetryConfiguration {
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.dnd.namuiwiki.domain.dashboard;


import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard;
import com.dnd.namuiwiki.domain.statistic.model.Statistics;
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 com.dnd.namuiwiki.domain.wiki.WikiType;
import com.dnd.namuiwiki.external.DiscordClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class DashboardInternalProxyService {
private final DashboardRepository dashboardRepository;
private final DiscordClient discordClient;

@Retryable(
retryFor = {OptimisticLockingFailureException.class},
maxAttempts = 3,
backoff = @Backoff(delay = 30 * 1000L)
)
public void updateDashboard(User owner, WikiType wikiType, Period period, Relation relation, List<Answer> answers) {
insertDashboardIfNotExist(owner, wikiType, period, relation, answers);
Dashboard dashboard = dashboardRepository.findByUserAndWikiTypeAndPeriodAndRelation(owner, wikiType, period, relation)
.orElseGet(() -> {
Dashboard d = Dashboard.builder()
.user(owner)
.wikiType(wikiType)
.period(period)
.relation(relation)
.statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList()))
.build();
return dashboardRepository.save(d);
});

dashboard.updateStatistics(answers);
dashboardRepository.save(dashboard);
}

@Recover
private void recoverForUpdateDashboard(Exception e) {
log.error("Failed to update dashboard", e);
discordClient.sendMessage(e.toString());
}

private void insertDashboardIfNotExist(User owner, WikiType wikiType, Period period, Relation relation, List<Answer> answers) {
if (!dashboardRepository.existsByUserAndWikiTypeAndPeriodAndRelation(owner, wikiType, period, relation)) {
dashboardRepository.save(Dashboard.builder()
.user(owner)
.wikiType(wikiType)
.period(period)
.relation(relation)
.statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList()))
.build());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import java.util.Optional;

public interface DashboardRepository extends MongoRepository<Dashboard, String>, DashboardCustomRepository {
public interface DashboardRepository extends MongoRepository<Dashboard, String> {
Optional<Dashboard> findByUserAndWikiTypeAndPeriodAndRelation(User user, WikiType wikiType, Period period, Relation relation);

boolean existsByUserAndWikiTypeAndPeriodAndRelation(User user, WikiType wikiType, Period period, Relation relation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic;
import com.dnd.namuiwiki.domain.statistic.model.Statistics;
import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import com.dnd.namuiwiki.domain.survey.type.Period;
import com.dnd.namuiwiki.domain.survey.type.Relation;
import com.dnd.namuiwiki.domain.user.UserRepository;
Expand All @@ -26,14 +27,17 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class DashboardService {
private final UserRepository userRepository;
private final DashboardRepository dashboardRepository;
private final DashboardInternalProxyService dashboardInternalProxyService;
private final StatisticsService statisticsService;
private final QuestionRepository questionRepository;

Expand All @@ -45,6 +49,21 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, WikiType wik
return dashboard.map(value -> convertToDto(value.getStatistics(), period, relation)).orElse(null);
}

public void updateDashboards(Survey survey) {
User owner = survey.getOwner();
WikiType wikiType = survey.getWikiType();
Period period = survey.getPeriod();
Relation relation = survey.getRelation();

var answers = survey.getAnswers().stream()
.filter(answer -> answer.getQuestion().getDashboardType().getStatisticsType().isNotNone())
.toList();

dashboardInternalProxyService.updateDashboard(owner, wikiType, null, null, answers);
dashboardInternalProxyService.updateDashboard(owner, wikiType, period, null, answers);
dashboardInternalProxyService.updateDashboard(owner, wikiType, null, relation, answers);
}

private DashboardDto convertToDto(Statistics statistics, Period period, Relation relation) {
List<Question> questions = questionRepository.findAll();

Expand All @@ -67,7 +86,7 @@ private DashboardDto convertToDto(Statistics statistics, Period period, Relation
} else {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_DASHBOARD_TYPE);
}
}).toList();
}).sorted(Comparator.comparingLong(DashboardComponentV2::getDashboardOrder)).collect(Collectors.toList());

return new DashboardDto(dashboardComponents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AverageDashboardComponent extends DashboardComponentV2 {
private final long entireAverage;

public AverageDashboardComponent(DashboardType dashboardType, Statistic statistic, long entireAverage, Question question) {
super(dashboardType, question.getId(), question.getTitle(), question.getName());
super(dashboardType, question.getId(), question.getTitle(), question.getName(), question.getDashboardOrder());

if (!dashboardType.isAverageType()) {
throw new IllegalArgumentException("Required AverageDashboardType");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BinaryDashboardComponent extends DashboardComponentV2 {
private final int percentage;

public BinaryDashboardComponent(Statistic statistic, Question question) {
super(DashboardType.BINARY, question.getId(), question.getTitle(), question.getName());
super(DashboardType.BINARY, question.getId(), question.getTitle(), question.getName(), question.getDashboardOrder());

if (!dashboardType.isBinaryType()) {
throw new IllegalArgumentException("Required BinaryDashboardType");
Expand Down
Loading

0 comments on commit e0fb724

Please sign in to comment.