From 65d30de29dc9302b65eb4dc108d862b46c7578ce Mon Sep 17 00:00:00 2001 From: eun-seong Date: Tue, 5 Mar 2024 18:09:00 +0900 Subject: [PATCH 01/22] =?UTF-8?q?refactor:=20Statistics=EB=A5=BC=20dashboa?= =?UTF-8?q?rd=20=EB=8F=84=EB=A9=94=EC=9D=B8=EC=9C=BC=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dnd/namuiwiki/domain/dashboard/DashboardService.java | 2 +- .../{statistic => dashboard}/model/AverageStatistic.java | 2 +- .../domain/dashboard/model/BestWorthDashboardComponent.java | 2 -- .../domain/dashboard/model/CharacterDashboardComponent.java | 4 ---- .../namuiwiki/domain/dashboard/model/DashboardComponent.java | 1 - .../domain/dashboard/model/HappyDashboardComponent.java | 2 -- .../domain/{statistic => dashboard}/model/Legend.java | 2 +- .../domain/dashboard/model/MoneyDashboardComponent.java | 2 -- .../domain/{statistic => dashboard}/model/RatioStatistic.java | 2 +- .../domain/dashboard/model/SadDashboardComponent.java | 2 -- .../domain/{statistic => dashboard}/model/Statistic.java | 2 +- .../domain/{statistic => dashboard}/model/Statistics.java | 2 +- .../namuiwiki/domain/dashboard/model/entity/Dashboard.java | 2 +- .../com/dnd/namuiwiki/domain/statistic/StatisticsService.java | 2 +- 14 files changed, 8 insertions(+), 21 deletions(-) rename src/main/java/com/dnd/namuiwiki/domain/{statistic => dashboard}/model/AverageStatistic.java (98%) rename src/main/java/com/dnd/namuiwiki/domain/{statistic => dashboard}/model/Legend.java (84%) rename src/main/java/com/dnd/namuiwiki/domain/{statistic => dashboard}/model/RatioStatistic.java (97%) rename src/main/java/com/dnd/namuiwiki/domain/{statistic => dashboard}/model/Statistic.java (96%) rename src/main/java/com/dnd/namuiwiki/domain/{statistic => dashboard}/model/Statistics.java (97%) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index df3c34b2..25723680 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -16,7 +16,7 @@ import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.StatisticsService; import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; +import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.type.Period; import com.dnd.namuiwiki.domain.survey.type.Relation; diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java similarity index 98% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageStatistic.java rename to src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java index 394e6f7c..8d04345e 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java @@ -1,4 +1,4 @@ -package com.dnd.namuiwiki.domain.statistic.model; +package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java index bd42b144..71ee5e60 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java @@ -2,8 +2,6 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; import java.util.List; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java index ecfc8a5d..850aed8d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java @@ -4,10 +4,6 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorType; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.question.type.QuestionName; -import com.dnd.namuiwiki.domain.statistic.model.Legend; -import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java index 5f018d02..6a3abd71 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java @@ -1,7 +1,6 @@ package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java index 041587e3..cd4f3d47 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java @@ -2,8 +2,6 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; import java.util.List; diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java similarity index 84% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java rename to src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java index dd75f858..8bea0e02 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java @@ -1,4 +1,4 @@ -package com.dnd.namuiwiki.domain.statistic.model; +package com.dnd.namuiwiki.domain.dashboard.model; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java index 6da4ab6d..b35f978e 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java @@ -1,8 +1,6 @@ package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.statistic.model.AverageStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; @Getter diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java similarity index 97% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java rename to src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java index 7d47d342..678b9279 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java @@ -1,4 +1,4 @@ -package com.dnd.namuiwiki.domain.statistic.model; +package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java index b1a7b08d..ceb5a0f4 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java @@ -2,8 +2,6 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; import java.util.List; diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Statistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java similarity index 96% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/model/Statistic.java rename to src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java index e3968b1e..1db6c9a6 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Statistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java @@ -1,4 +1,4 @@ -package com.dnd.namuiwiki.domain.statistic.model; +package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java similarity index 97% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/model/Statistics.java rename to src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index 5c0e0576..e45975bd 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -1,4 +1,4 @@ -package com.dnd.namuiwiki.domain.statistic.model; +package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.question.entity.Question; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index 8215334b..64052625 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -1,7 +1,7 @@ package com.dnd.namuiwiki.domain.dashboard.model.entity; import com.dnd.namuiwiki.common.model.BaseTimeEntity; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; +import com.dnd.namuiwiki.domain.dashboard.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; 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 d953380a..784d320b 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -7,7 +7,7 @@ import com.dnd.namuiwiki.domain.option.entity.Option; import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; -import com.dnd.namuiwiki.domain.statistic.model.Statistics; +import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import com.dnd.namuiwiki.domain.survey.model.entity.Survey; From e678c00dcf22eb236d9d10399b8f48148f00e8fc Mon Sep 17 00:00:00 2001 From: eun-seong Date: Tue, 5 Mar 2024 18:31:22 +0900 Subject: [PATCH 02/22] =?UTF-8?q?refactor:=20=EC=9C=A0=EC=A0=80=ED=86=B5?= =?UTF-8?q?=EA=B3=84=EB=9F=89=EA=B3=BC=20=EC=A0=84=EC=B2=B4=ED=86=B5?= =?UTF-8?q?=EA=B3=84=EB=9F=89=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 35 +++++++++++++++++++ .../domain/statistic/StatisticsService.java | 29 --------------- .../domain/survey/SurveyService.java | 3 ++ .../domain/survey/SurveyServiceTest.java | 5 ++- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index 25723680..890fd86e 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -18,6 +18,7 @@ import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; import com.dnd.namuiwiki.domain.dashboard.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; @@ -83,4 +84,38 @@ private User findByWikiId(String wikiId) { .orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.NOT_FOUND_USER)); } + + public void updateStatistics(Survey survey) { + User owner = survey.getOwner(); + Period period = survey.getPeriod(); + Relation relation = survey.getRelation(); + + var statisticalAnswers = survey.getAnswers().stream() + .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsType().isNotNone()) + .toList(); + + updateDashboards(owner, period, relation, statisticalAnswers); + } + + private void updateDashboards(User owner, Period period, Relation relation, List statisticalAnswers) { + updateDashboardByCategory(owner, null, null, statisticalAnswers); + updateDashboardByCategory(owner, period, null, statisticalAnswers); + updateDashboardByCategory(owner, null, relation, statisticalAnswers); + } + + private void updateDashboardByCategory(User owner, Period period, Relation relation, List answers) { + Dashboard dashboard = dashboardRepository.findByUserAndPeriodAndRelation(owner, period, relation) + .orElseGet(() -> { + Dashboard newDashboard = Dashboard.builder() + .user(owner) + .period(period) + .relation(relation) + .statistics(Statistics.from(answers.stream().map(Survey.Answer::getQuestion).toList())) + .build(); + return dashboardRepository.save(newDashboard); + }); + dashboard.updateStatistics(answers); + dashboardRepository.save(dashboard); + } + } 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 784d320b..00f5bc31 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -2,18 +2,14 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; -import com.dnd.namuiwiki.domain.dashboard.DashboardRepository; -import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard; import com.dnd.namuiwiki.domain.option.entity.Option; import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; -import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; 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; import com.dnd.namuiwiki.domain.survey.type.Relation; -import com.dnd.namuiwiki.domain.user.entity.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -22,11 +18,9 @@ @Service @RequiredArgsConstructor public class StatisticsService { - private final DashboardRepository dashboardRepository; private final StatisticsRepository statisticsRepository; public void updateStatistics(Survey survey) { - User owner = survey.getOwner(); Period period = survey.getPeriod(); Relation relation = survey.getRelation(); @@ -34,9 +28,7 @@ public void updateStatistics(Survey survey) { .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsType().isNotNone()) .toList(); - updateDashboards(owner, period, relation, statisticalAnswers); updateBorrowingLimitStatistic(period, relation, statisticalAnswers); - } public PopulationStatistic getPopulationStatistic(Period period, Relation relation, QuestionName questionName) { @@ -50,27 +42,6 @@ public PopulationStatistic getPopulationStatistic(Period period, Relation relati .build()); } - private void updateDashboards(User owner, Period period, Relation relation, List statisticalAnswers) { - updateDashboardByCategory(owner, null, null, statisticalAnswers); - updateDashboardByCategory(owner, period, null, statisticalAnswers); - updateDashboardByCategory(owner, null, relation, statisticalAnswers); - } - - private void updateDashboardByCategory(User owner, Period period, Relation relation, List answers) { - Dashboard dashboard = dashboardRepository.findByUserAndPeriodAndRelation(owner, period, relation) - .orElseGet(() -> { - Dashboard newDashboard = Dashboard.builder() - .user(owner) - .period(period) - .relation(relation) - .statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList())) - .build(); - return dashboardRepository.save(newDashboard); - }); - dashboard.updateStatistics(answers); - dashboardRepository.save(dashboard); - } - private void updateBorrowingLimitStatistic(Period period, Relation relation, List answers) { Answer borroingLimitAnswer = answers.stream() .filter(answer -> answer.getQuestion().getName() == QuestionName.BORROWING_LIMIT) diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java index 10feb6b7..4b8d8260 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java @@ -3,6 +3,7 @@ import com.dnd.namuiwiki.common.dto.PageableDto; import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; +import com.dnd.namuiwiki.domain.dashboard.DashboardService; import com.dnd.namuiwiki.domain.jwt.JwtProvider; import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto; import com.dnd.namuiwiki.domain.option.OptionRepository; @@ -43,6 +44,7 @@ public class SurveyService { private final OptionRepository optionRepository; private final JwtProvider jwtProvider; private final StatisticsService statisticsService; + private final DashboardService dashboardService; public CreateSurveyResponse createSurvey(CreateSurveyRequest request, String accessToken) { User owner = getUserByWikiId(request.getOwner()); @@ -59,6 +61,7 @@ public CreateSurveyResponse createSurvey(CreateSurveyRequest request, String acc .answers(surveyAnswer) .build()); + dashboardService.updateStatistics(survey); statisticsService.updateStatistics(survey); return new CreateSurveyResponse(survey.getId()); diff --git a/src/test/java/com/dnd/namuiwiki/domain/survey/SurveyServiceTest.java b/src/test/java/com/dnd/namuiwiki/domain/survey/SurveyServiceTest.java index a7514328..37d65817 100644 --- a/src/test/java/com/dnd/namuiwiki/domain/survey/SurveyServiceTest.java +++ b/src/test/java/com/dnd/namuiwiki/domain/survey/SurveyServiceTest.java @@ -1,6 +1,7 @@ package com.dnd.namuiwiki.domain.survey; import com.dnd.namuiwiki.common.exception.ApplicationErrorException; +import com.dnd.namuiwiki.domain.dashboard.DashboardService; import com.dnd.namuiwiki.domain.jwt.JwtProvider; import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto; import com.dnd.namuiwiki.domain.option.OptionRepository; @@ -47,12 +48,14 @@ class SurveyServiceTest { private JwtProvider jwtProvider; @Mock private StatisticsService statisticsService; + @Mock + private DashboardService dashboardService; private SurveyService surveyService; @BeforeEach void beforeEach() { - surveyService = new SurveyService(userRepository, surveyRepository, questionRepository, optionRepository, jwtProvider, statisticsService); + surveyService = new SurveyService(userRepository, surveyRepository, questionRepository, optionRepository, jwtProvider, statisticsService, dashboardService); } @Test From 2a0263836980626527d58957598aa9cf92fb5316 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Tue, 5 Mar 2024 20:03:03 +0900 Subject: [PATCH 03/22] =?UTF-8?q?refactor:=20Statistic=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=EC=B2=B4=20=EC=83=9D=EC=84=B1=EC=9E=90=20protected?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java | 2 +- .../dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java index 8d04345e..c2f4b6c8 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatistic.java @@ -48,7 +48,7 @@ public void updateAverage(Long newNumber) { average = ArithmeticUtils.calculateAverage(totalCount, average, newNumber); } - public static AverageStatistic create(Question question) { + protected static AverageStatistic create(Question question) { return new AverageStatistic( question.getId(), question.getName(), diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java index 678b9279..bdb02f2b 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java @@ -35,7 +35,7 @@ public List getLegends() { return legends.values().stream().toList(); } - public static RatioStatistic create(Question question) { + protected static RatioStatistic create(Question question) { Map legends = new HashMap<>(); question.getOptions().forEach((key, value) -> legends.put(key, new Legend(key, value.getText(), value.getValue(), 0L))); return new RatioStatistic( From 40d646b07f0eb99ec5331c8145d5da8ca4594467 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Tue, 5 Mar 2024 20:52:48 +0900 Subject: [PATCH 04/22] =?UTF-8?q?refactor:=20DashboardComponent=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=9D=B8=EC=9E=90=EB=A5=BC=20sta?= =?UTF-8?q?tistic=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 39 ++++++++----------- .../model/BestWorthDashboardComponent.java | 8 ++-- .../model/CharacterDashboardComponent.java | 5 +-- .../dashboard/model/DashboardComponent.java | 4 +- .../model/HappyDashboardComponent.java | 8 ++-- .../model/MoneyDashboardComponent.java | 10 +++-- .../model/SadDashboardComponent.java | 8 ++-- 7 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index 890fd86e..97ee14af 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -8,16 +8,16 @@ import com.dnd.namuiwiki.domain.dashboard.model.HappyDashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.MoneyDashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.SadDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.Statistics; 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.QuestionRepository; -import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.StatisticsService; import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; -import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; +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; import com.dnd.namuiwiki.domain.survey.type.Relation; @@ -35,7 +35,6 @@ public class DashboardService { private final UserRepository userRepository; private final DashboardRepository dashboardRepository; private final StatisticsService statisticsService; - private final QuestionRepository questionRepository; public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period period, Relation relation) { validateFilterCategory(period, relation); @@ -46,31 +45,27 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio return null; } - List questions = questionRepository.findAll(); - Statistics statistics = dashboard.get().getStatistics(); + return createDashboardDto(period, relation, statistics); + } + + private DashboardDto createDashboardDto(Period period, Relation relation, Statistics statistics) { List dashboardComponents = List.of( - new BestWorthDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.CORE_VALUE)), - new HappyDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.HAPPY_BEHAVIOR)), - new SadDashboardComponent(statistics, getQuestionIdByQuestionNAme(questions, QuestionName.SAD_ANGRY_BEHAVIOR)), - new CharacterDashboardComponent(statistics), - getMoneyDashboardComponent(statistics, period, relation, getQuestionIdByQuestionNAme(questions, QuestionName.BORROWING_LIMIT)) + new BestWorthDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), + new HappyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.HAPPY)), + new SadDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.SAD)), + new CharacterDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.CHARACTER)), + getMoneyDashboardComponent(statistics, period, relation) ); return new DashboardDto(dashboardComponents); } - private String getQuestionIdByQuestionNAme(List questions, QuestionName questionName) { - return questions.stream().filter(q -> q.getName() == questionName) - .findFirst().orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_QUESTION_ID)) - .getId(); - } - - private MoneyDashboardComponent getMoneyDashboardComponent(Statistics statistics, Period period, Relation relation, String questionId) { + private MoneyDashboardComponent getMoneyDashboardComponent(Statistics statistics, Period period, Relation relation) { PopulationStatistic populationStatistic = statisticsService.getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); BorrowingLimitEntireStatistic statistic = (BorrowingLimitEntireStatistic) populationStatistic.getStatistic(); long entireAverage = statistic.getBorrowingMoneyLimitEntireAverage(); - return new MoneyDashboardComponent(statistics, entireAverage, questionId); + return new MoneyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.MONEY), entireAverage); } private void validateFilterCategory(Period period, Relation relation) { @@ -97,20 +92,20 @@ public void updateStatistics(Survey survey) { updateDashboards(owner, period, relation, statisticalAnswers); } - private void updateDashboards(User owner, Period period, Relation relation, List statisticalAnswers) { + private void updateDashboards(User owner, Period period, Relation relation, List statisticalAnswers) { updateDashboardByCategory(owner, null, null, statisticalAnswers); updateDashboardByCategory(owner, period, null, statisticalAnswers); updateDashboardByCategory(owner, null, relation, statisticalAnswers); } - private void updateDashboardByCategory(User owner, Period period, Relation relation, List answers) { + private void updateDashboardByCategory(User owner, Period period, Relation relation, List answers) { Dashboard dashboard = dashboardRepository.findByUserAndPeriodAndRelation(owner, period, relation) .orElseGet(() -> { Dashboard newDashboard = Dashboard.builder() .user(owner) .period(period) .relation(relation) - .statistics(Statistics.from(answers.stream().map(Survey.Answer::getQuestion).toList())) + .statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList())) .build(); return dashboardRepository.save(newDashboard); }); diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java index 71ee5e60..ff806dc2 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java @@ -11,16 +11,16 @@ public class BestWorthDashboardComponent extends DashboardComponent { private final String questionId; private List rank; - public BestWorthDashboardComponent(Statistics statistics, String questionId) { + public BestWorthDashboardComponent(List statistics) { super(DashboardType.BEST_WORTH); - this.questionId = questionId; + this.questionId = statistics.get(0).getQuestionId(); calculate(statistics); } @Override - public void calculate(Statistics statistics) { - RatioStatistic bestWorth = (RatioStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).get(0); + public void calculate(List statistics) { + RatioStatistic bestWorth = (RatioStatistic) statistics.get(0); Long totalCount = bestWorth.getTotalCount(); this.rank = bestWorth.getLegends().stream() diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java index 850aed8d..a8681de5 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java @@ -14,15 +14,14 @@ public class CharacterDashboardComponent extends DashboardComponent { private List characters; - public CharacterDashboardComponent(Statistics statistics) { + public CharacterDashboardComponent(List statistics) { super(DashboardType.CHARACTER); this.characters = new ArrayList<>(); calculate(statistics); } @Override - public void calculate(Statistics statistics) { - List character = statistics.getStatisticsByDashboardType(this.dashboardType); + public void calculate(List character) { this.characters = character.stream().map(Character::from).toList(); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java index 6a3abd71..5eee317e 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java @@ -5,6 +5,8 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import java.util.List; + @Schema(description = "대시보드 타입", oneOf = { BestWorthDashboardComponent.class, CharacterDashboardComponent.class, @@ -17,6 +19,6 @@ public abstract class DashboardComponent { protected final DashboardType dashboardType; - public abstract void calculate(Statistics statistics); + public abstract void calculate(List statistics); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java index cd4f3d47..6c523db0 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java @@ -11,16 +11,16 @@ public class HappyDashboardComponent extends DashboardComponent { private List rank; private final String questionId; - public HappyDashboardComponent(Statistics statistics, String questionId) { + public HappyDashboardComponent(List statistics) { super(DashboardType.HAPPY); - this.questionId = questionId; + this.questionId = statistics.get(0).getQuestionId(); calculate(statistics); } @Override - public void calculate(Statistics statistics) { - RatioStatistic happy = (RatioStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).get(0); + public void calculate(List statistics) { + RatioStatistic happy = (RatioStatistic) statistics.get(0); Long totalCount = happy.getTotalCount(); this.rank = happy.getLegends().stream() .map(legend -> { diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java index b35f978e..05d3dd2c 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java @@ -3,6 +3,8 @@ import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import lombok.Getter; +import java.util.List; + @Getter public class MoneyDashboardComponent extends DashboardComponent { private final String questionId; @@ -10,17 +12,17 @@ public class MoneyDashboardComponent extends DashboardComponent { private long average; private long entireAverage; - public MoneyDashboardComponent(Statistics statistics, long entireAverage, String questionId) { + public MoneyDashboardComponent(List statistics, long entireAverage) { super(DashboardType.MONEY); this.entireAverage = entireAverage; - this.questionId = questionId; + this.questionId = statistics.get(0).getQuestionId(); calculate(statistics); } @Override - public void calculate(Statistics statistics) { - AverageStatistic money = (AverageStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).get(0); + public void calculate(List statistics) { + AverageStatistic money = (AverageStatistic) statistics.get(0); this.peopleCount = money.getTotalCount(); this.average = money.getAverage(); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java index ceb5a0f4..91cfbf7d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java @@ -11,16 +11,16 @@ public class SadDashboardComponent extends DashboardComponent { private final String questionId; private List rank; - public SadDashboardComponent(Statistics statistics, String questionId) { + public SadDashboardComponent(List statistics) { super(DashboardType.SAD); - this.questionId = questionId; + this.questionId = statistics.get(0).getQuestionId(); calculate(statistics); } @Override - public void calculate(Statistics statistics) { - RatioStatistic sad = (RatioStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).get(0); + public void calculate(List statistics) { + RatioStatistic sad = (RatioStatistic) statistics.get(0); Long totalCount = sad.getTotalCount(); this.rank = sad.getLegends().stream() .map(legend -> { From 8f60a9e3a9d5f6d4c91c5274ff3910c6c9a2e683 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 03:42:24 +0900 Subject: [PATCH 05/22] =?UTF-8?q?refactor:=20DashboardComponent=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=A1=9C=EC=A7=81=20Entity=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 40 +------------------ .../dashboard/model/entity/Dashboard.java | 33 +++++++++++++++ 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index 97ee14af..58a6c85f 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -2,20 +2,11 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; -import com.dnd.namuiwiki.domain.dashboard.model.BestWorthDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.CharacterDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.HappyDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.MoneyDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.SadDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.Statistics; 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.BorrowingLimitEntireStatistic; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import com.dnd.namuiwiki.domain.survey.model.entity.Survey; @@ -45,27 +36,8 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio return null; } - Statistics statistics = dashboard.get().getStatistics(); - return createDashboardDto(period, relation, statistics); - } - - private DashboardDto createDashboardDto(Period period, Relation relation, Statistics statistics) { - List dashboardComponents = List.of( - new BestWorthDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), - new HappyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.HAPPY)), - new SadDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.SAD)), - new CharacterDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.CHARACTER)), - getMoneyDashboardComponent(statistics, period, relation) - ); - return new DashboardDto(dashboardComponents); - } - - private MoneyDashboardComponent getMoneyDashboardComponent(Statistics statistics, Period period, Relation relation) { PopulationStatistic populationStatistic = statisticsService.getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); - BorrowingLimitEntireStatistic statistic = (BorrowingLimitEntireStatistic) populationStatistic.getStatistic(); - long entireAverage = statistic.getBorrowingMoneyLimitEntireAverage(); - - return new MoneyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.MONEY), entireAverage); + return dashboard.get().convertDashboardDto(populationStatistic); } private void validateFilterCategory(Period period, Relation relation) { @@ -100,15 +72,7 @@ private void updateDashboards(User owner, Period period, Relation relation, List private void updateDashboardByCategory(User owner, Period period, Relation relation, List answers) { Dashboard dashboard = dashboardRepository.findByUserAndPeriodAndRelation(owner, period, relation) - .orElseGet(() -> { - Dashboard newDashboard = Dashboard.builder() - .user(owner) - .period(period) - .relation(relation) - .statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList())) - .build(); - return dashboardRepository.save(newDashboard); - }); + .orElseGet(() -> dashboardRepository.save(Dashboard.createNew(owner, period, relation, answers))); dashboard.updateStatistics(answers); dashboardRepository.save(dashboard); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index 64052625..288ec98f 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -1,7 +1,17 @@ package com.dnd.namuiwiki.domain.dashboard.model.entity; import com.dnd.namuiwiki.common.model.BaseTimeEntity; +import com.dnd.namuiwiki.domain.dashboard.model.BestWorthDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.CharacterDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.HappyDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.MoneyDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.SadDashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.Statistics; +import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; +import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; 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; @@ -35,4 +45,27 @@ public void updateStatistics(List answer) { statistics.updateStatistics(answer); } + public DashboardDto convertDashboardDto(PopulationStatistic populationStatistic) { + BorrowingLimitEntireStatistic statistic = (BorrowingLimitEntireStatistic) populationStatistic.getStatistic(); + long entireAverage = statistic.getBorrowingMoneyLimitEntireAverage(); + + List dashboardComponents = List.of( + new BestWorthDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), + new HappyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.HAPPY)), + new SadDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.SAD)), + new CharacterDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.CHARACTER)), + new MoneyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.MONEY), entireAverage) + ); + return new DashboardDto(dashboardComponents); + } + + public static Dashboard createNew(User owner, Period period, Relation relation, List answers) { + return Dashboard.builder() + .user(owner) + .period(period) + .relation(relation) + .statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList())) + .build(); + } + } From e13bb2c1a3009f93b96868133a87315847c23bce Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 17:29:08 +0900 Subject: [PATCH 06/22] =?UTF-8?q?refactor:=20RadioStatistic=20=EB=8C=80?= =?UTF-8?q?=EC=8B=9C=EB=B3=B4=EB=93=9C=20=EC=BB=B4=ED=8F=AC=EB=84=8C?= =?UTF-8?q?=ED=8A=B8=20=EC=A0=95=EB=A0=AC=ED=95=98=EC=97=AC=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BestWorthDashboardComponent.java | 1 + .../model/HappyDashboardComponent.java | 1 + .../model/SadDashboardComponent.java | 1 + .../BestWorthDashboardComponentTest.java | 43 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java index ff806dc2..3fb92850 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java @@ -31,6 +31,7 @@ public void calculate(List statistics) { int percentage = (int) (legend.getCount() * 100 / totalCount); return new RatioDto(legend.getText(), percentage); }) + .sorted((a, b) -> b.getPercentage() - a.getPercentage()) .toList(); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java index 6c523db0..ffb946df 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java @@ -30,6 +30,7 @@ public void calculate(List statistics) { int percentage = (int) (legend.getCount() * 100 / totalCount); return new RatioDto(legend.getText(), percentage); }) + .sorted((a, b) -> b.getPercentage() - a.getPercentage()) .toList(); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java index 91cfbf7d..28e06c99 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java @@ -30,6 +30,7 @@ public void calculate(List statistics) { int percentage = (int) (legend.getCount() * 100 / totalCount); return new RatioDto(legend.getText(), percentage); }) + .sorted((a, b) -> b.getPercentage() - a.getPercentage()) .toList(); } diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java new file mode 100644 index 00000000..e91100dc --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java @@ -0,0 +1,43 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class BestWorthDashboardComponentTest { + @Test + @DisplayName("범례별 비율을 계산하여 비율이 높은 순서대로 정렬하여 반환한다.") + void convertStatisticsToDashboardComponentDto() { + // given + long totalCount = 5; + long opt1Count = 3; + long opt2Count = 2; + Statistic statistic = new RatioStatistic( + "questionId", + QuestionName.CORE_VALUE, + DashboardType.BEST_WORTH, + totalCount, + Map.of("op1", new Legend("op1", "legend1", "legend1", opt1Count), + "op2", new Legend("op2", "legend2", "legend2", opt2Count))); + + // when + BestWorthDashboardComponent bestWorthDashboardComponent = new BestWorthDashboardComponent(List.of(statistic)); + + // then + int expectedOpt1Percentage = 60; + int expectedOpt2Percentage = 40; + assertThat(bestWorthDashboardComponent.getRank().stream().mapToInt(RatioDto::getPercentage)) + .containsExactly(expectedOpt1Percentage, expectedOpt2Percentage); + + } +} From 070b4c1b212917efb607db845e2378e01eb33e2c Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 17:42:49 +0900 Subject: [PATCH 07/22] =?UTF-8?q?test:=20character=20=EB=8C=80=EC=8B=9C?= =?UTF-8?q?=EB=B3=B4=EB=93=9C=20=ED=83=80=EC=9E=85=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CharacterDashboardComponentTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java new file mode 100644 index 00000000..58f4f2b0 --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java @@ -0,0 +1,52 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class CharacterDashboardComponentTest { + @Test + @DisplayName("질문마다 범례별 비율을 계산하여 true인 값이 많을 경우 character value를 true로 반환한다.") + void convertStatisticsToDashboardComponentDto() { + // given + long totalCount = 5; + + Legend legend1 = new Legend("op1", "legend1", true, 3L); + Legend legend2 = new Legend("op2", "legend2", false, 2L); + + Legend legend3 = new Legend("op3", "legend3", true, 0L); + Legend legend4 = new Legend("op4", "legend4", false, 5L); + + Statistic statistic1 = new RatioStatistic( + "questionId", + QuestionName.MBTI_IMMERSION, + DashboardType.CHARACTER, + totalCount, + Map.of("op1", legend1, "op2", legend2)); + Statistic statistic2 = new RatioStatistic( + "questionId", + QuestionName.FRIENDLINESS_LEVEL, + DashboardType.CHARACTER, + totalCount, + Map.of("op3", legend3, "op4", legend4)); + + // when + CharacterDashboardComponent characterDashboardComponent = new CharacterDashboardComponent(List.of(statistic1, statistic2)); + + // then + boolean statistic1Result = true; + boolean statistic2Result = false; + assertThat(characterDashboardComponent.getCharacters().stream().map(CharacterDashboardComponent.Character::isValue)) + .containsExactly(statistic1Result, statistic2Result); + + } +} From c70e7b07fb425e414d3818eaa74b08ad36f77dd4 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 17:50:15 +0900 Subject: [PATCH 08/22] =?UTF-8?q?test:=20money=20=EB=8C=80=EC=8B=9C?= =?UTF-8?q?=EB=B3=B4=EB=93=9C=20=ED=83=80=EC=9E=85=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/MoneyDashboardComponentTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java new file mode 100644 index 00000000..02378d98 --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java @@ -0,0 +1,41 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class MoneyDashboardComponentTest { + @Test + @DisplayName("AverageStatistic에 설정된 평균 값을 반환한다.") + void convertStatisticsToDashboardComponentDto() { + // given + long totalCount = 5; + long average = 1000; + long entireAverage = 2000; + + Statistic statistic = new AverageStatistic( + "questionId", + QuestionName.BORROWING_LIMIT, + DashboardType.MONEY, + totalCount, + average); + + // when + MoneyDashboardComponent moneyDashboardComponent = new MoneyDashboardComponent(List.of(statistic), entireAverage); + + // then + long expectedAverage = 1000; + long expectedEntireAverage = 2000; + assertThat(moneyDashboardComponent.getEntireAverage()).isEqualTo(expectedEntireAverage); + assertThat(moneyDashboardComponent.getAverage()).isEqualTo(expectedAverage); + + } +} From 245443a27ff85b55e97069cb6fefdac4ce7f8c45 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 18:11:38 +0900 Subject: [PATCH 09/22] =?UTF-8?q?test:=20RatioStatistic=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1(#1?= =?UTF-8?q?04)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/model/RatioStatisticTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatisticTest.java diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatisticTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatisticTest.java new file mode 100644 index 00000000..2d148011 --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatisticTest.java @@ -0,0 +1,75 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.option.entity.Option; +import com.dnd.namuiwiki.domain.question.entity.Question; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import com.dnd.namuiwiki.domain.question.type.QuestionType; +import com.dnd.namuiwiki.domain.survey.model.entity.Answer; +import com.dnd.namuiwiki.domain.survey.type.AnswerType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class RatioStatisticTest { + + RatioStatistic statistic; + private final Option option1 = Option.builder().id("op1").value("legend1").text("legend1").build(); + private final Option option2 = Option.builder().id("op2").value("legend2").text("legend2").build(); + private final Question question = Question.builder() + .id("questionId") + .name(QuestionName.CORE_VALUE) + .dashboardType(DashboardType.BEST_WORTH) + .type(QuestionType.MULTIPLE_CHOICE) + .options(Map.of("op1", option1, "op2", option2)) + .build(); + + @BeforeEach + void beforeEach() { + long totalCount = 5; + long opt1Count = 3; + long opt2Count = 2; + statistic = new RatioStatistic( + question.getId(), + question.getName(), + question.getDashboardType(), + totalCount, + Map.of("op1", new Legend(option1.getId(), option1.getText(), option1.getValue(), opt1Count), + "op2", new Legend(option2.getId(), option2.getText(), option2.getValue(), opt2Count))); + } + + @Test + @DisplayName("통계 업데이트 시 totalCount가 1 증가한다.") + void increaseOneForTotalCount() { + // given + Answer answer = new Answer(question, AnswerType.OPTION, "op1", "reason"); + + // when + statistic.updateStatistic(answer); + + // then + long expectedTotalCount = 6; + assertThat(statistic.getTotalCount()).isEqualTo(expectedTotalCount); + } + + @Test + @DisplayName("통계 업데이트 시 선택한 옵션 count가 1 증가한다.") + void increaseOneForSelectedLegendCount() { + // given + Answer answer = new Answer(question, AnswerType.OPTION, "op1", "reason"); + + // when + statistic.updateStatistic(answer); + + // then + long expectedOpt1Count = 4; + long expectedOpt2Count = 2; + assertThat(statistic.getLegend("op1").get().getCount()).isEqualTo(expectedOpt1Count); + assertThat(statistic.getLegend("op2").get().getCount()).isEqualTo(expectedOpt2Count); + } + +} From 43b783cec920db366eb6e06e41f22c752c667924 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 18:18:59 +0900 Subject: [PATCH 10/22] =?UTF-8?q?test:=20AverageStatistic=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1(#1?= =?UTF-8?q?04)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/model/AverageStatisticTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatisticTest.java diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatisticTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatisticTest.java new file mode 100644 index 00000000..c387d92f --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageStatisticTest.java @@ -0,0 +1,71 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.option.entity.Option; +import com.dnd.namuiwiki.domain.question.entity.Question; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import com.dnd.namuiwiki.domain.question.type.QuestionType; +import com.dnd.namuiwiki.domain.survey.model.entity.Answer; +import com.dnd.namuiwiki.domain.survey.type.AnswerType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class AverageStatisticTest { + + AverageStatistic statistic; + private final Option option1 = Option.builder().id("op1").value(10).text("legend1").build(); + private final Option option2 = Option.builder().id("op2").value(100).text("legend2").build(); + private final Question question = Question.builder() + .id("questionId") + .name(QuestionName.CORE_VALUE) + .dashboardType(DashboardType.BEST_WORTH) + .type(QuestionType.MULTIPLE_CHOICE) + .options(Map.of("op1", option1, "op2", option2)) + .build(); + + @BeforeEach + void beforeEach() { + long totalCount = 5; + long average = 1000; + statistic = new AverageStatistic( + question.getId(), + question.getName(), + question.getDashboardType(), + totalCount, + average); + } + + @Test + @DisplayName("통계 업데이트 시 totalCount가 1 증가한다.") + void increaseOneForTotalCount() { + // given + Answer answer = new Answer(question, AnswerType.OPTION, "op1", "reason"); + + // when + statistic.updateStatistic(answer); + + // then + long expectedTotalCount = 6; + assertThat(statistic.getTotalCount()).isEqualTo(expectedTotalCount); + } + + @Test + @DisplayName("옵션 선택 시, 해당 옵션의 value가 추가된 평균으로 업데이트 된다.") + void updateAverage() { + // given + Answer answer = new Answer(question, AnswerType.OPTION, "op1", "reason"); + + // when + statistic.updateStatistic(answer); + + // then + long expectedAverage = (1000 * 5 + 10) / 6; + assertThat(statistic.getAverage()).isEqualTo(expectedAverage); + } + +} From bc7da8aa440a7260f79c10ee5a58e1698a2c8be9 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 18:46:50 +0900 Subject: [PATCH 11/22] =?UTF-8?q?refactor:=20dto=20=EB=B3=80=ED=99=98=20?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=20=EB=B3=84=20=EB=8C=80=EC=8B=9C=EB=B3=B4?= =?UTF-8?q?=EB=93=9C=20=EC=BB=B4=ED=8F=AC=EB=84=8C=ED=8A=B8=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/AverageDashboardComponent.java | 30 ++++++++++ .../model/BinaryDashboardComponent.java | 60 +++++++++++++++++++ .../model/RatioDashboardComponent.java | 38 ++++++++++++ .../dashboard/model/entity/Dashboard.java | 18 +++--- .../model/AverageDashboardComponentTest.java | 41 +++++++++++++ .../model/BinaryDashboardComponentTest.java | 52 ++++++++++++++++ .../model/RatioDashboardComponentTest.java | 43 +++++++++++++ 7 files changed, 272 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java create mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java create mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponentTest.java create mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java new file mode 100644 index 00000000..de83465d --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java @@ -0,0 +1,30 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import lombok.Getter; + +import java.util.List; + +@Getter +public class AverageDashboardComponent extends DashboardComponent { + private final String questionId; + private long peopleCount; + private long average; + private long entireAverage; + + public AverageDashboardComponent(DashboardType dashboardType, List statistics, long entireAverage) { + super(dashboardType); + this.entireAverage = entireAverage; + this.questionId = statistics.get(0).getQuestionId(); + + calculate(statistics); + } + + @Override + public void calculate(List statistics) { + AverageStatistic money = (AverageStatistic) statistics.get(0); + this.peopleCount = money.getTotalCount(); + this.average = money.getAverage(); + } + +} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java new file mode 100644 index 00000000..ebb7a76a --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java @@ -0,0 +1,60 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.common.exception.ApplicationErrorException; +import com.dnd.namuiwiki.common.exception.ApplicationErrorType; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Getter +public class BinaryDashboardComponent extends DashboardComponent { + private List characters; + + public BinaryDashboardComponent(DashboardType dashboardType, List statistics) { + super(dashboardType); + this.characters = new ArrayList<>(); + calculate(statistics); + } + + @Override + public void calculate(List character) { + this.characters = character.stream().map(BinaryMetric::from).toList(); + } + + @RequiredArgsConstructor + @Getter + static class BinaryMetric { + private final String name; + private final boolean value; + private final String questionId; + + private static BinaryMetric from(Statistic statistic) { + RatioStatistic ratioStatistic = (RatioStatistic) statistic; + List legends = ratioStatistic.getLegends(); + QuestionName questionName = ratioStatistic.getQuestionName(); + + return new BinaryMetric( + questionName.name(), + getCharacterRatioResult(legends), + statistic.getQuestionId() + ); + } + + private static Legend getLegendByValue(List legends, boolean value) { + return legends.stream() + .filter(legend -> value == (boolean) legend.getValue()) + .findFirst() + .orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR)); + } + + private static boolean getCharacterRatioResult(List legends) { + return getLegendByValue(legends, true).getCount() >= + getLegendByValue(legends, false).getCount(); + } + } + +} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java new file mode 100644 index 00000000..07e1cb68 --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java @@ -0,0 +1,38 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import lombok.Getter; + +import java.util.List; + +@Getter +public class RatioDashboardComponent extends DashboardComponent { + private final String questionId; + private List rank; + + public RatioDashboardComponent(DashboardType dashboardType, List statistics) { + super(dashboardType); + this.questionId = statistics.get(0).getQuestionId(); + + calculate(statistics); + } + + @Override + public void calculate(List statistics) { + RatioStatistic bestWorth = (RatioStatistic) statistics.get(0); + Long totalCount = bestWorth.getTotalCount(); + + this.rank = bestWorth.getLegends().stream() + .map(legend -> { + if (totalCount == 0) { + return new RatioDto(legend.getText(), 0); + } + int percentage = (int) (legend.getCount() * 100 / totalCount); + return new RatioDto(legend.getText(), percentage); + }) + .sorted((a, b) -> b.getPercentage() - a.getPercentage()) + .toList(); + } + +} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index 288ec98f..f8b91a77 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -1,12 +1,10 @@ package com.dnd.namuiwiki.domain.dashboard.model.entity; import com.dnd.namuiwiki.common.model.BaseTimeEntity; -import com.dnd.namuiwiki.domain.dashboard.model.BestWorthDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.CharacterDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.AverageDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.BinaryDashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.HappyDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.MoneyDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.SadDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.RatioDashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; @@ -50,11 +48,11 @@ public DashboardDto convertDashboardDto(PopulationStatistic populationStatistic) long entireAverage = statistic.getBorrowingMoneyLimitEntireAverage(); List dashboardComponents = List.of( - new BestWorthDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), - new HappyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.HAPPY)), - new SadDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.SAD)), - new CharacterDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.CHARACTER)), - new MoneyDashboardComponent(statistics.getStatisticsByDashboardType(DashboardType.MONEY), entireAverage) + new RatioDashboardComponent(DashboardType.BEST_WORTH, statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), + new RatioDashboardComponent(DashboardType.HAPPY, statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), + new RatioDashboardComponent(DashboardType.SAD, statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), + new BinaryDashboardComponent(DashboardType.CHARACTER, statistics.getStatisticsByDashboardType(DashboardType.CHARACTER)), + new AverageDashboardComponent(DashboardType.MONEY, statistics.getStatisticsByDashboardType(DashboardType.MONEY), entireAverage) ); return new DashboardDto(dashboardComponents); } diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java new file mode 100644 index 00000000..cf0bce4d --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java @@ -0,0 +1,41 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class AverageDashboardComponentTest { + @Test + @DisplayName("AverageStatistic에 설정된 평균 값을 반환한다.") + void convertStatisticsToDashboardComponentDto() { + // given + long totalCount = 5; + long average = 1000; + long entireAverage = 2000; + + Statistic statistic = new AverageStatistic( + "questionId", + QuestionName.BORROWING_LIMIT, + DashboardType.MONEY, + totalCount, + average); + + // when + AverageDashboardComponent averageDashboardComponent = new AverageDashboardComponent(DashboardType.MONEY, List.of(statistic), entireAverage); + + // then + long expectedAverage = 1000; + long expectedEntireAverage = 2000; + assertThat(averageDashboardComponent.getEntireAverage()).isEqualTo(expectedEntireAverage); + assertThat(averageDashboardComponent.getAverage()).isEqualTo(expectedAverage); + + } +} diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponentTest.java new file mode 100644 index 00000000..69e09ac5 --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponentTest.java @@ -0,0 +1,52 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class BinaryDashboardComponentTest { + @Test + @DisplayName("질문마다 범례별 비율을 계산하여 true인 값이 많을 경우 character value를 true로 반환한다.") + void convertStatisticsToDashboardComponentDto() { + // given + long totalCount = 5; + + Legend legend1 = new Legend("op1", "legend1", true, 3L); + Legend legend2 = new Legend("op2", "legend2", false, 2L); + + Legend legend3 = new Legend("op3", "legend3", true, 0L); + Legend legend4 = new Legend("op4", "legend4", false, 5L); + + Statistic statistic1 = new RatioStatistic( + "questionId", + QuestionName.MBTI_IMMERSION, + DashboardType.CHARACTER, + totalCount, + Map.of("op1", legend1, "op2", legend2)); + Statistic statistic2 = new RatioStatistic( + "questionId", + QuestionName.FRIENDLINESS_LEVEL, + DashboardType.CHARACTER, + totalCount, + Map.of("op3", legend3, "op4", legend4)); + + // when + BinaryDashboardComponent binaryDashboardComponent = new BinaryDashboardComponent(DashboardType.CHARACTER, List.of(statistic1, statistic2)); + + // then + boolean statistic1Result = true; + boolean statistic2Result = false; + assertThat(binaryDashboardComponent.getCharacters().stream().map(BinaryDashboardComponent.BinaryMetric::isValue)) + .containsExactly(statistic1Result, statistic2Result); + + } +} diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java new file mode 100644 index 00000000..a14704a7 --- /dev/null +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java @@ -0,0 +1,43 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.question.type.QuestionName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class RatioDashboardComponentTest { + @Test + @DisplayName("범례별 비율을 계산하여 비율이 높은 순서대로 정렬하여 반환한다.") + void convertStatisticsToDashboardComponentDto() { + // given + long totalCount = 5; + long opt1Count = 3; + long opt2Count = 2; + Statistic statistic = new RatioStatistic( + "questionId", + QuestionName.CORE_VALUE, + DashboardType.BEST_WORTH, + totalCount, + Map.of("op1", new Legend("op1", "legend1", "legend1", opt1Count), + "op2", new Legend("op2", "legend2", "legend2", opt2Count))); + + // when + RatioDashboardComponent ratioDashboardComponent = new RatioDashboardComponent(DashboardType.BEST_WORTH, List.of(statistic)); + + // then + int expectedOpt1Percentage = 60; + int expectedOpt2Percentage = 40; + assertThat(ratioDashboardComponent.getRank().stream().mapToInt(RatioDto::getPercentage)) + .containsExactly(expectedOpt1Percentage, expectedOpt2Percentage); + + } +} From 568f363ee9c3dd6c89c9359c3603fbc1af68b55f Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:26:21 +0900 Subject: [PATCH 12/22] =?UTF-8?q?refactor:=20=EB=8C=80=EC=8B=9C=EB=B3=B4?= =?UTF-8?q?=EB=93=9C=ED=86=B5=EA=B3=84=ED=83=80=EC=9E=85=20=EB=B3=84=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/AverageDashboardComponent.java | 28 ++++++++----------- .../model/BinaryDashboardComponent.java | 5 ++-- .../model/RatioDashboardComponent.java | 15 +++++----- .../model/AverageDashboardComponentTest.java | 10 +++++-- .../model/RatioDashboardComponentTest.java | 3 +- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java index de83465d..1518416d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponent.java @@ -1,30 +1,26 @@ package com.dnd.namuiwiki.domain.dashboard.model; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.statistic.model.AverageEntireStatistic; +import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import lombok.Getter; -import java.util.List; - @Getter public class AverageDashboardComponent extends DashboardComponent { private final String questionId; - private long peopleCount; - private long average; - private long entireAverage; + private final long peopleCount; + private final long average; + private final long entireAverage; - public AverageDashboardComponent(DashboardType dashboardType, List statistics, long entireAverage) { + public AverageDashboardComponent(DashboardType dashboardType, Statistic statistic, PopulationStatistic populationStatistic) { super(dashboardType); - this.entireAverage = entireAverage; - this.questionId = statistics.get(0).getQuestionId(); - - calculate(statistics); - } + AverageEntireStatistic entireStatistic = (AverageEntireStatistic) populationStatistic.getStatistic(); + AverageStatistic averageStatistic = (AverageStatistic) statistic; - @Override - public void calculate(List statistics) { - AverageStatistic money = (AverageStatistic) statistics.get(0); - this.peopleCount = money.getTotalCount(); - this.average = money.getAverage(); + this.entireAverage = entireStatistic.getEntireAverage(); + this.questionId = statistic.getQuestionId(); + this.peopleCount = averageStatistic.getTotalCount(); + this.average = averageStatistic.getAverage(); } } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java index ebb7a76a..4511c267 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java @@ -17,11 +17,10 @@ public class BinaryDashboardComponent extends DashboardComponent { public BinaryDashboardComponent(DashboardType dashboardType, List statistics) { super(dashboardType); this.characters = new ArrayList<>(); - calculate(statistics); + initiate(statistics); } - @Override - public void calculate(List character) { + public void initiate(List character) { this.characters = character.stream().map(BinaryMetric::from).toList(); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java index 07e1cb68..243f6ceb 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java @@ -11,19 +11,18 @@ public class RatioDashboardComponent extends DashboardComponent { private final String questionId; private List rank; - public RatioDashboardComponent(DashboardType dashboardType, List statistics) { + public RatioDashboardComponent(DashboardType dashboardType, Statistic statistic) { super(dashboardType); - this.questionId = statistics.get(0).getQuestionId(); + RatioStatistic ratioStatistic = (RatioStatistic) statistic; - calculate(statistics); + this.questionId = ratioStatistic.getQuestionId(); + initiate(ratioStatistic); } - @Override - public void calculate(List statistics) { - RatioStatistic bestWorth = (RatioStatistic) statistics.get(0); - Long totalCount = bestWorth.getTotalCount(); + public void initiate(RatioStatistic statistic) { + Long totalCount = statistic.getTotalCount(); - this.rank = bestWorth.getLegends().stream() + this.rank = statistic.getLegends().stream() .map(legend -> { if (totalCount == 0) { return new RatioDto(legend.getText(), 0); diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java index cf0bce4d..5ca15580 100644 --- a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/AverageDashboardComponentTest.java @@ -2,13 +2,13 @@ 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 org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(MockitoExtension.class) @@ -28,8 +28,12 @@ void convertStatisticsToDashboardComponentDto() { totalCount, average); + PopulationStatistic populationStatistic = PopulationStatistic.builder() + .statistic(new AverageEntireStatistic(entireAverage, totalCount)) + .build(); + // when - AverageDashboardComponent averageDashboardComponent = new AverageDashboardComponent(DashboardType.MONEY, List.of(statistic), entireAverage); + AverageDashboardComponent averageDashboardComponent = new AverageDashboardComponent(DashboardType.MONEY, statistic, populationStatistic); // then long expectedAverage = 1000; diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java index a14704a7..61d8e05c 100644 --- a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java +++ b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponentTest.java @@ -8,7 +8,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.List; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -31,7 +30,7 @@ void convertStatisticsToDashboardComponentDto() { "op2", new Legend("op2", "legend2", "legend2", opt2Count))); // when - RatioDashboardComponent ratioDashboardComponent = new RatioDashboardComponent(DashboardType.BEST_WORTH, List.of(statistic)); + RatioDashboardComponent ratioDashboardComponent = new RatioDashboardComponent(DashboardType.BEST_WORTH, statistic); // then int expectedOpt1Percentage = 60; From 3de35d46c2d43a8e171df992a1def36ccd458411 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:26:45 +0900 Subject: [PATCH 13/22] =?UTF-8?q?refactor:=20DashboardComponent=20Factory?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/model/DashboardFactory.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardFactory.java diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardFactory.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardFactory.java new file mode 100644 index 00000000..fca113b0 --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardFactory.java @@ -0,0 +1,42 @@ +package com.dnd.namuiwiki.domain.dashboard.model; + +import com.dnd.namuiwiki.domain.dashboard.type.DashboardStatisticType; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; + +import java.util.List; + +public abstract class DashboardFactory { + + public static DashboardComponent create(DashboardType dashboardType, List statistics) { + DashboardStatisticType type = dashboardType.getDashboardStatisticType(); + switch (type) { + case RATIO -> { + validateStatisticsSize(statistics, 1); + return new RatioDashboardComponent(dashboardType, statistics.get(0)); + } + case BINARY -> { + return new BinaryDashboardComponent(dashboardType, statistics); + } + default -> throw new IllegalArgumentException("Not supported type: " + type); + } + } + + public static DashboardComponent create(DashboardType dashboardType, List statistics, PopulationStatistic populationStatistic) { + DashboardStatisticType type = dashboardType.getDashboardStatisticType(); + switch (type) { + case AVERAGE -> { + validateStatisticsSize(statistics, 1); + return new AverageDashboardComponent(dashboardType, statistics.get(0), populationStatistic); + } + default -> throw new IllegalArgumentException("Not supported type: " + type); + } + } + + private static void validateStatisticsSize(List statistics, int size) { + if (statistics.size() != size) { + throw new IllegalArgumentException(String.format("This Dashboard type should have only %d statistic", size)); + } + } + +} From 4e444afc1d7d4f899511cbb56df4fe105d2ada5d Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:29:42 +0900 Subject: [PATCH 14/22] =?UTF-8?q?refactor:=20=EB=8C=80=EC=8B=9C=EB=B3=B4?= =?UTF-8?q?=EB=93=9C=20=ED=83=80=EC=9E=85=EC=9D=84=20key=EB=A1=9C=20?= =?UTF-8?q?=ED=95=9C=20statistic=20map=20=EC=A1=B0=ED=9A=8C=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=B6=94=EA=B0=80(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../namuiwiki/domain/dashboard/model/Statistics.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index e45975bd..042e806c 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -43,6 +43,17 @@ public List getStatisticsByDashboardType(DashboardType dashboardType) .toList(); } + public Map> mapStatisticsByDashboardType() { + Map> statistics = new HashMap<>(); + this.statistics.values().forEach(statistic -> { + DashboardType dashboardType = statistic.getDashboardType(); + statistics.putIfAbsent(dashboardType, List.of()); + statistics.put(dashboardType, List.of(statistic)); + }); + + return statistics; + } + public static Statistics from(List questions) { Statistics statistics = new Statistics(new HashMap<>()); questions.stream() From 2127de596d24d6c23809395c471002a46c1dc623 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:30:29 +0900 Subject: [PATCH 15/22] =?UTF-8?q?refactor:=20Dashboard=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9D=84=20DashboardFactory=EB=A1=9C=20=EA=B5=90?= =?UTF-8?q?=EC=B2=B4(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BestWorthDashboardComponent.java | 38 ------------ .../model/CharacterDashboardComponent.java | 60 ------------------- .../dashboard/model/DashboardComponent.java | 12 +--- .../model/HappyDashboardComponent.java | 37 ------------ .../model/MoneyDashboardComponent.java | 30 ---------- .../model/SadDashboardComponent.java | 37 ------------ .../domain/dashboard/model/Statistics.java | 6 -- .../dashboard/model/entity/Dashboard.java | 18 +++--- .../type/DashboardStatisticType.java | 5 ++ .../domain/dashboard/type/DashboardType.java | 22 ++++--- .../BestWorthDashboardComponentTest.java | 43 ------------- .../CharacterDashboardComponentTest.java | 52 ---------------- .../model/MoneyDashboardComponentTest.java | 41 ------------- 13 files changed, 29 insertions(+), 372 deletions(-) delete mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java delete mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java delete mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java delete mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java delete mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java create mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java delete mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java delete mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java delete mode 100644 src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java deleted file mode 100644 index 3fb92850..00000000 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponent.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import lombok.Getter; - -import java.util.List; - -@Getter -public class BestWorthDashboardComponent extends DashboardComponent { - private final String questionId; - private List rank; - - public BestWorthDashboardComponent(List statistics) { - super(DashboardType.BEST_WORTH); - this.questionId = statistics.get(0).getQuestionId(); - - calculate(statistics); - } - - @Override - public void calculate(List statistics) { - RatioStatistic bestWorth = (RatioStatistic) statistics.get(0); - Long totalCount = bestWorth.getTotalCount(); - - this.rank = bestWorth.getLegends().stream() - .map(legend -> { - if (totalCount == 0) { - return new RatioDto(legend.getText(), 0); - } - int percentage = (int) (legend.getCount() * 100 / totalCount); - return new RatioDto(legend.getText(), percentage); - }) - .sorted((a, b) -> b.getPercentage() - a.getPercentage()) - .toList(); - } - -} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java deleted file mode 100644 index a8681de5..00000000 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponent.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.common.exception.ApplicationErrorException; -import com.dnd.namuiwiki.common.exception.ApplicationErrorType; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.question.type.QuestionName; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import java.util.ArrayList; -import java.util.List; - -@Getter -public class CharacterDashboardComponent extends DashboardComponent { - private List characters; - - public CharacterDashboardComponent(List statistics) { - super(DashboardType.CHARACTER); - this.characters = new ArrayList<>(); - calculate(statistics); - } - - @Override - public void calculate(List character) { - this.characters = character.stream().map(Character::from).toList(); - } - - @RequiredArgsConstructor - @Getter - static class Character { - private final String name; - private final boolean value; - private final String questionId; - - private static Character from(Statistic statistic) { - RatioStatistic ratioStatistic = (RatioStatistic) statistic; - List legends = ratioStatistic.getLegends(); - QuestionName questionName = ratioStatistic.getQuestionName(); - - return new Character( - questionName.name(), - getCharacterRatioResult(legends), - statistic.getQuestionId() - ); - } - - private static Legend getLegendByValue(List legends, boolean value) { - return legends.stream() - .filter(legend -> value == (boolean) legend.getValue()) - .findFirst() - .orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR)); - } - - private static boolean getCharacterRatioResult(List legends) { - return getLegendByValue(legends, true).getCount() >= - getLegendByValue(legends, false).getCount(); - } - } - -} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java index 5eee317e..c35f2545 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/DashboardComponent.java @@ -5,20 +5,14 @@ import lombok.AllArgsConstructor; import lombok.Getter; -import java.util.List; - @Schema(description = "대시보드 타입", oneOf = { - BestWorthDashboardComponent.class, - CharacterDashboardComponent.class, - HappyDashboardComponent.class, - MoneyDashboardComponent.class, - SadDashboardComponent.class}) + AverageDashboardComponent.class, + BinaryDashboardComponent.class, + RatioDashboardComponent.class}) @AllArgsConstructor @Getter public abstract class DashboardComponent { protected final DashboardType dashboardType; - public abstract void calculate(List statistics); - } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java deleted file mode 100644 index ffb946df..00000000 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/HappyDashboardComponent.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import lombok.Getter; - -import java.util.List; - -@Getter -public class HappyDashboardComponent extends DashboardComponent { - private List rank; - private final String questionId; - - public HappyDashboardComponent(List statistics) { - super(DashboardType.HAPPY); - this.questionId = statistics.get(0).getQuestionId(); - - calculate(statistics); - } - - @Override - public void calculate(List statistics) { - RatioStatistic happy = (RatioStatistic) statistics.get(0); - Long totalCount = happy.getTotalCount(); - this.rank = happy.getLegends().stream() - .map(legend -> { - if (totalCount == 0) { - return new RatioDto(legend.getText(), 0); - } - int percentage = (int) (legend.getCount() * 100 / totalCount); - return new RatioDto(legend.getText(), percentage); - }) - .sorted((a, b) -> b.getPercentage() - a.getPercentage()) - .toList(); - } - -} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java deleted file mode 100644 index 05d3dd2c..00000000 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponent.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import lombok.Getter; - -import java.util.List; - -@Getter -public class MoneyDashboardComponent extends DashboardComponent { - private final String questionId; - private long peopleCount; - private long average; - private long entireAverage; - - public MoneyDashboardComponent(List statistics, long entireAverage) { - super(DashboardType.MONEY); - this.entireAverage = entireAverage; - this.questionId = statistics.get(0).getQuestionId(); - - calculate(statistics); - } - - @Override - public void calculate(List statistics) { - AverageStatistic money = (AverageStatistic) statistics.get(0); - this.peopleCount = money.getTotalCount(); - this.average = money.getAverage(); - } - -} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java deleted file mode 100644 index 28e06c99..00000000 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/SadDashboardComponent.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import lombok.Getter; - -import java.util.List; - -@Getter -public class SadDashboardComponent extends DashboardComponent { - private final String questionId; - private List rank; - - public SadDashboardComponent(List statistics) { - super(DashboardType.SAD); - this.questionId = statistics.get(0).getQuestionId(); - - calculate(statistics); - } - - @Override - public void calculate(List statistics) { - RatioStatistic sad = (RatioStatistic) statistics.get(0); - Long totalCount = sad.getTotalCount(); - this.rank = sad.getLegends().stream() - .map(legend -> { - if (totalCount == 0) { - return new RatioDto(legend.getText(), 0); - } - int percentage = (int) (legend.getCount() * 100 / totalCount); - return new RatioDto(legend.getText(), percentage); - }) - .sorted((a, b) -> b.getPercentage() - a.getPercentage()) - .toList(); - } - -} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index 042e806c..c20b40e8 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -37,12 +37,6 @@ public void updateStatistics(List answers) { }); } - public List getStatisticsByDashboardType(DashboardType dashboardType) { - return statistics.values().stream() - .filter(statistic -> statistic.getDashboardType().equals(dashboardType)) - .toList(); - } - public Map> mapStatisticsByDashboardType() { Map> statistics = new HashMap<>(); this.statistics.values().forEach(statistic -> { diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index f8b91a77..89d14749 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -1,14 +1,11 @@ package com.dnd.namuiwiki.domain.dashboard.model.entity; import com.dnd.namuiwiki.common.model.BaseTimeEntity; -import com.dnd.namuiwiki.domain.dashboard.model.AverageDashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.BinaryDashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; -import com.dnd.namuiwiki.domain.dashboard.model.RatioDashboardComponent; +import com.dnd.namuiwiki.domain.dashboard.model.DashboardFactory; import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import com.dnd.namuiwiki.domain.survey.type.Period; @@ -44,15 +41,14 @@ public void updateStatistics(List answer) { } public DashboardDto convertDashboardDto(PopulationStatistic populationStatistic) { - BorrowingLimitEntireStatistic statistic = (BorrowingLimitEntireStatistic) populationStatistic.getStatistic(); - long entireAverage = statistic.getBorrowingMoneyLimitEntireAverage(); + var dashboardTypeListMap = statistics.mapStatisticsByDashboardType(); List dashboardComponents = List.of( - new RatioDashboardComponent(DashboardType.BEST_WORTH, statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), - new RatioDashboardComponent(DashboardType.HAPPY, statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), - new RatioDashboardComponent(DashboardType.SAD, statistics.getStatisticsByDashboardType(DashboardType.BEST_WORTH)), - new BinaryDashboardComponent(DashboardType.CHARACTER, statistics.getStatisticsByDashboardType(DashboardType.CHARACTER)), - new AverageDashboardComponent(DashboardType.MONEY, statistics.getStatisticsByDashboardType(DashboardType.MONEY), entireAverage) + DashboardFactory.create(DashboardType.BEST_WORTH, dashboardTypeListMap.get(DashboardType.BEST_WORTH)), + DashboardFactory.create(DashboardType.SAD, dashboardTypeListMap.get(DashboardType.SAD)), + DashboardFactory.create(DashboardType.HAPPY, dashboardTypeListMap.get(DashboardType.HAPPY)), + DashboardFactory.create(DashboardType.CHARACTER, dashboardTypeListMap.get(DashboardType.CHARACTER)), + DashboardFactory.create(DashboardType.MONEY, dashboardTypeListMap.get(DashboardType.MONEY), populationStatistic) ); return new DashboardDto(dashboardComponents); } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java new file mode 100644 index 00000000..a4a2b605 --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java @@ -0,0 +1,5 @@ +package com.dnd.namuiwiki.domain.dashboard.type; + +public enum DashboardStatisticType { + RATIO, AVERAGE, BINARY; +} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java index 5319fab0..6e4122e9 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java @@ -4,20 +4,26 @@ public enum DashboardType { - BEST_WORTH(StatisticsType.RATIO), - CHARACTER(StatisticsType.RATIO), - MONEY(StatisticsType.AVERAGE), - HAPPY(StatisticsType.RATIO), - SAD(StatisticsType.RATIO), - NONE(StatisticsType.NONE); - - private DashboardType(StatisticsType statisticsType) { + BEST_WORTH(StatisticsType.RATIO, DashboardStatisticType.RATIO), + CHARACTER(StatisticsType.RATIO, DashboardStatisticType.BINARY), + MONEY(StatisticsType.AVERAGE, DashboardStatisticType.AVERAGE), + HAPPY(StatisticsType.RATIO, DashboardStatisticType.RATIO), + SAD(StatisticsType.RATIO, DashboardStatisticType.RATIO); + + private DashboardType(StatisticsType statisticsType, DashboardStatisticType dashboardStatisticType) { this.statisticsType = statisticsType; + this.dashboardStatisticType = dashboardStatisticType; } private final StatisticsType statisticsType; + private final DashboardStatisticType dashboardStatisticType; public StatisticsType getStatisticsType() { return statisticsType; } + + public DashboardStatisticType getDashboardStatisticType() { + return dashboardStatisticType; + } + } diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java deleted file mode 100644 index e91100dc..00000000 --- a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/BestWorthDashboardComponentTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.question.type.QuestionName; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(MockitoExtension.class) -class BestWorthDashboardComponentTest { - @Test - @DisplayName("범례별 비율을 계산하여 비율이 높은 순서대로 정렬하여 반환한다.") - void convertStatisticsToDashboardComponentDto() { - // given - long totalCount = 5; - long opt1Count = 3; - long opt2Count = 2; - Statistic statistic = new RatioStatistic( - "questionId", - QuestionName.CORE_VALUE, - DashboardType.BEST_WORTH, - totalCount, - Map.of("op1", new Legend("op1", "legend1", "legend1", opt1Count), - "op2", new Legend("op2", "legend2", "legend2", opt2Count))); - - // when - BestWorthDashboardComponent bestWorthDashboardComponent = new BestWorthDashboardComponent(List.of(statistic)); - - // then - int expectedOpt1Percentage = 60; - int expectedOpt2Percentage = 40; - assertThat(bestWorthDashboardComponent.getRank().stream().mapToInt(RatioDto::getPercentage)) - .containsExactly(expectedOpt1Percentage, expectedOpt2Percentage); - - } -} diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java deleted file mode 100644 index 58f4f2b0..00000000 --- a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/CharacterDashboardComponentTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.question.type.QuestionName; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(MockitoExtension.class) -class CharacterDashboardComponentTest { - @Test - @DisplayName("질문마다 범례별 비율을 계산하여 true인 값이 많을 경우 character value를 true로 반환한다.") - void convertStatisticsToDashboardComponentDto() { - // given - long totalCount = 5; - - Legend legend1 = new Legend("op1", "legend1", true, 3L); - Legend legend2 = new Legend("op2", "legend2", false, 2L); - - Legend legend3 = new Legend("op3", "legend3", true, 0L); - Legend legend4 = new Legend("op4", "legend4", false, 5L); - - Statistic statistic1 = new RatioStatistic( - "questionId", - QuestionName.MBTI_IMMERSION, - DashboardType.CHARACTER, - totalCount, - Map.of("op1", legend1, "op2", legend2)); - Statistic statistic2 = new RatioStatistic( - "questionId", - QuestionName.FRIENDLINESS_LEVEL, - DashboardType.CHARACTER, - totalCount, - Map.of("op3", legend3, "op4", legend4)); - - // when - CharacterDashboardComponent characterDashboardComponent = new CharacterDashboardComponent(List.of(statistic1, statistic2)); - - // then - boolean statistic1Result = true; - boolean statistic2Result = false; - assertThat(characterDashboardComponent.getCharacters().stream().map(CharacterDashboardComponent.Character::isValue)) - .containsExactly(statistic1Result, statistic2Result); - - } -} diff --git a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java b/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java deleted file mode 100644 index 02378d98..00000000 --- a/src/test/java/com/dnd/namuiwiki/domain/dashboard/model/MoneyDashboardComponentTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.dnd.namuiwiki.domain.dashboard.model; - -import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; -import com.dnd.namuiwiki.domain.question.type.QuestionName; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(MockitoExtension.class) -class MoneyDashboardComponentTest { - @Test - @DisplayName("AverageStatistic에 설정된 평균 값을 반환한다.") - void convertStatisticsToDashboardComponentDto() { - // given - long totalCount = 5; - long average = 1000; - long entireAverage = 2000; - - Statistic statistic = new AverageStatistic( - "questionId", - QuestionName.BORROWING_LIMIT, - DashboardType.MONEY, - totalCount, - average); - - // when - MoneyDashboardComponent moneyDashboardComponent = new MoneyDashboardComponent(List.of(statistic), entireAverage); - - // then - long expectedAverage = 1000; - long expectedEntireAverage = 2000; - assertThat(moneyDashboardComponent.getEntireAverage()).isEqualTo(expectedEntireAverage); - assertThat(moneyDashboardComponent.getAverage()).isEqualTo(expectedAverage); - - } -} From 360adf308228b3d8824b2e0f628e02f33a6680c1 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:30:58 +0900 Subject: [PATCH 16/22] =?UTF-8?q?refactor:=20BorrowingLimitEntireStatistic?= =?UTF-8?q?=EC=9D=84=20AverageEntireStatistic=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/statistic/StatisticsService.java | 6 +++--- ...eStatistic.java => AverageEntireStatistic.java} | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) rename src/main/java/com/dnd/namuiwiki/domain/statistic/model/{BorrowingLimitEntireStatistic.java => AverageEntireStatistic.java} (50%) 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 00f5bc31..e97034ea 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -4,7 +4,7 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorType; import com.dnd.namuiwiki.domain.option.entity.Option; import com.dnd.namuiwiki.domain.question.type.QuestionName; -import com.dnd.namuiwiki.domain.statistic.model.BorrowingLimitEntireStatistic; +import com.dnd.namuiwiki.domain.statistic.model.AverageEntireStatistic; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import com.dnd.namuiwiki.domain.survey.model.entity.Survey; @@ -35,7 +35,7 @@ public PopulationStatistic getPopulationStatistic(Period period, Relation relati return statisticsRepository .findByPeriodAndRelationAndQuestionName(period, relation, questionName) .orElseGet(() -> PopulationStatistic.builder() - .statistic(new BorrowingLimitEntireStatistic(0L, 0L)) + .statistic(new AverageEntireStatistic(0L, 0L)) .period(period) .questionName(questionName) .relation(relation) @@ -64,7 +64,7 @@ private void updateBorrowingLimitStatistic(Period period, Relation relation, Lis private void updateBorrowingLimitStatisticByCategory(Period period, Relation relation, long borrowingLimit) { PopulationStatistic populationStatistic = getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); - BorrowingLimitEntireStatistic statistic = (BorrowingLimitEntireStatistic) populationStatistic.getStatistic(); + AverageEntireStatistic statistic = (AverageEntireStatistic) populationStatistic.getStatistic(); statistic.updateStatistic(String.valueOf(borrowingLimit)); populationStatistic.setStatistic(statistic); diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/BorrowingLimitEntireStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java similarity index 50% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/model/BorrowingLimitEntireStatistic.java rename to src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java index 12304d16..3e1d6cd2 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/BorrowingLimitEntireStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/AverageEntireStatistic.java @@ -6,27 +6,27 @@ @Getter @AllArgsConstructor -public class BorrowingLimitEntireStatistic implements EntireStatistic { +public class AverageEntireStatistic implements EntireStatistic { - private long borrowingMoneyLimitEntireAverage; + private long entireAverage; private long peopleCount; @Override public void updateStatistic(String... args) { - long borrowingLimit = Long.parseLong(args[0]); - long newAverage = ArithmeticUtils.calculateAverage(peopleCount, borrowingMoneyLimitEntireAverage, borrowingLimit); + long newValue = Long.parseLong(args[0]); + long newAverage = ArithmeticUtils.calculateAverage(peopleCount, entireAverage, newValue); increasePeopleCount(); - setBorrowingMoneyLimitEntireAverage(newAverage); + setEntireAverage(newAverage); } private void increasePeopleCount() { peopleCount++; } - private void setBorrowingMoneyLimitEntireAverage(long borrowingMoneyLimitEntireAverage) { - this.borrowingMoneyLimitEntireAverage = borrowingMoneyLimitEntireAverage; + private void setEntireAverage(long borrowingMoneyLimitEntireAverage) { + this.entireAverage = borrowingMoneyLimitEntireAverage; } } From d95bc0e65cbd8ea94c6f75112b8968ada9298661 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:48:19 +0900 Subject: [PATCH 17/22] =?UTF-8?q?rename:=20StatisticsType=20->=20Statistic?= =?UTF-8?q?sCalculationType=20=EC=9C=BC=EB=A1=9C=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 2 +- .../domain/dashboard/model/Statistic.java | 6 ++--- .../domain/dashboard/model/Statistics.java | 8 +++---- .../domain/dashboard/type/DashboardType.java | 22 +++++++++---------- .../domain/statistic/StatisticsService.java | 2 +- ...pe.java => StatisticsCalculationType.java} | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) rename src/main/java/com/dnd/namuiwiki/domain/statistic/type/{StatisticsType.java => StatisticsCalculationType.java} (79%) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index 58a6c85f..11b16ddb 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -58,7 +58,7 @@ public void updateStatistics(Survey survey) { Relation relation = survey.getRelation(); var statisticalAnswers = survey.getAnswers().stream() - .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsType().isNotNone()) + .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsCalculationType().isNotNone()) .toList(); updateDashboards(owner, period, relation, statisticalAnswers); diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java index 1db6c9a6..650cc2b6 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistic.java @@ -5,7 +5,7 @@ import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.question.type.QuestionName; -import com.dnd.namuiwiki.domain.statistic.type.StatisticsType; +import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import lombok.AllArgsConstructor; import lombok.Getter; @@ -22,8 +22,8 @@ public Long increaseTotalCount() { return ++totalCount; } - public static Statistic create(Question question, StatisticsType statisticsType) { - switch (statisticsType) { + public static Statistic create(Question question, StatisticsCalculationType statisticsCalculationType) { + switch (statisticsCalculationType) { case RATIO: return RatioStatistic.create(question); case AVERAGE: diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index c20b40e8..7527c110 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -2,7 +2,7 @@ import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.question.entity.Question; -import com.dnd.namuiwiki.domain.statistic.type.StatisticsType; +import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; @@ -18,8 +18,8 @@ public class Statistics { private Map statistics; public Statistic createAndPut(Question question) { - StatisticsType statisticsType = question.getDashboardType().getStatisticsType(); - Statistic statistic = Statistic.create(question, statisticsType); + StatisticsCalculationType statisticsCalculationType = question.getDashboardType().getStatisticsCalculationType(); + Statistic statistic = Statistic.create(question, statisticsCalculationType); statistics.put(question.getId(), statistic); return statistic; } @@ -51,7 +51,7 @@ public Map> mapStatisticsByDashboardType() { public static Statistics from(List questions) { Statistics statistics = new Statistics(new HashMap<>()); questions.stream() - .filter(question -> question.getDashboardType().getStatisticsType().isNotNone()) + .filter(question -> question.getDashboardType().getStatisticsCalculationType().isNotNone()) .forEach(statistics::createAndPut); return statistics; } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java index 6e4122e9..3686e326 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java @@ -1,25 +1,25 @@ package com.dnd.namuiwiki.domain.dashboard.type; -import com.dnd.namuiwiki.domain.statistic.type.StatisticsType; +import com.dnd.namuiwiki.domain.statistic.type.StatisticsCalculationType; public enum DashboardType { - BEST_WORTH(StatisticsType.RATIO, DashboardStatisticType.RATIO), - CHARACTER(StatisticsType.RATIO, DashboardStatisticType.BINARY), - MONEY(StatisticsType.AVERAGE, DashboardStatisticType.AVERAGE), - HAPPY(StatisticsType.RATIO, DashboardStatisticType.RATIO), - SAD(StatisticsType.RATIO, DashboardStatisticType.RATIO); + BEST_WORTH(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO), + CHARACTER(StatisticsCalculationType.RATIO, DashboardStatisticType.BINARY), + MONEY(StatisticsCalculationType.AVERAGE, DashboardStatisticType.AVERAGE), + HAPPY(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO), + SAD(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO); - private DashboardType(StatisticsType statisticsType, DashboardStatisticType dashboardStatisticType) { - this.statisticsType = statisticsType; + private DashboardType(StatisticsCalculationType statisticsCalculationType, DashboardStatisticType dashboardStatisticType) { + this.statisticsCalculationType = statisticsCalculationType; this.dashboardStatisticType = dashboardStatisticType; } - private final StatisticsType statisticsType; + private final StatisticsCalculationType statisticsCalculationType; private final DashboardStatisticType dashboardStatisticType; - public StatisticsType getStatisticsType() { - return statisticsType; + public StatisticsCalculationType getStatisticsCalculationType() { + return statisticsCalculationType; } public DashboardStatisticType getDashboardStatisticType() { 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 e97034ea..1ec2d994 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/StatisticsService.java @@ -25,7 +25,7 @@ public void updateStatistics(Survey survey) { Relation relation = survey.getRelation(); var statisticalAnswers = survey.getAnswers().stream() - .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsType().isNotNone()) + .filter(answer -> answer.getQuestion().getDashboardType().getStatisticsCalculationType().isNotNone()) .toList(); updateBorrowingLimitStatistic(period, relation, statisticalAnswers); diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsType.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java similarity index 79% rename from src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsType.java rename to src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java index 0fcd5cb1..bd8a742e 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/type/StatisticsCalculationType.java @@ -1,6 +1,6 @@ package com.dnd.namuiwiki.domain.statistic.type; -public enum StatisticsType { +public enum StatisticsCalculationType { RATIO, AVERAGE, From 82e60ecbde000f5faeb292b21eca8fdb18832454 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 20:53:37 +0900 Subject: [PATCH 18/22] =?UTF-8?q?rename:=20DashboareType=EC=97=90=20Analys?= =?UTF-8?q?isType=20=EC=B6=94=EA=B0=80(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/type/AnalysisType.java | 13 ++++++++++ .../domain/dashboard/type/DashboardType.java | 24 +++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java new file mode 100644 index 00000000..caf75a40 --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java @@ -0,0 +1,13 @@ +package com.dnd.namuiwiki.domain.dashboard.type; + +public enum AnalysisType { + USER, POPULATION; + + public boolean isUser() { + return this == USER; + } + + public boolean isPopulation() { + return this == POPULATION; + } +} diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java index 3686e326..93afe76c 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java @@ -4,19 +4,25 @@ public enum DashboardType { - BEST_WORTH(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO), - CHARACTER(StatisticsCalculationType.RATIO, DashboardStatisticType.BINARY), - MONEY(StatisticsCalculationType.AVERAGE, DashboardStatisticType.AVERAGE), - HAPPY(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO), - SAD(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO); - - private DashboardType(StatisticsCalculationType statisticsCalculationType, DashboardStatisticType dashboardStatisticType) { + BEST_WORTH(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO, AnalysisType.USER), + CHARACTER(StatisticsCalculationType.RATIO, DashboardStatisticType.BINARY, AnalysisType.USER), + MONEY(StatisticsCalculationType.AVERAGE, DashboardStatisticType.AVERAGE, AnalysisType.POPULATION), + HAPPY(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO, AnalysisType.USER), + SAD(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO, AnalysisType.USER); + + private DashboardType( + StatisticsCalculationType statisticsCalculationType, + DashboardStatisticType dashboardStatisticType, + AnalysisType analysisType + ) { this.statisticsCalculationType = statisticsCalculationType; this.dashboardStatisticType = dashboardStatisticType; + this.analysisType = analysisType; } private final StatisticsCalculationType statisticsCalculationType; private final DashboardStatisticType dashboardStatisticType; + private final AnalysisType analysisType; public StatisticsCalculationType getStatisticsCalculationType() { return statisticsCalculationType; @@ -26,4 +32,8 @@ public DashboardStatisticType getDashboardStatisticType() { return dashboardStatisticType; } + public AnalysisType getAnalysisType() { + return analysisType; + } + } From 32b6c779ee4281a4f54df563d49f49e000ee8a42 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 21:05:42 +0900 Subject: [PATCH 19/22] =?UTF-8?q?rename:=20Dashboard=EB=A5=BC=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=EC=99=80=20=EC=A0=84=EC=B2=B4=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EC=97=AC=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 16 +++++++++++--- .../domain/dashboard/model/Statistics.java | 6 +++++ .../dashboard/model/entity/Dashboard.java | 22 ++++++++++--------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index 11b16ddb..dd306bc0 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -2,8 +2,10 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; +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; @@ -17,6 +19,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -31,13 +34,20 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio validateFilterCategory(period, relation); User user = findByWikiId(tokenUserInfoDto.getWikiId()); - Optional dashboard = dashboardRepository.findByUserAndPeriodAndRelation(user, period, relation); - if (dashboard.isEmpty()) { + Optional optionalDashboard = dashboardRepository.findByUserAndPeriodAndRelation(user, period, relation); + if (optionalDashboard.isEmpty()) { return null; } + Dashboard dashboard = optionalDashboard.get(); + List userDashboards = dashboard.getUserDashboards(); PopulationStatistic populationStatistic = statisticsService.getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); - return dashboard.get().convertDashboardDto(populationStatistic); + DashboardComponent populationDashboard = dashboard.getPopulationDashboard(populationStatistic, DashboardType.MONEY); + + List components = new ArrayList<>(userDashboards); + components.add(populationDashboard); + + return new DashboardDto(components); } private void validateFilterCategory(Period period, Relation relation) { diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index 7527c110..1b14c37d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -37,6 +37,12 @@ public void updateStatistics(List answers) { }); } + public List getStatisticsByDashboardType(DashboardType dashboardType) { + return statistics.values().stream() + .filter(statistic -> statistic.getDashboardType().equals(dashboardType)) + .toList(); + } + public Map> mapStatisticsByDashboardType() { Map> statistics = new HashMap<>(); this.statistics.values().forEach(statistic -> { diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index 89d14749..d706245d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -3,8 +3,8 @@ import com.dnd.namuiwiki.common.model.BaseTimeEntity; import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.DashboardFactory; +import com.dnd.namuiwiki.domain.dashboard.model.Statistic; import com.dnd.namuiwiki.domain.dashboard.model.Statistics; -import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.statistic.model.entity.PopulationStatistic; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; @@ -40,17 +40,19 @@ public void updateStatistics(List answer) { statistics.updateStatistics(answer); } - public DashboardDto convertDashboardDto(PopulationStatistic populationStatistic) { + public List getUserDashboards() { var dashboardTypeListMap = statistics.mapStatisticsByDashboardType(); - List dashboardComponents = List.of( - DashboardFactory.create(DashboardType.BEST_WORTH, dashboardTypeListMap.get(DashboardType.BEST_WORTH)), - DashboardFactory.create(DashboardType.SAD, dashboardTypeListMap.get(DashboardType.SAD)), - DashboardFactory.create(DashboardType.HAPPY, dashboardTypeListMap.get(DashboardType.HAPPY)), - DashboardFactory.create(DashboardType.CHARACTER, dashboardTypeListMap.get(DashboardType.CHARACTER)), - DashboardFactory.create(DashboardType.MONEY, dashboardTypeListMap.get(DashboardType.MONEY), populationStatistic) - ); - return new DashboardDto(dashboardComponents); + return dashboardTypeListMap.entrySet().stream() + .filter(entry -> entry.getKey().getAnalysisType().isUser()) + .map(entry -> DashboardFactory.create(entry.getKey(), entry.getValue())) + .toList(); + } + + public DashboardComponent getPopulationDashboard(PopulationStatistic populationStatistic, DashboardType dashboardType) { + List statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType); + + return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic); } public static Dashboard createNew(User owner, Period period, Relation relation, List answers) { From e767efcf2569b11dcebf218baf4dc733fa3a9151 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Thu, 7 Mar 2024 00:34:07 +0900 Subject: [PATCH 20/22] =?UTF-8?q?rename:=20Option=EC=9D=84=20=ED=86=B5?= =?UTF-8?q?=ED=95=B4=20Legend=20=EC=83=9D=EC=84=B1=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dnd/namuiwiki/domain/dashboard/model/Legend.java | 5 +++++ .../dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java index 8bea0e02..bfec7a6d 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Legend.java @@ -1,5 +1,6 @@ package com.dnd.namuiwiki.domain.dashboard.model; +import com.dnd.namuiwiki.domain.option.entity.Option; import lombok.AllArgsConstructor; import lombok.Getter; @@ -14,4 +15,8 @@ public class Legend { public Long increaseCount() { return ++count; } + + public static Legend from(Option option) { + return new Legend(option.getId(), option.getText(), option.getValue(), 0L); + } } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java index bdb02f2b..416e86a6 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioStatistic.java @@ -37,7 +37,7 @@ public List getLegends() { protected static RatioStatistic create(Question question) { Map legends = new HashMap<>(); - question.getOptions().forEach((key, value) -> legends.put(key, new Legend(key, value.getText(), value.getValue(), 0L))); + question.getOptions().forEach((key, value) -> legends.put(key, Legend.from(value))); return new RatioStatistic( question.getId(), question.getName(), @@ -56,7 +56,7 @@ public void updateStatistic(Answer answer) { .orElseGet(() -> { Option option = question.getOption(optionId) .orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_OPTION_ID)); - return new Legend(option.getId(), option.getText(), option.getValue(), 0L); + return Legend.from(option); }); legend.increaseCount(); } From 7e8cc34b853f2442168fcc533cbbcbd5692599ff Mon Sep 17 00:00:00 2001 From: eun-seong Date: Thu, 7 Mar 2024 02:02:17 +0900 Subject: [PATCH 21/22] =?UTF-8?q?fix:=20DashboareType=EC=97=90=20NONE=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java | 2 +- .../domain/dashboard/type/DashboardStatisticType.java | 2 +- .../com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java index caf75a40..e788e3ad 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/AnalysisType.java @@ -1,7 +1,7 @@ package com.dnd.namuiwiki.domain.dashboard.type; public enum AnalysisType { - USER, POPULATION; + USER, POPULATION, NONE; public boolean isUser() { return this == USER; diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java index a4a2b605..cb5d2738 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardStatisticType.java @@ -1,5 +1,5 @@ package com.dnd.namuiwiki.domain.dashboard.type; public enum DashboardStatisticType { - RATIO, AVERAGE, BINARY; + RATIO, AVERAGE, BINARY, NONE; } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java index 93afe76c..49415667 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/type/DashboardType.java @@ -8,7 +8,8 @@ public enum DashboardType { CHARACTER(StatisticsCalculationType.RATIO, DashboardStatisticType.BINARY, AnalysisType.USER), MONEY(StatisticsCalculationType.AVERAGE, DashboardStatisticType.AVERAGE, AnalysisType.POPULATION), HAPPY(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO, AnalysisType.USER), - SAD(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO, AnalysisType.USER); + SAD(StatisticsCalculationType.RATIO, DashboardStatisticType.RATIO, AnalysisType.USER), + NONE(StatisticsCalculationType.NONE, DashboardStatisticType.NONE, AnalysisType.NONE); private DashboardType( StatisticsCalculationType statisticsCalculationType, From 3069d783c93be0afbf5d86c6d0179d62a1d8a158 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Thu, 7 Mar 2024 02:17:49 +0900 Subject: [PATCH 22/22] =?UTF-8?q?fix:=20Map=20value=EC=97=90=20=ED=95=AD?= =?UTF-8?q?=EC=83=81=20=EC=83=88=EB=A1=9C=EC=9A=B4=20List=20=EB=93=A4?= =?UTF-8?q?=EC=96=B4=EA=B0=80=EB=8D=98=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dnd/namuiwiki/domain/dashboard/model/Statistics.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index 1b14c37d..b0372bcd 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -7,6 +7,7 @@ import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -47,8 +48,8 @@ public Map> mapStatisticsByDashboardType() { Map> statistics = new HashMap<>(); this.statistics.values().forEach(statistic -> { DashboardType dashboardType = statistic.getDashboardType(); - statistics.putIfAbsent(dashboardType, List.of()); - statistics.put(dashboardType, List.of(statistic)); + statistics.putIfAbsent(dashboardType, new ArrayList<>()); + statistics.get(dashboardType).add(statistic); }); return statistics;