From 5d57ab01f3270128f892fc8c3655db31f7bb1b48 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Tue, 19 Mar 2024 22:13:25 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8C=80=EC=8B=9C=EB=B3=B4=EB=93=9C=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EC=A7=81=EC=A0=91=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EC=98=B5=EC=85=98=20=ED=8D=BC=EC=84=BC=ED=8A=B8=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=EC=97=90=20=ED=8F=AC=ED=95=A8(#128)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BestWorthDashboardComponent.java | 35 +++++++++++++----- .../model/HappyDashboardComponent.java | 36 ++++++++++++++----- .../model/SadDashboardComponent.java | 36 ++++++++++++++----- .../domain/dashboard/model/dto/RatioDto.java | 6 +++- 4 files changed, 85 insertions(+), 28 deletions(-) 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..918651b3 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,11 +2,14 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.statistic.model.Legend; import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Getter public class BestWorthDashboardComponent extends DashboardComponent { @@ -25,15 +28,29 @@ public void calculate(Statistics statistics) { RatioStatistic bestWorth = (RatioStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).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); - }) - .toList(); + List legends = bestWorth.getLegends(); + int optionPercentage = 0; + + this.rank = new ArrayList<>(); + + for (Legend legend : legends) { + if (totalCount == 0) { + rank.add(new RatioDto(legend.getText(), 0)); + } + int percentage = (int) (legend.getCount() * 100 / totalCount); + optionPercentage += percentage; + rank.add(new RatioDto(legend.getText(), percentage)); + } + + // 직접입력인 legend 인거 찾아서 새로 업데이트 + updateManualLegendPercentage(optionPercentage); + } + + private void updateManualLegendPercentage(int optionPercentage) { + Optional manualLegend = this.rank.stream() + .filter(legend -> legend.getLegend().contains("직접 입력")) + .findFirst(); + manualLegend.ifPresent(ratioDto -> ratioDto.setPercentage(100 - optionPercentage)); } } 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..2663c9c0 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,11 +2,14 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.statistic.model.Legend; import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Getter public class HappyDashboardComponent extends DashboardComponent { @@ -24,15 +27,30 @@ public HappyDashboardComponent(Statistics statistics, String questionId) { public void calculate(Statistics statistics) { RatioStatistic happy = (RatioStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).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); - }) - .toList(); + + List legends = happy.getLegends(); + int optionPercentage = 0; + + this.rank = new ArrayList<>(); + + for (Legend legend : legends) { + if (totalCount == 0) { + rank.add(new RatioDto(legend.getText(), 0)); + } + int percentage = (int) (legend.getCount() * 100 / totalCount); + optionPercentage += percentage; + rank.add(new RatioDto(legend.getText(), percentage)); + } + + // 직접입력인 legend 인거 찾아서 새로 업데이트 + updateManualLegendPercentage(optionPercentage); + } + + private void updateManualLegendPercentage(int optionPercentage) { + Optional manualLegend = this.rank.stream() + .filter(legend -> legend.getLegend().contains("직접 입력")) + .findFirst(); + manualLegend.ifPresent(ratioDto -> ratioDto.setPercentage(100 - optionPercentage)); } } 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..18af7fd7 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,11 +2,14 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.statistic.model.Legend; import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; import com.dnd.namuiwiki.domain.statistic.model.Statistics; import lombok.Getter; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Getter public class SadDashboardComponent extends DashboardComponent { @@ -24,15 +27,30 @@ public SadDashboardComponent(Statistics statistics, String questionId) { public void calculate(Statistics statistics) { RatioStatistic sad = (RatioStatistic) statistics.getStatisticsByDashboardType(this.dashboardType).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); - }) - .toList(); + + List legends = sad.getLegends(); + int optionPercentage = 0; + + this.rank = new ArrayList<>(); + + for (Legend legend : legends) { + if (totalCount == 0) { + rank.add(new RatioDto(legend.getText(), 0)); + } + int percentage = (int) (legend.getCount() * 100 / totalCount); + optionPercentage += percentage; + rank.add(new RatioDto(legend.getText(), percentage)); + } + + // 직접입력인 legend 인거 찾아서 새로 업데이트 + updateManualLegendPercentage(optionPercentage); + } + + private void updateManualLegendPercentage(int optionPercentage) { + Optional manualLegend = this.rank.stream() + .filter(legend -> legend.getLegend().contains("직접 입력")) + .findFirst(); + manualLegend.ifPresent(ratioDto -> ratioDto.setPercentage(100 - optionPercentage)); } } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/dto/RatioDto.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/dto/RatioDto.java index 022bccd5..03282f9e 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/dto/RatioDto.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/dto/RatioDto.java @@ -7,5 +7,9 @@ @Getter public class RatioDto { private final String legend; - private final int percentage; + private int percentage; + + public void setPercentage(int percentage) { + this.percentage = percentage; + } }