Skip to content

Commit

Permalink
Support getting solved status while querying problems
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Aug 23, 2024
1 parent b13e983 commit e3b524f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cmd/web_server/handler/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func getProblemInfoList(ginCtx *gin.Context) {

problemInfoList, total, err := problem_service.GetProblemInfoList(
ginCtx,
&limit,
&offset,
nil,
&limit, &offset,
)
if err != nil {
gin_utils.NewInternalError(ginCtx, err.Error())
Expand Down
14 changes: 7 additions & 7 deletions models/judge/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
user_model "github.com/oj-lab/oj-lab-platform/models/user"
)

type JudgeTaskStatus string
type JudgeStatus string

const (
JudgeTaskStatusPending JudgeTaskStatus = "pending"
JudgeTaskStatusWaiting JudgeTaskStatus = "waiting"
JudgeTaskStatusRunning JudgeTaskStatus = "running"
JudgeTaskStatusFinished JudgeTaskStatus = "finished"
JudgeStatusPending JudgeStatus = "pending"
JudgeStatusWaiting JudgeStatus = "waiting"
JudgeStatusRunning JudgeStatus = "running"
JudgeStatusFinished JudgeStatus = "finished"
)

type ProgrammingLanguage string
Expand All @@ -39,7 +39,7 @@ type Judge struct {
Problem problem_model.Problem `json:"problem"`
Code string `json:"code" gorm:"not null"`
Language ProgrammingLanguage `json:"language" gorm:"not null"`
Status JudgeTaskStatus `json:"status" gorm:"default:pending"`
Status JudgeStatus `json:"status" gorm:"default:pending"`
ResultCount uint `json:"resultCount"`
Results []JudgeResult `json:"results" gorm:"foreignKey:JudgeUID"`
Verdict JudgeVerdict `json:"verdict"`
Expand All @@ -56,7 +56,7 @@ func NewJudge(
ProblemSlug: problemSlug,
Code: code,
Language: language,
Status: JudgeTaskStatusPending,
Status: JudgeStatusPending,
}
}

Expand Down
41 changes: 24 additions & 17 deletions models/judge/judge_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ func GetJudge(tx *gorm.DB, uid uuid.UUID) (*Judge, error) {

type GetJudgeOptions struct {
Selection []string
Statuses []JudgeTaskStatus
UserAccount *string
ProblemSlug *string
ProblemSlugs []string
Statuses []JudgeStatus
Verdicts []JudgeVerdict
Offset *int
Limit *int
OrderByColumns []models.OrderByColumnOption
}

func buildGetJudgeTXByOptions(
func buildGetJudgesTXByOptions(
tx *gorm.DB, options GetJudgeOptions, isCount bool,
) *gorm.DB {
tx = tx.Model(&Judge{}).
Expand All @@ -59,12 +60,15 @@ func buildGetJudgeTXByOptions(
if options.UserAccount != nil {
tx = tx.Where("user_account = ?", *options.UserAccount)
}
if options.ProblemSlug != nil {
tx = tx.Where("problem_slug = ?", *options.ProblemSlug)
}
if len(options.Statuses) > 0 {
tx = tx.Where("status IN ?", options.Statuses)
}
if len(options.Verdicts) > 0 {
tx = tx.Where("verdict IN ?", options.Verdicts)
}
if len(options.ProblemSlugs) > 0 {
tx = tx.Where("problem_slug IN ?", options.ProblemSlugs)
}

if !isCount {
if options.Offset != nil {
Expand All @@ -84,24 +88,27 @@ func buildGetJudgeTXByOptions(
return tx
}

func CountJudgeByOptions(tx *gorm.DB, options GetJudgeOptions) (int64, error) {
tx = buildGetJudgesTXByOptions(tx, options, true)
var count int64
err := tx.Count(&count).Error
if err != nil {
return 0, err
}
return count, nil
}

func GetJudgeListByOptions(
tx *gorm.DB, options GetJudgeOptions,
) ([]*Judge, int64, error) {
tx = buildGetJudgeTXByOptions(tx, options, false)
) ([]*Judge, error) {
tx = buildGetJudgesTXByOptions(tx, options, false)
var judges []*Judge
err := tx.Find(&judges).Error
if err != nil {
return nil, 0, err
}

tx = buildGetJudgeTXByOptions(tx, options, true)
var count int64
err = tx.Count(&count).Error
if err != nil {
return nil, 0, err
return nil, err
}

return judges, count, nil
return judges, nil
}

func UpdateJudge(tx *gorm.DB, judge Judge) error {
Expand Down
2 changes: 1 addition & 1 deletion models/problem/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Problem struct {
Title string `json:"title" gorm:"not null"`
Description *string `json:"description,omitempty"`
Tags []*ProblemTag `json:"tags" gorm:"many2many:problem_problem_tags;"`
Solved bool `json:"solved,omitempty" gorm:"-"`
IsAccepted bool `json:"solved,omitempty" gorm:"-"`
}

type ProblemTag struct {
Expand Down
7 changes: 6 additions & 1 deletion services/judge/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func GetJudgeList(
ctx context.Context, options judge_model.GetJudgeOptions,
) ([]*judge_model.Judge, int64, error) {
db := gorm_agent.GetDefaultDB()
judges, total, err := judge_model.GetJudgeListByOptions(db, options)

total, err := judge_model.CountJudgeByOptions(db, options)
if err != nil {
return nil, 0, err
}
judges, err := judge_model.GetJudgeListByOptions(db, options)
if err != nil {
return nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/judge/judge_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CreateJudgeResult(
if judge == nil {
return nil, ErrJudgeNotFound
}
if judge.Status != judge_model.JudgeTaskStatusRunning {
if judge.Status != judge_model.JudgeStatusRunning {
return nil, ErrInvalidJudgeStatus
}

Expand Down
4 changes: 2 additions & 2 deletions services/judge/judge_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func PickJudgeTask(ctx context.Context, consumer string) (*judge_model.JudgeTask
db := gorm_agent.GetDefaultDB()
err = judge_model.UpdateJudge(db, judge_model.Judge{
UID: uuid.MustParse(task.JudgeUID),
Status: judge_model.JudgeTaskStatusRunning,
Status: judge_model.JudgeStatusRunning,
})
if err != nil {
return nil, err
Expand All @@ -34,7 +34,7 @@ func ReportJudgeTask(

err := judge_model.UpdateJudge(db, judge_model.Judge{
RedisStreamID: streamID,
Status: judge_model.JudgeTaskStatusFinished,
Status: judge_model.JudgeStatusFinished,
Verdict: verdict,
})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion services/judge/judge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func TestCreateJudge(t *testing.T) {
}

db := gorm_agent.GetDefaultDB()
judges, _, err := judge_model.GetJudgeListByOptions(db, judge_model.GetJudgeOptions{OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}}})
judges, err := judge_model.GetJudgeListByOptions(db,
judge_model.GetJudgeOptions{OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}}})
if err != nil || len(judges) == 0 {
t.Error(err)
}
Expand Down
32 changes: 31 additions & 1 deletion services/problem/problem_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package problem_service
import (
"context"

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

func GetProblemInfoList(_ context.Context, limit, offset *int) ([]problem_model.Problem, int64, error) {
func GetProblemInfoList(
_ context.Context,
account *string,
limit, offset *int,
) ([]problem_model.Problem, int64, error) {
db := gorm_agent.GetDefaultDB()
getOptions := problem_model.GetProblemOptions{
Selection: problem_model.ProblemInfoSelection,
Expand All @@ -24,5 +29,30 @@ func GetProblemInfoList(_ context.Context, limit, offset *int) ([]problem_model.
return nil, 0, err
}

if account != nil {
problemSlugs := []string{}
for _, problem := range problemList {
problemSlugs = append(problemSlugs, problem.Slug)
}
acceptedJudgeList, err := judge_model.GetJudgeListByOptions(db, judge_model.GetJudgeOptions{
UserAccount: account,
ProblemSlugs: problemSlugs,
Statuses: []judge_model.JudgeStatus{judge_model.JudgeStatusFinished},
Verdicts: []judge_model.JudgeVerdict{judge_model.JudgeVerdictAccepted},
})
if err != nil {
return nil, 0, err
}
acceptedProblemSlugs := map[string]bool{}
for _, judge := range acceptedJudgeList {
acceptedProblemSlugs[judge.ProblemSlug] = true
}
for i, problem := range problemList {
if _, ok := acceptedProblemSlugs[problem.Slug]; ok {
problemList[i].IsAccepted = true
}
}
}

return problemList, total, nil
}

0 comments on commit e3b524f

Please sign in to comment.