feat(assessment): find the attempt behind a leaked task text
This commit is contained in:
1 parent
bb917bf62e
commit
5a731c25b7
26 files changed
+2500
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var _ outbound.AssessmentLeakTraceRepository = assessmentLeakTraceRepository{}
|
||||
|
||||
func (unit *UnitOfWork) AssessmentLeakTraces() outbound.AssessmentLeakTraceRepository {
|
||||
return assessmentLeakTraceRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
type assessmentLeakTraceRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func (repository assessmentLeakTraceRepository) ListRealAttempts(
|
||||
ctx context.Context,
|
||||
after domain.AssessmentAttemptID,
|
||||
limit int32,
|
||||
) ([]outbound.AssessmentTraceAttempt, error) {
|
||||
const location = "assessmentLeakTraceRepository.ListRealAttempts"
|
||||
if limit < 1 {
|
||||
return nil, fmt.Errorf("%s - a positive limit is required: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
rows, err := repository.queries.ListAssessmentTraceAttempts(ctx, sqlc.ListAssessmentTraceAttemptsParams{
|
||||
AfterID: pgUUID(after.UUID()), RowLimit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - list attempts: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
attempts := make([]outbound.AssessmentTraceAttempt, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if !row.ID.Valid || !row.CreatedAt.Valid || !row.ExpiresAt.Valid {
|
||||
return nil, fmt.Errorf("%s - stored attempt is incomplete: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
attempts = append(attempts, outbound.AssessmentTraceAttempt{
|
||||
ID: domain.AssessmentAttemptID(row.ID.Bytes), CreatedAt: row.CreatedAt.Time.UTC(), ExpiresAt: row.ExpiresAt.Time.UTC(),
|
||||
})
|
||||
}
|
||||
return attempts, nil
|
||||
}
|
||||
|
||||
func (repository assessmentLeakTraceRepository) FindStatementsContaining(
|
||||
ctx context.Context,
|
||||
fragment string,
|
||||
limit int32,
|
||||
) ([]outbound.AssessmentTraceStatement, error) {
|
||||
const location = "assessmentLeakTraceRepository.FindStatementsContaining"
|
||||
if fragment == "" || limit < 1 {
|
||||
return nil, fmt.Errorf("%s - a fragment and a positive limit are required: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
rows, err := repository.queries.FindAssessmentTraceStatements(ctx, sqlc.FindAssessmentTraceStatementsParams{
|
||||
Fragment: fragment, RowLimit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - find statements: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
statements := make([]outbound.AssessmentTraceStatement, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if !row.AttemptID.Valid {
|
||||
return nil, fmt.Errorf("%s - stored statement has no attempt: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
statements = append(statements, outbound.AssessmentTraceStatement{
|
||||
AttemptID: domain.AssessmentAttemptID(row.AttemptID.Bytes), Statement: row.Statement,
|
||||
})
|
||||
}
|
||||
return statements, nil
|
||||
}
|
||||
|
||||
func (repository assessmentLeakTraceRepository) RecordSearch(ctx context.Context, record domain.AssessmentLeakTraceRecord) error {
|
||||
const location = "assessmentLeakTraceRepository.RecordSearch"
|
||||
if err := record.Validate(); err != nil {
|
||||
return fmt.Errorf("%s - record is invalid: %w", location, err)
|
||||
}
|
||||
method := pgtype.Text{}
|
||||
if record.Method != "" {
|
||||
method = pgtype.Text{String: string(record.Method), Valid: true}
|
||||
}
|
||||
attempt := pgtype.UUID{}
|
||||
if !record.AttemptID.IsZero() {
|
||||
attempt = pgUUID(record.AttemptID.UUID())
|
||||
}
|
||||
if err := repository.queries.InsertAssessmentLeakTrace(ctx, sqlc.InsertAssessmentLeakTraceParams{
|
||||
ID: pgUUID(record.ID), Actor: string(record.Actor), SearchedAt: pgTime(record.SearchedAt),
|
||||
TextDigest: record.TextDigest[:], KeyVersion: record.KeyVersion, TextRunes: record.TextRunes,
|
||||
Status: string(record.Status), Method: method, AttemptID: attempt,
|
||||
Candidates: record.Candidates, EvidenceBits: record.EvidenceBits,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - insert audit record: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: assessment_leak_traces.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const findAssessmentTraceStatements = `-- name: FindAssessmentTraceStatements :many
|
||||
SELECT
|
||||
item.attempt_id,
|
||||
(item.variant ->> 'Statement')::text AS statement
|
||||
FROM assessment_attempt_items AS item
|
||||
INNER JOIN assessment_attempts AS attempt ON item.attempt_id = attempt.id
|
||||
WHERE
|
||||
attempt.kind IN ('root', 'corrective')
|
||||
AND position($1::text IN regexp_replace((item.variant ->> 'Statement')::text, '\s+', ' ', 'g')) > 0
|
||||
ORDER BY item.attempt_id, item.ordinal
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type FindAssessmentTraceStatementsParams struct {
|
||||
Fragment string
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
type FindAssessmentTraceStatementsRow struct {
|
||||
AttemptID pgtype.UUID
|
||||
Statement string
|
||||
}
|
||||
|
||||
// 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
|
||||
// fine for an operator command and not for a request path.
|
||||
func (q *Queries) FindAssessmentTraceStatements(ctx context.Context, arg FindAssessmentTraceStatementsParams) ([]FindAssessmentTraceStatementsRow, error) {
|
||||
rows, err := q.db.Query(ctx, findAssessmentTraceStatements, arg.Fragment, arg.RowLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []FindAssessmentTraceStatementsRow
|
||||
for rows.Next() {
|
||||
var i FindAssessmentTraceStatementsRow
|
||||
if err := rows.Scan(&i.AttemptID, &i.Statement); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertAssessmentLeakTrace = `-- name: InsertAssessmentLeakTrace :exec
|
||||
INSERT INTO assessment_leak_traces (
|
||||
id,
|
||||
actor,
|
||||
searched_at,
|
||||
text_digest,
|
||||
key_version,
|
||||
text_runes,
|
||||
status,
|
||||
method,
|
||||
attempt_id,
|
||||
candidates,
|
||||
evidence_bits
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10,
|
||||
$11
|
||||
)
|
||||
`
|
||||
|
||||
type InsertAssessmentLeakTraceParams struct {
|
||||
ID pgtype.UUID
|
||||
Actor string
|
||||
SearchedAt pgtype.Timestamptz
|
||||
TextDigest []byte
|
||||
KeyVersion int16
|
||||
TextRunes int32
|
||||
Status string
|
||||
Method pgtype.Text
|
||||
AttemptID pgtype.UUID
|
||||
Candidates int32
|
||||
EvidenceBits int32
|
||||
}
|
||||
|
||||
func (q *Queries) InsertAssessmentLeakTrace(ctx context.Context, arg InsertAssessmentLeakTraceParams) error {
|
||||
_, err := q.db.Exec(ctx, insertAssessmentLeakTrace,
|
||||
arg.ID,
|
||||
arg.Actor,
|
||||
arg.SearchedAt,
|
||||
arg.TextDigest,
|
||||
arg.KeyVersion,
|
||||
arg.TextRunes,
|
||||
arg.Status,
|
||||
arg.Method,
|
||||
arg.AttemptID,
|
||||
arg.Candidates,
|
||||
arg.EvidenceBits,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listAssessmentTraceAttempts = `-- name: ListAssessmentTraceAttempts :many
|
||||
SELECT id, created_at, expires_at
|
||||
FROM assessment_attempts
|
||||
WHERE kind IN ('root', 'corrective') AND id > $1
|
||||
ORDER BY id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListAssessmentTraceAttemptsParams struct {
|
||||
AfterID pgtype.UUID
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
type ListAssessmentTraceAttemptsRow struct {
|
||||
ID pgtype.UUID
|
||||
CreatedAt pgtype.Timestamptz
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Real attempts in ascending identifier order, so that a caller can walk all of
|
||||
// them in pages. Only the identifier and the time span are read.
|
||||
func (q *Queries) ListAssessmentTraceAttempts(ctx context.Context, arg ListAssessmentTraceAttemptsParams) ([]ListAssessmentTraceAttemptsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAssessmentTraceAttempts, arg.AfterID, arg.RowLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListAssessmentTraceAttemptsRow
|
||||
for rows.Next() {
|
||||
var i ListAssessmentTraceAttemptsRow
|
||||
if err := rows.Scan(&i.ID, &i.CreatedAt, &i.ExpiresAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -215,6 +215,20 @@ type AssessmentFamily struct {
|
||||
RetiredBy pgtype.Text
|
||||
}
|
||||
|
||||
type AssessmentLeakTrace struct {
|
||||
ID pgtype.UUID
|
||||
Actor string
|
||||
SearchedAt pgtype.Timestamptz
|
||||
TextDigest []byte
|
||||
KeyVersion int16
|
||||
TextRunes int32
|
||||
Status string
|
||||
Method pgtype.Text
|
||||
AttemptID pgtype.UUID
|
||||
Candidates int32
|
||||
EvidenceBits int32
|
||||
}
|
||||
|
||||
type AssessmentReviewRead struct {
|
||||
ID pgtype.UUID
|
||||
AttemptID pgtype.UUID
|
||||
|
||||
@@ -277,6 +277,11 @@ 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)
|
||||
// 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
|
||||
// fine for an operator command and not for a request path.
|
||||
FindAssessmentTraceStatements(ctx context.Context, arg FindAssessmentTraceStatementsParams) ([]FindAssessmentTraceStatementsRow, error)
|
||||
FindCompanyCardViewQuotaUsage(ctx context.Context, arg FindCompanyCardViewQuotaUsageParams) (pgtype.Timestamptz, error)
|
||||
FindCompanyInvitationQuotaUsage(ctx context.Context, arg FindCompanyInvitationQuotaUsageParams) (pgtype.UUID, error)
|
||||
// Returns the newest open reservation of a delivery under a provider account
|
||||
@@ -480,6 +485,7 @@ type Querier interface {
|
||||
// returns no row instead of an error, so a conflict does not abort the
|
||||
// surrounding transaction.
|
||||
InsertAssessmentFamily(ctx context.Context, arg InsertAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
InsertAssessmentLeakTrace(ctx context.Context, arg InsertAssessmentLeakTraceParams) error
|
||||
InsertAssessmentOriginMark(ctx context.Context, arg InsertAssessmentOriginMarkParams) error
|
||||
InsertAssessmentReviewRead(ctx context.Context, arg InsertAssessmentReviewReadParams) error
|
||||
// The attempt row is already locked by the caller. Store a parent before its
|
||||
@@ -677,6 +683,9 @@ type Querier interface {
|
||||
// Marks exist only where the session or the network changed. The bound keeps a
|
||||
// request that flaps between networks from growing the list without limit.
|
||||
ListAssessmentOriginMarks(ctx context.Context, attemptID pgtype.UUID) ([]AssessmentAttemptOriginMark, error)
|
||||
// Real attempts in ascending identifier order, so that a caller can walk all of
|
||||
// them in pages. Only the identifier and the time span are read.
|
||||
ListAssessmentTraceAttempts(ctx context.Context, arg ListAssessmentTraceAttemptsParams) ([]ListAssessmentTraceAttemptsRow, error)
|
||||
// Reads the exact family membership pinned by a training binding.
|
||||
ListAssessmentTrainingBindingFamilies(ctx context.Context, bindingID pgtype.UUID) ([]ListAssessmentTrainingBindingFamiliesRow, error)
|
||||
ListCandidateAssessmentCategories(ctx context.Context, candidateID pgtype.UUID) ([]CandidateAssessmentCategory, error)
|
||||
|
||||
Reference in new issue
Block a user