Skip to content

Commit

Permalink
Merge pull request #182 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
λŒ€μ‹œλ³΄λ“œ νƒ€μž… 톡일
  • Loading branch information
eun-seong authored Aug 13, 2024
2 parents 476aa70 + 77ec33b commit b56b133
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.dnd.namuiwiki.domain.dashboard.model.AverageDashboardComponent;
import com.dnd.namuiwiki.domain.dashboard.model.BinaryDashboardComponent;
import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponentV2;
import com.dnd.namuiwiki.domain.dashboard.model.RankDashboardComponent;
import com.dnd.namuiwiki.domain.dashboard.model.RatioDashboardComponent;
import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto;
import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard;
Expand All @@ -27,7 +28,6 @@

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
Expand All @@ -42,53 +42,32 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, WikiType wik

User user = findByWikiId(tokenUserInfoDto.getWikiId());
Optional<Dashboard> dashboard = dashboardRepository.findByUserAndWikiTypeAndPeriodAndRelation(user, wikiType, period, relation);
if (dashboard.isEmpty()) {
return null;
}


if (wikiType.isNamui()) {
Stream<DashboardType> namuiDashboardTypes = Stream.of(
DashboardType.BEST_WORTH,
DashboardType.MONEY,
DashboardType.HAPPY,
DashboardType.SAD,
DashboardType.CHARACTER);
return convertToDto(namuiDashboardTypes, dashboard, period, relation);
} else if (wikiType.isRomance()) {
Stream<DashboardType> romanceDashboardTypes = Stream.of(
DashboardType.BUBBLE_CHART,
DashboardType.BAR_CHART,
DashboardType.BINARY);
return convertToDto(romanceDashboardTypes, dashboard, period, relation);
} else {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_WIKI_TYPE);
}
return dashboard.map(value -> convertToDto(value.getStatistics(), period, relation)).orElse(null);
}

private DashboardDto convertToDto(Stream<DashboardType> namuiDashboardTypes, Optional<Dashboard> dashboard, Period period, Relation relation) {
private DashboardDto convertToDto(Statistics statistics, Period period, Relation relation) {
List<Question> questions = questionRepository.findAll();

Statistics statistics = dashboard.get().getStatistics();
List<DashboardComponentV2> dashboardComponents = namuiDashboardTypes.flatMap(
dashboardType -> statistics.getStatisticsByDashboardType(dashboardType).stream()
.map(statistic -> {
Question question = questions.stream()
.filter(q -> q.getId().equals(statistic.getQuestionId()))
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_QUESTION_ID));
if (dashboardType.isBinaryType()) {
return new BinaryDashboardComponent(statistic, question);
} else if (dashboardType.isAverageType()) {
long entireAverage = getEntireAverage(period, relation, question.getName());
return new AverageDashboardComponent(dashboardType, statistic, entireAverage, question);
} else if (dashboardType.isRatioType()) {
return new RatioDashboardComponent(dashboardType, statistic, question);
} else {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_DASHBOARD_TYPE);
}
}))
.toList();
List<DashboardComponentV2> dashboardComponents = statistics.get().stream()
.map(statistic -> {
Question question = questions.stream()
.filter(q -> q.getId().equals(statistic.getQuestionId()))
.findFirst()
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INVALID_QUESTION_ID));
DashboardType dashboardType = statistic.getDashboardType();
if (dashboardType.isBinaryType()) {
return new BinaryDashboardComponent(statistic, question);
} else if (dashboardType.isAverageType()) {
long entireAverage = getEntireAverage(period, relation, question.getName());
return new AverageDashboardComponent(dashboardType, statistic, entireAverage, question);
} else if (dashboardType.isRatioType()) {
return new RatioDashboardComponent(dashboardType, statistic, question);
} else if (dashboardType.isRankType()) {
return new RankDashboardComponent(dashboardType, statistic, question);
} else {
throw new ApplicationErrorException(ApplicationErrorType.INVALID_DASHBOARD_TYPE);
}
}).toList();

return new DashboardDto(dashboardComponents);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dnd.namuiwiki.domain.dashboard.model;

