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

feat: add passrate for problem #105

Merged
merged 4 commits into from
Sep 24, 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
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
}
}
13 changes: 12 additions & 1 deletion cmd/web_server/handler/judge_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,31 @@ 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
}

judgeUID, err := judge_service.GetJudgeUIDFromStreamID(body.RedisStreamID)
if err != nil {
gin_utils.NewInternalError(ginCtx, err.Error())
return
}

if err := judge_service.ReportJudgeTask(
ginCtx, body.Consumer, body.RedisStreamID, verdict,
); 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 config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
level = "debug"
pretty_json = true
time_on = true
time_format = "2006-01-02 15:04:05"
time_format = "2000-01-01 00:00:00"

[database]
dsn = "user=postgres password=postgres host=localhost port=5432 dbname=oj_lab sslmode=disable TimeZone=Asia/Shanghai"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ require (
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/paulmach/orb v0.11.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -189,8 +189,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
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 GetJudgeUIDFromStreamID(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
10 changes: 9 additions & 1 deletion services/judge/judge_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ func ReportJudgeTask(
if err != nil {
return err
}

return nil
}

func GetJudgeUIDFromStreamID(streamID string) (*uuid.UUID, error) {
db := gorm_agent.GetDefaultDB()
judgeUID, err := judge_model.GetJudgeUIDFromStreamID(db, streamID)
if err != nil {
return nil, err
}
return judgeUID, nil
}

type VerdictJson struct {
Verdict string `json:"verdict"`
TimeUsage Time `json:"time_usage"`
Expand Down
19 changes: 19 additions & 0 deletions services/judge/judge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ func TestCreateJudge(t *testing.T) {
asserts.Equal(judge.Language, insert_judge.Language)
}

func TestGetJudgeUIDFromStreamid(t *testing.T) {
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
judge := &judge_model.Judge{
Language: judge_model.ProgrammingLanguageCpp,
ProblemSlug: "test-judge-service",
}
judge, err := CreateJudge(ctx, *judge)
if err != nil {
t.Error(err)
}

judgeUID, err := GetJudgeUIDFromStreamID(judge.RedisStreamID)
if err != nil {
t.Error(err)
}
asserts := assert.New(t)
asserts.Equal(judge.UID, *judgeUID)
}

func TestUpsertJudgeCache(t *testing.T) {
// previous WA || later WA || previous AC || later AC || GetBeforeSubmission
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
Expand Down
Loading