Files
lincodex/backend/internal/adapter/secondary/postgres/sqlc/assessment_availability.sql.go
T

366 lines
13 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: assessment_availability.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getAssessmentAvailabilityActiveAttempt = `-- name: GetAssessmentAvailabilityActiveAttempt :one
SELECT id, expires_at
FROM assessment_attempts
WHERE user_id = $1
AND specialization = $2
AND kind IN ('root', 'corrective')
AND state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize')
ORDER BY created_at DESC, id DESC
LIMIT 1
`
type GetAssessmentAvailabilityActiveAttemptParams struct {
UserID pgtype.UUID
Specialization pgtype.Text
}
type GetAssessmentAvailabilityActiveAttemptRow struct {
ID pgtype.UUID
ExpiresAt pgtype.Timestamptz
}
// Reads an active real attempt without taking the write lock used by create.
// The availability GET never expires or otherwise mutates this row.
func (q *Queries) GetAssessmentAvailabilityActiveAttempt(ctx context.Context, arg GetAssessmentAvailabilityActiveAttemptParams) (GetAssessmentAvailabilityActiveAttemptRow, error) {
row := q.db.QueryRow(ctx, getAssessmentAvailabilityActiveAttempt, arg.UserID, arg.Specialization)
var i GetAssessmentAvailabilityActiveAttemptRow
err := row.Scan(&i.ID, &i.ExpiresAt)
return i, err
}
const getAssessmentRootQuotaAvailability = `-- name: GetAssessmentRootQuotaAvailability :one
WITH eligible AS (
SELECT attempt.id, attempt.created_at
FROM assessment_attempts AS attempt
LEFT JOIN assessment_attempt_results AS result
ON result.attempt_id = attempt.id AND result.user_id = attempt.user_id
WHERE attempt.user_id = $3
AND attempt.kind = 'root'
AND attempt.state <> 'insufficient_bank'
AND attempt.created_at >= $4::timestamptz
AND attempt.created_at <= $5::timestamptz
AND COALESCE(result.result_snapshot->>'not_determined_reason', '')
NOT IN ('insufficient_bank', 'sandbox_unavailable')
), total AS (
SELECT count(*)::bigint AS attempt_count FROM eligible
)
SELECT
total.attempt_count,
CASE WHEN total.attempt_count >= $1::bigint THEN (
SELECT eligible.created_at
+ make_interval(secs => $2::double precision)
+ interval '1 microsecond'
FROM eligible
ORDER BY eligible.created_at, eligible.id
LIMIT 1
OFFSET (total.attempt_count - $1::bigint)
) ELSE NULL::timestamptz END AS available_at
FROM total
`
type GetAssessmentRootQuotaAvailabilityParams struct {
MaxAttempts int64
WindowSeconds float64
UserID pgtype.UUID
Since pgtype.Timestamptz
AsOf pgtype.Timestamptz
}
type GetAssessmentRootQuotaAvailabilityRow struct {
AttemptCount int64
AvailableAt pgtype.Timestamptz
}
// Counts eligible root attempts in a policy window and computes the instant
// at which one additional root slot opens. The microsecond adjustment matches
// the creation predicate created_at >= since at PostgreSQL timestamp precision.
func (q *Queries) GetAssessmentRootQuotaAvailability(ctx context.Context, arg GetAssessmentRootQuotaAvailabilityParams) (GetAssessmentRootQuotaAvailabilityRow, error) {
row := q.db.QueryRow(ctx, getAssessmentRootQuotaAvailability,
arg.MaxAttempts,
arg.WindowSeconds,
arg.UserID,
arg.Since,
arg.AsOf,
)
var i GetAssessmentRootQuotaAvailabilityRow
err := row.Scan(&i.AttemptCount, &i.AvailableAt)
return i, err
}
const listAssessmentAvailabilityRootSummaries = `-- name: ListAssessmentAvailabilityRootSummaries :many
SELECT
root.target_grade AS root_target_grade,
root.created_at AS root_created_at,
root.updated_at AS root_updated_at,
root.expires_at AS root_expires_at,
root.state AS root_state,
root.outcome AS root_outcome,
root.chain_deadline AS root_chain_deadline,
latest.target_grade AS latest_target_grade,
latest.updated_at AS latest_updated_at,
latest.expires_at AS latest_expires_at,
latest.state AS latest_state,
latest.outcome AS latest_outcome,
latest.chain_deadline AS latest_chain_deadline,
COALESCE(latest_result.result_snapshot->>'not_determined_reason', '')::text AS latest_platform_error
FROM assessment_attempts AS root
JOIN LATERAL (
SELECT attempt.id, attempt.user_id, attempt.kind, attempt.mode, attempt.state, attempt.outcome, attempt.bank_category, attempt.specialization, attempt.target_grade, attempt.task_language, attempt.root_attempt_id, attempt.product_policy_version_id, attempt.product_policy_version, attempt.grading_policy_version_id, attempt.opening_id, attempt.opening_manifest_sha256, attempt.training_binding_id, attempt.training_manifest_sha256, attempt.bank_version, attempt.algorithm_version, attempt.created_at, attempt.updated_at, attempt.expires_at, attempt.chain_deadline, attempt.idle_timeout_seconds, attempt.max_sessions, attempt.sessions_used, attempt.session_started_at, attempt.last_activity_at, attempt.version, attempt.ended_at
FROM assessment_attempts AS attempt
WHERE attempt.user_id = root.user_id
AND attempt.root_attempt_id = root.id
AND attempt.kind IN ('root', 'corrective')
ORDER BY attempt.created_at DESC, attempt.id DESC
LIMIT 1
) AS latest ON true
LEFT JOIN assessment_attempt_results AS root_result
ON root_result.attempt_id = root.id AND root_result.user_id = root.user_id
LEFT JOIN assessment_attempt_results AS latest_result
ON latest_result.attempt_id = latest.id AND latest_result.user_id = latest.user_id
WHERE root.user_id = $1
AND root.specialization = $2
AND root.kind = 'root'
AND root.created_at >= $3::timestamptz
AND root.state <> 'insufficient_bank'
AND COALESCE(root_result.result_snapshot->>'not_determined_reason', '')
NOT IN ('insufficient_bank', 'sandbox_unavailable')
ORDER BY root.created_at DESC, root.id DESC
`
type ListAssessmentAvailabilityRootSummariesParams struct {
UserID pgtype.UUID
Specialization pgtype.Text
Since pgtype.Timestamptz
}
type ListAssessmentAvailabilityRootSummariesRow struct {
RootTargetGrade pgtype.Text
RootCreatedAt pgtype.Timestamptz
RootUpdatedAt pgtype.Timestamptz
RootExpiresAt pgtype.Timestamptz
RootState string
RootOutcome pgtype.Text
RootChainDeadline pgtype.Timestamptz
LatestTargetGrade pgtype.Text
LatestUpdatedAt pgtype.Timestamptz
LatestExpiresAt pgtype.Timestamptz
LatestState string
LatestOutcome pgtype.Text
LatestChainDeadline pgtype.Timestamptz
LatestPlatformError string
}
// Reads roots within the policy horizon and the last header in each correction
// chain. It deliberately omits frozen items, answers, and row locks.
func (q *Queries) ListAssessmentAvailabilityRootSummaries(ctx context.Context, arg ListAssessmentAvailabilityRootSummariesParams) ([]ListAssessmentAvailabilityRootSummariesRow, error) {
rows, err := q.db.Query(ctx, listAssessmentAvailabilityRootSummaries, arg.UserID, arg.Specialization, arg.Since)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListAssessmentAvailabilityRootSummariesRow
for rows.Next() {
var i ListAssessmentAvailabilityRootSummariesRow
if err := rows.Scan(
&i.RootTargetGrade,
&i.RootCreatedAt,
&i.RootUpdatedAt,
&i.RootExpiresAt,
&i.RootState,
&i.RootOutcome,
&i.RootChainDeadline,
&i.LatestTargetGrade,
&i.LatestUpdatedAt,
&i.LatestExpiresAt,
&i.LatestState,
&i.LatestOutcome,
&i.LatestChainDeadline,
&i.LatestPlatformError,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listAssessmentDisclosedFamilyKeys = `-- name: ListAssessmentDisclosedFamilyKeys :many
WITH progress AS (
SELECT
attempt.id AS attempt_id,
attempt.created_at,
COALESCE(max(item.ordinal) FILTER (WHERE item.answered_at IS NOT NULL), 0) + 1 AS last_disclosed_ordinal
FROM assessment_attempts AS attempt
JOIN assessment_attempt_items AS item ON item.attempt_id = attempt.id
WHERE attempt.user_id = $2
AND attempt.kind IN ('root', 'corrective')
AND attempt.expires_at > $1::timestamptz
GROUP BY attempt.id
), disclosed AS (
SELECT
item.family_key,
CASE
WHEN item.ordinal = 1 THEN progress.created_at
ELSE previous.answered_at
END AS disclosed_at
FROM progress
JOIN assessment_attempt_items AS item ON item.attempt_id = progress.attempt_id
LEFT JOIN assessment_attempt_items AS previous
ON previous.attempt_id = item.attempt_id AND previous.ordinal = item.ordinal - 1
WHERE item.ordinal <= progress.last_disclosed_ordinal
)
SELECT DISTINCT disclosed.family_key
FROM disclosed
WHERE disclosed.disclosed_at > $1::timestamptz
ORDER BY disclosed.family_key
`
type ListAssessmentDisclosedFamilyKeysParams struct {
Since pgtype.Timestamptz
UserID pgtype.UUID
}
// Counts family instances disclosed after the non-repeat boundary. The first
// item is disclosed when the attempt is created; each later item is disclosed
// when the preceding answer is accepted. This per-item timestamp prevents a
// long-running attempt from making later items appear older than they are.
// Counting the first unanswered item after expiry is conservative when the
// final response was lost, while frozen future items remain excluded.
func (q *Queries) ListAssessmentDisclosedFamilyKeys(ctx context.Context, arg ListAssessmentDisclosedFamilyKeysParams) ([]string, error) {
rows, err := q.db.Query(ctx, listAssessmentDisclosedFamilyKeys, arg.Since, arg.UserID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []string
for rows.Next() {
var family_key string
if err := rows.Scan(&family_key); err != nil {
return nil, err
}
items = append(items, family_key)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listCurrentAssessmentAvailabilitySelections = `-- name: ListCurrentAssessmentAvailabilitySelections :many
WITH current_policy AS (
SELECT id, version
FROM policy_versions
WHERE policy_key = 'product' AND effective_at <= $1::timestamptz
ORDER BY version DESC
LIMIT 1
), current_openings AS (
SELECT DISTINCT ON (category)
id,
category,
policy_version_id,
required_families,
opened_at
FROM assessment_category_openings
ORDER BY category, opened_at DESC, id DESC
)
SELECT
opening.id AS opening_id,
opening.category,
opening.policy_version_id,
opening.required_families,
COALESCE((
SELECT count(DISTINCT family.family_key)::integer
FROM assessment_families AS family
WHERE family.category = opening.category AND family.lifecycle = 'published'
), 0)::integer AS published_count,
policy.id AS effective_policy_id,
policy.version AS effective_policy_version,
binding.specialization,
binding.grade,
binding.task_languages,
binding.published_manifest_sha256,
COALESCE((
SELECT jsonb_agg(
jsonb_build_object(
'family_key', family.family_key,
'version', family.version,
'content_sha256', encode(family.content_sha256, 'hex')
) ORDER BY family.family_key, family.version
)
FROM assessment_families AS family
WHERE family.category = opening.category AND family.lifecycle = 'published'
), '[]'::jsonb)::text AS published_manifest
FROM current_openings AS opening
JOIN assessment_category_catalog_bindings AS binding
ON binding.opening_id = opening.id
LEFT JOIN current_policy AS policy ON true
ORDER BY binding.specialization, binding.grade, binding.task_languages, opening.category
`
type ListCurrentAssessmentAvailabilitySelectionsRow struct {
OpeningID pgtype.UUID
Category string
PolicyVersionID pgtype.UUID
RequiredFamilies int32
PublishedCount int32
EffectivePolicyID pgtype.UUID
EffectivePolicyVersion pgtype.Int4
Specialization string
Grade string
TaskLanguages []string
PublishedManifestSha256 []byte
PublishedManifest string
}
// Returns current reviewed grade/language bindings and the live published
// family manifest at one caller-supplied database time. The adapter verifies
// the pinned manifest hash before exposing only family keys.
func (q *Queries) ListCurrentAssessmentAvailabilitySelections(ctx context.Context, asOf pgtype.Timestamptz) ([]ListCurrentAssessmentAvailabilitySelectionsRow, error) {
rows, err := q.db.Query(ctx, listCurrentAssessmentAvailabilitySelections, asOf)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListCurrentAssessmentAvailabilitySelectionsRow
for rows.Next() {
var i ListCurrentAssessmentAvailabilitySelectionsRow
if err := rows.Scan(
&i.OpeningID,
&i.Category,
&i.PolicyVersionID,
&i.RequiredFamilies,
&i.PublishedCount,
&i.EffectivePolicyID,
&i.EffectivePolicyVersion,
&i.Specialization,
&i.Grade,
&i.TaskLanguages,
&i.PublishedManifestSha256,
&i.PublishedManifest,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}