import com.dnd.namuiwiki.domain.dashboard.model.dto.RankDto;
import com.dnd.namuiwiki.domain.dashboard.type.DashboardType;
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.statistic.model.RankStatistic;
import com.dnd.namuiwiki.domain.statistic.model.Statistic;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class RankDashboardComponent extends DashboardComponentV2 {
private final List<RankDto> rank;

public RankDashboardComponent(DashboardType dashboardType, Statistic statistic, Question question) {
super(dashboardType, question.getId(), question.getTitle(), question.getName());

if (!dashboardType.isRankType()) {
throw new IllegalArgumentException("Required RankDashboardType");
}

this.rank = getRank((RankStatistic) statistic);
}

private List<RankDto> getRank(RankStatistic rankStatistic) {
List<RankDto> rankList = new ArrayList<>(rankStatistic.getRanks().values().stream().toList());
rankList.sort((o1, o2) -> Double.compare(o2.getPercentage(), o1.getPercentage()));
return rankList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dnd.namuiwiki.domain.dashboard.model.dto;

import com.dnd.namuiwiki.domain.option.entity.Option;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class RankDto {
private String text;
private int point;
private int percentage;

public static RankDto optionToRankDto(Option option) {
return new RankDto(option.getText(), 0, 0);
}

public void addPoint(int point) {
this.point += point;
}

public void updatePercentage(int percentage) {
this.percentage = percentage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum DashboardType {
AVERAGE_BAR_CHART(StatisticsType.AVERAGE),
HAPPY(StatisticsType.RATIO),
SAD(StatisticsType.RATIO),
RANK(StatisticsType.RANK),
NONE(StatisticsType.NONE),
;

Expand All @@ -36,4 +37,8 @@ public boolean isBinaryType() {
public boolean isAverageType() {
return this.statisticsType == StatisticsType.AVERAGE || this == MONEY;
}

public boolean isRankType() {
return this == RANK;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.dnd.namuiwiki.domain.statistic.model;

import com.dnd.namuiwiki.domain.dashboard.model.dto.RankDto;
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.survey.model.entity.Answer;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
public class RankStatistic extends Statistic {
private final Map<String, RankDto> ranks;

public RankStatistic(String questionId, QuestionName questionName, DashboardType dashboardType, Long totalCount, Map<String, RankDto> ranks) {
super(questionId, questionName, dashboardType, totalCount);
this.ranks = ranks;
}

public static RankStatistic create(Question question) {
Map<String, RankDto> rankMap = question.getOptions().values().stream()
.collect(Collectors.toMap(Option::getId, RankDto::optionToRankDto));
return new RankStatistic(
question.getId(),
question.getName(),
question.getDashboardType(),
0L,
rankMap);
}

@Override
public void updateStatistic(Answer answer) {
increaseTotalCount();

if (answer.getType().isOptionList()) {
updateRankMap(answer);
}
}

private void updateRankMap(Answer answer) {
// rankMap point update
List<String> answerList = (List<String>) answer.getAnswer();
for (int i = 0; i < 5; i++) {
int point = 5 - i;
String optionId = answerList.get(i);
ranks.get(optionId).addPoint(point);
}

// rankMap percentage update
long totalPoint = this.totalCount * (5 + 4 + 3 + 2 + 1);
ranks.forEach((key, value) -> {
int percentage = (int) (value.getPoint() / totalPoint);
value.updatePercentage(percentage);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static Statistic create(Question question, StatisticsType statisticsType)
return RatioStatistic.create(question);
case AVERAGE:
return AverageStatistic.create(question);
case RANK:
return RankStatistic.create(question);
default:
throw new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR, "Invalid statistics type");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
public class Statistics {
private Map<String, Statistic> statistics;

public List<Statistic> get() {
return statistics.values().stream().toList();
}

public Statistic createAndPut(Question question) {
StatisticsType statisticsType = question.getDashboardType().getStatisticsType();
Statistic statistic = Statistic.create(question, statisticsType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum StatisticsType {

RATIO,
AVERAGE,
RANK,
NONE;

public boolean isNotNone() {
Expand Down

0 comments on commit b56b133

Please sign in to comment.