Skip to content

Commit

Permalink
refactor: PopulationStatistic의 DashboareType에 따라 DashboareComponent 생…
Browse files Browse the repository at this point in the history
…성하도록 수정(#114)
  • Loading branch information
eun-seong committed Mar 6, 2024
1 parent 2cd1145 commit be32c3e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent;
import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto;
import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard;
import com.dnd.namuiwiki.domain.dashboard.type.DashboardType;
import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto;
import com.dnd.namuiwiki.domain.question.type.QuestionName;
import com.dnd.namuiwiki.domain.statistic.StatisticsService;
import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
Expand Down Expand Up @@ -40,12 +38,12 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio
}
Dashboard dashboard = optionalDashboard.get();

List<PopulationStatistic> populationStatistics = statisticsService.getPopulationStatistics(period, relation);
List<DashboardComponent> populationDashboards = dashboard.getPopulationDashboards(populationStatistics);
List<DashboardComponent> userDashboards = dashboard.getUserDashboards();
PopulationStatistic populationStatistic = statisticsService.getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT);
DashboardComponent populationDashboard = dashboard.getPopulationDashboard(populationStatistic, DashboardType.MONEY);

List<DashboardComponent> components = new ArrayList<>(userDashboards);
components.add(populationDashboard);
components.addAll(populationDashboards);

return new DashboardDto(components);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ public List<DashboardComponent> getUserDashboards() {
.toList();
}

public DashboardComponent getPopulationDashboard(PopulationStatistic populationStatistic, DashboardType dashboardType) {
List<Statistic> statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType);

return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic);
public List<DashboardComponent> getPopulationDashboards(List<PopulationStatistic> populationStatistics) {
return populationStatistics.stream()
.map(populationStatistic -> {
DashboardType dashboardType = populationStatistic.getDashboardType();
List<Statistic> statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType);
return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic);
})
.toList();
}

public static Dashboard createNew(User owner, Period period, Relation relation, List<Answer> answers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.dnd.namuiwiki.domain.survey.type.Relation;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;
import java.util.Optional;

public interface StatisticsRepository extends MongoRepository<PopulationStatistic, String> {
Optional<PopulationStatistic> findByPeriodAndRelationAndQuestionName(Period period, Relation relation, QuestionName name);

List<PopulationStatistic> findAllByPeriodAndRelation(Period period, Relation relation);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.dnd.namuiwiki.domain.statistic;

import com.dnd.namuiwiki.common.exception.ApplicationErrorException;
import com.dnd.namuiwiki.common.exception.ApplicationErrorType;
import com.dnd.namuiwiki.domain.option.entity.Option;
import com.dnd.namuiwiki.domain.dashboard.type.DashboardType;
import com.dnd.namuiwiki.domain.question.type.QuestionName;
import com.dnd.namuiwiki.domain.statistic.model.AverageEntireStatistic;
import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic;
import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import com.dnd.namuiwiki.domain.survey.type.Period;
Expand All @@ -24,14 +23,34 @@ public void updateStatistics(Survey survey) {
Period period = survey.getPeriod();
Relation relation = survey.getRelation();

var statisticalAnswers = survey.getAnswers().stream()
.filter(answer -> answer.getQuestion().getDashboardType().getStatisticsCalculationType().isNotNone())
.toList();
survey.getAnswers().stream()
.filter(answer -> answer.getQuestion().getDashboardType().getAnalysisType().isPopulation())
.forEach(answer -> {
QuestionName questionName = answer.getQuestion().getName();
DashboardType dashboardType = answer.getQuestion().getDashboardType();
StatisticsCalculationType calculationType = dashboardType.getStatisticsCalculationType();
if (calculationType.isAverage()) {
updateAverageStatistic(questionName, period, relation, answer);
}
});
}

private void updateAverageStatistic(QuestionName questionName, Period period, Relation relation, Answer answer) {
long newValue = answer.getAnswer(Long.class);

updateBorrowingLimitStatistic(period, relation, statisticalAnswers);
updateAverageStatisticByCategory(questionName, null, null, newValue);
updateAverageStatisticByCategory(questionName, period, null, newValue);
updateAverageStatisticByCategory(questionName, null, relation, newValue);
}

public PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) {
private void updateAverageStatisticByCategory(QuestionName questionName, Period period, Relation relation, long newValue) {
PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, questionName);
populationStatistic.updateStatistic(String.valueOf(newValue));

statisticsRepository.save(populationStatistic);
}

private PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) {
return statisticsRepository
.findByPeriodAndRelationAndQuestionName(period, relation, questionName)
.orElseGet(() -> PopulationStatistic.builder()
Expand All @@ -42,34 +61,8 @@ public PopulationStatistic getPopulationStatistic(Period period, Relation relati
.build());
}

private void updateBorrowingLimitStatistic(Period period, Relation relation, List<Answer> answers) {
Answer borroingLimitAnswer = answers.stream()
.filter(answer -> answer.getQuestion().getName() == QuestionName.BORROWING_LIMIT)
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_DATA_ARGUMENT));

