Skip to content

Commit

Permalink
Stats: recalculate user stats after processing problem
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Sep 15, 2024
1 parent 42d91a3 commit a4caae9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> cq = cb.createQuery(Integer.class);
Root<StatsUserProblemModel> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface StatsUserProblemDao extends Dao<StatsUserProblemModel> {
Map<String, Long> selectCountsAcceptedByProblemJids(Collection<String> problemJids);
Map<String, Long> selectCountsTriedByProblemJids(Collection<String> problemJids);
long selectCountTriedByUserJid(String userJid);
long selectTotalScoreByUserJid(String userJid);
int selectTotalScoreByUserJid(String userJid);
Map<String, Long> selectCountsVerdictByUserJid(String userJid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

Expand All @@ -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<StatsUserProblemModel> maybeModel =
statsUserProblemDao.selectByUserJidAndProblemJid(s.getUserJid(), s.getProblemJid());
Expand All @@ -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();
Expand Down Expand Up @@ -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<StatsUserModel> 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);
}
}
Expand Down

0 comments on commit a4caae9

Please sign in to comment.