feat(solution): update solution fields

This commit is contained in:
Vyacheslav1557 2025-04-01 18:01:20 +05:00
parent a27e311526
commit d1d8566b98
5 changed files with 163 additions and 82 deletions

View file

@ -3,17 +3,29 @@ package models
import "time"
type Solution struct {
Id int32 `db:"id"`
TaskId int32 `db:"task_id"`
ParticipantId int32 `db:"participant_id"`
Solution string `db:"solution"`
State int32 `db:"state"`
Score int32 `db:"score"`
Penalty int32 `db:"penalty"`
TotalScore int32 `db:"total_score"`
Language int32 `db:"language"`
UpdatedAt time.Time `db:"updated_at"`
CreatedAt time.Time `db:"created_at"`
Id int32 `db:"id"`
ParticipantId int32 `db:"participant_id"`
ParticipantName string `db:"participant_name"`
Solution string `db:"solution"`
State int32 `db:"state"`
Score int32 `db:"score"`
Penalty int32 `db:"penalty"`
TimeStat int32 `db:"time_stat"`
MemoryStat int32 `db:"memory_stat"`
Language int32 `db:"language"`
TaskId int32 `db:"task_id"`
TaskPosition int32 `db:"task_position"`
TaskTitle string `db:"task_title"`
ContestId int32 `db:"contest_id"`
ContestTitle string `db:"contest_title"`
UpdatedAt time.Time `db:"updated_at"`
CreatedAt time.Time `db:"created_at"`
}
type SolutionCreation struct {
@ -25,17 +37,27 @@ type SolutionCreation struct {
}
type SolutionsListItem struct {
Id int32 `db:"id"`
TaskId int32 `db:"task_id"`
ContestId int32 `db:"contest_id"`
ParticipantId int32 `db:"participant_id"`
State int32 `db:"state"`
Score int32 `db:"score"`
Penalty int32 `db:"penalty"`
TotalScore int32 `db:"total_score"`
Language int32 `db:"language"`
UpdatedAt time.Time `db:"updated_at"`
CreatedAt time.Time `db:"created_at"`
Id int32 `db:"id"`
ParticipantId int32 `db:"participant_id"`
ParticipantName string `db:"participant_name"`
State int32 `db:"state"`
Score int32 `db:"score"`
Penalty int32 `db:"penalty"`
TimeStat int32 `db:"time_stat"`
MemoryStat int32 `db:"memory_stat"`
Language int32 `db:"language"`
TaskId int32 `db:"task_id"`
TaskPosition int32 `db:"task_position"`
TaskTitle string `db:"task_title"`
ContestId int32 `db:"contest_id"`
ContestTitle string `db:"contest_title"`
UpdatedAt time.Time `db:"updated_at"`
CreatedAt time.Time `db:"created_at"`
}
type SolutionsList struct {

View file

@ -538,32 +538,54 @@ func PTLI2PTLI(p models.ParticipantsListItem) testerv1.ParticipantsListItem {
func SLI2SLI(s models.SolutionsListItem) testerv1.SolutionsListItem {
return testerv1.SolutionsListItem{
ContestId: s.ContestId,
CreatedAt: s.CreatedAt,
Id: s.Id,
Language: s.Language,
ParticipantId: s.ParticipantId,
Penalty: s.Penalty,
Score: s.Score,
State: s.State,
TaskId: s.TaskId,
TotalScore: s.TotalScore,
UpdatedAt: s.UpdatedAt,
Id: s.Id,
ParticipantId: s.ParticipantId,
ParticipantName: s.ParticipantName,
State: s.State,
Score: s.Score,
Penalty: s.Penalty,
TimeStat: s.TimeStat,
MemoryStat: s.MemoryStat,
Language: s.Language,
TaskId: s.TaskId,
TaskPosition: s.TaskPosition,
TaskTitle: s.TaskTitle,
ContestId: s.ContestId,
ContestTitle: s.ContestTitle,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
}
}
func S2S(s models.Solution) testerv1.Solution {
return testerv1.Solution{
Id: s.Id,
TaskId: s.TaskId,
ParticipantId: s.ParticipantId,
Solution: s.Solution,
State: s.State,
Score: s.Score,
Penalty: s.Penalty,
TotalScore: s.TotalScore,
Language: s.Language,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
Id: s.Id,
ParticipantId: s.ParticipantId,
ParticipantName: s.ParticipantName,
Solution: s.Solution,
State: s.State,
Score: s.Score,
Penalty: s.Penalty,
TimeStat: s.TimeStat,
MemoryStat: s.MemoryStat,
Language: s.Language,
TaskId: s.TaskId,
TaskPosition: s.TaskPosition,
TaskTitle: s.TaskTitle,
ContestId: s.ContestId,
ContestTitle: s.ContestTitle,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
}
}

View file

@ -317,44 +317,56 @@ func (r *ContestRepository) ListSolutions(ctx context.Context, filter models.Sol
const op = "ContestRepository.ListSolutions"
baseQuery := `
SELECT
s.id,
s.task_id,
t.contest_id,
s.participant_id,
s.state,
s.score,
s.penalty,
s.total_score,
s.language,
s.updated_at,
s.created_at
FROM solutions s
LEFT JOIN tasks t ON s.task_id = t.id
WHERE 1=1
SELECT s.id,
s.participant_id,
p2.name as participant_name,
s.state,
s.score,
s.penalty,
s.time_stat,
s.memory_stat,
s.language,
s.task_id,
t.position as task_position,
p.title as task_title,
t.contest_id,
c.title,
s.updated_at,
s.created_at
FROM solutions s
LEFT JOIN tasks t ON s.task_id = t.id
LEFT JOIN problems p ON t.problem_id = p.id
LEFT JOIN contests c ON t.contest_id = c.id
LEFT JOIN participants p2 on s.participant_id = p2.id
WHERE 1=1
`
var conditions []string
var args []interface{}
if filter.ContestId != nil {
conditions = append(conditions, "contest_id = ?")
conditions = append(conditions, "s.contest_id = ?")
args = append(args, *filter.ContestId)
}
if filter.ParticipantId != nil {
conditions = append(conditions, "participant_id = ?")
conditions = append(conditions, "s.participant_id = ?")
args = append(args, *filter.ParticipantId)
}
if filter.TaskId != nil {
conditions = append(conditions, "task_id = ?")
conditions = append(conditions, "s.task_id = ?")
args = append(args, *filter.TaskId)
}
if filter.Language != nil {
conditions = append(conditions, "language = ?")
conditions = append(conditions, "s.language = ?")
args = append(args, *filter.Language)
}
if filter.State != nil {
conditions = append(conditions, "state = ?")
conditions = append(conditions, "s.state = ?")
args = append(args, *filter.State)
}
@ -462,42 +474,66 @@ ORDER BY t.position;
solutionsQuery = `
WITH RankedSolutions AS (
SELECT
SELECT
s.id,
s.task_id,
s.participant_id,
p2.name as participant_name,
s.state,
s.score,
s.penalty,
s.total_score,
s.time_stat,
s.memory_stat,
s.language,
s.created_at,
s.updated_at,
s.task_id,
t.position as task_position,
p.title as task_title,
t.contest_id,
c.title as contest_title,
s.updated_at,
s.created_at,
ROW_NUMBER() OVER (
PARTITION BY s.participant_id, s.task_id
ORDER BY
PARTITION BY s.participant_id, s.task_id
ORDER BY
CASE WHEN s.state = 5 THEN 0 ELSE 1 END,
s.created_at
) as rn
) as rn
FROM solutions s
JOIN tasks t ON s.task_id = t.id
LEFT JOIN tasks t ON s.task_id = t.id
LEFT JOIN problems p ON t.problem_id = p.id
LEFT JOIN contests c ON t.contest_id = c.id
LEFT JOIN participants p2 on s.participant_id = p2.id
WHERE t.contest_id = ?
)
SELECT
SELECT
rs.id,
rs.task_id,
rs.contest_id,
rs.participant_id,
rs.participant_name,
rs.state,
rs.score,
rs.penalty,
rs.total_score,
rs.time_stat,
rs.memory_stat,
rs.language,
rs.created_at,
rs.updated_at
rs.task_id,
rs.task_position,
rs.task_title,
rs.contest_id,
rs.contest_title,
rs.updated_at,
rs.created_at
FROM RankedSolutions rs
WHERE rs.rn = 1
WHERE rs.rn = 1;
`
participantsQuery = `

View file

@ -107,7 +107,8 @@ CREATE TABLE IF NOT EXISTS solutions
state integer NOT NULL DEFAULT 1,
score integer NOT NULL DEFAULT 0,
penalty integer NOT NULL,
total_score integer NOT NULL DEFAULT 0,
time_stat integer NOT NULL DEFAULT 0,
memory_stat integer NOT NULL DEFAULT 0,
language integer NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now(),
created_at timestamptz NOT NULL DEFAULT now(),

2
proto

@ -1 +1 @@
Subproject commit 16781a46412eea455f27372045c216126c39d628
Subproject commit 1fbee7ba29c358c76d1c835ac6999ce9e1b59ee9