Search in sources :

Example 1 with UserStatistics

use of cn.edu.zju.acm.onlinejudge.util.UserStatistics in project zoj by licheng.

the class ShowUserStatusAction method execute.

/**
     * Method execute
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    UserProfile user = null;
    String handle = context.getRequest().getParameter("handle");
    if (handle != null && handle.length() > 0) {
        // TODO cache?
        user = PersistenceManager.getInstance().getUserPersistence().getUserProfileByHandle(handle);
    } else if (context.getRequest().getParameter("userId") != null) {
        long userId = Utility.parseLong(context.getRequest().getParameter("userId"));
        if (userId != -1) {
            user = PersistenceManager.getInstance().getUserPersistence().getUserProfile(userId);
        }
    } else {
        user = context.getUserProfile();
    }
    AbstractContest contest = null;
    if (user != null) {
        long contestId = Utility.parseLong(context.getRequest().getParameter("contestId"));
        if (contestId == -1) {
            contestId = ShowUserStatusAction.defaultProblemSetId;
        }
        contest = ContestManager.getInstance().getContest(contestId);
    }
    if (contest != null) {
        context.setAttribute("contest", contest);
        ActionForward forward = this.checkContestViewPermission(mapping, context, null, true);
        if (forward != null) {
            contest = null;
        }
    }
    UserStatistics statistics = null;
    UserPreference pref = null;
    if (contest != null && user != null) {
        // TODO cache?
        pref = PersistenceManager.getInstance().getUserPersistence().getUserPreference(user.getId());
        statistics = StatisticsManager.getInstance().getUserStatistics(contest.getId(), user.getId());
    }
    context.setAttribute("user", user);
    context.setAttribute("preference", pref);
    context.setAttribute("contest", contest);
    context.setAttribute("UserStatistics", statistics);
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserStatistics(cn.edu.zju.acm.onlinejudge.util.UserStatistics) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) ActionForward(org.apache.struts.action.ActionForward)

Example 2 with UserStatistics

use of cn.edu.zju.acm.onlinejudge.util.UserStatistics in project zoj by licheng.

the class ShowProblemsAction method execute.

/**
     * ShowProblemsAction.
     * 
     * @param mapping
     *            action mapping
     * @param form
     *            action form
     * @param request
     *            http servlet request
     * @param response
     *            http servlet response
     * 
     * @return action forward instance
     * 
     * @throws Exception
     *             any errors happened
     */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    // check contest
    //System.out.println("in show problems");
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("showProblems.do");
    //boolean isCourse=context.getRequest().getRequestURI().endsWith("showCourseProblems.do");
    ActionForward forward = this.checkContestViewPermission(mapping, context, isProblemset, true);
    if (forward != null) {
        return forward;
    }
    AbstractContest contest = context.getContest();
    //System.out.println("" + contest.getId());
    long problemsCount = ContestManager.getInstance().getProblemsCount(contest.getId());
    long pageNumber = Utility.parseLong(context.getRequest().getParameter("pageNumber"));
    if (pageNumber < 1) {
        pageNumber = 1;
    }
    long problemsPerPage = 100;
    if (problemsCount <= (pageNumber - 1) * problemsPerPage) {
        pageNumber = 1;
    }
    long totalPages = 1;
    if (problemsCount > 0) {
        totalPages = (problemsCount - 1) / problemsPerPage + 1;
    }
    List<Problem> oproblems = ContestManager.getInstance().getContestProblems(contest.getId(), (int) ((pageNumber - 1) * problemsPerPage), (int) problemsPerPage);
    List<Problem> problems = new ArrayList<Problem>();
    problems.addAll(oproblems);
    ContestStatistics contestStatistics = null;
    contestStatistics = StatisticsManager.getInstance().getContestStatistics(contest.getId(), (int) ((pageNumber - 1) * problemsPerPage), (int) problemsPerPage);
    for (int i = 0; i < problems.size(); ++i) {
        Problem p = (Problem) problems.get(i);
        int ac = contestStatistics.getCount(i, 0);
        int total = contestStatistics.getProblemCount(i);
        p.setTotal(total);
        p.setAC(ac);
        if (total == 0) {
            p.setRatio(0);
        } else {
            p.setRatio((double) ac / (double) total);
        }
    }
    String order = context.getRequest().getParameter("order");
    if (order != null) {
        if (order.equalsIgnoreCase("ratio")) {
            Collections.sort(problems, new Comparator() {

                public int compare(Object o1, Object o2) {
                    return (((Problem) o2).getRatio() - ((Problem) o1).getRatio()) > 0 ? 1 : -1;
                }
            });
        } else if (order.equalsIgnoreCase("ac")) {
            Collections.sort(problems, new Comparator() {

                public int compare(Object o1, Object o2) {
                    return ((Problem) o2).getAC() - ((Problem) o1).getAC();
                }
            });
        } else if (order.equalsIgnoreCase("all")) {
            Collections.sort(problems, new Comparator() {

                public int compare(Object o1, Object o2) {
                    return ((Problem) o2).getTotal() - ((Problem) o1).getTotal();
                }
            });
        }
    }
    UserStatistics userStatistics = null;
    if (context.getUserProfile() != null && problems.size() > 0) {
        userStatistics = StatisticsManager.getInstance().getUserStatistics(contest.getId(), context.getUserProfile().getId());
    }
    context.setAttribute("problems", problems);
    context.setAttribute("pageNumber", (new Long(pageNumber)).toString());
    context.setAttribute("ContestStatistics", contestStatistics);
    context.setAttribute("UserStatistics", userStatistics);
    context.setAttribute("totalPages", new Long(totalPages));
    context.setAttribute("currentPage", new Long(pageNumber));
    if (this.checkContestAdminPermission(mapping, context, isProblemset, true) == null && "true".equalsIgnoreCase(context.getRequest().getParameter("check"))) {
        List<String> checkMessages = new ArrayList<String>();
        for (Object obj : problems) {
            Problem p = (Problem) obj;
            this.checkExists("Text", this.getReferenceLength(p, ReferenceType.DESCRIPTION), "ERROR", checkMessages, p);
            this.checkExists("Input", this.getReferenceLength(p, ReferenceType.INPUT), "ERROR", checkMessages, p);
            if (p.isChecker()) {
                this.checkExists("Output", this.getReferenceLength(p, ReferenceType.OUTPUT), "WARNING", checkMessages, p);
                // checkExists("Checker", getReferenceLength(p, ReferenceType.CHECKER), "WARNING", checkMessages,
                // p);
                this.checkExists("Checker source", this.getReferenceLength(p, ReferenceType.CHECKER_SOURCE), "ERROR", checkMessages, p);
            } else {
                this.checkExists("Output", this.getReferenceLength(p, ReferenceType.OUTPUT), "ERROR", checkMessages, p);
            }
            this.checkExists("Judge solution", this.getReferenceLength(p, ReferenceType.JUDGE_SOLUTION), "WARNING", checkMessages, p);
        }
        context.setAttribute("CheckMessages", checkMessages);
    }
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ArrayList(java.util.ArrayList) ActionForward(org.apache.struts.action.ActionForward) UserStatistics(cn.edu.zju.acm.onlinejudge.util.UserStatistics) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ContestStatistics(cn.edu.zju.acm.onlinejudge.util.ContestStatistics)

