Skip to content

Commit

Permalink
Change columns in judges table.
Browse files Browse the repository at this point in the history
  • Loading branch information
mraron committed Apr 9, 2024
1 parent 78e5fc8 commit e0ad4d1
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 114 deletions.
2 changes: 1 addition & 1 deletion cmd/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {
},

RunE: func(cmd *cobra.Command, args []string) error {
g, err := glue.New(judge2.NewClient("http://localhost:8888"), glue.WithDatabaseOption(cfg.Database))
g, err := glue.New(judge.NewClient("http://localhost:8888"), glue.WithDatabaseOption(cfg.Database))
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command {
}
}

server := judge2.NewServer(
server := judge.NewServer(
slog.Default(),
&judge2.Judge{
&judge.Judge{
SandboxProvider: provider,
ProblemStore: store,
LanguageStore: language.DefaultStore,
RateLimit: cfg.UpdateStatusLimitEvery,
},
store,
judge2.WithPortServerOption(cfg.Port),
judge.WithPortServerOption(cfg.Port),
)

return server.Run()
Expand Down
18 changes: 4 additions & 14 deletions cmd/testproblem.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"io"
"os"

"github.com/mraron/njudge/internal/judge"

"github.com/mraron/njudge/pkg/language"
"github.com/mraron/njudge/pkg/language/sandbox"
"github.com/mraron/njudge/pkg/problems"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var TestProblemArgs struct {
Expand All @@ -26,7 +14,8 @@ var TestProblemArgs struct {
var TestProblemCmd = &cobra.Command{
Use: "testproblem",
RunE: func(cmd *cobra.Command, args []string) error {
sp := sandbox.NewProvider()
//todo
/*sp := sandbox.NewProvider()
s1, _ := sandbox.NewIsolate(50)
sp.Put(s1)
s2, _ := sandbox.NewIsolate(51)
Expand Down Expand Up @@ -64,7 +53,8 @@ var TestProblemCmd = &cobra.Command{
fmt.Println(string(res))
}
return err
return err*/
return nil
},
}

Expand Down
8 changes: 4 additions & 4 deletions internal/glue/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type Glue struct {
Judge judge2.Judger
Judge judge.Judger

Submissions njudge.Submissions
Problems njudge.Problems
Expand All @@ -41,7 +41,7 @@ func WithDatabaseOption(cfg config.Database) Option {
}
}

func New(judge judge2.Judger, opts ...Option) (*Glue, error) {
func New(judge judge.Judger, opts ...Option) (*Glue, error) {
glue := &Glue{
Judge: judge,
}
Expand Down Expand Up @@ -69,12 +69,12 @@ func (g *Glue) ProcessSubmission(ctx context.Context, sub njudge.Submission) err
}

ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
status, err := g.Judge.Judge(ctx, judge2.Submission{
status, err := g.Judge.Judge(ctx, judge.Submission{
ID: strconv.Itoa(sub.ID),
Problem: prob.Problem,
Language: sub.Language,
Source: sub.Source,
}, func(result judge2.Result) error {
}, func(result judge.Result) error {
if result.Status == nil {
return fmt.Errorf("received nil status, error: %v", result.Error)
}
Expand Down
16 changes: 8 additions & 8 deletions internal/glue/glue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

type TestJudger struct {
f func(ctx context.Context, sub judge2.Submission, callback judge2.ResultCallback) (*problems.Status, error)
f func(ctx context.Context, sub judge.Submission, callback judge.ResultCallback) (*problems.Status, error)
}

func (t TestJudger) Judge(ctx context.Context, sub judge2.Submission, callback judge2.ResultCallback) (*problems.Status, error) {
func (t TestJudger) Judge(ctx context.Context, sub judge.Submission, callback judge.ResultCallback) (*problems.Status, error) {
return t.f(ctx, sub, callback)
}

Expand Down Expand Up @@ -50,7 +50,7 @@ func (f FakeProblems) Update(ctx context.Context, p njudge.Problem, fields []str

func TestGlue_ProcessSubmission(t *testing.T) {
type fields struct {
Judge judge2.Judger
Judge judge.Judger
Submissions njudge.Submissions
Problems njudge.Problems
}
Expand All @@ -70,7 +70,7 @@ func TestGlue_ProcessSubmission(t *testing.T) {
name: "compilation error",
fields: fields{
Judge: TestJudger{
f: func(ctx context.Context, sub judge2.Submission, callback judge2.ResultCallback) (*problems.Status, error) {
f: func(ctx context.Context, sub judge.Submission, callback judge.ResultCallback) (*problems.Status, error) {
return &problems.Status{
Compiled: false,
CompilerOutput: "compilation error",
Expand All @@ -94,8 +94,8 @@ func TestGlue_ProcessSubmission(t *testing.T) {
{
name: "error while running",
fields: fields{
Judge: TestJudger{f: func(ctx context.Context, sub judge2.Submission, callback judge2.ResultCallback) (*problems.Status, error) {
_ = callback(judge2.Result{
Judge: TestJudger{f: func(ctx context.Context, sub judge.Submission, callback judge.ResultCallback) (*problems.Status, error) {
_ = callback(judge.Result{
Index: 1,
Test: "test1",
Status: &problems.Status{
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestGlue_ProcessSubmission(t *testing.T) {
{
name: "no callback run RE",
fields: fields{
Judge: TestJudger{f: func(ctx context.Context, sub judge2.Submission, callback judge2.ResultCallback) (*problems.Status, error) {
Judge: TestJudger{f: func(ctx context.Context, sub judge.Submission, callback judge.ResultCallback) (*problems.Status, error) {
return &problems.Status{
Compiled: true,
CompilerOutput: "",
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestJudgeIntegration(t *testing.T) {
store := problems.NewFsStore("../judge/testdata")
_ = store.UpdateProblems()

judge := &judge2.Judge{
judge := &judge.Judge{
SandboxProvider: sandbox.NewProvider().Put(s1).Put(s2),
ProblemStore: store,
LanguageStore: language.DefaultStore,
Expand Down
3 changes: 1 addition & 2 deletions internal/judge/judge.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package judge2
package judge

import (
"bufio"
Expand Down Expand Up @@ -32,7 +32,6 @@ type Judge struct {
}

func (j *Judge) Judge(ctx context.Context, sub Submission, callback ResultCallback) (*problems.Status, error) {
fmt.Println(sub)
problem, err := j.ProblemStore.GetProblem(sub.Problem)
if err != nil {
return nil, err
Expand Down
16 changes: 15 additions & 1 deletion internal/judge/judge_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package judge2
package judge

import (
"context"
Expand Down Expand Up @@ -102,4 +102,18 @@ print(a+b)
}, nil)
assert.Nil(t, res)
assert.ErrorIs(t, err, language.ErrorLanguageNotFound)

res, err = judge.Judge(context.Background(), Submission{
ID: "",
Problem: "aplusb",
Language: "cpp14",
Source: []byte(`int main() {}
`),
}, func(result Result) error {
return nil
})
assert.NoError(t, err)
assert.NotNil(t, res)
assert.True(t, res.Compiled)

}
2 changes: 1 addition & 1 deletion internal/judge/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package judge2
package judge

import (
"encoding/json"
Expand Down
23 changes: 23 additions & 0 deletions internal/njudge/db/migrations/12_change_judges.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

alter table public.judges
drop column problem_list, drop column language_list;

alter table public.judges
add ping text,
add column host text,
add column port text,
add column state text;

update public.judges set
host = split_part(url, ':', 1),
port = split_part(url, ':', 2),
state = '{}';

alter table public.judges alter column host set not null,
alter column port set not null,
alter column state set not null;

alter table public.judges
drop column url ;


11 changes: 11 additions & 0 deletions internal/njudge/db/migrations/12_change_judges.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
alter table public.judges
add url text ;

update public.judges set
url = CONCAT(host, ':', port) ;

alter table public.judges
drop column ping, drop column host, drop column port, drop column state;

alter table public.judges
add column problem_list text[], add column language_list text[];
10 changes: 10 additions & 0 deletions internal/njudge/judge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package njudge

type Judge struct {
ID int
Name string
URL string
Online bool
ProblemList []string
LanguageList []string
}
47 changes: 8 additions & 39 deletions internal/web/handlers/api/judges.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"

"github.com/mraron/njudge/internal/njudge/db/models"
"github.com/mraron/njudge/internal/web/helpers"
"github.com/mraron/njudge/internal/web/helpers/pagination"

"github.com/volatiletech/sqlboiler/v4/boil"
Expand All @@ -24,7 +23,7 @@ func (JudgeDataProvider) Identifier() string {
return "id"
}

func (dp JudgeDataProvider) List(data *pagination.Data) ([]*helpers.Judge, error) {
func (dp JudgeDataProvider) List(data *pagination.Data) ([]*models.Judge, error) {
qms := make([]QueryMod, 0)
if data.SortField != "" {
qms = append(qms, OrderBy(data.SortField+" "+data.SortDir))
Expand All @@ -35,42 +34,19 @@ func (dp JudgeDataProvider) List(data *pagination.Data) ([]*helpers.Judge, error
qms = append(qms, Offset(data.PerPage*(data.Page-1)))
}

orig, err := models.Judges(qms...).All(context.TODO(), dp.DB)
if err != nil {
return nil, err
}

lst := make([]*helpers.Judge, len(orig))
for i := 0; i < len(orig); i++ {
elem := helpers.NewJudgeFromModelsJudge(orig[i])
lst[i] = elem
}

return lst, nil
return models.Judges(qms...).All(context.TODO(), dp.DB)
}

func (dp JudgeDataProvider) Count() (int64, error) {
return models.Judges().Count(context.TODO(), dp.DB)
}

func (dp JudgeDataProvider) Get(id string) (*helpers.Judge, error) {
elem, err := models.Judges(Where("id = ?", id)).One(context.TODO(), dp.DB)
if err != nil {
return nil, err
}

res := helpers.NewJudgeFromModelsJudge(elem)
return res, nil
func (dp JudgeDataProvider) Get(id string) (*models.Judge, error) {
return models.Judges(Where("id = ?", id)).One(context.TODO(), dp.DB)
}

func (dp JudgeDataProvider) Insert(elem *helpers.Judge) error {
model := models.Judge{}
model.Host = elem.Host
model.Port = elem.Port

err := model.Insert(context.TODO(), dp.DB, boil.Infer())
elem.Id = int64(model.ID)
return err
func (dp JudgeDataProvider) Insert(elem *models.Judge) error {
return elem.Insert(context.TODO(), dp.DB, boil.Infer())
}

func (dp JudgeDataProvider) Delete(id string) error {
Expand All @@ -83,14 +59,7 @@ func (dp JudgeDataProvider) Delete(id string) error {
return err
}

func (dp JudgeDataProvider) Update(id string, elem *helpers.Judge) error {
model, err := models.Judges(Where("id=?", id)).One(context.TODO(), dp.DB)
if err != nil {
return err
}

model.Host = elem.Host
model.Port = elem.Port
_, err = model.Update(context.TODO(), dp.DB, boil.Infer())
func (dp JudgeDataProvider) Update(id string, elem *models.Judge) error {
_, err := elem.Update(context.TODO(), dp.DB, boil.Infer())
return err
}
35 changes: 0 additions & 35 deletions internal/web/helpers/judge.go

This file was deleted.

10 changes: 5 additions & 5 deletions internal/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (s *Server) prepareRoutes(e *echo.Echo) {
v1.DELETE("/partials/:name", api.Delete[models.Partial](partialDataProvider))

judgeDataProvider := api.JudgeDataProvider{DB: s.DB}
v1.GET("/judges", api.GetList[helpers.Judge](judgeDataProvider))
v1.POST("/judges", api.Post[helpers.Judge](judgeDataProvider))
v1.GET("/judges/:id", api.Get[helpers.Judge](judgeDataProvider))
v1.PUT("/judges/:id", api.Put[helpers.Judge](judgeDataProvider))
v1.DELETE("/judges/:id", api.Delete[helpers.Judge](judgeDataProvider))
v1.GET("/judges", api.GetList[models.Judge](judgeDataProvider))
v1.POST("/judges", api.Post[models.Judge](judgeDataProvider))
v1.GET("/judges/:id", api.Get[models.Judge](judgeDataProvider))
v1.PUT("/judges/:id", api.Put[models.Judge](judgeDataProvider))
v1.DELETE("/judges/:id", api.Delete[models.Judge](judgeDataProvider))

userDataProvider := api.UserDataProvider{DB: s.DB}
v1.GET("/users", api.GetList[models.User](userDataProvider))
Expand Down
2 changes: 1 addition & 1 deletion pkg/problems/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func TestFeedbackType_UnmarshalJSON(t *testing.T) {
{"ioi", "\"FeedbackIOI\"", FeedbackIOI, assert.NoError},
{"acm", "\"FeedbackACM\"", FeedbackACM, assert.NoError},
{"lazyioi", "\"FeedbackLazyIOI\"", FeedbackLazyIOI, assert.NoError},
{"other", "\"lol\"", FeedbackUnknown, assert.Error},
{"other", "\"lol\"", FeedbackUnknown, assert.NoError},
}

for _, tt := range tests {
Expand Down

0 comments on commit e0ad4d1

Please sign in to comment.