Skip to content

Commit

Permalink
refactor(server): pattern: properly use module dependency clients (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar authored Jun 24, 2023
1 parent a2b67de commit 891e553
Show file tree
Hide file tree
Showing 38 changed files with 311 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
import judgels.jerahmeel.api.chapter.lesson.ChapterLessonsResponse;
import judgels.jerahmeel.chapter.ChapterStore;
import judgels.jerahmeel.role.RoleChecker;
import judgels.sandalphon.SandalphonClient;
import judgels.sandalphon.api.lesson.LessonInfo;
import judgels.sandalphon.api.lesson.LessonStatement;
import judgels.sandalphon.lesson.LessonClient;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

Expand All @@ -42,7 +42,7 @@ public class ChapterLessonResource {
@Inject protected RoleChecker roleChecker;
@Inject protected ChapterStore chapterStore;
@Inject protected ChapterLessonStore lessonStore;
@Inject protected LessonClient lessonClient;
@Inject protected SandalphonClient sandalphonClient;

@Inject public ChapterLessonResource() {}

Expand All @@ -65,7 +65,7 @@ public void setLessons(
checkArgument(aliases.size() == data.size(), "Lesson aliases must be unique");
checkArgument(slugs.size() == data.size(), "Lesson slugs must be unique");

Map<String, String> slugToJidMap = lessonClient.translateAllowedSlugsToJids(actorJid, slugs);
Map<String, String> slugToJidMap = sandalphonClient.translateAllowedLessonSlugsToJids(actorJid, slugs);

List<ChapterLesson> setData = data.stream().filter(cp -> slugToJidMap.containsKey(cp.getSlug())).map(lesson ->
new ChapterLesson.Builder()
Expand All @@ -89,7 +89,7 @@ public ChapterLessonsResponse getLessons(

List<ChapterLesson> lessons = lessonStore.getLessons(chapterJid);
Set<String> lessonJids = lessons.stream().map(ChapterLesson::getLessonJid).collect(Collectors.toSet());
Map<String, LessonInfo> lessonsMap = lessonClient.getLessons(lessonJids);
Map<String, LessonInfo> lessonsMap = sandalphonClient.getLessons(lessonJids);

return new ChapterLessonsResponse.Builder()
.data(lessons)
Expand All @@ -114,8 +114,8 @@ public ChapterLessonStatement getLessonStatement(

ChapterLesson lesson = checkFound(lessonStore.getLessonByAlias(chapterJid, lessonAlias));
String lessonJid = lesson.getLessonJid();
LessonInfo lessonInfo = lessonClient.getLesson(lessonJid);
LessonStatement statement = lessonClient.getLessonStatement(req, uriInfo, lesson.getLessonJid(), language);
LessonInfo lessonInfo = sandalphonClient.getLesson(lessonJid);
LessonStatement statement = sandalphonClient.getLessonStatement(req, uriInfo, lesson.getLessonJid(), language);

return new ChapterLessonStatement.Builder()
.defaultLanguage(lessonInfo.getDefaultLanguage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import judgels.jerahmeel.chapter.ChapterStore;
import judgels.jerahmeel.role.RoleChecker;
import judgels.jerahmeel.stats.StatsStore;
import judgels.sandalphon.SandalphonClient;
import judgels.sandalphon.api.problem.ProblemInfo;
import judgels.sandalphon.api.problem.ProblemType;
import judgels.sandalphon.problem.ProblemClient;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

Expand All @@ -44,8 +44,8 @@ public class ChapterProblemResource {
@Inject protected RoleChecker roleChecker;
@Inject protected ChapterStore chapterStore;
@Inject protected ChapterProblemStore problemStore;
@Inject protected ProblemClient problemClient;
@Inject protected StatsStore statsStore;
@Inject protected SandalphonClient sandalphonClient;

@Inject public ChapterProblemResource() {}

Expand All @@ -68,7 +68,7 @@ public void setProblems(
checkArgument(aliases.size() == data.size(), "Problem aliases must be unique");
checkArgument(slugs.size() == data.size(), "Problem slugs must be unique");

Map<String, String> slugToJidMap = problemClient.translateAllowedSlugsToJids(actorJid, slugs);
Map<String, String> slugToJidMap = sandalphonClient.translateAllowedProblemSlugsToJids(actorJid, slugs);

List<ChapterProblem> setData = data.stream().filter(cp -> slugToJidMap.containsKey(cp.getSlug())).map(problem ->
new ChapterProblem.Builder()
Expand All @@ -93,7 +93,7 @@ public ChapterProblemsResponse getProblems(

List<ChapterProblem> problems = problemStore.getProblems(chapterJid);
Set<String> problemJids = problems.stream().map(ChapterProblem::getProblemJid).collect(Collectors.toSet());
Map<String, ProblemInfo> problemsMap = problemClient.getProblems(problemJids);
Map<String, ProblemInfo> problemsMap = sandalphonClient.getProblems(problemJids);
Map<String, ProblemProgress> problemProgressesMap = statsStore.getProblemProgressesMap(actorJid, problemJids);

return new ChapterProblemsResponse.Builder()
Expand All @@ -120,7 +120,7 @@ public ChapterProblemWorksheet getProblemWorksheet(

ChapterProblem problem = checkFound(problemStore.getProblemByAlias(chapterJid, problemAlias));
String problemJid = problem.getProblemJid();
ProblemInfo problemInfo = problemClient.getProblem(problemJid);
ProblemInfo problemInfo = sandalphonClient.getProblem(problemJid);

Optional<String> reasonNotAllowedToSubmit = authHeader.isPresent()
? Optional.empty()
Expand All @@ -132,7 +132,7 @@ public ChapterProblemWorksheet getProblemWorksheet(
.languages(problemInfo.getTitlesByLanguage().keySet())
.problem(problem)
.worksheet(new judgels.sandalphon.api.problem.programming.ProblemWorksheet.Builder()
.from(problemClient.getProgrammingProblemWorksheet(req, uriInfo, problemJid, language))
.from(sandalphonClient.getProgrammingProblemWorksheet(req, uriInfo, problemJid, language))
.reasonNotAllowedToSubmit(reasonNotAllowedToSubmit)
.build())
.build();
Expand All @@ -142,7 +142,7 @@ public ChapterProblemWorksheet getProblemWorksheet(
.languages(problemInfo.getTitlesByLanguage().keySet())
.problem(problem)
.worksheet(new judgels.sandalphon.api.problem.bundle.ProblemWorksheet.Builder()
.from(problemClient.getBundleProblemWorksheetWithoutAnswerKey(req, uriInfo, problemJid, language))
.from(sandalphonClient.getBundleProblemWorksheetWithoutAnswerKey(req, uriInfo, problemJid, language))
.reasonNotAllowedToSubmit(reasonNotAllowedToSubmit)
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import judgels.jerahmeel.course.CourseStore;
import judgels.jerahmeel.role.RoleChecker;
import judgels.jerahmeel.stats.StatsStore;
import judgels.jophiel.user.UserClient;
import judgels.jophiel.JophielClient;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

Expand All @@ -52,7 +52,7 @@ public class CourseChapterResource {
@Inject protected ChapterStore chapterStore;
@Inject protected ChapterLessonStore chapterLessonStore;
@Inject protected StatsStore statsStore;
@Inject protected UserClient userClient;
@Inject protected JophielClient jophielClient;

@Inject public CourseChapterResource() {}

Expand Down Expand Up @@ -146,7 +146,7 @@ public CourseChapterUserProgressesResponse getChapterUserProgresses(
.collect(toList());

Map<String, String> usernameToJidsMap =
userClient.translateUsernamesToJids(ImmutableSet.copyOf(data.getUsernames()));
jophielClient.translateUsernamesToJids(ImmutableSet.copyOf(data.getUsernames()));
Map<String, Map<String, Integer>> userSolvedProblemsMap = statsStore.getUserChapterSolvedProblemsMap(
ImmutableSet.copyOf(usernameToJidsMap.values()),
chapterJids);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import judgels.jerahmeel.difficulty.ProblemDifficultyStore;
import judgels.jerahmeel.stats.StatsStore;
import judgels.persistence.api.Page;
import judgels.sandalphon.problem.ProblemClient;
import judgels.sandalphon.SandalphonClient;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

Expand All @@ -31,7 +31,7 @@ public class ProblemResource {
@Inject protected ProblemStore problemStore;
@Inject protected StatsStore statsStore;
@Inject protected ProblemDifficultyStore difficultyStore;
@Inject protected ProblemClient problemClient;
@Inject protected SandalphonClient sandalphonClient;

@Inject public ProblemResource() {}

Expand All @@ -47,7 +47,7 @@ public ProblemsResponse getProblems(

Set<String> allowedProblemJids = null;
if (!tags.isEmpty()) {
allowedProblemJids = problemClient.getProblemJidsByTags(tags);
allowedProblemJids = sandalphonClient.getProblemJidsByTags(tags);
}

Page<ProblemSetProblemInfo> problems = problemStore.getProblems(allowedProblemJids, pageNumber, PAGE_SIZE);
Expand All @@ -57,8 +57,8 @@ public ProblemsResponse getProblems(

return new ProblemsResponse.Builder()
.data(problems)
.problemsMap(problemClient.getProblems(problemJids))
.problemMetadatasMap(problemClient.getProblemMetadatas(problemJids))
.problemsMap(sandalphonClient.getProblems(problemJids))
.problemMetadatasMap(sandalphonClient.getProblemMetadatas(problemJids))
.problemDifficultiesMap(difficultyStore.getProblemDifficultiesMap(problemJids))
.problemProgressesMap(statsStore.getProblemProgressesMap(actorJid, problemJids))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import judgels.jerahmeel.problemset.problem.ProblemSetProblemStore;
import judgels.jerahmeel.role.RoleChecker;
import judgels.jerahmeel.stats.StatsStore;
import judgels.jophiel.JophielClient;
import judgels.jophiel.api.profile.Profile;
import judgels.jophiel.user.UserClient;
import judgels.persistence.api.Page;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;
Expand All @@ -57,7 +57,7 @@ public class ProblemSetResource {
@Inject protected ProblemSetProblemStore problemSetProblemStore;
@Inject protected ArchiveStore archiveStore;
@Inject protected StatsStore statsStore;
@Inject protected UserClient userClient;
@Inject protected JophielClient jophielClient;

@Inject public ProblemSetResource() {}

Expand Down Expand Up @@ -94,7 +94,7 @@ public ProblemSetsResponse getProblemSets(
for (ProblemSet problemSet : problemSets.getPage()) {
descriptions = descriptions.concat(problemSet.getDescription());
}
Map<String, Profile> profilesMap = userClient.parseProfiles(descriptions);
Map<String, Profile> profilesMap = jophielClient.parseProfiles(descriptions);

return new ProblemSetsResponse.Builder()
.data(problemSets)
Expand Down Expand Up @@ -190,7 +190,7 @@ public ProblemSetUserProgressesResponse getProblemSetUserProgresses(ProblemSetUs
Map<String, List<ProblemSetProblem>> problemsMap = problemSetProblemStore.getProblems(problemSetJids);

Set<String> usernames = ImmutableSet.copyOf(data.getUsernames());
Map<String, String> usernameToJidsMap = userClient.translateUsernamesToJids(usernames);
Map<String, String> usernameToJidsMap = jophielClient.translateUsernamesToJids(usernames);

Set<String> userJids = ImmutableSet.copyOf(usernameToJidsMap.values());
Set<String> problemJids = problemsMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@
import judgels.jerahmeel.problemset.ProblemSetStore;
import judgels.jerahmeel.role.RoleChecker;
import judgels.jerahmeel.stats.StatsStore;
import judgels.jerahmeel.uriel.ContestClient;
import judgels.jophiel.JophielClient;
import judgels.jophiel.api.profile.Profile;
import judgels.jophiel.user.UserClient;
import judgels.sandalphon.SandalphonClient;
import judgels.sandalphon.api.problem.ProblemEditorialInfo;
import judgels.sandalphon.api.problem.ProblemInfo;
import judgels.sandalphon.api.problem.ProblemMetadata;
import judgels.sandalphon.api.problem.ProblemType;
import judgels.sandalphon.problem.ProblemClient;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;
import judgels.uriel.UrielClient;
import judgels.uriel.api.contest.ContestInfo;

@Path("/api/v2/problemsets/{problemSetJid}/problems")
Expand All @@ -60,10 +60,10 @@ public class ProblemSetProblemResource {
@Inject protected ProblemSetStore problemSetStore;
@Inject protected ProblemSetProblemStore problemStore;
@Inject protected ProblemDifficultyStore difficultyStore;
@Inject protected UserClient userClient;
@Inject protected ProblemClient problemClient;
@Inject protected ContestClient contestClient;
@Inject protected StatsStore statsStore;
@Inject protected JophielClient jophielClient;
@Inject protected SandalphonClient sandalphonClient;
@Inject protected UrielClient urielClient;

@Inject public ProblemSetProblemResource() {}

Expand All @@ -86,14 +86,14 @@ public void setProblems(
checkArgument(aliases.size() == data.size(), "Problem aliases must be unique");
checkArgument(slugs.size() == data.size(), "Problem slugs must be unique");

Map<String, String> slugToJidMap = problemClient.translateAllowedSlugsToJids(actorJid, slugs);
Map<String, String> slugToJidMap = sandalphonClient.translateAllowedProblemSlugsToJids(actorJid, slugs);

Set<String> contestSlugs = data.stream()
.map(ProblemSetProblemData::getContestSlugs)
.flatMap(List::stream)
.collect(Collectors.toSet());

Map<String, String> contestSlugToJidMap = contestClient.translateSlugsToJids(contestSlugs);
Map<String, String> contestSlugToJidMap = urielClient.translateContestSlugsToJids(contestSlugs);

Set<String> notAllowedContestSlugs = data.stream()
.map(ProblemSetProblemData::getContestSlugs)
Expand All @@ -118,7 +118,7 @@ public void setProblems(
.collect(Collectors.toList());

Map<String, Boolean> problemVisibilitiesMap = problemStore.setProblems(problemSetJid, setData);
problemClient.setProblemVisibilityTagsByJids(problemVisibilitiesMap);
sandalphonClient.setProblemVisibilityTagsByJids(problemVisibilitiesMap);
}

@GET
Expand All @@ -142,12 +142,12 @@ public ProblemSetProblemsResponse getProblems(

return new ProblemSetProblemsResponse.Builder()
.data(problems)
.problemsMap(problemClient.getProblems(problemJids))
.problemMetadatasMap(problemClient.getProblemMetadatas(problemJids))
.problemsMap(sandalphonClient.getProblems(problemJids))
.problemMetadatasMap(sandalphonClient.getProblemMetadatas(problemJids))
.problemDifficultiesMap(difficultyStore.getProblemDifficultiesMap(problemJids))
.problemProgressesMap(statsStore.getProblemProgressesMap(actorJid, problemJids))
.contestsMap(roleChecker.isAdmin(actorJid)
? contestClient.getContestsByJids(contestJids)
? urielClient.getContestsByJids(contestJids)
: ImmutableMap.of())
.build();
}
Expand Down Expand Up @@ -184,7 +184,7 @@ public ProblemSetProblemWorksheet getProblemWorksheet(

ProblemSetProblem problem = checkFound(problemStore.getProblemByAlias(problemSetJid, problemAlias));
String problemJid = problem.getProblemJid();
ProblemInfo problemInfo = problemClient.getProblem(problemJid);
ProblemInfo problemInfo = sandalphonClient.getProblem(problemJid);

Optional<String> reasonNotAllowedToSubmit = authHeader.isPresent()
? Optional.empty()
Expand All @@ -196,7 +196,7 @@ public ProblemSetProblemWorksheet getProblemWorksheet(
.languages(problemInfo.getTitlesByLanguage().keySet())
.problem(problem)
.worksheet(new judgels.sandalphon.api.problem.programming.ProblemWorksheet.Builder()
.from(problemClient.getProgrammingProblemWorksheet(req, uriInfo, problemJid, language))
.from(sandalphonClient.getProgrammingProblemWorksheet(req, uriInfo, problemJid, language))
.reasonNotAllowedToSubmit(reasonNotAllowedToSubmit)
.build())
.build();
Expand All @@ -206,7 +206,7 @@ public ProblemSetProblemWorksheet getProblemWorksheet(
.languages(problemInfo.getTitlesByLanguage().keySet())
.problem(problem)
.worksheet(new judgels.sandalphon.api.problem.bundle.ProblemWorksheet.Builder()
.from(problemClient.getBundleProblemWorksheetWithoutAnswerKey(req, uriInfo, problemJid, language))
.from(sandalphonClient.getBundleProblemWorksheetWithoutAnswerKey(req, uriInfo, problemJid, language))
.reasonNotAllowedToSubmit(reasonNotAllowedToSubmit)
.build())
.build();
Expand All @@ -229,12 +229,12 @@ public ProblemReportResponse getProblemReport(
String problemJid = problem.getProblemJid();
Set<String> problemJids = ImmutableSet.of(problemJid);

ProblemMetadata metadata = problemClient.getProblemMetadata(problem.getProblemJid());
ProblemMetadata metadata = sandalphonClient.getProblemMetadata(problem.getProblemJid());
ProblemDifficulty difficulty = difficultyStore.getProblemDifficultiesMap(problemJids).get(problemJid);
ProblemTopStats topStats = statsStore.getProblemTopStats(problemJid);
ProblemProgress progress = statsStore.getProblemProgressesMap(actorJid, problemJids).get(problemJid);

Map<String, ContestInfo> contestsMap = contestClient.getContestsByJids(ImmutableSet.copyOf(problem.getContestJids()));
Map<String, ContestInfo> contestsMap = urielClient.getContestsByJids(ImmutableSet.copyOf(problem.getContestJids()));
List<ContestInfo> contests = problem.getContestJids().stream()
.filter(contestsMap::containsKey)
.map(contestsMap::get)
Expand All @@ -245,7 +245,7 @@ public ProblemReportResponse getProblemReport(
topStats.getTopUsersByScore().forEach(e -> userJids.add(e.getUserJid()));
topStats.getTopUsersByTime().forEach(e -> userJids.add(e.getUserJid()));
topStats.getTopUsersByMemory().forEach(e -> userJids.add(e.getUserJid()));
Map<String, Profile> profilesMap = userClient.getProfiles(userJids);
Map<String, Profile> profilesMap = jophielClient.getProfiles(userJids);

return new ProblemReportResponse.Builder()
.metadata(metadata)
Expand All @@ -270,7 +270,7 @@ public ProblemEditorialResponse getProblemEditorial(
checkFound(problemSetStore.getProblemSetByJid(problemSetJid));

ProblemSetProblem problem = checkFound(problemStore.getProblemByAlias(problemSetJid, problemAlias));
ProblemEditorialInfo editorial = checkFound(problemClient.getProblemEditorial(problem.getProblemJid(), uriInfo.getBaseUri(), language));
ProblemEditorialInfo editorial = checkFound(sandalphonClient.getProblemEditorial(problem.getProblemJid(), uriInfo.getBaseUri(), language));
return new ProblemEditorialResponse.Builder()
.editorial(editorial)
.build();
Expand Down
Loading

0 comments on commit 891e553

Please sign in to comment.