long borrowingLimit;
if (borroingLimitAnswer.getType().isOption()) {
Option option = borroingLimitAnswer.getQuestion().getOptions().get(borroingLimitAnswer.getAnswer().toString());
borrowingLimit = (long) option.getValue();
} else {
borrowingLimit = (long) borroingLimitAnswer.getAnswer();
}

updateBorrowingLimitStatisticByCategory(null, null, borrowingLimit);
updateBorrowingLimitStatisticByCategory(period, null, borrowingLimit);
updateBorrowingLimitStatisticByCategory(null, relation, borrowingLimit);
}

private void updateBorrowingLimitStatisticByCategory(Period period, Relation relation, long borrowingLimit) {
PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT);

AverageEntireStatistic statistic = (AverageEntireStatistic) populationStatistic.getStatistic();
statistic.updateStatistic(String.valueOf(borrowingLimit));

populationStatistic.setStatistic(statistic);

statisticsRepository.save(populationStatistic);
public List<PopulationStatistic> getPopulationStatistics(Period period, Relation relation) {
return statisticsRepository.findAllByPeriodAndRelation(period, relation);
}

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

import com.dnd.namuiwiki.domain.dashboard.type.DashboardType;
import com.dnd.namuiwiki.domain.question.type.QuestionName;
import com.dnd.namuiwiki.domain.statistic.model.EntireStatistic;
import com.dnd.namuiwiki.domain.survey.type.Period;
Expand All @@ -25,6 +26,12 @@ public class PopulationStatistic {

private QuestionName questionName;

private DashboardType dashboardType;

private EntireStatistic statistic;

public void updateStatistic(String... args) {
statistic.updateStatistic(args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ public boolean isNotNone() {
return this != NONE;
}

public boolean isRatio() {
return this == RATIO;
}

public boolean isAverage() {
return this == AVERAGE;
}

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

import com.dnd.namuiwiki.common.exception.ApplicationErrorException;
import com.dnd.namuiwiki.common.exception.ApplicationErrorType;
import com.dnd.namuiwiki.domain.option.entity.Option;
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.question.type.QuestionType;
import com.dnd.namuiwiki.domain.survey.type.AnswerType;
Expand Down Expand Up @@ -42,6 +43,35 @@ public Answer(Question question, AnswerType type, Object answer, String reason)
this.reason = reason;
}

public <T> T getAnswer(Class<T> returnType) {
Object value;

if (type.isOption()) {
Option option = question.getOptions().get(answer.toString());
value = option.getValue();
} else {
value = answer;
}

if (value == null) {
throw new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR);
}

if (returnType == Long.class) {
if (isNumericAnswerNeeded()) {
return (T) value;
} else {
throw new ApplicationErrorException(ApplicationErrorType.NOT_INTEGER_ANSWER);
}
}

return returnType.cast(value);
}

private boolean isNumericAnswerNeeded() {
return question.getType().isNumericType() && type.isManual();
}

private void validateBorrowingLimit(long longAnswer) {
if (!(0 <= longAnswer && longAnswer <= 1_000_000_000)) {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_BORROWING_LIMIT);
Expand Down

0 comments on commit be32c3e

Please sign in to comment.