Skip to content

Commit

Permalink
Merge pull request #259 from softwaremagico/249-global-competitor-sta…
Browse files Browse the repository at this point in the history
…tistics-takes-too-long

249 global competitor statistics takes too long
  • Loading branch information
softwaremagico authored Nov 14, 2023
2 parents d01ab15 + 76ebd72 commit 3237604
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![GitHub commit activity](https://img.shields.io/github/commit-activity/y/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager)
[![GitHub last commit](https://img.shields.io/github/last-commit/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager)
[![CircleCI](https://circleci.com/gh/softwaremagico/KendoTournamentManager.svg?style=shield)](https://circleci.com/gh/softwaremagico/KendoTournamentManager)
[![Time](https://img.shields.io/badge/development-514.5h-blueviolet.svg)]()
[![Time](https://img.shields.io/badge/development-516.5h-blueviolet.svg)]()

[![Powered by](https://img.shields.io/badge/powered%20by%20java-orange.svg?logo=OpenJDK&logoColor=white)]()
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=kendo-tournament-backend&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=kendo-tournament-backend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,10 @@ private List<Achievement> generateDarumaAchievementGold(Tournament tournament) {
* @return the generated achievements.
*/
private List<Achievement> generateStormtrooperSyndromeAchievement(Tournament tournament) {
if (tournament.getTeamSize() == 1) {
return new ArrayList<>();
}

final List<Team> teams = new ArrayList<>(getTeamsFromTournament());
//Ensure that team is in fights.
final Set<Team> teamsWithFights = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
import com.softwaremagico.kt.core.exceptions.GroupNotFoundException;
import com.softwaremagico.kt.core.providers.GroupProvider;
import com.softwaremagico.kt.core.providers.RankingProvider;
import com.softwaremagico.kt.core.providers.TournamentProvider;
import com.softwaremagico.kt.core.score.CompetitorRanking;
import com.softwaremagico.kt.persistence.entities.Group;
import com.softwaremagico.kt.persistence.entities.Team;
import com.softwaremagico.kt.persistence.entities.Tournament;
import com.softwaremagico.kt.persistence.values.ScoreType;
import com.softwaremagico.kt.persistence.values.TournamentType;
import org.springframework.cache.annotation.CacheEvict;
Expand Down Expand Up @@ -88,11 +90,13 @@ public class RankingController {

private final ScoreOfTeamConverter scoreOfTeamConverter;

private final TournamentProvider tournamentProvider;

public RankingController(GroupProvider groupProvider, GroupConverter groupConverter,
TournamentConverter tournamentConverter, FightConverter fightConverter,
TeamConverter teamConverter, DuelConverter duelConverter, ParticipantConverter participantConverter,
RankingProvider rankingProvider, ScoreOfCompetitorConverter scoreOfCompetitorConverter,
ScoreOfTeamConverter scoreOfTeamConverter) {
ScoreOfTeamConverter scoreOfTeamConverter, TournamentProvider tournamentProvider) {
this.groupProvider = groupProvider;
this.groupConverter = groupConverter;
this.tournamentConverter = tournamentConverter;
Expand All @@ -103,6 +107,7 @@ public RankingController(GroupProvider groupProvider, GroupConverter groupConver
this.rankingProvider = rankingProvider;
this.scoreOfCompetitorConverter = scoreOfCompetitorConverter;
this.scoreOfTeamConverter = scoreOfTeamConverter;
this.tournamentProvider = tournamentProvider;
}

private static Set<ParticipantDTO> getParticipants(List<TeamDTO> teams) {
Expand Down Expand Up @@ -228,13 +233,14 @@ public List<ScoreOfCompetitorDTO> getCompetitorsGlobalScoreRanking(Collection<Pa
return getCompetitorsGlobalScoreRanking(competitors, ScoreType.DEFAULT);
}

@Cacheable("competitors-ranking")

public List<ScoreOfCompetitorDTO> getCompetitorsGlobalScoreRanking(Collection<ParticipantDTO> competitors, ScoreType scoreType) {
final List<Tournament> tournaments = tournamentProvider.getAll();
return scoreOfCompetitorConverter.convertAll(rankingProvider.getCompetitorsGlobalScoreRanking(
participantConverter.reverseAll(competitors),
scoreType
)
.stream().map(ScoreOfCompetitorConverterRequest::new).toList());
.stream().map(scoreOfCompetitor -> new ScoreOfCompetitorConverterRequest(scoreOfCompetitor, tournaments)).toList());
}

@Cacheable("competitors-ranking")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ protected DuelDTO convertElement(DuelConverterRequest from) {
duelDTO.setCompetitor2(participantConverter.convert(
new ParticipantConverterRequest(from.getEntity().getCompetitor2())));
try {
//Converter can have the tournament defined already.
if (from.getTournament() != null) {
if (from.getTournamentDTO() != null) {
duelDTO.setTournament(from.getTournamentDTO());
} else if (from.getTournament() != null) {
//Converter can have the tournament defined already.
duelDTO.setTournament(tournamentConverter.convert(
new TournamentConverterRequest(from.getTournament())));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.softwaremagico.kt.core.converters.models.TournamentConverterRequest;
import com.softwaremagico.kt.core.providers.TournamentProvider;
import com.softwaremagico.kt.persistence.entities.Fight;
import com.softwaremagico.kt.persistence.entities.Tournament;
import org.hibernate.LazyInitializationException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
Expand Down Expand Up @@ -59,24 +58,32 @@ protected FightDTO convertElement(FightConverterRequest from) {
final FightDTO fightDTO = new FightDTO();
BeanUtils.copyProperties(from.getEntity(), fightDTO, ConverterUtils.getNullPropertyNames(from.getEntity()));

//Getting the tournament to send to duels and avoid hundreds of calls.
Tournament tournament;
try {
tournament = from.getEntity().getTournament();
fightDTO.setTournament(tournamentConverter.convert(new TournamentConverterRequest(tournament)));
if (from.getTournamentDTO() != null) {
fightDTO.setTournament(from.getTournamentDTO());
} else if (from.getTournament() != null) {
//Converter can have the tournament defined already.
fightDTO.setTournament(tournamentConverter.convert(
new TournamentConverterRequest(from.getTournament())));
} else {
fightDTO.setTournament(tournamentConverter.convert(
new TournamentConverterRequest(from.getEntity().getTournament())));
}
} catch (LazyInitializationException | FatalBeanException e) {
tournament = tournamentProvider.get(from.getEntity().getTournament().getId()).orElse(null);
fightDTO.setTournament(tournamentConverter.convert(new TournamentConverterRequest(tournament)));
fightDTO.setTournament(tournamentConverter.convert(
new TournamentConverterRequest(tournamentProvider.get(from.getEntity().getTournament().getId()).orElse(null))));
}

//Getting the tournament to send to duels and avoid hundreds of calls.

fightDTO.setTeam1(teamConverter.convert(
new TeamConverterRequest(from.getEntity().getTeam1(), tournament)));
new TeamConverterRequest(from.getEntity().getTeam1(), fightDTO.getTournament())));
fightDTO.setTeam2(teamConverter.convert(
new TeamConverterRequest(from.getEntity().getTeam2(), tournament)));
new TeamConverterRequest(from.getEntity().getTeam2(), fightDTO.getTournament())));

fightDTO.setDuels(new ArrayList<>());
final Tournament finalTournament = tournament;
from.getEntity().getDuels().forEach(duel -> fightDTO.getDuels().add(duelConverter.convert(new DuelConverterRequest(duel, finalTournament))));
from.getEntity().getDuels().forEach(duel -> fightDTO.getDuels().add(duelConverter.convert(
new DuelConverterRequest(duel, fightDTO.getTournament()))));
return fightDTO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ protected GroupDTO convertElement(GroupConverterRequest from) {
new TournamentConverterRequest(from.getEntity().getTournament())));
groupDTO.setFights(new ArrayList<>());
from.getEntity().getFights().forEach(fight ->
groupDTO.getFights().add(fightConverter.convert(new FightConverterRequest(fight))));
groupDTO.getFights().add(fightConverter.convert(new FightConverterRequest(fight, groupDTO.getTournament()))));
groupDTO.setUnties(new ArrayList<>());
from.getEntity().getUnties().forEach(duel ->
groupDTO.getUnties().add(duelConverter.convert(new DuelConverterRequest(duel))));
groupDTO.getUnties().add(duelConverter.convert(new DuelConverterRequest(duel, groupDTO.getTournament()))));
groupDTO.setTeams(new ArrayList<>());
from.getEntity().getTeams().forEach(team ->
groupDTO.getTeams().add(teamConverter.convert(new TeamConverterRequest(team))));
groupDTO.getTeams().add(teamConverter.convert(new TeamConverterRequest(team, groupDTO.getTournament()))));
return groupDTO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@
*/

import com.softwaremagico.kt.core.controller.models.ScoreOfCompetitorDTO;
import com.softwaremagico.kt.core.controller.models.TournamentDTO;
import com.softwaremagico.kt.core.converters.models.DuelConverterRequest;
import com.softwaremagico.kt.core.converters.models.FightConverterRequest;
import com.softwaremagico.kt.core.converters.models.ParticipantConverterRequest;
import com.softwaremagico.kt.core.converters.models.ScoreOfCompetitorConverterRequest;
import com.softwaremagico.kt.core.converters.models.TournamentConverterRequest;
import com.softwaremagico.kt.core.providers.TournamentProvider;
import com.softwaremagico.kt.core.score.ScoreOfCompetitor;
import com.softwaremagico.kt.persistence.entities.Fight;
import com.softwaremagico.kt.persistence.entities.Tournament;
import org.hibernate.LazyInitializationException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class ScoreOfCompetitorConverter extends ElementConverter<ScoreOfCompetitor, ScoreOfCompetitorDTO, ScoreOfCompetitorConverterRequest> {
Expand All @@ -41,22 +52,46 @@ public class ScoreOfCompetitorConverter extends ElementConverter<ScoreOfCompetit

private final DuelConverter duelConverter;

private final TournamentConverter tournamentConverter;

private final TournamentProvider tournamentProvider;

public ScoreOfCompetitorConverter(ParticipantConverter participantConverter, FightConverter fightConverter,
DuelConverter duelConverter) {
DuelConverter duelConverter, TournamentConverter tournamentConverter,
TournamentProvider tournamentProvider) {
this.participantConverter = participantConverter;
this.fightConverter = fightConverter;
this.duelConverter = duelConverter;
this.tournamentConverter = tournamentConverter;
this.tournamentProvider = tournamentProvider;
}

@Override
protected ScoreOfCompetitorDTO convertElement(ScoreOfCompetitorConverterRequest from) {
final ScoreOfCompetitorDTO scoreOfCompetitorDTO = new ScoreOfCompetitorDTO();
BeanUtils.copyProperties(from.getEntity(), scoreOfCompetitorDTO, ConverterUtils.getNullPropertyNames(from.getEntity()));

Set<Tournament> tournaments;
try {
if (from.getTournaments() != null) {
tournaments = new HashSet<>(from.getTournaments());
} else {
tournaments = from.getEntity().getFights().stream().map(Fight::getTournament).collect(Collectors.toSet());
}
} catch (LazyInitializationException e) {
tournaments = new HashSet<>(tournamentProvider.findByIdIn(from.getEntity().getFights().stream()
.map(fight -> fight.getTournament().getId()).collect(Collectors.toSet())));
}

final Map<Integer, TournamentDTO> tournamentDTOs = tournamentConverter.convertAll(tournaments.stream()
.map(TournamentConverterRequest::new).collect(Collectors.toList()))
.stream().collect(Collectors.toMap(TournamentDTO::getId, Function.identity()));

scoreOfCompetitorDTO.setCompetitor(participantConverter.convert(new ParticipantConverterRequest(from.getEntity().getCompetitor())));
scoreOfCompetitorDTO.setFights(fightConverter.convertAll(from.getEntity().getFights().stream()
.map(FightConverterRequest::new).toList()));
.map(fight -> new FightConverterRequest(fight, tournamentDTOs.get(fight.getTournament().getId()))).toList()));
scoreOfCompetitorDTO.setUnties(duelConverter.convertAll(from.getEntity().getUnties().stream()
.map(DuelConverterRequest::new).toList()));
.map(duel -> new DuelConverterRequest(duel, tournamentDTOs.get(duel.getTournament().getId()))).toList()));
return scoreOfCompetitorDTO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
*/

import com.softwaremagico.kt.core.controller.models.ScoreOfTeamDTO;
import com.softwaremagico.kt.core.controller.models.TournamentDTO;
import com.softwaremagico.kt.core.converters.models.DuelConverterRequest;
import com.softwaremagico.kt.core.converters.models.FightConverterRequest;
import com.softwaremagico.kt.core.converters.models.ScoreOfTeamConverterRequest;
import com.softwaremagico.kt.core.converters.models.TeamConverterRequest;
import com.softwaremagico.kt.core.converters.models.TournamentConverterRequest;
import com.softwaremagico.kt.core.score.ScoreOfTeam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
Expand All @@ -42,21 +44,27 @@ public class ScoreOfTeamConverter extends ElementConverter<ScoreOfTeam, ScoreOfT

private final DuelConverter duelConverter;

public ScoreOfTeamConverter(TeamConverter teamConverter, FightConverter fightConverter, DuelConverter duelConverter) {
private final TournamentConverter tournamentConverter;

public ScoreOfTeamConverter(TeamConverter teamConverter, FightConverter fightConverter, DuelConverter duelConverter,
TournamentConverter tournamentConverter) {
this.teamConverter = teamConverter;
this.fightConverter = fightConverter;
this.duelConverter = duelConverter;
this.tournamentConverter = tournamentConverter;
}

@Override
protected ScoreOfTeamDTO convertElement(ScoreOfTeamConverterRequest from) {
final ScoreOfTeamDTO scoreOfTeamDTO = new ScoreOfTeamDTO();
BeanUtils.copyProperties(from.getEntity(), scoreOfTeamDTO, ConverterUtils.getNullPropertyNames(from.getEntity()));
scoreOfTeamDTO.setTeam(teamConverter.convert(new TeamConverterRequest(from.getEntity().getTeam())));
final TournamentDTO tournamentDTO = tournamentConverter.convertElement(
new TournamentConverterRequest(from.getEntity().getTeam().getTournament()));
scoreOfTeamDTO.setTeam(teamConverter.convert(new TeamConverterRequest(from.getEntity().getTeam(), tournamentDTO)));
scoreOfTeamDTO.setFights(fightConverter.convertAll(from.getEntity().getFights().stream()
.map(FightConverterRequest::new).toList()));
.map(fight -> new FightConverterRequest(fight, tournamentDTO)).toList()));
scoreOfTeamDTO.setUnties(duelConverter.convertAll(from.getEntity().getUnties().stream()
.map(DuelConverterRequest::new).toList()));
.map(duel -> new DuelConverterRequest(duel, tournamentDTO)).toList()));
return scoreOfTeamDTO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ protected TeamDTO convertElement(TeamConverterRequest from) {
teamDTO.setMembers(new ArrayList<>());

try {
//Converter can have the tournament defined already.
if (from.getTournament() != null) {
if (from.getTournamentDTO() != null) {
teamDTO.setTournament(from.getTournamentDTO());
} else if (from.getTournament() != null) {
//Converter can have the tournament defined already.
teamDTO.setTournament(tournamentConverter.convert(
new TournamentConverterRequest(from.getTournament())));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,37 @@
* #L%
*/

import com.softwaremagico.kt.core.controller.models.TournamentDTO;
import com.softwaremagico.kt.persistence.entities.Duel;
import com.softwaremagico.kt.persistence.entities.Tournament;

public class DuelConverterRequest extends ConverterRequest<Duel> {
private final Tournament tournament;
private final TournamentDTO tournamentDTO;

public DuelConverterRequest(Duel entity) {
super(entity);
this.tournament = null;
this.tournamentDTO = null;
}

public DuelConverterRequest(Duel entity, Tournament tournament) {
super(entity);
this.tournament = tournament;
this.tournamentDTO = null;
}

public DuelConverterRequest(Duel entity, TournamentDTO tournamentDTO) {
super(entity);
this.tournamentDTO = tournamentDTO;
this.tournament = null;
}

public Tournament getTournament() {
return tournament;
}

public TournamentDTO getTournamentDTO() {
return tournamentDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,37 @@
* #L%
*/

import com.softwaremagico.kt.core.controller.models.TournamentDTO;
import com.softwaremagico.kt.persistence.entities.Fight;
import com.softwaremagico.kt.persistence.entities.Tournament;

public class FightConverterRequest extends ConverterRequest<Fight> {
private final Tournament tournament;
private final TournamentDTO tournamentDTO;

public FightConverterRequest(Fight entity) {
super(entity);
this.tournament = null;
this.tournamentDTO = null;
}

public FightConverterRequest(Fight entity, Tournament tournament) {
super(entity);
this.tournament = tournament;
this.tournamentDTO = null;
}

public FightConverterRequest(Fight entity, TournamentDTO tournamentDTO) {
super(entity);
this.tournamentDTO = tournamentDTO;
this.tournament = null;
}

public Tournament getTournament() {
return tournament;
}

public TournamentDTO getTournamentDTO() {
return tournamentDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,25 @@
*/

import com.softwaremagico.kt.core.score.ScoreOfCompetitor;
import com.softwaremagico.kt.persistence.entities.Tournament;

import java.util.Collection;

public class ScoreOfCompetitorConverterRequest extends ConverterRequest<ScoreOfCompetitor> {

private final Collection<Tournament> tournaments;

public ScoreOfCompetitorConverterRequest(ScoreOfCompetitor entity) {
super(entity);
this.tournaments = null;
}

public ScoreOfCompetitorConverterRequest(ScoreOfCompetitor entity, Collection<Tournament> tournaments) {
super(entity);
this.tournaments = tournaments;
}

public Collection<Tournament> getTournaments() {
return tournaments;
}
}
Loading

0 comments on commit 3237604

Please sign in to comment.