-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
984 additions
and
782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ dist.zip | |
|
||
# Ignore generated files | ||
*.pb.go | ||
**/swaggo-gen/docs.go | ||
**/swaggo_gen/docs.go | ||
|
||
bin/ | ||
__debug_bin** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,91 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
"github.com/oj-lab/oj-lab-platform/models" | ||
judge_model "github.com/oj-lab/oj-lab-platform/models/judge" | ||
"github.com/oj-lab/oj-lab-platform/modules" | ||
judge_service "github.com/oj-lab/oj-lab-platform/services/judge" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
func SetupJudgeRoute(baseRoute *gin.RouterGroup) { | ||
func SetupJudgeRouter(baseRoute *gin.RouterGroup) { | ||
g := baseRoute.Group("/judge") | ||
{ | ||
g.POST("/task/pick", postPickJudgeTask) | ||
g.POST("/task/report", postReportJudgeTaskResult) | ||
g.GET("", getJudgeList) | ||
g.GET("/:uid", getJudge) | ||
} | ||
} | ||
|
||
type PickJudgeTaskBody struct { | ||
Consumer string `json:"consumer"` | ||
} | ||
|
||
func postPickJudgeTask(ginCtx *gin.Context) { | ||
body := PickJudgeTaskBody{} | ||
if err := ginCtx.ShouldBindJSON(&body); err != nil { | ||
_ = ginCtx.Error(err) | ||
return | ||
} | ||
|
||
task, err := judge_service.PickJudgeTask(ginCtx, body.Consumer) | ||
if err == redis.Nil { | ||
ginCtx.Status(204) | ||
func getJudge(ginCtx *gin.Context) { | ||
uidString := ginCtx.Param("uid") | ||
uid, err := uuid.Parse(uidString) | ||
if err != nil { | ||
modules.NewInvalidParamError("uid", "invalid uid").AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
judge, err := judge_service.GetJudge(ginCtx, uid) | ||
if err != nil { | ||
_ = ginCtx.Error(err) | ||
modules.NewInternalError(fmt.Sprintf("failed to get judge: %v", err)).AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
ginCtx.JSON(200, gin.H{ | ||
"task": task, | ||
}) | ||
ginCtx.JSON(200, judge) | ||
} | ||
|
||
type ReportJudgeTaskResultBody struct { | ||
Consumer string `json:"consumer"` | ||
StreamID string `json:"stream_id"` | ||
VerdictJson string `json:"verdict_json"` | ||
type getJudgeListResponse struct { | ||
Total int64 `json:"total"` | ||
List []*judge_model.Judge `json:"list"` | ||
} | ||
|
||
func postReportJudgeTaskResult(ginCtx *gin.Context) { | ||
body := ReportJudgeTaskResultBody{} | ||
if err := ginCtx.ShouldBindJSON(&body); err != nil { | ||
_ = ginCtx.Error(err) | ||
// Get Judge List | ||
// | ||
// @Summary Get Judge list | ||
// @Description Get Judge list | ||
// @Tags judge | ||
// @Accept json | ||
// @Param limit query int false "limit" | ||
// @Param offset query int false "offset" | ||
// @Router /judge [get] getJudgeListResponse | ||
func getJudgeList(ginCtx *gin.Context) { | ||
limitQuery, _ := ginCtx.GetQuery("limit") | ||
offsetQuery, _ := ginCtx.GetQuery("offset") | ||
if limitQuery == "" { | ||
limitQuery = "10" | ||
} | ||
if offsetQuery == "" { | ||
offsetQuery = "0" | ||
} | ||
|
||
limit, err := strconv.Atoi(limitQuery) | ||
if err != nil { | ||
modules.NewInvalidParamError("limit", "invalid limit").AppendToGin(ginCtx) | ||
return | ||
} | ||
offset, err := strconv.Atoi(offsetQuery) | ||
if err != nil { | ||
modules.NewInvalidParamError("offset", "invalid offset").AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
options := judge_model.GetJudgeOptions{ | ||
Limit: &limit, | ||
Offset: &offset, | ||
OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}}, | ||
} | ||
|
||
if err := judge_service.ReportJudgeTaskResult(ginCtx, body.Consumer, body.StreamID, body.VerdictJson); err != nil { | ||
_ = ginCtx.Error(err) | ||
judges, total, err := judge_service.GetJudgeList(ginCtx, options) | ||
if err != nil { | ||
modules.NewInternalError(fmt.Sprintf("failed to get judge list: %v", err)).AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
ginCtx.JSON(200, gin.H{ | ||
"message": "success", | ||
ginCtx.JSON(200, getJudgeListResponse{ | ||
Total: total, | ||
List: judges, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
judge_model "github.com/oj-lab/oj-lab-platform/models/judge" | ||
"github.com/oj-lab/oj-lab-platform/modules" | ||
judge_service "github.com/oj-lab/oj-lab-platform/services/judge" | ||
) | ||
|
||
func SetupJudgeResultRouter(baseRoute *gin.RouterGroup) { | ||
g := baseRoute.Group("/judge") | ||
{ | ||
g.PUT("/task/report/result-count", putReportJudgeResultCount) | ||
g.POST("/task/report/result", postReportJudgeResult) | ||
} | ||
} | ||
|
||
type ReportJudgeResultCountBody struct { | ||
JudgeUID string `json:"judgeUID"` | ||
ResultCount uint `json:"resultCount"` | ||
} | ||
|
||
func putReportJudgeResultCount(ginCtx *gin.Context) { | ||
body := ReportJudgeResultCountBody{} | ||
if err := ginCtx.ShouldBindJSON(&body); err != nil { | ||
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
judgeUID, err := uuid.Parse(body.JudgeUID) | ||
if err != nil { | ||
modules.NewInvalidParamError("judgeUID", "invalid judgeUID").AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
if err := judge_service.ReportJudgeResultCount( | ||
ginCtx, judgeUID, body.ResultCount, | ||
); err != nil { | ||
modules.NewInternalError(err.Error()).AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
ginCtx.JSON(200, gin.H{ | ||
"message": "success", | ||
}) | ||
} | ||
|
||
type ReportJudgeResultBody struct { | ||
JudgeUIDString string `json:"judgeUID"` | ||
VerdictString string `json:"verdict"` | ||
TimeUsageMS uint `json:"timeUsageMS"` | ||
MemoryUsageByte uint `json:"memoryUsageByte"` | ||
} | ||
|
||
func postReportJudgeResult(ginCtx *gin.Context) { | ||
body := ReportJudgeResultBody{} | ||
if err := ginCtx.ShouldBindJSON(&body); err != nil { | ||
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx) | ||
} | ||
|
||
judgeUID, err := uuid.Parse(body.JudgeUIDString) | ||
if err != nil { | ||
modules.NewInvalidParamError("judgeUID", "invalid judgeUID").AppendToGin(ginCtx) | ||
return | ||
} | ||
verdict := judge_model.JudgeVerdict(body.VerdictString) | ||
if !verdict.IsValid() { | ||
modules.NewInvalidParamError("verdict", "invalid verdict").AppendToGin(ginCtx) | ||
return | ||
} | ||
|
||
judge_service.CreateJudgeResult(ginCtx, judge_model.JudgeResult{ | ||
JudgeUID: judgeUID, | ||
Verdict: verdict, | ||
TimeUsageMS: body.TimeUsageMS, | ||
MemoryUsageByte: body.MemoryUsageByte, | ||
}) | ||
} |
Oops, something went wrong.