Compare commits
No commits in common. "develop" and "listproblems-solved-count" have entirely different histories.
develop
...
listproble
15 changed files with 97 additions and 393 deletions
2
.gitmodules
vendored
2
.gitmodules
vendored
|
@ -1,3 +1,3 @@
|
||||||
[submodule "proto"]
|
[submodule "proto"]
|
||||||
path = contracts
|
path = proto
|
||||||
url = https://git.sch9.ru/new_gate/contracts
|
url = https://git.sch9.ru/new_gate/contracts
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -1,7 +1,7 @@
|
||||||
tag = latest
|
tag = latest
|
||||||
|
|
||||||
gen:
|
gen:
|
||||||
@oapi-codegen --config=config.yaml ./contracts/tester/v1/openapi.yaml
|
@oapi-codegen --config=config.yaml ./proto/tester/v1/openapi.yaml
|
||||||
dev: gen
|
dev: gen
|
||||||
@go run main.go
|
@go run main.go
|
||||||
build: gen
|
build: gen
|
||||||
|
|
108
README.md
108
README.md
|
@ -1,108 +0,0 @@
|
||||||
# ms-tester
|
|
||||||
|
|
||||||
`ms-tester` is a microservice designed for managing programming competitions. It provides backend functionality for handling problems, contests, participants, and their submissions. The service is developed in Go. PostgreSQL serves as the relational database. Pandoc is used to convert problem statements from LaTeX to HTML.
|
|
||||||
|
|
||||||
For understanding the architecture, see the [documentation](https://git.sch9.ru/new_gate/docs).
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
|
|
||||||
Before you begin, ensure you have the following dependencies installed:
|
|
||||||
|
|
||||||
* **Docker** and **Docker Compose**: To run PostgreSQL, Pandoc.
|
|
||||||
* **Goose**: For applying database migrations (`go install github.com/pressly/goose/v3/cmd/goose@latest`).
|
|
||||||
* **oapi-codegen**: For generating OpenAPI code (`go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@latest`).
|
|
||||||
|
|
||||||
### 1. Running Dependencies
|
|
||||||
|
|
||||||
You can run PostgreSQL and Pandoc using Docker Compose, for example:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
version: '3.8'
|
|
||||||
services:
|
|
||||||
pandoc:
|
|
||||||
image: pandoc/latex
|
|
||||||
ports:
|
|
||||||
- "4000:3030" # Exposes Pandoc server on port 4000 locally
|
|
||||||
command: "server" # Runs Pandoc in server mode
|
|
||||||
postgres:
|
|
||||||
image: postgres:14.1-alpine # Uses PostgreSQL 14.1 Alpine image
|
|
||||||
restart: always # Ensures the container restarts if it stops
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: postgres # Default user
|
|
||||||
POSTGRES_PASSWORD: supersecretpassword # Default password (change for production!)
|
|
||||||
POSTGRES_DB: postgres # Default database name
|
|
||||||
ports:
|
|
||||||
- '5432:5432' # Exposes PostgreSQL on the standard port 5432
|
|
||||||
volumes:
|
|
||||||
- ./postgres-data:/var/lib/postgresql/data # Persists database data locally
|
|
||||||
healthcheck:
|
|
||||||
test: pg_isready -U postgres -d postgres # Command to check if PostgreSQL is ready
|
|
||||||
interval: 10s # Check every 10 seconds
|
|
||||||
timeout: 3s # Wait 3 seconds for the check to respond
|
|
||||||
retries: 5 # Try 5 times before marking as unhealthy
|
|
||||||
volumes:
|
|
||||||
postgres-data: # Defines the named volume for data persistence
|
|
||||||
```
|
|
||||||
|
|
||||||
Start the services in detached mode:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker-compose up -d
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Configuration
|
|
||||||
|
|
||||||
The application uses environment variables for configuration. Create a `.env` file in the project root. The minimum required variables are:
|
|
||||||
|
|
||||||
```dotenv
|
|
||||||
# Environment type (development or production)
|
|
||||||
ENV=dev # or prod
|
|
||||||
|
|
||||||
# Address of the running Pandoc service
|
|
||||||
PANDOC=http://localhost:4000
|
|
||||||
|
|
||||||
# Address and port where the ms-tester service will listen
|
|
||||||
ADDRESS=localhost:8080
|
|
||||||
|
|
||||||
# PostgreSQL connection string (Data Source Name)
|
|
||||||
# Format: postgres://user:password@host:port/database?sslmode=disable
|
|
||||||
POSTGRES_DSN=postgres://username:supersecretpassword@localhost:5432/db_name?sslmode=disable
|
|
||||||
|
|
||||||
# Secret key for signing and verifying JWT tokens
|
|
||||||
JWT_SECRET=your_super_secret_jwt_key
|
|
||||||
```
|
|
||||||
|
|
||||||
**Important:** Replace `supersecretpassword` and `your_super_secret_jwt_key` with secure, unique values, especially for a production environment.
|
|
||||||
|
|
||||||
### 3. Database Migrations
|
|
||||||
|
|
||||||
The project uses `goose` to manage the database schema.
|
|
||||||
|
|
||||||
1. Ensure `goose` is installed:
|
|
||||||
```bash
|
|
||||||
go install github.com/pressly/goose/v3/cmd/goose@latest
|
|
||||||
```
|
|
||||||
2. Apply the migrations to the running PostgreSQL database. Make sure the connection string in the command matches the `POSTGRES_DSN` from your `.env` file:
|
|
||||||
```bash
|
|
||||||
goose -dir ./migrations postgres "postgres://postgres:supersecretpassword@localhost:5432/postgres?sslmode=disable" up
|
|
||||||
```
|
|
||||||
|
|
||||||
### 4. OpenAPI Code Generation
|
|
||||||
|
|
||||||
The project uses OpenAPI to define its API. Go code for handlers and models is generated based on this specification using `oapi-codegen`.
|
|
||||||
|
|
||||||
Run the generation command:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
make gen
|
|
||||||
```
|
|
||||||
|
|
||||||
### 5. Running the Application
|
|
||||||
|
|
||||||
Start the `ms-tester` service:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
go run ./main.go
|
|
||||||
```
|
|
||||||
|
|
||||||
After starting, the service will be available at the address specified in the `ADDRESS` variable in your `.env` file (e.g., `http://localhost:8080`).
|
|
|
@ -2,4 +2,4 @@ package: testerv1
|
||||||
generate:
|
generate:
|
||||||
fiber-server: true
|
fiber-server: true
|
||||||
models: true
|
models: true
|
||||||
output: ./contracts/tester/v1/tester.go
|
output: ./proto/tester/v1/tester.go
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit f00483d24a53a243734c793fc24e02d52d39fdab
|
|
|
@ -3,29 +3,17 @@ package models
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Solution struct {
|
type Solution struct {
|
||||||
Id int32 `db:"id"`
|
Id int32 `db:"id"`
|
||||||
|
TaskId int32 `db:"task_id"`
|
||||||
ParticipantId int32 `db:"participant_id"`
|
ParticipantId int32 `db:"participant_id"`
|
||||||
ParticipantName string `db:"participant_name"`
|
Solution string `db:"solution"`
|
||||||
|
State int32 `db:"state"`
|
||||||
Solution string `db:"solution"`
|
Score int32 `db:"score"`
|
||||||
|
Penalty int32 `db:"penalty"`
|
||||||
State int32 `db:"state"`
|
TotalScore int32 `db:"total_score"`
|
||||||
Score int32 `db:"score"`
|
Language int32 `db:"language"`
|
||||||
Penalty int32 `db:"penalty"`
|
UpdatedAt time.Time `db:"updated_at"`
|
||||||
TimeStat int32 `db:"time_stat"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
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 {
|
type SolutionCreation struct {
|
||||||
|
@ -37,27 +25,17 @@ type SolutionCreation struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SolutionsListItem struct {
|
type SolutionsListItem struct {
|
||||||
Id int32 `db:"id"`
|
Id int32 `db:"id"`
|
||||||
|
TaskId int32 `db:"task_id"`
|
||||||
ParticipantId int32 `db:"participant_id"`
|
ContestId int32 `db:"contest_id"`
|
||||||
ParticipantName string `db:"participant_name"`
|
ParticipantId int32 `db:"participant_id"`
|
||||||
|
State int32 `db:"state"`
|
||||||
State int32 `db:"state"`
|
Score int32 `db:"score"`
|
||||||
Score int32 `db:"score"`
|
Penalty int32 `db:"penalty"`
|
||||||
Penalty int32 `db:"penalty"`
|
TotalScore int32 `db:"total_score"`
|
||||||
TimeStat int32 `db:"time_stat"`
|
Language int32 `db:"language"`
|
||||||
MemoryStat int32 `db:"memory_stat"`
|
UpdatedAt time.Time `db:"updated_at"`
|
||||||
Language int32 `db:"language"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
|
|
||||||
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 {
|
type SolutionsList struct {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package tester
|
package tester
|
||||||
|
|
||||||
import (
|
import (
|
||||||
testerv1 "git.sch9.ru/new_gate/ms-tester/contracts/tester/v1"
|
testerv1 "git.sch9.ru/new_gate/ms-tester/proto/tester/v1"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package rest
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
testerv1 "git.sch9.ru/new_gate/ms-tester/contracts/tester/v1"
|
|
||||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||||
"git.sch9.ru/new_gate/ms-tester/internal/tester"
|
"git.sch9.ru/new_gate/ms-tester/internal/tester"
|
||||||
"git.sch9.ru/new_gate/ms-tester/pkg"
|
"git.sch9.ru/new_gate/ms-tester/pkg"
|
||||||
|
testerv1 "git.sch9.ru/new_gate/ms-tester/proto/tester/v1"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
@ -99,15 +99,6 @@ func (h *TesterHandlers) GetContest(c *fiber.Ctx, id int32) error {
|
||||||
return c.SendStatus(pkg.ToREST(err))
|
return c.SendStatus(pkg.ToREST(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
var participantId int32 = 2
|
|
||||||
|
|
||||||
solutions, err := h.contestsUC.ReadBestSolutions(c.Context(), id, participantId)
|
|
||||||
|
|
||||||
m := make(map[int32]*models.Solution)
|
|
||||||
|
|
||||||
for i := 0; i < len(solutions); i++ {
|
|
||||||
m[solutions[i].TaskPosition] = solutions[i]
|
|
||||||
}
|
|
||||||
resp := testerv1.GetContestResponse{
|
resp := testerv1.GetContestResponse{
|
||||||
Contest: C2C(*contest),
|
Contest: C2C(*contest),
|
||||||
Tasks: make([]struct {
|
Tasks: make([]struct {
|
||||||
|
@ -117,15 +108,11 @@ func (h *TesterHandlers) GetContest(c *fiber.Ctx, id int32) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, task := range tasks {
|
for i, task := range tasks {
|
||||||
solution := testerv1.Solution{}
|
|
||||||
if sol, ok := m[task.Position]; ok {
|
|
||||||
solution = S2S(*sol)
|
|
||||||
}
|
|
||||||
resp.Tasks[i] = struct {
|
resp.Tasks[i] = struct {
|
||||||
Solution testerv1.Solution `json:"solution"`
|
Solution testerv1.Solution `json:"solution"`
|
||||||
Task testerv1.TasksListItem `json:"task"`
|
Task testerv1.TasksListItem `json:"task"`
|
||||||
}{
|
}{
|
||||||
Solution: solution,
|
Solution: testerv1.Solution{},
|
||||||
Task: TLI2TLI(*task),
|
Task: TLI2TLI(*task),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -552,54 +539,32 @@ func PTLI2PTLI(p models.ParticipantsListItem) testerv1.ParticipantsListItem {
|
||||||
|
|
||||||
func SLI2SLI(s models.SolutionsListItem) testerv1.SolutionsListItem {
|
func SLI2SLI(s models.SolutionsListItem) testerv1.SolutionsListItem {
|
||||||
return testerv1.SolutionsListItem{
|
return testerv1.SolutionsListItem{
|
||||||
Id: s.Id,
|
ContestId: s.ContestId,
|
||||||
|
CreatedAt: s.CreatedAt,
|
||||||
ParticipantId: s.ParticipantId,
|
Id: s.Id,
|
||||||
ParticipantName: s.ParticipantName,
|
Language: s.Language,
|
||||||
|
ParticipantId: s.ParticipantId,
|
||||||
State: s.State,
|
Penalty: s.Penalty,
|
||||||
Score: s.Score,
|
Score: s.Score,
|
||||||
Penalty: s.Penalty,
|
State: s.State,
|
||||||
TimeStat: s.TimeStat,
|
TaskId: s.TaskId,
|
||||||
MemoryStat: s.MemoryStat,
|
TotalScore: s.TotalScore,
|
||||||
Language: s.Language,
|
UpdatedAt: s.UpdatedAt,
|
||||||
|
|
||||||
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 {
|
func S2S(s models.Solution) testerv1.Solution {
|
||||||
return testerv1.Solution{
|
return testerv1.Solution{
|
||||||
Id: s.Id,
|
Id: s.Id,
|
||||||
|
TaskId: s.TaskId,
|
||||||
ParticipantId: s.ParticipantId,
|
ParticipantId: s.ParticipantId,
|
||||||
ParticipantName: s.ParticipantName,
|
Solution: s.Solution,
|
||||||
|
State: s.State,
|
||||||
Solution: s.Solution,
|
Score: s.Score,
|
||||||
|
Penalty: s.Penalty,
|
||||||
State: s.State,
|
TotalScore: s.TotalScore,
|
||||||
Score: s.Score,
|
Language: s.Language,
|
||||||
Penalty: s.Penalty,
|
CreatedAt: s.CreatedAt,
|
||||||
TimeStat: s.TimeStat,
|
UpdatedAt: s.UpdatedAt,
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,5 +49,4 @@ type ContestRepository interface {
|
||||||
ListSolutions(ctx context.Context, filter models.SolutionsFilter) (*models.SolutionsList, error)
|
ListSolutions(ctx context.Context, filter models.SolutionsFilter) (*models.SolutionsList, error)
|
||||||
ReadTask(ctx context.Context, id int32) (*models.Task, error)
|
ReadTask(ctx context.Context, id int32) (*models.Task, error)
|
||||||
ReadMonitor(ctx context.Context, id int32) (*models.Monitor, error)
|
ReadMonitor(ctx context.Context, id int32) (*models.Monitor, error)
|
||||||
ReadBestSolutions(ctx context.Context, contestId int32, participantId int32) ([]*models.Solution, error)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -317,56 +317,44 @@ func (r *ContestRepository) ListSolutions(ctx context.Context, filter models.Sol
|
||||||
const op = "ContestRepository.ListSolutions"
|
const op = "ContestRepository.ListSolutions"
|
||||||
|
|
||||||
baseQuery := `
|
baseQuery := `
|
||||||
SELECT s.id,
|
SELECT
|
||||||
|
s.id,
|
||||||
s.participant_id,
|
s.task_id,
|
||||||
p2.name as participant_name,
|
t.contest_id,
|
||||||
|
s.participant_id,
|
||||||
s.state,
|
s.state,
|
||||||
s.score,
|
s.score,
|
||||||
s.penalty,
|
s.penalty,
|
||||||
s.time_stat,
|
s.total_score,
|
||||||
s.memory_stat,
|
s.language,
|
||||||
s.language,
|
s.updated_at,
|
||||||
|
s.created_at
|
||||||
s.task_id,
|
FROM solutions s
|
||||||
t.position as task_position,
|
LEFT JOIN tasks t ON s.task_id = t.id
|
||||||
p.title as task_title,
|
WHERE 1=1
|
||||||
|
|
||||||
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 conditions []string
|
||||||
var args []interface{}
|
var args []interface{}
|
||||||
|
|
||||||
if filter.ContestId != nil {
|
if filter.ContestId != nil {
|
||||||
conditions = append(conditions, "s.contest_id = ?")
|
conditions = append(conditions, "contest_id = ?")
|
||||||
args = append(args, *filter.ContestId)
|
args = append(args, *filter.ContestId)
|
||||||
}
|
}
|
||||||
if filter.ParticipantId != nil {
|
if filter.ParticipantId != nil {
|
||||||
conditions = append(conditions, "s.participant_id = ?")
|
conditions = append(conditions, "participant_id = ?")
|
||||||
args = append(args, *filter.ParticipantId)
|
args = append(args, *filter.ParticipantId)
|
||||||
}
|
}
|
||||||
if filter.TaskId != nil {
|
if filter.TaskId != nil {
|
||||||
conditions = append(conditions, "s.task_id = ?")
|
conditions = append(conditions, "task_id = ?")
|
||||||
args = append(args, *filter.TaskId)
|
args = append(args, *filter.TaskId)
|
||||||
}
|
}
|
||||||
if filter.Language != nil {
|
if filter.Language != nil {
|
||||||
conditions = append(conditions, "s.language = ?")
|
conditions = append(conditions, "language = ?")
|
||||||
args = append(args, *filter.Language)
|
args = append(args, *filter.Language)
|
||||||
}
|
}
|
||||||
if filter.State != nil {
|
if filter.State != nil {
|
||||||
conditions = append(conditions, "s.state = ?")
|
conditions = append(conditions, "state = ?")
|
||||||
args = append(args, *filter.State)
|
args = append(args, *filter.State)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,64 +464,40 @@ ORDER BY t.position;
|
||||||
WITH RankedSolutions AS (
|
WITH RankedSolutions AS (
|
||||||
SELECT
|
SELECT
|
||||||
s.id,
|
s.id,
|
||||||
|
s.task_id,
|
||||||
s.participant_id,
|
s.participant_id,
|
||||||
p2.name as participant_name,
|
|
||||||
|
|
||||||
s.state,
|
s.state,
|
||||||
s.score,
|
s.score,
|
||||||
s.penalty,
|
s.penalty,
|
||||||
s.time_stat,
|
s.total_score,
|
||||||
s.memory_stat,
|
|
||||||
s.language,
|
s.language,
|
||||||
|
|
||||||
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,
|
s.created_at,
|
||||||
|
s.updated_at,
|
||||||
|
t.contest_id,
|
||||||
ROW_NUMBER() OVER (
|
ROW_NUMBER() OVER (
|
||||||
PARTITION BY s.participant_id, s.task_id
|
PARTITION BY s.participant_id, s.task_id
|
||||||
ORDER BY
|
ORDER BY
|
||||||
CASE WHEN s.state = 5 THEN 0 ELSE 1 END,
|
CASE WHEN s.state = 5 THEN 0 ELSE 1 END,
|
||||||
s.created_at
|
s.created_at
|
||||||
) as rn
|
) as rn
|
||||||
FROM solutions s
|
FROM solutions s
|
||||||
LEFT JOIN tasks t ON s.task_id = t.id
|
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 = ?
|
WHERE t.contest_id = ?
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
rs.id,
|
rs.id,
|
||||||
|
rs.task_id,
|
||||||
|
rs.contest_id,
|
||||||
rs.participant_id,
|
rs.participant_id,
|
||||||
rs.participant_name,
|
|
||||||
|
|
||||||
rs.state,
|
rs.state,
|
||||||
rs.score,
|
rs.score,
|
||||||
rs.penalty,
|
rs.penalty,
|
||||||
rs.time_stat,
|
rs.total_score,
|
||||||
rs.memory_stat,
|
|
||||||
rs.language,
|
rs.language,
|
||||||
|
rs.created_at,
|
||||||
rs.task_id,
|
rs.updated_at
|
||||||
rs.task_position,
|
|
||||||
rs.task_title,
|
|
||||||
|
|
||||||
rs.contest_id,
|
|
||||||
rs.contest_title,
|
|
||||||
|
|
||||||
rs.updated_at,
|
|
||||||
rs.created_at
|
|
||||||
FROM RankedSolutions rs
|
FROM RankedSolutions rs
|
||||||
WHERE rs.rn = 1;
|
WHERE rs.rn = 1
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
participantsQuery = `
|
participantsQuery = `
|
||||||
|
@ -626,65 +590,3 @@ func (r *ContestRepository) ReadMonitor(ctx context.Context, contestId int32) (*
|
||||||
|
|
||||||
return &monitor, nil
|
return &monitor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
// state=5 - AC
|
|
||||||
readBestSolutions = `
|
|
||||||
WITH contest_tasks AS (
|
|
||||||
SELECT t.id AS task_id,
|
|
||||||
t.position AS task_position,
|
|
||||||
t.contest_id,
|
|
||||||
t.problem_id,
|
|
||||||
t.created_at,
|
|
||||||
t.updated_at,
|
|
||||||
p.title AS task_title,
|
|
||||||
c.title AS contest_title
|
|
||||||
FROM tasks t
|
|
||||||
LEFT JOIN problems p ON p.id = t.problem_id
|
|
||||||
LEFT JOIN contests c ON c.id = t.contest_id
|
|
||||||
WHERE t.contest_id = ?
|
|
||||||
),
|
|
||||||
best_solutions AS (
|
|
||||||
SELECT DISTINCT ON (s.task_id)
|
|
||||||
*
|
|
||||||
FROM solutions s
|
|
||||||
WHERE s.participant_id = ?
|
|
||||||
ORDER BY s.task_id, s.score DESC, s.created_at DESC
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
s.id,
|
|
||||||
s.participant_id,
|
|
||||||
p.name AS participant_name,
|
|
||||||
s.solution,
|
|
||||||
s.state,
|
|
||||||
s.score,
|
|
||||||
s.penalty,
|
|
||||||
s.time_stat,
|
|
||||||
s.memory_stat,
|
|
||||||
s.language,
|
|
||||||
ct.task_id,
|
|
||||||
ct.task_position,
|
|
||||||
ct.task_title,
|
|
||||||
ct.contest_id,
|
|
||||||
ct.contest_title,
|
|
||||||
s.updated_at,
|
|
||||||
s.created_at
|
|
||||||
FROM contest_tasks ct
|
|
||||||
LEFT JOIN best_solutions s ON s.task_id = ct.task_id
|
|
||||||
LEFT JOIN participants p ON p.id = s.participant_id WHERE s.id IS NOT NULL
|
|
||||||
ORDER BY ct.task_position
|
|
||||||
`
|
|
||||||
)
|
|
||||||
|
|
||||||
func (r *ContestRepository) ReadBestSolutions(ctx context.Context, contestId int32, participantId int32) ([]*models.Solution, error) {
|
|
||||||
const op = "ContestRepository.ReadBestSolutions"
|
|
||||||
var solutions []*models.Solution
|
|
||||||
query := r.db.Rebind(readBestSolutions)
|
|
||||||
err := r.db.SelectContext(ctx, &solutions, query, contestId, participantId)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, handlePgErr(err, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
return solutions, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -31,5 +31,4 @@ type ContestUseCase interface {
|
||||||
ListSolutions(ctx context.Context, filter models.SolutionsFilter) (*models.SolutionsList, error)
|
ListSolutions(ctx context.Context, filter models.SolutionsFilter) (*models.SolutionsList, error)
|
||||||
ReadTask(ctx context.Context, id int32) (*models.Task, error)
|
ReadTask(ctx context.Context, id int32) (*models.Task, error)
|
||||||
ReadMonitor(ctx context.Context, id int32) (*models.Monitor, error)
|
ReadMonitor(ctx context.Context, id int32) (*models.Monitor, error)
|
||||||
ReadBestSolutions(ctx context.Context, contestId int32, participantId int32) ([]*models.Solution, error)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,7 +85,3 @@ func (uc *ContestUseCase) ReadTask(ctx context.Context, id int32) (*models.Task,
|
||||||
func (uc *ContestUseCase) ReadMonitor(ctx context.Context, contestId int32) (*models.Monitor, error) {
|
func (uc *ContestUseCase) ReadMonitor(ctx context.Context, contestId int32) (*models.Monitor, error) {
|
||||||
return uc.contestRepo.ReadMonitor(ctx, contestId)
|
return uc.contestRepo.ReadMonitor(ctx, contestId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ContestUseCase) ReadBestSolutions(ctx context.Context, contestId int32, participantId int32) ([]*models.Solution, error) {
|
|
||||||
return uc.contestRepo.ReadBestSolutions(ctx, contestId, participantId)
|
|
||||||
}
|
|
||||||
|
|
2
main.go
2
main.go
|
@ -3,11 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.sch9.ru/new_gate/ms-tester/config"
|
"git.sch9.ru/new_gate/ms-tester/config"
|
||||||
testerv1 "git.sch9.ru/new_gate/ms-tester/contracts/tester/v1"
|
|
||||||
"git.sch9.ru/new_gate/ms-tester/internal/tester/delivery/rest"
|
"git.sch9.ru/new_gate/ms-tester/internal/tester/delivery/rest"
|
||||||
problemsRepository "git.sch9.ru/new_gate/ms-tester/internal/tester/repository"
|
problemsRepository "git.sch9.ru/new_gate/ms-tester/internal/tester/repository"
|
||||||
testerUseCase "git.sch9.ru/new_gate/ms-tester/internal/tester/usecase"
|
testerUseCase "git.sch9.ru/new_gate/ms-tester/internal/tester/usecase"
|
||||||
"git.sch9.ru/new_gate/ms-tester/pkg"
|
"git.sch9.ru/new_gate/ms-tester/pkg"
|
||||||
|
testerv1 "git.sch9.ru/new_gate/ms-tester/proto/tester/v1"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
fiberlogger "github.com/gofiber/fiber/v2/middleware/logger"
|
fiberlogger "github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
"github.com/ilyakaznacheev/cleanenv"
|
"github.com/ilyakaznacheev/cleanenv"
|
||||||
|
|
|
@ -9,24 +9,6 @@ BEGIN
|
||||||
END;
|
END;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE FUNCTION check_max_tasks() RETURNS TRIGGER
|
|
||||||
LANGUAGE plpgsql AS
|
|
||||||
$$
|
|
||||||
DECLARE
|
|
||||||
max_on_contest_tasks_amount integer := 50;
|
|
||||||
BEGIN
|
|
||||||
IF (
|
|
||||||
SELECT count(*) FROM tasks
|
|
||||||
WHERE contest_id = NEW.contest_id
|
|
||||||
) >= (
|
|
||||||
max_on_contest_tasks_amount
|
|
||||||
) THEN
|
|
||||||
RAISE EXCEPTION 'Exceeded max tasks for this contest';
|
|
||||||
END IF;
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS problems
|
CREATE TABLE IF NOT EXISTS problems
|
||||||
(
|
(
|
||||||
id serial NOT NULL,
|
id serial NOT NULL,
|
||||||
|
@ -91,11 +73,6 @@ CREATE TABLE IF NOT EXISTS tasks
|
||||||
CHECK (position >= 0)
|
CHECK (position >= 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TRIGGER max_tasks_on_contest_check
|
|
||||||
BEFORE INSERT ON tasks
|
|
||||||
FOR EACH STATEMENT
|
|
||||||
EXECUTE FUNCTION check_max_tasks();
|
|
||||||
|
|
||||||
CREATE TRIGGER on_tasks_update
|
CREATE TRIGGER on_tasks_update
|
||||||
BEFORE UPDATE
|
BEFORE UPDATE
|
||||||
ON tasks
|
ON tasks
|
||||||
|
@ -121,7 +98,6 @@ CREATE TRIGGER on_participants_update
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
EXECUTE PROCEDURE updated_at_update();
|
EXECUTE PROCEDURE updated_at_update();
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS solutions
|
CREATE TABLE IF NOT EXISTS solutions
|
||||||
(
|
(
|
||||||
id serial NOT NULL,
|
id serial NOT NULL,
|
||||||
|
@ -131,8 +107,7 @@ CREATE TABLE IF NOT EXISTS solutions
|
||||||
state integer NOT NULL DEFAULT 1,
|
state integer NOT NULL DEFAULT 1,
|
||||||
score integer NOT NULL DEFAULT 0,
|
score integer NOT NULL DEFAULT 0,
|
||||||
penalty integer NOT NULL,
|
penalty integer NOT NULL,
|
||||||
time_stat integer NOT NULL DEFAULT 0,
|
total_score integer NOT NULL DEFAULT 0,
|
||||||
memory_stat integer NOT NULL DEFAULT 0,
|
|
||||||
language integer NOT NULL,
|
language integer NOT NULL,
|
||||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||||
created_at timestamptz NOT NULL DEFAULT now(),
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
@ -149,17 +124,15 @@ EXECUTE PROCEDURE updated_at_update();
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
-- +goose StatementBegin
|
-- +goose StatementBegin
|
||||||
DROP TRIGGER IF EXISTS on_solutions_update ON solutions;
|
|
||||||
DROP TABLE IF EXISTS solutions;
|
|
||||||
DROP TRIGGER IF EXISTS on_participants_update ON participants;
|
|
||||||
DROP TABLE IF EXISTS participants;
|
|
||||||
DROP TRIGGER IF EXISTS on_tasks_update ON tasks;
|
|
||||||
DROP TRIGGER IF EXISTS max_tasks_on_contest_check ON tasks;
|
|
||||||
DROP TABLE IF EXISTS tasks;
|
|
||||||
DROP TRIGGER IF EXISTS on_problems_update ON problems;
|
DROP TRIGGER IF EXISTS on_problems_update ON problems;
|
||||||
DROP TABLE IF EXISTS problems;
|
DROP TABLE IF EXISTS problems;
|
||||||
DROP TRIGGER IF EXISTS on_contests_update ON contests;
|
DROP TRIGGER IF EXISTS on_contests_update ON contests;
|
||||||
DROP TABLE IF EXISTS contests;
|
DROP TABLE IF EXISTS contests;
|
||||||
DROP FUNCTION IF EXISTS updated_at_update();
|
DROP TRIGGER IF EXISTS on_tasks_update ON tasks;
|
||||||
DROP FUNCTION IF EXISTS check_max_tasks();
|
DROP TABLE IF EXISTS tasks;
|
||||||
|
DROP TRIGGER IF EXISTS on_participants_update ON participants;
|
||||||
|
DROP TABLE IF EXISTS participants;
|
||||||
|
DROP TRIGGER IF EXISTS on_solutions_update ON solutions;
|
||||||
|
DROP TABLE IF EXISTS solutions;
|
||||||
|
DROP FUNCTION updated_at_update();
|
||||||
-- +goose StatementEnd
|
-- +goose StatementEnd
|
1
proto
Submodule
1
proto
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 16781a46412eea455f27372045c216126c39d628
|
Loading…
Add table
Reference in a new issue