Files
lincodex/backend/internal/port/outbound/assessment_signal.go
T

92 lines
4.3 KiB
Go

package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var (
ErrAssessmentSignalOutcomeNotFound = errors.New("assessment signal outcome not found")
ErrAssessmentReviewNotFound = errors.New("assessment attempt review not found")
)
// AssessmentReviewInputs are the stored facts a review is computed from, apart
// from the comparison with other attempts. They hold no answer, address, or
// session identifier.
type AssessmentReviewInputs struct {
Answers []domain.AssessmentResponseSample
Marks []domain.AssessmentOriginMark
ClientFocusLossMS int64
ClientClipboardCount int32
ClientFullscreenExit int32
}
// AssessmentDuplicateQuery selects the pairs of attempts that share answers.
// The caps come from the policy of the attempt being evaluated.
type AssessmentDuplicateQuery struct {
IntegerCapBits int32
SequenceCapBits int32
// MinBits drops pairs with less evidence.
MinBits int32
// EndedOnly keeps only attempts that are over at Now: terminal, or past
// their deadline.
EndedOnly bool
Now time.Time
Limit int32
}
// AssessmentDuplicatePair is one other attempt that shares answers with the
// evaluated one. It is used inside the usecase only: nothing that identifies
// the other attempt or its candidate leaves it.
type AssessmentDuplicatePair struct {
AttemptID domain.AssessmentAttemptID
PolicyVersionID domain.PolicyVersionID
EvidenceBits int32
IntegerItems int32
SequenceItems int32
}
type AssessmentSignalRepositories interface {
AssessmentSignals() AssessmentSignalRepository
}
// AssessmentSignalRepository persists event metadata and the immutable
// candidate receipt in the current unit of work.
type AssessmentSignalRepository interface {
CountRecentOutcomes(ctx context.Context, attemptID domain.AssessmentAttemptID, since time.Time) (int64, time.Time, error)
CountAttemptEvents(ctx context.Context, attemptID domain.AssessmentAttemptID) (int64, error)
SaveBatch(ctx context.Context, batch domain.AssessmentSignalBatch) error
SaveOutcome(ctx context.Context, scope AssessmentAttemptIdempotencyScope, attemptID domain.AssessmentAttemptID, receipt domain.AssessmentSignalReceipt) error
DeleteExpiredOutcome(ctx context.Context, scope AssessmentAttemptIdempotencyScope) error
GetOutcome(ctx context.Context, scope AssessmentAttemptIdempotencyScope) (domain.AssessmentSignalReceipt, error)
// SaveAnswerObservation and SaveOriginMark store server-side facts of an
// accepted request in the current unit of work. The caller holds the row
// lock of the attempt; no other lock is taken.
SaveAnswerObservation(ctx context.Context, observation domain.AssessmentAnswerObservation) error
SaveOriginMark(ctx context.Context, mark domain.AssessmentOriginMark) error
LastOriginMark(ctx context.Context, attemptID domain.AssessmentAttemptID) (domain.AssessmentOriginMark, bool, error)
ReviewInputs(ctx context.Context, attemptID domain.AssessmentAttemptID) (AssessmentReviewInputs, error)
// DuplicatePairs lists the other attempts that share answers with the given
// one, strongest first and bounded by the query.
DuplicatePairs(ctx context.Context, attemptID domain.AssessmentAttemptID, query AssessmentDuplicateQuery) ([]AssessmentDuplicatePair, error)
// ReviewSubject returns the product policy version a real attempt is pinned
// to; found is false for an unknown or training attempt.
ReviewSubject(ctx context.Context, attemptID domain.AssessmentAttemptID) (domain.PolicyVersionID, bool, error)
// LockReview takes the transaction-scoped lock of one attempt's review. A
// job locks every review it will write, in ascending attempt order, before
// it reads any fact, so concurrent evaluations of one attempt run one after
// another and the later one reads what the earlier one committed.
LockReview(ctx context.Context, attemptID domain.AssessmentAttemptID) error
// SaveReview stores the review; the times come from the database clock.
SaveReview(ctx context.Context, review domain.AssessmentAttemptReview) error
GetReview(ctx context.Context, attemptID domain.AssessmentAttemptID) (domain.AssessmentAttemptReview, error)
RecordReviewRead(ctx context.Context, id uuid.UUID, attemptID domain.AssessmentAttemptID, actor domain.AssessmentReviewActor, found bool, at time.Time) error
}