feat(practice): store code submissions and practice states apart from attempts
This commit is contained in:
1 parent
d5e169e71b
commit
51770eea6c
61 files changed
+8584
-135
No files matched your search
@@ -0,0 +1,285 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var _ outbound.AssessmentPracticeRepositories = (*UnitOfWork)(nil)
|
||||
|
||||
func (unit *UnitOfWork) AssessmentPractices() outbound.AssessmentPracticeRepository {
|
||||
return assessmentPracticeRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
type assessmentPracticeRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) Create(ctx context.Context, practice domain.AssessmentPractice) error {
|
||||
const location = "assessmentPracticeRepository.Create"
|
||||
if practice.Status != domain.AssessmentPracticeNotStarted {
|
||||
return fmt.Errorf("%s - a new practice must not be started: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
if err := practice.Validate(); err != nil {
|
||||
return fmt.Errorf("%s - validate practice: %w", location, err)
|
||||
}
|
||||
task, err := json.Marshal(practice.Task)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - encode task: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
if err := repository.queries.InsertAssessmentPractice(ctx, sqlc.InsertAssessmentPracticeParams{
|
||||
ID: pgUUID(practice.ID), AttemptID: pgUUID(practice.AttemptID.UUID()), UserID: pgUUID(practice.UserID),
|
||||
Kind: string(practice.Kind), Task: task, CreatedAt: pgTime(practice.CreatedAt),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - insert practice: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) LockAttempt(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
attemptID domain.AssessmentAttemptID,
|
||||
) (outbound.PracticeAttemptLock, error) {
|
||||
const location = "assessmentPracticeRepository.LockAttempt"
|
||||
row, err := repository.queries.LockAssessmentAttemptForPractice(ctx, sqlc.LockAssessmentAttemptForPracticeParams{
|
||||
AttemptID: pgUUID(attemptID.UUID()), UserID: pgUUID(userID),
|
||||
})
|
||||
if isNoRows(err) {
|
||||
return outbound.PracticeAttemptLock{}, fmt.Errorf("%s - lock attempt: %w", location, outbound.ErrAssessmentAttemptNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return outbound.PracticeAttemptLock{}, fmt.Errorf("%s - lock attempt: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
if !row.ExpiresAt.Valid || !row.DatabaseTime.Valid {
|
||||
return outbound.PracticeAttemptLock{}, errors.New(location + " - attempt deadline or database time is missing")
|
||||
}
|
||||
return outbound.PracticeAttemptLock{
|
||||
State: domain.AssessmentAttemptState(row.State),
|
||||
ExpiresAt: row.ExpiresAt.Time.UTC(),
|
||||
DatabaseTime: row.DatabaseTime.Time.UTC().Truncate(time.Microsecond),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) LockPractice(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
attemptID domain.AssessmentAttemptID,
|
||||
practiceID uuid.UUID,
|
||||
) (domain.AssessmentPractice, error) {
|
||||
const location = "assessmentPracticeRepository.LockPractice"
|
||||
row, err := repository.queries.LockAssessmentPractice(ctx, sqlc.LockAssessmentPracticeParams{
|
||||
ID: pgUUID(practiceID), AttemptID: pgUUID(attemptID.UUID()), UserID: pgUUID(userID),
|
||||
})
|
||||
if isNoRows(err) {
|
||||
return domain.AssessmentPractice{}, fmt.Errorf("%s - lock practice: %w", location, outbound.ErrAssessmentPracticeNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return domain.AssessmentPractice{}, fmt.Errorf("%s - lock practice: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
practice, err := restorePractice(practiceRow{
|
||||
ID: row.ID, AttemptID: row.AttemptID, UserID: row.UserID, Kind: row.Kind, Task: row.Task, Status: row.Status,
|
||||
FailureReason: row.FailureReason, SubmittedAt: row.SubmittedAt, CreatedAt: row.CreatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentPractice{}, fmt.Errorf("%s - restore practice: %w", location, err)
|
||||
}
|
||||
return practice, nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) SaveState(ctx context.Context, practice domain.AssessmentPractice, updatedAt time.Time) error {
|
||||
const location = "assessmentPracticeRepository.SaveState"
|
||||
if err := practice.Validate(); err != nil {
|
||||
return fmt.Errorf("%s - validate practice: %w", location, err)
|
||||
}
|
||||
if updatedAt.IsZero() {
|
||||
return errors.New(location + " - update time is required")
|
||||
}
|
||||
submittedAt := pgtype.Timestamptz{}
|
||||
if !practice.SubmittedAt.IsZero() {
|
||||
submittedAt = pgTime(practice.SubmittedAt)
|
||||
}
|
||||
updated, err := repository.queries.UpdateAssessmentPracticeState(ctx, sqlc.UpdateAssessmentPracticeStateParams{
|
||||
ID: pgUUID(practice.ID), Status: string(practice.Status), FailureReason: pgOptionalText(string(practice.FailureReason)),
|
||||
SubmittedAt: submittedAt, UpdatedAt: pgTime(updatedAt),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - update practice: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
if updated != 1 {
|
||||
return fmt.Errorf("%s - update practice: %w", location, outbound.ErrAssessmentPracticeNotFound)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) FindByScope(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
scope [outbound.IdempotencyDigestSize]byte,
|
||||
) (outbound.PracticeSubmissionReplay, bool, error) {
|
||||
const location = "assessmentPracticeRepository.FindByScope"
|
||||
row, err := repository.queries.FindAssessmentPracticeSubmissionByScope(ctx, sqlc.FindAssessmentPracticeSubmissionByScopeParams{
|
||||
UserID: pgUUID(userID), IdempotencyScope: scope[:],
|
||||
})
|
||||
if isNoRows(err) {
|
||||
return outbound.PracticeSubmissionReplay{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return outbound.PracticeSubmissionReplay{}, false, fmt.Errorf("%s - find submission: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
if !row.ID.Valid || !row.PracticeID.Valid || !domain.PracticeSubmissionMode(row.Mode).IsKnown() {
|
||||
return outbound.PracticeSubmissionReplay{}, false, fmt.Errorf("%s - stored submission is incomplete: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
return outbound.PracticeSubmissionReplay{
|
||||
ID: row.ID.Bytes, PracticeID: row.PracticeID.Bytes, Mode: domain.PracticeSubmissionMode(row.Mode),
|
||||
}, true, nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) LatestFinal(ctx context.Context, practiceID uuid.UUID) (outbound.PracticeFinalSolution, bool, error) {
|
||||
const location = "assessmentPracticeRepository.LatestFinal"
|
||||
row, err := repository.queries.GetLatestFinalPracticeSubmission(ctx, pgUUID(practiceID))
|
||||
if isNoRows(err) {
|
||||
return outbound.PracticeFinalSolution{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return outbound.PracticeFinalSolution{}, false, fmt.Errorf("%s - read latest final submission: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
solution := outbound.PracticeFinalSolution{SubmissionID: row.ID.Bytes, Language: domain.TaskLanguageCode(row.Language)}
|
||||
if !row.ID.Valid || !solution.Language.IsKnown() || len(row.SourceSha256) != len(solution.SourceDigest) {
|
||||
return outbound.PracticeFinalSolution{}, false, fmt.Errorf("%s - stored submission is incomplete: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
copy(solution.SourceDigest[:], row.SourceSha256)
|
||||
return solution, true, nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) Submit(
|
||||
ctx context.Context,
|
||||
submission domain.PracticeSubmission,
|
||||
scope [outbound.IdempotencyDigestSize]byte,
|
||||
) error {
|
||||
const location = "assessmentPracticeRepository.Submit"
|
||||
if submission.ID == uuid.Nil || submission.PracticeID == uuid.Nil || submission.UserID == uuid.Nil ||
|
||||
!submission.Mode.IsKnown() || !submission.Language.IsKnown() || submission.CreatedAt.IsZero() {
|
||||
return fmt.Errorf("%s - submission is incomplete: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
var customTests []byte
|
||||
if len(submission.CustomTests) > 0 {
|
||||
encoded, err := json.Marshal(submission.CustomTests)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - encode custom tests: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
customTests = encoded
|
||||
}
|
||||
digest := domain.DigestPracticeSource(submission.Source)
|
||||
if err := repository.queries.InsertAssessmentPracticeSubmission(ctx, sqlc.InsertAssessmentPracticeSubmissionParams{
|
||||
ID: pgUUID(submission.ID), PracticeID: pgUUID(submission.PracticeID), UserID: pgUUID(submission.UserID),
|
||||
Mode: string(submission.Mode), Language: string(submission.Language), Source: submission.Source,
|
||||
SourceSha256: digest[:], CustomTests: customTests, IdempotencyScope: scope[:], CreatedAt: pgTime(submission.CreatedAt),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - insert submission: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
if err := repository.queries.InsertAssessmentPracticeJob(ctx, sqlc.InsertAssessmentPracticeJobParams{
|
||||
SubmissionID: pgUUID(submission.ID), Mode: string(submission.Mode), CreatedAt: pgTime(submission.CreatedAt),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - queue job: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository assessmentPracticeRepository) GetRun(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
attemptID domain.AssessmentAttemptID,
|
||||
runID uuid.UUID,
|
||||
) (domain.PracticeRunView, error) {
|
||||
const location = "assessmentPracticeRepository.GetRun"
|
||||
row, err := repository.queries.GetAssessmentPracticeRun(ctx, sqlc.GetAssessmentPracticeRunParams{
|
||||
ID: pgUUID(runID), UserID: pgUUID(userID), AttemptID: pgUUID(attemptID.UUID()),
|
||||
})
|
||||
if isNoRows(err) {
|
||||
return domain.PracticeRunView{}, fmt.Errorf("%s - read run: %w", location, outbound.ErrPracticeRunNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return domain.PracticeRunView{}, fmt.Errorf("%s - read run: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
view := domain.PracticeRunView{
|
||||
ID: row.ID.Bytes, PracticeID: row.PracticeID.Bytes, State: domain.PracticeJobState(row.State),
|
||||
CreatedAt: row.CreatedAt.Time.UTC(),
|
||||
}
|
||||
if !row.ID.Valid || !row.PracticeID.Valid || !row.CreatedAt.Valid || !view.State.IsKnown() {
|
||||
return domain.PracticeRunView{}, fmt.Errorf("%s - stored run is incomplete: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
if row.FinishedAt.Valid {
|
||||
finishedAt := row.FinishedAt.Time.UTC()
|
||||
view.FinishedAt = &finishedAt
|
||||
}
|
||||
if len(row.Result) > 0 {
|
||||
result, err := decodePracticeRunResult(row.Result)
|
||||
if err != nil {
|
||||
return domain.PracticeRunView{}, fmt.Errorf("%s - restore result: %w", location, err)
|
||||
}
|
||||
public := result.Public()
|
||||
view.Result = &public
|
||||
}
|
||||
return view, nil
|
||||
}
|
||||
|
||||
// practiceRow is the stored form of a practice that several queries read.
|
||||
type practiceRow struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Kind string
|
||||
Task []byte
|
||||
Status string
|
||||
FailureReason pgtype.Text
|
||||
SubmittedAt pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func restorePractice(row practiceRow) (domain.AssessmentPractice, error) {
|
||||
const location = "postgres.restorePractice"
|
||||
if !row.ID.Valid || !row.AttemptID.Valid || !row.UserID.Valid || !row.CreatedAt.Valid {
|
||||
return domain.AssessmentPractice{}, fmt.Errorf("%s - stored practice is incomplete: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
var task domain.PracticeTask
|
||||
if err := json.Unmarshal(row.Task, &task); err != nil {
|
||||
return domain.AssessmentPractice{}, fmt.Errorf("%s - decode task: %w", location, domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
practice := domain.AssessmentPractice{
|
||||
ID: row.ID.Bytes, AttemptID: domain.AssessmentAttemptID(row.AttemptID.Bytes), UserID: row.UserID.Bytes,
|
||||
Kind: domain.AssessmentPracticeKind(row.Kind), Task: task, Status: domain.AssessmentPracticeStatus(row.Status),
|
||||
CreatedAt: row.CreatedAt.Time.UTC(),
|
||||
}
|
||||
if row.FailureReason.Valid {
|
||||
practice.FailureReason = domain.AssessmentPracticeFailureReason(row.FailureReason.String)
|
||||
}
|
||||
if row.SubmittedAt.Valid {
|
||||
practice.SubmittedAt = row.SubmittedAt.Time.UTC()
|
||||
}
|
||||
if err := practice.Validate(); err != nil {
|
||||
return domain.AssessmentPractice{}, fmt.Errorf("%s - validate practice: %w", location, err)
|
||||
}
|
||||
return practice, nil
|
||||
}
|
||||
|
||||
func decodePracticeRunResult(encoded []byte) (domain.PracticeRunResult, error) {
|
||||
var result domain.PracticeRunResult
|
||||
if err := json.Unmarshal(encoded, &result); err != nil {
|
||||
return domain.PracticeRunResult{}, fmt.Errorf("postgres.decodePracticeRunResult - decode result: %w", domain.ErrInvalidAssessmentPractice)
|
||||
}
|
||||
if err := result.Validate(); err != nil {
|
||||
return domain.PracticeRunResult{}, fmt.Errorf("postgres.decodePracticeRunResult - validate result: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: assessment_practice.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const findAssessmentPracticeSubmissionByScope = `-- name: FindAssessmentPracticeSubmissionByScope :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.practice_id,
|
||||
submission.mode
|
||||
FROM assessment_practice_submissions AS submission
|
||||
WHERE
|
||||
submission.user_id = $1
|
||||
AND submission.idempotency_scope = $2
|
||||
ORDER BY submission.created_at DESC, submission.id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type FindAssessmentPracticeSubmissionByScopeParams struct {
|
||||
UserID pgtype.UUID
|
||||
IdempotencyScope []byte
|
||||
}
|
||||
|
||||
type FindAssessmentPracticeSubmissionByScopeRow struct {
|
||||
ID pgtype.UUID
|
||||
PracticeID pgtype.UUID
|
||||
Mode string
|
||||
}
|
||||
|
||||
// Finds the latest submission of the owner under an idempotency scope, which
|
||||
// a replay returns.
|
||||
func (q *Queries) FindAssessmentPracticeSubmissionByScope(ctx context.Context, arg FindAssessmentPracticeSubmissionByScopeParams) (FindAssessmentPracticeSubmissionByScopeRow, error) {
|
||||
row := q.db.QueryRow(ctx, findAssessmentPracticeSubmissionByScope, arg.UserID, arg.IdempotencyScope)
|
||||
var i FindAssessmentPracticeSubmissionByScopeRow
|
||||
err := row.Scan(&i.ID, &i.PracticeID, &i.Mode)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAssessmentPracticeRun = `-- name: GetAssessmentPracticeRun :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.practice_id,
|
||||
submission.created_at,
|
||||
submission.result,
|
||||
job.state,
|
||||
job.finished_at
|
||||
FROM assessment_practice_submissions AS submission
|
||||
INNER JOIN assessment_practices AS practice ON submission.practice_id = practice.id
|
||||
INNER JOIN assessment_practice_jobs AS job ON submission.id = job.submission_id
|
||||
WHERE
|
||||
submission.id = $1
|
||||
AND submission.user_id = $2
|
||||
AND submission.mode = 'public'
|
||||
AND practice.attempt_id = $3
|
||||
`
|
||||
|
||||
type GetAssessmentPracticeRunParams struct {
|
||||
ID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
}
|
||||
|
||||
type GetAssessmentPracticeRunRow struct {
|
||||
ID pgtype.UUID
|
||||
PracticeID pgtype.UUID
|
||||
CreatedAt pgtype.Timestamptz
|
||||
Result []byte
|
||||
State string
|
||||
FinishedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Reads a debug run of the owner's attempt with the state of its job.
|
||||
func (q *Queries) GetAssessmentPracticeRun(ctx context.Context, arg GetAssessmentPracticeRunParams) (GetAssessmentPracticeRunRow, error) {
|
||||
row := q.db.QueryRow(ctx, getAssessmentPracticeRun, arg.ID, arg.UserID, arg.AttemptID)
|
||||
var i GetAssessmentPracticeRunRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.PracticeID,
|
||||
&i.CreatedAt,
|
||||
&i.Result,
|
||||
&i.State,
|
||||
&i.FinishedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getLatestFinalPracticeSubmission = `-- name: GetLatestFinalPracticeSubmission :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.language,
|
||||
submission.source_sha256
|
||||
FROM assessment_practice_submissions AS submission
|
||||
WHERE
|
||||
submission.practice_id = $1
|
||||
AND submission.mode = 'final'
|
||||
ORDER BY submission.created_at DESC, submission.id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetLatestFinalPracticeSubmissionRow struct {
|
||||
ID pgtype.UUID
|
||||
Language string
|
||||
SourceSha256 []byte
|
||||
}
|
||||
|
||||
// The latest final submission of a practice: the solution the practice
|
||||
// state describes.
|
||||
func (q *Queries) GetLatestFinalPracticeSubmission(ctx context.Context, practiceID pgtype.UUID) (GetLatestFinalPracticeSubmissionRow, error) {
|
||||
row := q.db.QueryRow(ctx, getLatestFinalPracticeSubmission, practiceID)
|
||||
var i GetLatestFinalPracticeSubmissionRow
|
||||
err := row.Scan(&i.ID, &i.Language, &i.SourceSha256)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertAssessmentPractice = `-- name: InsertAssessmentPractice :exec
|
||||
INSERT INTO assessment_practices (
|
||||
id,
|
||||
attempt_id,
|
||||
user_id,
|
||||
kind,
|
||||
task,
|
||||
status,
|
||||
failure_reason,
|
||||
submitted_at,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
'not_started',
|
||||
NULL,
|
||||
NULL,
|
||||
$6,
|
||||
$6
|
||||
)
|
||||
`
|
||||
|
||||
type InsertAssessmentPracticeParams struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Kind string
|
||||
Task []byte
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) InsertAssessmentPractice(ctx context.Context, arg InsertAssessmentPracticeParams) error {
|
||||
_, err := q.db.Exec(ctx, insertAssessmentPractice,
|
||||
arg.ID,
|
||||
arg.AttemptID,
|
||||
arg.UserID,
|
||||
arg.Kind,
|
||||
arg.Task,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertAssessmentPracticeJob = `-- name: InsertAssessmentPracticeJob :exec
|
||||
INSERT INTO assessment_practice_jobs (
|
||||
submission_id,
|
||||
mode,
|
||||
state,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
'queued',
|
||||
$3,
|
||||
$3
|
||||
)
|
||||
`
|
||||
|
||||
type InsertAssessmentPracticeJobParams struct {
|
||||
SubmissionID pgtype.UUID
|
||||
Mode string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Queues the execution job of a submission in the transaction that stores
|
||||
// the submission.
|
||||
func (q *Queries) InsertAssessmentPracticeJob(ctx context.Context, arg InsertAssessmentPracticeJobParams) error {
|
||||
_, err := q.db.Exec(ctx, insertAssessmentPracticeJob, arg.SubmissionID, arg.Mode, arg.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertAssessmentPracticeSubmission = `-- name: InsertAssessmentPracticeSubmission :exec
|
||||
INSERT INTO assessment_practice_submissions (
|
||||
id,
|
||||
practice_id,
|
||||
user_id,
|
||||
mode,
|
||||
language,
|
||||
source,
|
||||
source_sha256,
|
||||
custom_tests,
|
||||
idempotency_scope,
|
||||
created_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10
|
||||
)
|
||||
`
|
||||
|
||||
type InsertAssessmentPracticeSubmissionParams struct {
|
||||
ID pgtype.UUID
|
||||
PracticeID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Mode string
|
||||
Language string
|
||||
Source string
|
||||
SourceSha256 []byte
|
||||
CustomTests []byte
|
||||
IdempotencyScope []byte
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) InsertAssessmentPracticeSubmission(ctx context.Context, arg InsertAssessmentPracticeSubmissionParams) error {
|
||||
_, err := q.db.Exec(ctx, insertAssessmentPracticeSubmission,
|
||||
arg.ID,
|
||||
arg.PracticeID,
|
||||
arg.UserID,
|
||||
arg.Mode,
|
||||
arg.Language,
|
||||
arg.Source,
|
||||
arg.SourceSha256,
|
||||
arg.CustomTests,
|
||||
arg.IdempotencyScope,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const lockAssessmentAttemptForPractice = `-- name: LockAssessmentAttemptForPractice :one
|
||||
|
||||
SELECT
|
||||
attempt.state,
|
||||
attempt.expires_at,
|
||||
clock_timestamp()::timestamptz AS database_time
|
||||
FROM assessment_attempts AS attempt
|
||||
WHERE
|
||||
attempt.id = $1
|
||||
AND attempt.user_id = $2
|
||||
FOR SHARE
|
||||
`
|
||||
|
||||
type LockAssessmentAttemptForPracticeParams struct {
|
||||
AttemptID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
}
|
||||
|
||||
type LockAssessmentAttemptForPracticeRow struct {
|
||||
State string
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
DatabaseTime pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Lock order of a practice command: the idempotency record, the attempt in
|
||||
// share mode, then the practice row. The attempt flows lock the attempt
|
||||
// first as well, so a practice command never waits for them in the opposite
|
||||
// order.
|
||||
// Locks the owner's attempt in share mode, so its state cannot change while
|
||||
// a practice command decides on it, and reads the database time after the
|
||||
// lock.
|
||||
func (q *Queries) LockAssessmentAttemptForPractice(ctx context.Context, arg LockAssessmentAttemptForPracticeParams) (LockAssessmentAttemptForPracticeRow, error) {
|
||||
row := q.db.QueryRow(ctx, lockAssessmentAttemptForPractice, arg.AttemptID, arg.UserID)
|
||||
var i LockAssessmentAttemptForPracticeRow
|
||||
err := row.Scan(&i.State, &i.ExpiresAt, &i.DatabaseTime)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockAssessmentPractice = `-- name: LockAssessmentPractice :one
|
||||
SELECT
|
||||
practice.id,
|
||||
practice.attempt_id,
|
||||
practice.user_id,
|
||||
practice.kind,
|
||||
practice.task,
|
||||
practice.status,
|
||||
practice.failure_reason,
|
||||
practice.submitted_at,
|
||||
practice.created_at
|
||||
FROM assessment_practices AS practice
|
||||
WHERE
|
||||
practice.id = $1
|
||||
AND practice.attempt_id = $2
|
||||
AND practice.user_id = $3
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
type LockAssessmentPracticeParams struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
}
|
||||
|
||||
type LockAssessmentPracticeRow struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Kind string
|
||||
Task []byte
|
||||
Status string
|
||||
FailureReason pgtype.Text
|
||||
SubmittedAt pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Locks one practice of the owner's attempt for a state change.
|
||||
func (q *Queries) LockAssessmentPractice(ctx context.Context, arg LockAssessmentPracticeParams) (LockAssessmentPracticeRow, error) {
|
||||
row := q.db.QueryRow(ctx, lockAssessmentPractice, arg.ID, arg.AttemptID, arg.UserID)
|
||||
var i LockAssessmentPracticeRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttemptID,
|
||||
&i.UserID,
|
||||
&i.Kind,
|
||||
&i.Task,
|
||||
&i.Status,
|
||||
&i.FailureReason,
|
||||
&i.SubmittedAt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateAssessmentPracticeState = `-- name: UpdateAssessmentPracticeState :execrows
|
||||
UPDATE assessment_practices
|
||||
SET
|
||||
status = $1,
|
||||
failure_reason = $2,
|
||||
submitted_at = $3,
|
||||
updated_at = greatest(updated_at, $4::timestamptz)
|
||||
WHERE id = $5
|
||||
`
|
||||
|
||||
type UpdateAssessmentPracticeStateParams struct {
|
||||
Status string
|
||||
FailureReason pgtype.Text
|
||||
SubmittedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
ID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateAssessmentPracticeState(ctx context.Context, arg UpdateAssessmentPracticeStateParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateAssessmentPracticeState,
|
||||
arg.Status,
|
||||
arg.FailureReason,
|
||||
arg.SubmittedAt,
|
||||
arg.UpdatedAt,
|
||||
arg.ID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -229,6 +229,43 @@ type AssessmentLeakTrace struct {
|
||||
EvidenceBits int32
|
||||
}
|
||||
|
||||
type AssessmentPractice struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Kind string
|
||||
Task []byte
|
||||
Status string
|
||||
FailureReason pgtype.Text
|
||||
SubmittedAt pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type AssessmentPracticeJob struct {
|
||||
SubmissionID pgtype.UUID
|
||||
Mode string
|
||||
State string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
FinishedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type AssessmentPracticeSubmission struct {
|
||||
ID pgtype.UUID
|
||||
PracticeID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Mode string
|
||||
Language string
|
||||
Source string
|
||||
SourceSha256 []byte
|
||||
CustomTests []byte
|
||||
IdempotencyScope []byte
|
||||
Result []byte
|
||||
ResultAt pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type AssessmentReviewRead struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
|
||||
@@ -277,6 +277,9 @@ type Querier interface {
|
||||
// NULL retry_at. due_at keeps the deadline. The same guard as the
|
||||
// completion applies.
|
||||
FailScheduledJob(ctx context.Context, arg FailScheduledJobParams) (int64, error)
|
||||
// Finds the latest submission of the owner under an idempotency scope, which
|
||||
// a replay returns.
|
||||
FindAssessmentPracticeSubmissionByScope(ctx context.Context, arg FindAssessmentPracticeSubmissionByScopeParams) (FindAssessmentPracticeSubmissionByScopeRow, error)
|
||||
// The unmarked statements of real attempts whose text, with runs of white space
|
||||
// collapsed, contains the fragment. The fragment is matched literally: position
|
||||
// is not a pattern. The scan reads the stored variant of every item, which is
|
||||
@@ -314,6 +317,8 @@ type Querier interface {
|
||||
GetAssessmentClientSignalTotals(ctx context.Context, attemptID pgtype.UUID) (GetAssessmentClientSignalTotalsRow, error)
|
||||
GetAssessmentFamilyByID(ctx context.Context, id pgtype.UUID) (AssessmentFamily, error)
|
||||
GetAssessmentFamilyVersion(ctx context.Context, arg GetAssessmentFamilyVersionParams) (AssessmentFamily, error)
|
||||
// Reads a debug run of the owner's attempt with the state of its job.
|
||||
GetAssessmentPracticeRun(ctx context.Context, arg GetAssessmentPracticeRunParams) (GetAssessmentPracticeRunRow, error)
|
||||
// The attempt a review job works on: real attempts only.
|
||||
GetAssessmentReviewSubject(ctx context.Context, attemptID pgtype.UUID) (pgtype.UUID, error)
|
||||
// Counts eligible root attempts in a policy window and computes the instant
|
||||
@@ -390,6 +395,9 @@ type Querier interface {
|
||||
// for the account's own view of its mail. It selects no address and no
|
||||
// content. The recipient index narrows the scan to the account's rows.
|
||||
GetLatestDeliveryStatus(ctx context.Context, arg GetLatestDeliveryStatusParams) (GetLatestDeliveryStatusRow, error)
|
||||
// The latest final submission of a practice: the solution the practice
|
||||
// state describes.
|
||||
GetLatestFinalPracticeSubmission(ctx context.Context, practiceID pgtype.UUID) (GetLatestFinalPracticeSubmissionRow, error)
|
||||
// The pending credentials of the newest token of a registration, for a
|
||||
// resend, which issues its token with the same credentials.
|
||||
GetLatestPendingRegistration(ctx context.Context, arg GetLatestPendingRegistrationParams) (GetLatestPendingRegistrationRow, error)
|
||||
@@ -487,6 +495,11 @@ type Querier interface {
|
||||
InsertAssessmentFamily(ctx context.Context, arg InsertAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
InsertAssessmentLeakTrace(ctx context.Context, arg InsertAssessmentLeakTraceParams) error
|
||||
InsertAssessmentOriginMark(ctx context.Context, arg InsertAssessmentOriginMarkParams) error
|
||||
InsertAssessmentPractice(ctx context.Context, arg InsertAssessmentPracticeParams) error
|
||||
// Queues the execution job of a submission in the transaction that stores
|
||||
// the submission.
|
||||
InsertAssessmentPracticeJob(ctx context.Context, arg InsertAssessmentPracticeJobParams) error
|
||||
InsertAssessmentPracticeSubmission(ctx context.Context, arg InsertAssessmentPracticeSubmissionParams) error
|
||||
InsertAssessmentReviewRead(ctx context.Context, arg InsertAssessmentReviewReadParams) error
|
||||
// The attempt row is already locked by the caller. Store a parent before its
|
||||
// bounded child events, in the same transaction as the immutable receipt.
|
||||
@@ -829,6 +842,14 @@ type Querier interface {
|
||||
LockActiveMembership(ctx context.Context, arg LockActiveMembershipParams) (LockActiveMembershipRow, error)
|
||||
// Locks one owner-scoped attempt while a session command performs its CAS.
|
||||
LockAssessmentAttemptForCandidate(ctx context.Context, arg LockAssessmentAttemptForCandidateParams) (AssessmentAttempt, error)
|
||||
// Lock order of a practice command: the idempotency record, the attempt in
|
||||
// share mode, then the practice row. The attempt flows lock the attempt
|
||||
// first as well, so a practice command never waits for them in the opposite
|
||||
// order.
|
||||
// Locks the owner's attempt in share mode, so its state cannot change while
|
||||
// a practice command decides on it, and reads the database time after the
|
||||
// lock.
|
||||
LockAssessmentAttemptForPractice(ctx context.Context, arg LockAssessmentAttemptForPracticeParams) (LockAssessmentAttemptForPracticeRow, error)
|
||||
// Serializes root quota checks for one candidate before taking its optional
|
||||
// specialization or training lock. All callers acquire these locks in order.
|
||||
LockAssessmentAttemptScope(ctx context.Context, arg LockAssessmentAttemptScopeParams) error
|
||||
@@ -836,6 +857,8 @@ type Querier interface {
|
||||
// published or open the category, until the transaction ends. The key is
|
||||
// namespaced, so it cannot collide with another use of advisory locks.
|
||||
LockAssessmentCategory(ctx context.Context, category string) error
|
||||
// Locks one practice of the owner's attempt for a state change.
|
||||
LockAssessmentPractice(ctx context.Context, arg LockAssessmentPracticeParams) (LockAssessmentPracticeRow, error)
|
||||
// Serializes the evaluations of one review. A job takes these locks for every
|
||||
// attempt it will write, in ascending attempt order, before it reads any fact,
|
||||
// so what it reads is what the previous writer committed.
|
||||
@@ -1220,6 +1243,7 @@ type Querier interface {
|
||||
// Applies an answer, expiry, or finalization transition after the attempt row
|
||||
// is locked. Session limits and all immutable pins remain unchanged.
|
||||
UpdateAssessmentAttemptTransition(ctx context.Context, arg UpdateAssessmentAttemptTransitionParams) (AssessmentAttempt, error)
|
||||
UpdateAssessmentPracticeState(ctx context.Context, arg UpdateAssessmentPracticeStateParams) (int64, error)
|
||||
// Replaces the publication over the version the caller read and advances the
|
||||
// version in the same statement. No row returns for a stale version, which
|
||||
// the caller answers as a version conflict. The update time never moves back,
|
||||
|
||||
Reference in new issue
Block a user