Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: recalculate user stats after processing problem #664

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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