Skip to content

Commit

Permalink
refactor: EntireStatistic 구현체 생성 메소드 추가(#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-seong committed Mar 6, 2024
1 parent be32c3e commit 142b4b7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<PopulationStatistic> getPopulationStatistics(Period period, Relation relation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Getter
@AllArgsConstructor
public class AverageEntireStatistic implements EntireStatistic {
public class AverageEntireStatistic extends EntireStatistic {

private long entireAverage;
private long peopleCount;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}

}

0 comments on commit 142b4b7

Please sign in to comment.