diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java index 1af895b..19be666 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -1,10 +1,7 @@ package com.dnd.namuiwiki.domain.statistic; -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.question.entity.Question; 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; @@ -26,39 +23,32 @@ public void updateStatistics(Survey survey) { 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(); + Question question = answer.getQuestion(); + var calculationType = question.getDashboardType().getStatisticsCalculationType(); if (calculationType.isAverage()) { - updateAverageStatistic(questionName, period, relation, answer); + updateAverageStatistic(question, period, relation, answer); } }); } - private void updateAverageStatistic(QuestionName questionName, Period period, Relation relation, Answer answer) { + private void updateAverageStatistic(Question question, Period period, Relation relation, Answer answer) { long newValue = answer.getAnswer(Long.class); - updateAverageStatisticByCategory(questionName, null, null, newValue); - updateAverageStatisticByCategory(questionName, period, null, newValue); - updateAverageStatisticByCategory(questionName, null, relation, newValue); + updateAverageStatisticByCategory(question, null, null, newValue); + updateAverageStatisticByCategory(question, period, null, newValue); + updateAverageStatisticByCategory(question, null, relation, newValue); } - private void updateAverageStatisticByCategory(QuestionName questionName, Period period, Relation relation, long newValue) { - PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, questionName); + private void updateAverageStatisticByCategory(Question question, Period period, Relation relation, long newValue) { + PopulationStatistic populationStatistic = getPopulationStatistic(question, period, relation); 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() - .statistic(new AverageEntireStatistic(0L, 0L)) - .period(period) - .questionName(questionName) - .relation(relation) - .build()); + private PopulationStatistic getPopulationStatistic(Question question, Period period, Relation relation) { + return statisticsRepository.findByPeriodAndRelationAndQuestionName(period, relation, question.getName()) + .orElseGet(() -> PopulationStatistic.from(period, relation, question.getName(), question.getDashboardType())); } public List getPopulationStatistics(Period period, Relation relation) { diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java index 3e1d6cd..bfb2dbb 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java @@ -6,7 +6,7 @@ @Getter @AllArgsConstructor -public class AverageEntireStatistic implements EntireStatistic { +public class AverageEntireStatistic extends EntireStatistic { private long entireAverage; private long peopleCount; diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java index 6fbd2a5..6123816 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/EntireStatistic.java @@ -1,5 +1,17 @@ package com.dnd.namuiwiki.domain.statistic.model; -public interface EntireStatistic { - void updateStatistic(String... arguments); +import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; + +public abstract class EntireStatistic { + public abstract void updateStatistic(String... arguments); + + public static EntireStatistic createEmpty(StatisticsCalculationType type) { + switch (type) { + case AVERAGE: + return new AverageEntireStatistic(0L, 0L); + default: + throw new IllegalArgumentException("Not supported type: " + type); + } + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java index 4144c42..fd908b8 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/entity/PopulationStatistic.java @@ -7,12 +7,10 @@ import com.dnd.namuiwiki.domain.survey.type.Relation; import lombok.Builder; import lombok.Getter; -import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Getter -@Setter @Builder @Document("statistics") public class PopulationStatistic { @@ -34,4 +32,14 @@ public void updateStatistic(String... args) { statistic.updateStatistic(args); } + public static PopulationStatistic from(Period period, Relation relation, QuestionName questionName, DashboardType dashboardType) { + return PopulationStatistic.builder() + .statistic(EntireStatistic.createEmpty(dashboardType.getStatisticsCalculationType())) + .dashboardType(dashboardType) + .period(period) + .questionName(questionName) + .relation(relation) + .build(); + } + }