Example 3 with UserStatistics

use of cn.edu.zju.acm.onlinejudge.util.UserStatistics in project zoj by licheng.

the class SubmissionPersistenceImpl method getUserStatistics.

public UserStatistics getUserStatistics(long contestId, long userId) throws PersistenceException {
    Connection conn = null;
    try {
        UserStatistics statistics = new UserStatistics(userId, contestId);
        conn = Database.createConnection();
        PreparedStatement ps = null;
        /*String sql =
                    "SELECT DISTINCT p.problem_id, p.code, p.title, s.judge_comment " +
                        SubmissionPersistenceImpl.GET_SUBMISSION_FROM_PART +
                        " AND s.user_profile_id=? AND s.judge_reply_id=? AND s.contest_id=?";*/
        String sql = "SELECT p.problem_id, p.code, p.title, s.judge_comment " + SubmissionPersistenceImpl.GET_SUBMISSION_FROM_PART + " AND s.user_profile_id=? AND s.judge_reply_id=? AND s.contest_id=?";
        sql = sql.replace("FORCE_INDEX", "USE INDEX (index_submission_user_reply_contest)");
        HashSet<Long> solvedid = new HashSet<Long>();
        HashSet<Long> confirmedid = new HashSet<Long>();
        List<Problem> solved = new ArrayList<Problem>();
        List<Problem> confirmed = new ArrayList<Problem>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setLong(1, userId);
            ps.setLong(2, JudgeReply.ACCEPTED.getId());
            ps.setLong(3, contestId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Long probemid = new Long(rs.getLong("problem_id"));
                if (!solvedid.contains(probemid)) {
                    Problem p = new Problem();
                    p.setContestId(contestId);
                    p.setId(rs.getLong("problem_id"));
                    p.setCode(rs.getString("code"));
                    p.setTitle(rs.getString("title"));
                    solved.add(p);
                    solvedid.add(probemid);
                }
                String comment = rs.getString("judge_comment");
                if (!confirmed.contains(probemid) && "Yes".equals(comment)) {
                    Problem p = new Problem();
                    p.setContestId(contestId);
                    p.setId(rs.getLong("problem_id"));
                    p.setCode(rs.getString("code"));
                    p.setTitle(rs.getString("title"));
                    confirmed.add(p);
                    confirmedid.add(probemid);
                }
            }
        } finally {
            Database.dispose(ps);
        }
        statistics.setSolved(new TreeSet<Problem>(solved));
        statistics.setConfirmed(new TreeSet<Problem>(confirmed));
        try {
            ps = conn.prepareStatement("SELECT judge_reply_id, count(*) FROM submission WHERE contest_id=? AND user_profile_id=? GROUP BY judge_reply_id");
            ps.setLong(1, contestId);
            ps.setLong(2, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                long jid = rs.getLong(1);
                int count = rs.getInt(2);
                statistics.setCount(jid, count);
            }
            return statistics;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the user statistics", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) UserStatistics(cn.edu.zju.acm.onlinejudge.util.UserStatistics) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) HashSet(java.util.HashSet)

Aggregations

UserStatistics (cn.edu.zju.acm.onlinejudge.util.UserStatistics)3 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)2 ArrayList (java.util.ArrayList)2 ActionForward (org.apache.struts.action.ActionForward)2 UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)1 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)1 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)1 ContestStatistics (cn.edu.zju.acm.onlinejudge.util.ContestStatistics)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1