Skip to content

Commit

Permalink
Merge pull request #206 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
fix: legend에서 option text, value 삭제
  • Loading branch information
rlacksgus97 authored Sep 30, 2024
2 parents 7661721 + 8cb2c4c commit 947633f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
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.option.entity.Option;
import com.dnd.namuiwiki.domain.question.entity.Question;
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 lombok.Getter;

import java.util.Map;

@Getter
public class BinaryDashboardComponent extends DashboardComponentV2 {
private final int percentage;
Expand All @@ -22,15 +25,21 @@ public BinaryDashboardComponent(Statistic statistic, Question question) {

RatioStatistic ratioStatistic = (RatioStatistic) statistic;

this.percentage = getPercentage(ratioStatistic);
this.percentage = getPercentage(ratioStatistic, question);
}

private int getPercentage(RatioStatistic ratioStatistic) {
private int getPercentage(RatioStatistic ratioStatistic, Question question) {
Legend trueLegend = ratioStatistic.getLegends().stream()
.filter(legend -> (boolean) legend.getValue())
.filter(legend -> (boolean) getValue(question, legend))
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR));
return (int) (trueLegend.getCount() * 100 / ratioStatistic.getTotalCount());
}

private Object getValue(Question question, Legend legend) {
Map<String, Option> options = question.getOptions();
Option option = options.get(legend.getOptionId());
return option.getValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto;
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.statistic.model.Legend;
import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic;
Expand All @@ -10,6 +11,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Getter
Expand All @@ -23,10 +25,10 @@ public RatioDashboardComponent(DashboardType dashboardType, Statistic statistic,
throw new IllegalArgumentException("Required RatioDashboardType");
}

calculate((RatioStatistic) statistic);
calculate((RatioStatistic) statistic, question);
}

private void calculate(RatioStatistic statistic) {
private void calculate(RatioStatistic statistic, Question question) {
Long totalCount = statistic.getTotalCount();

List<Legend> legends = statistic.getLegends();
Expand All @@ -36,12 +38,12 @@ private void calculate(RatioStatistic statistic) {

for (Legend legend : legends) {
if (totalCount == 0) {
rank.add(new RatioDto(legend.getText(), 0));
rank.add(new RatioDto(getText(question, legend), 0));
continue;
}
int percentage = (int) (legend.getCount() * 100 / totalCount);
optionPercentage += percentage;
rank.add(new RatioDto(legend.getText(), percentage));
rank.add(new RatioDto(getText(question, legend), percentage));
}

// 직접입력인 legend 인거 찾아서 새로 업데이트
Expand All @@ -55,4 +57,10 @@ private void updateManualLegendPercentage(int optionPercentage) {
manualLegend.ifPresent(ratioDto -> ratioDto.setPercentage(100 - optionPercentage));
}

private String getText(Question question, Legend legend) {
Map<String, Option> options = question.getOptions();
Option option = options.get(legend.getOptionId());
return option.getText();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
@AllArgsConstructor
public class Legend {
private String optionId;
private String text;
private Object value;
private Long count;

public Long increaseCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<Legend> getLegends() {

public static RatioStatistic create(Question question) {
Map<String, Legend> 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, new Legend(key, 0L)));
return new RatioStatistic(
question.getId(),
question.getName(),
Expand All @@ -61,7 +61,7 @@ private void increaseOptionCount(Answer answer) {
Legend legend = getLegend(optionId)
.orElseGet(() -> {
Option option = question.getOption(optionId);
return new Legend(option.getId(), option.getText(), option.getValue(), 0L);
return new Legend(option.getId(), 0L);
});
legend.increaseCount();
}
Expand Down

0 comments on commit 947633f

Please sign in to comment.