-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from dnd-side-project/dev
λμ보λ νμ ν΅μΌ
- Loading branch information
Showing
8 changed files
with
152 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RankDashboardComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/dnd/namuiwiki/domain/dashboard/model/dto/RankDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/dnd/namuiwiki/domain/statistic/model/RankStatistic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ public enum StatisticsType { | |
|
||
RATIO, | ||
AVERAGE, | ||
RANK, | ||
NONE; | ||
|
||
public boolean isNotNone() { | ||
|