feat(deploy): add an isolated demo stand with synthetic seed and guarded reset
This commit is contained in:
1 parent
bd23c839fd
commit
e1df4ff77c
26 files changed
+3292
-2
No files matched your search
@@ -0,0 +1,202 @@
|
||||
package demoseed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/assessment"
|
||||
)
|
||||
|
||||
// preparedWrongAnswers is the number of frozen items the prepared attempt
|
||||
// answers wrong, the last ones by ordinal. One mistake keeps the result above
|
||||
// the demo pass mark of 80% for any item count the demo policy allows (at
|
||||
// least eight scored items) and shows that the prepared answers are not a
|
||||
// flawless script of the reference solution.
|
||||
const preparedWrongAnswers = 1
|
||||
|
||||
// ensurePreparedAttempt returns the attempt behind the candidate's current
|
||||
// Backend category. Without one it continues an active attempt or creates a
|
||||
// new root attempt, answers every frozen item through the ordinary answer
|
||||
// operation, and finalizes it, so the result, the category, and the evidence
|
||||
// projection come from the same code and triggers as a candidate's attempt.
|
||||
func (seeder *Seeder) ensurePreparedAttempt(ctx context.Context, userID uuid.UUID) (domain.AssessmentAttemptID, bool, error) {
|
||||
attemptID, found, err := seeder.deps.Store.CurrentCategoryAttempt(ctx, userID, fullSpecialization)
|
||||
if err != nil {
|
||||
return domain.AssessmentAttemptID{}, false, fmt.Errorf("read current category: %w", err)
|
||||
}
|
||||
if found {
|
||||
return attemptID, false, nil
|
||||
}
|
||||
attempt, resumed, err := seeder.activeOrNewAttempt(ctx, userID)
|
||||
if err != nil {
|
||||
return domain.AssessmentAttemptID{}, false, err
|
||||
}
|
||||
if resumed {
|
||||
attempt, err = seeder.resumeSession(ctx, userID, attempt)
|
||||
if err != nil {
|
||||
return domain.AssessmentAttemptID{}, false, err
|
||||
}
|
||||
}
|
||||
version, err := seeder.answerItems(ctx, userID, attempt)
|
||||
if err != nil {
|
||||
return domain.AssessmentAttemptID{}, false, err
|
||||
}
|
||||
finalized, err := seeder.deps.Attempts.FinalizeAttempt(ctx, assessment.FinalizeAssessmentAttemptCommand{
|
||||
ActorID: userID, AttemptID: attempt.ID(), ExpectedVersion: version, IdempotencyKey: seedKey("finalize"),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentAttemptID{}, false, fmt.Errorf("finalize attempt: %w", err)
|
||||
}
|
||||
if finalized.Outcome.StatusCode != http.StatusOK {
|
||||
return domain.AssessmentAttemptID{}, false, fmt.Errorf("finalize answered %d %s: %w",
|
||||
finalized.Outcome.StatusCode, finalized.Outcome.ProblemCode, ErrPreparedAttemptFailed)
|
||||
}
|
||||
sourceID, found, err := seeder.deps.Store.CurrentCategoryAttempt(ctx, userID, fullSpecialization)
|
||||
if err != nil {
|
||||
return domain.AssessmentAttemptID{}, false, fmt.Errorf("read assigned category: %w", err)
|
||||
}
|
||||
if !found || sourceID != attempt.ID() {
|
||||
return domain.AssessmentAttemptID{}, false, ErrPreparedAttemptFailed
|
||||
}
|
||||
return attempt.ID(), true, nil
|
||||
}
|
||||
|
||||
// activeOrNewAttempt returns the active Backend attempt left by an
|
||||
// interrupted run, or creates the root attempt of the full category.
|
||||
func (seeder *Seeder) activeOrNewAttempt(ctx context.Context, userID uuid.UUID) (domain.AssessmentAttempt, bool, error) {
|
||||
var active domain.AssessmentAttempt
|
||||
found := false
|
||||
err := seeder.deps.Transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
var err error
|
||||
active, found, err = unit.AssessmentAttempts().GetActiveForSpecialization(ctx, userID, fullSpecialization)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentAttempt{}, false, fmt.Errorf("read active attempt: %w", err)
|
||||
}
|
||||
if found {
|
||||
return active, true, nil
|
||||
}
|
||||
specialization, grade, language := fullSpecialization, fullGrade, fullTaskLanguage
|
||||
created, err := seeder.deps.Attempts.CreateAttempt(ctx, assessment.CreateAttemptCommand{
|
||||
ActorID: userID, IdempotencyKey: seedKey("create"), Kind: domain.AssessmentAttemptKindRoot,
|
||||
Specialization: &specialization, TargetGrade: &grade, TaskLanguage: &language,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentAttempt{}, false, fmt.Errorf("create attempt: %w", err)
|
||||
}
|
||||
if created.Outcome.StatusCode != http.StatusCreated {
|
||||
return domain.AssessmentAttempt{}, false, fmt.Errorf("create answered %d %s: %w",
|
||||
created.Outcome.StatusCode, created.Outcome.ProblemCode, ErrPreparedAttemptFailed)
|
||||
}
|
||||
attempt, err := seeder.readAttempt(ctx, userID, created.Outcome.Response.ID)
|
||||
if err != nil {
|
||||
return domain.AssessmentAttempt{}, false, err
|
||||
}
|
||||
return attempt, false, nil
|
||||
}
|
||||
|
||||
// resumeSession opens a session on an attempt that an interrupted run left,
|
||||
// because its earlier session may have ended by the idle timeout.
|
||||
func (seeder *Seeder) resumeSession(ctx context.Context, userID uuid.UUID, attempt domain.AssessmentAttempt) (domain.AssessmentAttempt, error) {
|
||||
if _, err := seeder.deps.Attempts.StartSession(ctx, assessment.StartAttemptSessionCommand{
|
||||
ActorID: userID, AttemptID: attempt.ID(), ExpectedVersion: attempt.Version(), IdempotencyKey: seedKey("session"),
|
||||
}); err != nil {
|
||||
return domain.AssessmentAttempt{}, fmt.Errorf("resume attempt session: %w", err)
|
||||
}
|
||||
return seeder.readAttempt(ctx, userID, attempt.ID())
|
||||
}
|
||||
|
||||
func (seeder *Seeder) readAttempt(ctx context.Context, userID uuid.UUID, attemptID domain.AssessmentAttemptID) (domain.AssessmentAttempt, error) {
|
||||
var attempt domain.AssessmentAttempt
|
||||
err := seeder.deps.Transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
var err error
|
||||
attempt, err = unit.AssessmentAttempts().GetForCandidate(ctx, userID, attemptID)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentAttempt{}, fmt.Errorf("read attempt: %w", err)
|
||||
}
|
||||
return attempt, nil
|
||||
}
|
||||
|
||||
// answerItems answers every unanswered frozen item in order and returns the
|
||||
// attempt version after the last answer.
|
||||
func (seeder *Seeder) answerItems(ctx context.Context, userID uuid.UUID, attempt domain.AssessmentAttempt) (int64, error) {
|
||||
snapshot := attempt.Snapshot()
|
||||
items := slices.Clone(snapshot.Items)
|
||||
slices.SortFunc(items, func(left, right domain.AssessmentAttemptItemSnapshot) int {
|
||||
return int(left.Ordinal - right.Ordinal)
|
||||
})
|
||||
version := snapshot.Version
|
||||
for index, item := range items {
|
||||
if !item.AnsweredAt.IsZero() {
|
||||
continue
|
||||
}
|
||||
correct := index < len(items)-preparedWrongAnswers
|
||||
answer, err := integerAnswer(item.Variant, correct)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
result, err := seeder.deps.Attempts.SubmitAnswer(ctx, assessment.SubmitAttemptAnswerCommand{
|
||||
ActorID: userID, AttemptID: attempt.ID(), ItemID: item.ID, ExpectedVersion: version,
|
||||
IdempotencyKey: seedKey("answer"), Answer: answer,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("answer item %d: %w", item.Ordinal, err)
|
||||
}
|
||||
if result.Outcome.StatusCode != http.StatusOK {
|
||||
return 0, fmt.Errorf("answer item %d answered %d %s: %w",
|
||||
item.Ordinal, result.Outcome.StatusCode, result.Outcome.ProblemCode, ErrPreparedAttemptFailed)
|
||||
}
|
||||
version = result.Outcome.Response.Version
|
||||
}
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// integerAnswer builds the answer body of an integer item: the reference
|
||||
// answer, or the next integer for a wrong answer. It checks the intended
|
||||
// correctness against the variant, so a template change cannot silently turn
|
||||
// the prepared answers into wrong ones.
|
||||
func integerAnswer(variant domain.AssessmentVariant, correct bool) (json.RawMessage, error) {
|
||||
if variant.AnswerFormat != domain.AnswerFormatInteger {
|
||||
return nil, fmt.Errorf("demoseed.integerAnswer - unsupported answer format %q", variant.AnswerFormat)
|
||||
}
|
||||
reference, err := strconv.ParseInt(variant.ReferenceAnswer, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New("demoseed.integerAnswer - the reference answer is not an integer")
|
||||
}
|
||||
if !correct {
|
||||
reference++
|
||||
}
|
||||
answer, err := json.Marshal(struct {
|
||||
Format string `json:"format"`
|
||||
Number int64 `json:"number"`
|
||||
}{Format: "number", Number: reference})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("demoseed.integerAnswer - encode answer: %w", err)
|
||||
}
|
||||
_, matches, err := domain.CanonicalAssessmentAnswerForVariant(answer, variant)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("demoseed.integerAnswer - check answer: %w", err)
|
||||
}
|
||||
if matches != correct {
|
||||
return nil, errors.New("demoseed.integerAnswer - the answer does not have the intended correctness")
|
||||
}
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
// seedKey returns a fresh idempotency key of one seed command. The seed is
|
||||
// idempotent by the state it reads, not by replaying these keys.
|
||||
func seedKey(operation string) string {
|
||||
return "demo-seed:" + operation + ":" + uuid.NewString()
|
||||
}
|
||||
Reference in new issue
Block a user