From 690355809a717cdb8bfc0f80ea5a0c126ba37e63 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Sun, 15 Sep 2024 15:21:13 +0700 Subject: [PATCH] Stats: recalculate user stats after processing problem (#664) --- .../StatsUserProblemHibernateDao.java | 10 ++++- .../persistence/StatsUserProblemDao.java | 2 +- .../programming/StatsProcessor.java | 37 ++++++------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/StatsUserProblemHibernateDao.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/StatsUserProblemHibernateDao.java index 070a231ff..b791db1b9 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/StatsUserProblemHibernateDao.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/StatsUserProblemHibernateDao.java @@ -163,8 +163,14 @@ public long selectCountTriedByUserJid(String userJid) { } @Override - public long selectTotalScoreByUserJid(String userJid) { - return 0; + public int selectTotalScoreByUserJid(String userJid) { + CriteriaBuilder cb = currentSession().getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(Integer.class); + Root root = cq.from(getEntityClass()); + + cq.select(cb.sum(root.get(StatsUserProblemModel_.score))); + cq.where(cb.equal(root.get(StatsUserProblemModel_.userJid), userJid)); + return currentSession().createQuery(cq).getSingleResult(); } @Override diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/StatsUserProblemDao.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/StatsUserProblemDao.java index b0e42aaab..0afa152fc 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/StatsUserProblemDao.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/StatsUserProblemDao.java @@ -17,6 +17,6 @@ public interface StatsUserProblemDao extends Dao { Map selectCountsAcceptedByProblemJids(Collection problemJids); Map selectCountsTriedByProblemJids(Collection problemJids); long selectCountTriedByUserJid(String userJid); - long selectTotalScoreByUserJid(String userJid); + int selectTotalScoreByUserJid(String userJid); Map selectCountsVerdictByUserJid(String userJid); } diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/submission/programming/StatsProcessor.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/submission/programming/StatsProcessor.java index fb0baa614..e386c0004 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/submission/programming/StatsProcessor.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/submission/programming/StatsProcessor.java @@ -29,12 +29,6 @@ public class StatsProcessor implements SubmissionConsumer { private final StatsUserDao statsUserDao; private final StatsUserProblemDao statsUserProblemDao; - @SuppressWarnings("checkstyle:visibilitymodifier") - static class ProblemStatsResult { - int scoreDiff; - boolean becomesAccepted; - } - @Inject public StatsProcessor( ChapterProblemDao chapterProblemDao, @@ -58,21 +52,18 @@ public void accept(Submission submission) { return; } - ProblemStatsResult res = processProblemStats(submission); - if (res == null) { - return; + if (processProblemStats(submission)) { + processUserStats(submission); } - - processUserStats(submission, res.scoreDiff); } - private ProblemStatsResult processProblemStats(Submission s) { + private boolean processProblemStats(Submission s) { if (!s.getLatestGrading().isPresent()) { - return null; + return false; } Grading grading = s.getLatestGrading().get(); if (!grading.getDetails().isPresent()) { - return null; + return false; } GradingResultDetails details = grading.getDetails().get(); @@ -93,7 +84,6 @@ private ProblemStatsResult processProblemStats(Submission s) { boolean isNowAccepted = isAccepted(grading.getVerdict(), grading.getScore()); Verdict verdict = isNowAccepted ? Verdict.ACCEPTED : grading.getVerdict(); - int scoreDiff = grading.getScore(); Optional maybeModel = statsUserProblemDao.selectByUserJidAndProblemJid(s.getUserJid(), s.getProblemJid()); @@ -102,11 +92,7 @@ private ProblemStatsResult processProblemStats(Submission s) { StatsUserProblemModel model = maybeModel.get(); model.submissionJid = s.getJid(); - scoreDiff = grading.getScore() - model.score; isAlreadyAccepted = isAccepted(Verdicts.fromCode(model.verdict), model.score); - if (isAlreadyAccepted) { - scoreDiff = Math.max(0, scoreDiff); - } if (!isAlreadyAccepted || grading.getScore() >= model.score) { model.verdict = verdict.getCode(); @@ -135,22 +121,21 @@ private ProblemStatsResult processProblemStats(Submission s) { statsUserProblemDao.insert(model); } - ProblemStatsResult result = new ProblemStatsResult(); - result.scoreDiff = scoreDiff; - result.becomesAccepted = !isAlreadyAccepted && isNowAccepted; - return result; + return true; } - private void processUserStats(Submission s, int scoreDiff) { + private void processUserStats(Submission s) { + int totalScore = statsUserProblemDao.selectTotalScoreByUserJid(s.getUserJid()); + Optional maybeModel = statsUserDao.selectByUserJid(s.getUserJid()); if (maybeModel.isPresent()) { StatsUserModel model = maybeModel.get(); - model.score += scoreDiff; + model.score = totalScore; statsUserDao.update(model); } else { StatsUserModel model = new StatsUserModel(); model.userJid = s.getUserJid(); - model.score = scoreDiff; + model.score = totalScore; statsUserDao.insert(model); } }