Skip to content

Commit

Permalink
refactor: getPopulationStatistic 메소드 제거(#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 4e2f25c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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.question.entity.Question;
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 @@ -26,39 +24,35 @@ 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);
populationStatistic.updateStatistic(String.valueOf(newValue));

statisticsRepository.save(populationStatistic);
}

private PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) {
return statisticsRepository
.findByPeriodAndRelationAndQuestionName(period, relation, questionName)
private void updateAverageStatisticByCategory(Question question, Period period, Relation relation, long newValue) {
PopulationStatistic populationStatistic = statisticsRepository.findByPeriodAndRelationAndQuestionName(period, relation, question.getName())
.orElseGet(() -> PopulationStatistic.builder()
.statistic(new AverageEntireStatistic(0L, 0L))
.dashboardType(question.getDashboardType())
.period(period)
.questionName(questionName)
.questionName(question.getName())
.relation(relation)
.build());
;
populationStatistic.updateStatistic(String.valueOf(newValue));

statisticsRepository.save(populationStatistic);
}

public List<PopulationStatistic> getPopulationStatistics(Period period, Relation relation) {
Expand Down
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 Down

0 comments on commit 4e2f25c

Please sign in to comment.