Skip to content

Commit

Permalink
feat: add passrate for problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Zztrans committed Sep 22, 2024
1 parent f96816e commit 7531dd6
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 17 deletions.
6 changes: 0 additions & 6 deletions cmd/web_server/handler/judge_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,4 @@ func postReportJudgeResult(ginCtx *gin.Context) {
gin_utils.NewInternalError(ginCtx, err.Error())
return
}

err = judge_service.UpsertJudgeCache(ginCtx, judgeUID, verdict)
if err != nil {
gin_utils.NewInternalError(ginCtx, err.Error())
return
}
}
12 changes: 8 additions & 4 deletions cmd/web_server/handler/judge_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,24 @@ func putReportJudgeTask(ginCtx *gin.Context) {
_ = ginCtx.Error(err)
return
}

verdict := judge_model.JudgeVerdict(body.VerdictString)
if !verdict.IsValid() {
gin_utils.NewInvalidParamError(ginCtx, "verdict", "invalid verdict")
return
}

if err := judge_service.ReportJudgeTask(
ginCtx, body.Consumer, body.RedisStreamID, verdict,
); err != nil {
judgeUID, err := judge_service.ReportJudgeTask(ginCtx, body.Consumer, body.RedisStreamID, verdict)
if err != nil {
_ = ginCtx.Error(err)
return
}

err = judge_service.UpsertJudgeCache(ginCtx, *judgeUID, verdict)
if err != nil {
gin_utils.NewInternalError(ginCtx, err.Error())
return
}

ginCtx.JSON(200, gin.H{
"message": "success",
})
Expand Down
8 changes: 7 additions & 1 deletion cmd/web_server/handler/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ func getProblemInfoList(ginCtx *gin.Context) {
Limit: &limit,
}

account := ""
ls, err := middleware.GetLoginSessionFromGinCtx(ginCtx)
if err == nil {
account = ls.Key.Account
}

problemInfoList, total, err := problem_service.GetProblemInfoList(
ginCtx, "", options,
ginCtx, account, options,
)
if err != nil {
gin_utils.NewInternalError(ginCtx, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion models/judge/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
type Judge struct {
models.MetaFields
UID uuid.UUID `json:"UID" gorm:"primaryKey"`
RedisStreamID string `json:"redisStreamID"`
RedisStreamID string `json:"redisStreamID" gorm:"index:idx_redis_stream_id"`
UserAccount string `json:"userAccount" gorm:"not null"`
User user_model.User `json:"user"`
ProblemSlug string `json:"problemSlug" gorm:"not null"`
Expand Down
13 changes: 13 additions & 0 deletions models/judge/judge_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func CreateJudge(tx *gorm.DB, judge Judge) (*Judge, error) {
func GetBeforeSubmission(tx *gorm.DB, judge Judge) (int, error) {
var count int64
err := tx.Model(&Judge{}).
Where("user_account = ?", judge.UserAccount).
Where("problem_slug = ?", judge.ProblemSlug).
Where("create_at < ?", judge.CreateAt).
Where("status = ?", JudgeStatusFinished).
Where("verdict != ?", JudgeVerdictCompileError).
Count(&count).Error

if err != nil {
Expand All @@ -52,6 +55,16 @@ func GetJudge(tx *gorm.DB, uid uuid.UUID) (*Judge, error) {
return &judge, nil
}

func GetJudgeUIDByStreamID(tx *gorm.DB, RedisStreamID string) (*uuid.UUID, error) {
judge := Judge{}
err := tx.Model(&Judge{}).Select("uid").
Where("redis_stream_id = ?", RedisStreamID).First(&judge).Error
if err != nil {
return nil, err
}
return &judge.UID, nil
}

type GetJudgeOptions struct {
Selection []string
UserAccount string
Expand Down
2 changes: 2 additions & 0 deletions models/problem/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type Problem struct {
Difficulty ProblemDifficulty `json:"difficulty,omitempty"`
Tags []*ProblemTag `json:"tags" gorm:"many2many:problem_problem_tags;"`
Solved *bool `json:"solved,omitempty" gorm:"-"`
AcceptCount int `json:"acceptCount"`
SubmitCount int `json:"submitCount"`
}

type ProblemDifficulty string
Expand Down
9 changes: 8 additions & 1 deletion models/problem/problem_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,30 @@ func GetProblemTagViewList(tags []*ProblemTag) []ProblemTagView {
return tagView
}

var ProblemInfoSelection = append([]string{"slug", "title", "difficulty"}, models.MetaFieldsSelection...)
var ProblemInfoSelection = append([]string{"slug", "title", "difficulty", "accept_count", "submit_count"}, models.MetaFieldsSelection...)

type ProblemInfoView struct {
Slug string `json:"slug"`
Title string `json:"title"`
Difficulty ProblemDifficulty `json:"difficulty"`
Tags []ProblemTagView `json:"tags"`
Solved *bool `json:"solved,omitempty"`
PassRate float32 `json:"passRate"`
}

func (p Problem) ToProblemInfo() ProblemInfoView {
PassRate := float32(0)
if p.SubmitCount != 0 {
PassRate = float32(p.AcceptCount) / float32(p.SubmitCount)
}

return ProblemInfoView{
Slug: p.Slug,
Title: p.Title,
Difficulty: p.Difficulty,
Tags: GetProblemTagViewList(p.Tags),
Solved: p.Solved,
PassRate: PassRate,
}
}

Expand Down
13 changes: 13 additions & 0 deletions services/judge/judge_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/google/uuid"
judge_model "github.com/oj-lab/platform/models/judge"
problem_model "github.com/oj-lab/platform/models/problem"
gorm_agent "github.com/oj-lab/platform/modules/agent/gorm"
)

Expand All @@ -15,6 +16,12 @@ func UpsertJudgeCache(ctx context.Context, uid uuid.UUID, verdict judge_model.Ju
return err
}
// log_module.AppLogger().WithField("judge", judge).Debug("getjudge")
var problem *problem_model.Problem
problem, err = problem_model.GetProblem(db, judge.ProblemSlug)
if err != nil {
return err
}

var scoreCache *judge_model.JudgeScoreCache
var rankCache *judge_model.JudgeRankCache
for {
Expand Down Expand Up @@ -59,13 +66,19 @@ func UpsertJudgeCache(ctx context.Context, uid uuid.UUID, verdict judge_model.Ju
}
scoreCache.IsAccepted = true
rankCache.Points = rankCache.Points + extraPoint
problem.AcceptCount += extraPoint
scoreCache.SolveTime = judge.CreateAt
} else {
scoreCache.SubmissionCount += 1
}
rankCache.TotalSubmissions += scoreCache.SubmissionCount - preSubmissionCount
problem.SubmitCount += scoreCache.SubmissionCount - preSubmissionCount
// log_module.AppLogger().WithField("scoreCache", scoreCache).Debug("update scoreCache")

err = problem_model.UpdateProblem(db, *problem)
if err != nil {
return err
}
err = judge_model.UpdateJudgeScoreCache(db, *scoreCache)
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions services/judge/judge_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func PickJudgeTask(ctx context.Context, consumer string) (*judge_model.JudgeTask
func ReportJudgeTask(
ctx context.Context,
consumer string, streamID string, verdict judge_model.JudgeVerdict,
) error {
) (*uuid.UUID, error) {
db := gorm_agent.GetDefaultDB()

err := judge_model.UpdateJudge(db, judge_model.Judge{
Expand All @@ -38,15 +38,16 @@ func ReportJudgeTask(
Verdict: verdict,
})
if err != nil {
return err
return nil, err
}

err = judge_model.AckTaskFromStream(ctx, streamID)
if err != nil {
return err
return nil, err
}

return nil
judgeUID, err := judge_model.GetJudgeUIDByStreamID(db, streamID)

Check failure on line 49 in services/judge/judge_task.go

View workflow job for this annotation

GitHub Actions / test

ineffectual assignment to err (ineffassign)
return judgeUID, nil
}

type VerdictJson struct {
Expand Down

0 comments on commit 7531dd6

Please sign in to comment.