feat(assessment): add the review band of attempt signals and the disclosed collector
This commit is contained in:
1 parent
68286af8b7
commit
b3c6dc870b
124 files changed
+13522
-633
No files matched your search
@@ -0,0 +1,271 @@
|
||||
-- +goose Up
|
||||
-- Server-side signals of a real assessment attempt and the review band built
|
||||
-- from them. The band is a hint for a manual check by an operator. Nothing here
|
||||
-- is read by a result, a category, a quota, or any candidate or employer
|
||||
-- operation, and nothing stores an answer, an address, or a session token:
|
||||
-- answers become keyed fingerprints, sessions and networks become keyed
|
||||
-- digests that are bound to one attempt.
|
||||
|
||||
-- One row per accepted answer. The fingerprint is an HMAC of the normalized
|
||||
-- answer under a server secret, scoped to the family version, so answers of
|
||||
-- different candidates to different seed variants of one family can be compared
|
||||
-- without the answer text.
|
||||
CREATE TABLE assessment_answer_observations (
|
||||
attempt_id uuid NOT NULL,
|
||||
item_instance_id uuid NOT NULL,
|
||||
user_id uuid NOT NULL,
|
||||
family_id uuid NOT NULL REFERENCES assessment_families (id),
|
||||
variant_seed bigint NOT NULL,
|
||||
answer_format text NOT NULL,
|
||||
answer_fingerprint bytea NOT NULL,
|
||||
-- Key generation of the fingerprint. Only fingerprints of one generation
|
||||
-- are compared, so a future key rotation is an explicit step.
|
||||
key_version smallint NOT NULL,
|
||||
-- Server time from the previous answer or session start to this answer.
|
||||
elapsed_ms integer NOT NULL,
|
||||
-- True when the time starts at the previous answer, which is when the item
|
||||
-- was shown. The first item of a work session starts at the session start
|
||||
-- and is not used for the speed signals.
|
||||
measured_from_answer boolean NOT NULL,
|
||||
observed_at timestamptz NOT NULL,
|
||||
CONSTRAINT assessment_answer_observations_pkey PRIMARY KEY (attempt_id, item_instance_id),
|
||||
CONSTRAINT assessment_answer_observations_item_fk FOREIGN KEY (attempt_id, item_instance_id)
|
||||
REFERENCES assessment_attempt_items (attempt_id, id) ON DELETE CASCADE,
|
||||
CONSTRAINT assessment_answer_observations_owner_fk FOREIGN KEY (attempt_id, user_id)
|
||||
REFERENCES assessment_attempts (id, user_id) ON DELETE CASCADE,
|
||||
CONSTRAINT assessment_answer_observations_format CHECK (answer_format IN ('integer', 'integer_sequence')),
|
||||
CONSTRAINT assessment_answer_observations_fingerprint CHECK (octet_length(answer_fingerprint) = 32),
|
||||
CONSTRAINT assessment_answer_observations_key_version CHECK (key_version > 0),
|
||||
CONSTRAINT assessment_answer_observations_elapsed CHECK (elapsed_ms >= 0)
|
||||
);
|
||||
|
||||
-- Serves the comparison with other candidates' answers of the same family.
|
||||
CREATE INDEX assessment_answer_observations_match_idx
|
||||
ON assessment_answer_observations (family_id, key_version, answer_fingerprint);
|
||||
|
||||
-- Maintained counters of the observations: how many answers hold each
|
||||
-- fingerprint of a family version, and how many answers the family version has,
|
||||
-- both per key generation. The share of a value is read from them instead of
|
||||
-- being counted over the population at every comparison. They are derived
|
||||
-- caches without foreign keys and are only increased: an answer removed with an
|
||||
-- account stays counted, which keeps the population estimate and avoids lock
|
||||
-- contention on the deletion path.
|
||||
CREATE TABLE assessment_answer_fingerprint_holders (
|
||||
family_id uuid NOT NULL,
|
||||
key_version smallint NOT NULL,
|
||||
answer_fingerprint bytea NOT NULL,
|
||||
holders integer NOT NULL,
|
||||
CONSTRAINT assessment_answer_fingerprint_holders_pkey PRIMARY KEY (family_id, key_version, answer_fingerprint),
|
||||
CONSTRAINT assessment_answer_fingerprint_holders_fingerprint CHECK (octet_length(answer_fingerprint) = 32),
|
||||
CONSTRAINT assessment_answer_fingerprint_holders_count CHECK (holders > 0)
|
||||
);
|
||||
|
||||
CREATE TABLE assessment_answer_family_totals (
|
||||
family_id uuid NOT NULL,
|
||||
key_version smallint NOT NULL,
|
||||
answer_count integer NOT NULL,
|
||||
CONSTRAINT assessment_answer_family_totals_pkey PRIMARY KEY (family_id, key_version),
|
||||
CONSTRAINT assessment_answer_family_totals_count CHECK (answer_count > 0)
|
||||
);
|
||||
|
||||
-- The counters change in the transaction of the insert. A transaction touches
|
||||
-- one holders row and one totals row, always in this order, so two answers
|
||||
-- cannot deadlock on them.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_answer_observation_count() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
INSERT INTO assessment_answer_fingerprint_holders (family_id, key_version, answer_fingerprint, holders)
|
||||
VALUES (NEW.family_id, NEW.key_version, NEW.answer_fingerprint, 1)
|
||||
ON CONFLICT (family_id, key_version, answer_fingerprint)
|
||||
DO UPDATE SET holders = assessment_answer_fingerprint_holders.holders + 1;
|
||||
INSERT INTO assessment_answer_family_totals (family_id, key_version, answer_count)
|
||||
VALUES (NEW.family_id, NEW.key_version, 1)
|
||||
ON CONFLICT (family_id, key_version)
|
||||
DO UPDATE SET answer_count = assessment_answer_family_totals.answer_count + 1;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_answer_observation_count
|
||||
AFTER INSERT ON assessment_answer_observations
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_answer_observation_count();
|
||||
|
||||
-- Written when the session or the network of an attempt changes between two
|
||||
-- accepted answers or signal batches: which session and which network sent
|
||||
-- the request, as digests.
|
||||
CREATE TABLE assessment_attempt_origin_marks (
|
||||
id uuid NOT NULL,
|
||||
attempt_id uuid NOT NULL REFERENCES assessment_attempts (id) ON DELETE CASCADE,
|
||||
source text NOT NULL,
|
||||
observed_at timestamptz NOT NULL,
|
||||
session_digest bytea NOT NULL,
|
||||
network_digest bytea,
|
||||
key_version smallint NOT NULL,
|
||||
CONSTRAINT assessment_attempt_origin_marks_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT assessment_attempt_origin_marks_key_version CHECK (key_version > 0),
|
||||
CONSTRAINT assessment_attempt_origin_marks_source CHECK (source IN ('answer', 'signals')),
|
||||
CONSTRAINT assessment_attempt_origin_marks_session CHECK (octet_length(session_digest) = 32),
|
||||
CONSTRAINT assessment_attempt_origin_marks_network CHECK (
|
||||
network_digest IS NULL OR octet_length(network_digest) = 32
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX assessment_attempt_origin_marks_attempt_idx
|
||||
ON assessment_attempt_origin_marks (attempt_id, observed_at, id);
|
||||
|
||||
-- Observations and marks are facts: only the attempt cascade may remove them.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_signal_fact_immutable() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF TG_OP = 'DELETE' AND pg_trigger_depth() > 1 THEN
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23000', CONSTRAINT = 'assessment_signal_fact_immutable',
|
||||
MESSAGE = 'assessment signal facts are immutable';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_answer_observations_immutable
|
||||
BEFORE UPDATE OR DELETE ON assessment_answer_observations
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_signal_fact_immutable();
|
||||
|
||||
CREATE TRIGGER assessment_attempt_origin_marks_immutable
|
||||
BEFORE UPDATE OR DELETE ON assessment_attempt_origin_marks
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_signal_fact_immutable();
|
||||
|
||||
-- The review of an attempt. It is recomputed in place when a later attempt
|
||||
-- matches an answer, but it stays pinned to the product policy version of the
|
||||
-- attempt that produced it.
|
||||
CREATE TABLE assessment_attempt_reviews (
|
||||
attempt_id uuid NOT NULL REFERENCES assessment_attempts (id) ON DELETE CASCADE,
|
||||
policy_version_id uuid NOT NULL REFERENCES policy_versions (id),
|
||||
band text NOT NULL,
|
||||
reasons text[] NOT NULL,
|
||||
answered_items integer NOT NULL,
|
||||
fast_answers integer NOT NULL,
|
||||
deviating_answers integer NOT NULL,
|
||||
parallel_alternations integer NOT NULL,
|
||||
extra_networks integer NOT NULL,
|
||||
matched_integer_items integer NOT NULL,
|
||||
matched_sequence_items integer NOT NULL,
|
||||
duplicate_evidence_bits integer NOT NULL,
|
||||
client_focus_loss_ms bigint NOT NULL,
|
||||
client_clipboard_events integer NOT NULL,
|
||||
client_fullscreen_exits integer NOT NULL,
|
||||
evaluated_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
CONSTRAINT assessment_attempt_reviews_pkey PRIMARY KEY (attempt_id),
|
||||
CONSTRAINT assessment_attempt_reviews_band CHECK (band IN ('normal', 'attention', 'review')),
|
||||
CONSTRAINT assessment_attempt_reviews_reasons CHECK (
|
||||
reasons <@ ARRAY['fast_answers', 'deviating_answers', 'parallel_sessions', 'extra_networks',
|
||||
'duplicate_answers', 'client_signals']::text[]
|
||||
),
|
||||
CONSTRAINT assessment_attempt_reviews_counts CHECK (
|
||||
answered_items >= 0 AND fast_answers >= 0 AND deviating_answers >= 0 AND parallel_alternations >= 0
|
||||
AND extra_networks >= 0 AND matched_integer_items >= 0 AND matched_sequence_items >= 0
|
||||
AND duplicate_evidence_bits >= 0 AND client_focus_loss_ms >= 0 AND client_clipboard_events >= 0
|
||||
AND client_fullscreen_exits >= 0
|
||||
),
|
||||
CONSTRAINT assessment_attempt_reviews_update_order CHECK (updated_at >= evaluated_at)
|
||||
);
|
||||
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_attempt_review_guard() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF TG_OP = 'DELETE' THEN
|
||||
IF pg_trigger_depth() > 1 THEN
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
ELSIF NEW.attempt_id = OLD.attempt_id AND NEW.policy_version_id = OLD.policy_version_id
|
||||
AND NEW.evaluated_at = OLD.evaluated_at THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23000', CONSTRAINT = 'assessment_attempt_reviews_pinned',
|
||||
MESSAGE = 'assessment attempt review keeps its attempt, policy version, and first evaluation';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_attempt_review_guard
|
||||
BEFORE UPDATE OR DELETE ON assessment_attempt_reviews
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_review_guard();
|
||||
|
||||
-- Every operator read of a review is recorded, whether or not a review exists.
|
||||
-- The attempt is not a foreign key, so the record outlives an account deletion.
|
||||
CREATE TABLE assessment_review_reads (
|
||||
id uuid NOT NULL,
|
||||
attempt_id uuid NOT NULL,
|
||||
actor text NOT NULL,
|
||||
found boolean NOT NULL,
|
||||
read_at timestamptz NOT NULL,
|
||||
CONSTRAINT assessment_review_reads_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT assessment_review_reads_actor CHECK (
|
||||
char_length(actor) <= 128 AND actor ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX assessment_review_reads_attempt_idx
|
||||
ON assessment_review_reads (attempt_id, read_at);
|
||||
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_review_reads_immutable() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23000', CONSTRAINT = 'assessment_review_reads_immutable',
|
||||
MESSAGE = 'assessment review read records are immutable';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_review_reads_immutable
|
||||
BEFORE UPDATE OR DELETE ON assessment_review_reads
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_review_reads_immutable();
|
||||
|
||||
CREATE TRIGGER assessment_review_reads_no_truncate
|
||||
BEFORE TRUNCATE ON assessment_review_reads
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION assessment_review_reads_immutable();
|
||||
|
||||
-- +goose Down
|
||||
-- The audit of operator reads is durable evidence. Rolling the schema back must
|
||||
-- not remove it as a side effect: an operator exports or resolves it first.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM assessment_review_reads) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'assessment_review_reads_retained',
|
||||
TABLE = 'assessment_review_reads',
|
||||
MESSAGE = 'assessment review read audit must be retained before rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
DROP TRIGGER assessment_review_reads_no_truncate ON assessment_review_reads;
|
||||
DROP TRIGGER assessment_review_reads_immutable ON assessment_review_reads;
|
||||
DROP FUNCTION assessment_review_reads_immutable();
|
||||
DROP INDEX assessment_review_reads_attempt_idx;
|
||||
DROP TABLE assessment_review_reads;
|
||||
DROP TRIGGER assessment_attempt_review_guard ON assessment_attempt_reviews;
|
||||
DROP FUNCTION assessment_attempt_review_guard();
|
||||
DROP TABLE assessment_attempt_reviews;
|
||||
DROP TRIGGER assessment_answer_observation_count ON assessment_answer_observations;
|
||||
DROP FUNCTION assessment_answer_observation_count();
|
||||
DROP TABLE assessment_answer_family_totals;
|
||||
DROP TABLE assessment_answer_fingerprint_holders;
|
||||
DROP TRIGGER assessment_attempt_origin_marks_immutable ON assessment_attempt_origin_marks;
|
||||
DROP TRIGGER assessment_answer_observations_immutable ON assessment_answer_observations;
|
||||
DROP FUNCTION assessment_signal_fact_immutable();
|
||||
DROP INDEX assessment_attempt_origin_marks_attempt_idx;
|
||||
DROP TABLE assessment_attempt_origin_marks;
|
||||
DROP INDEX assessment_answer_observations_match_idx;
|
||||
DROP TABLE assessment_answer_observations;
|
||||
@@ -0,0 +1,15 @@
|
||||
-- +goose Up
|
||||
-- A record that a candidate was shown the notice about the collected
|
||||
-- assessment signals. It is not a consent: no operation reads it to allow or
|
||||
-- refuse signal storage. One row per account and notice version keeps the time
|
||||
-- of the first acknowledgement; a changed notice text uses a new version.
|
||||
CREATE TABLE assessment_signal_notice_acknowledgements (
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
notice_version integer NOT NULL,
|
||||
acknowledged_at timestamptz NOT NULL,
|
||||
CONSTRAINT assessment_signal_notice_acknowledgements_pkey PRIMARY KEY (user_id, notice_version),
|
||||
CONSTRAINT assessment_signal_notice_acknowledgements_version CHECK (notice_version >= 1)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE assessment_signal_notice_acknowledgements;
|
||||
@@ -0,0 +1,15 @@
|
||||
-- The caller has already checked that the user is a candidate. A missing row
|
||||
-- means the notice version was not acknowledged.
|
||||
-- name: GetAssessmentSignalNoticeAcknowledgement :one
|
||||
SELECT acknowledged_at
|
||||
FROM assessment_signal_notice_acknowledgements
|
||||
WHERE
|
||||
user_id = sqlc.arg(user_id)
|
||||
AND notice_version = sqlc.arg(notice_version);
|
||||
|
||||
-- The first acknowledgement wins; a repeat keeps the stored time and inserts
|
||||
-- nothing. The caller reads the stored row afterwards in the same transaction.
|
||||
-- name: InsertAssessmentSignalNoticeAcknowledgement :exec
|
||||
INSERT INTO assessment_signal_notice_acknowledgements (user_id, notice_version, acknowledged_at)
|
||||
VALUES (sqlc.arg(user_id), sqlc.arg(notice_version), sqlc.arg(acknowledged_at))
|
||||
ON CONFLICT (user_id, notice_version) DO NOTHING;
|
||||
@@ -0,0 +1,247 @@
|
||||
-- name: InsertAssessmentAnswerObservation :exec
|
||||
INSERT INTO assessment_answer_observations (
|
||||
attempt_id,
|
||||
item_instance_id,
|
||||
user_id,
|
||||
family_id,
|
||||
variant_seed,
|
||||
answer_format,
|
||||
answer_fingerprint,
|
||||
key_version,
|
||||
elapsed_ms,
|
||||
measured_from_answer,
|
||||
observed_at
|
||||
) VALUES (
|
||||
sqlc.arg(attempt_id),
|
||||
sqlc.arg(item_instance_id),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(family_id),
|
||||
sqlc.arg(variant_seed),
|
||||
sqlc.arg(answer_format),
|
||||
sqlc.arg(answer_fingerprint),
|
||||
sqlc.arg(key_version),
|
||||
sqlc.arg(elapsed_ms),
|
||||
sqlc.arg(measured_from_answer),
|
||||
sqlc.arg(observed_at)
|
||||
);
|
||||
|
||||
-- name: InsertAssessmentOriginMark :exec
|
||||
INSERT INTO assessment_attempt_origin_marks (
|
||||
id,
|
||||
attempt_id,
|
||||
source,
|
||||
observed_at,
|
||||
session_digest,
|
||||
network_digest,
|
||||
key_version
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(attempt_id),
|
||||
sqlc.arg(source),
|
||||
sqlc.arg(observed_at),
|
||||
sqlc.arg(session_digest),
|
||||
sqlc.narg(network_digest),
|
||||
sqlc.arg(key_version)
|
||||
);
|
||||
|
||||
-- An attempt holds at most 220 items, so the list is bounded by the attempt.
|
||||
-- name: ListAssessmentAnswerTimes :many
|
||||
SELECT answer_format, elapsed_ms, measured_from_answer
|
||||
FROM assessment_answer_observations
|
||||
WHERE attempt_id = sqlc.arg(attempt_id)
|
||||
ORDER BY observed_at, item_instance_id
|
||||
LIMIT 220;
|
||||
|
||||
-- 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.
|
||||
-- name: ListAssessmentOriginMarks :many
|
||||
SELECT id, attempt_id, source, observed_at, session_digest, network_digest, key_version
|
||||
FROM assessment_attempt_origin_marks
|
||||
WHERE attempt_id = sqlc.arg(attempt_id)
|
||||
ORDER BY observed_at, id
|
||||
LIMIT 5000;
|
||||
|
||||
-- The caller holds the attempt row lock, so the last mark is stable.
|
||||
-- name: GetLastAssessmentOriginMark :one
|
||||
SELECT id, attempt_id, source, observed_at, session_digest, network_digest, key_version
|
||||
FROM assessment_attempt_origin_marks
|
||||
WHERE attempt_id = sqlc.arg(attempt_id)
|
||||
ORDER BY observed_at DESC, id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- Compares this attempt with every other attempt separately. A matched answer
|
||||
-- is an equal fingerprint of the same family version under the same key
|
||||
-- generation, given by another candidate to a different seed variant. It
|
||||
-- carries -log2 of the share of answers that hold that fingerprint, read from
|
||||
-- the maintained counters, between
|
||||
-- zero and the cap of its format, and the bits of one pair add up. Pairs under
|
||||
-- min_bits are dropped, the strongest come first, and the list is bounded.
|
||||
-- An attempt has ended when its state is terminal or its deadline has passed.
|
||||
-- name: ListAssessmentDuplicatePairs :many
|
||||
WITH mine AS (
|
||||
SELECT
|
||||
own.family_id,
|
||||
own.key_version,
|
||||
own.answer_fingerprint,
|
||||
own.answer_format,
|
||||
own.variant_seed,
|
||||
own.user_id
|
||||
FROM assessment_answer_observations AS own
|
||||
WHERE own.attempt_id = sqlc.arg(attempt_id)
|
||||
),
|
||||
|
||||
pair_items AS (
|
||||
SELECT
|
||||
other.attempt_id AS other_attempt_id,
|
||||
mine.answer_format,
|
||||
least(
|
||||
CASE
|
||||
WHEN mine.answer_format = 'integer' THEN sqlc.arg(integer_cap_bits)::float8
|
||||
ELSE sqlc.arg(sequence_cap_bits)::float8
|
||||
END,
|
||||
greatest(0::float8, -ln(holders.holders::float8 / totals.answer_count::float8) / ln(2::float8))
|
||||
) AS bits
|
||||
FROM mine
|
||||
INNER JOIN assessment_answer_observations AS other
|
||||
ON mine.family_id = other.family_id
|
||||
AND mine.key_version = other.key_version
|
||||
AND mine.answer_fingerprint = other.answer_fingerprint
|
||||
AND mine.user_id <> other.user_id
|
||||
AND mine.variant_seed <> other.variant_seed
|
||||
INNER JOIN assessment_answer_fingerprint_holders AS holders
|
||||
ON mine.family_id = holders.family_id
|
||||
AND mine.key_version = holders.key_version
|
||||
AND mine.answer_fingerprint = holders.answer_fingerprint
|
||||
INNER JOIN assessment_answer_family_totals AS totals
|
||||
ON mine.family_id = totals.family_id
|
||||
AND mine.key_version = totals.key_version
|
||||
)
|
||||
|
||||
SELECT
|
||||
pair_items.other_attempt_id,
|
||||
other_attempt.product_policy_version_id,
|
||||
floor(sum(pair_items.bits))::integer AS evidence_bits,
|
||||
(count(*) FILTER (WHERE pair_items.answer_format = 'integer'))::integer AS integer_items,
|
||||
(count(*) FILTER (WHERE pair_items.answer_format = 'integer_sequence'))::integer AS sequence_items
|
||||
FROM pair_items
|
||||
INNER JOIN assessment_attempts AS other_attempt ON pair_items.other_attempt_id = other_attempt.id
|
||||
WHERE
|
||||
NOT sqlc.arg(ended_only)::boolean
|
||||
OR other_attempt.state IN ('completed', 'expired', 'insufficient_bank')
|
||||
OR other_attempt.expires_at <= sqlc.arg(now)::timestamptz
|
||||
GROUP BY pair_items.other_attempt_id, other_attempt.product_policy_version_id
|
||||
HAVING floor(sum(pair_items.bits)) >= sqlc.arg(min_bits)::float8
|
||||
ORDER BY evidence_bits DESC, pair_items.other_attempt_id
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
|
||||
-- The attempt a review job works on: real attempts only.
|
||||
-- name: GetAssessmentReviewSubject :one
|
||||
SELECT product_policy_version_id
|
||||
FROM assessment_attempts
|
||||
WHERE id = sqlc.arg(attempt_id) AND kind IN ('root', 'corrective');
|
||||
|
||||
-- name: GetAssessmentClientSignalTotals :one
|
||||
SELECT
|
||||
COALESCE(sum(duration_ms) FILTER (WHERE kind = 'focus_loss'), 0)::bigint AS focus_loss_ms,
|
||||
(count(*) FILTER (WHERE kind IN ('copy', 'paste', 'cut')))::integer AS clipboard_events,
|
||||
(count(*) FILTER (WHERE kind = 'fullscreen_exit'))::integer AS fullscreen_exits
|
||||
FROM assessment_signal_events
|
||||
WHERE attempt_id = sqlc.arg(attempt_id);
|
||||
|
||||
-- The times come from the database clock, never from a caller, and the update
|
||||
-- time cannot fall behind the stored times, so two jobs writing one review in
|
||||
-- either order keep the order check. The first evaluation time and the policy
|
||||
-- version stay; the guard trigger refuses a change of either.
|
||||
-- name: UpsertAssessmentAttemptReview :exec
|
||||
INSERT INTO assessment_attempt_reviews (
|
||||
attempt_id,
|
||||
policy_version_id,
|
||||
band,
|
||||
reasons,
|
||||
answered_items,
|
||||
fast_answers,
|
||||
deviating_answers,
|
||||
parallel_alternations,
|
||||
extra_networks,
|
||||
matched_integer_items,
|
||||
matched_sequence_items,
|
||||
duplicate_evidence_bits,
|
||||
client_focus_loss_ms,
|
||||
client_clipboard_events,
|
||||
client_fullscreen_exits,
|
||||
evaluated_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(attempt_id),
|
||||
sqlc.arg(policy_version_id),
|
||||
sqlc.arg(band),
|
||||
sqlc.arg(reasons)::text[],
|
||||
sqlc.arg(answered_items),
|
||||
sqlc.arg(fast_answers),
|
||||
sqlc.arg(deviating_answers),
|
||||
sqlc.arg(parallel_alternations),
|
||||
sqlc.arg(extra_networks),
|
||||
sqlc.arg(matched_integer_items),
|
||||
sqlc.arg(matched_sequence_items),
|
||||
sqlc.arg(duplicate_evidence_bits),
|
||||
sqlc.arg(client_focus_loss_ms),
|
||||
sqlc.arg(client_clipboard_events),
|
||||
sqlc.arg(client_fullscreen_exits),
|
||||
clock_timestamp(),
|
||||
clock_timestamp()
|
||||
)
|
||||
ON CONFLICT (attempt_id) DO UPDATE SET
|
||||
policy_version_id = EXCLUDED.policy_version_id,
|
||||
band = EXCLUDED.band,
|
||||
reasons = EXCLUDED.reasons,
|
||||
answered_items = EXCLUDED.answered_items,
|
||||
fast_answers = EXCLUDED.fast_answers,
|
||||
deviating_answers = EXCLUDED.deviating_answers,
|
||||
parallel_alternations = EXCLUDED.parallel_alternations,
|
||||
extra_networks = EXCLUDED.extra_networks,
|
||||
matched_integer_items = EXCLUDED.matched_integer_items,
|
||||
matched_sequence_items = EXCLUDED.matched_sequence_items,
|
||||
duplicate_evidence_bits = EXCLUDED.duplicate_evidence_bits,
|
||||
client_focus_loss_ms = EXCLUDED.client_focus_loss_ms,
|
||||
client_clipboard_events = EXCLUDED.client_clipboard_events,
|
||||
client_fullscreen_exits = EXCLUDED.client_fullscreen_exits,
|
||||
updated_at = greatest(
|
||||
clock_timestamp(), assessment_attempt_reviews.evaluated_at, assessment_attempt_reviews.updated_at
|
||||
);
|
||||
|
||||
-- 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.
|
||||
-- name: LockAssessmentReview :exec
|
||||
SELECT pg_advisory_xact_lock(hashtextextended(
|
||||
'lincodex.assessment.signal_review:' || sqlc.arg(attempt_id)::text,
|
||||
0
|
||||
));
|
||||
|
||||
-- name: GetAssessmentAttemptReview :one
|
||||
SELECT
|
||||
review.attempt_id,
|
||||
review.policy_version_id,
|
||||
policy.version AS policy_version,
|
||||
review.band,
|
||||
review.reasons,
|
||||
review.answered_items,
|
||||
review.fast_answers,
|
||||
review.deviating_answers,
|
||||
review.parallel_alternations,
|
||||
review.extra_networks,
|
||||
review.matched_integer_items,
|
||||
review.matched_sequence_items,
|
||||
review.duplicate_evidence_bits,
|
||||
review.client_focus_loss_ms,
|
||||
review.client_clipboard_events,
|
||||
review.client_fullscreen_exits,
|
||||
review.evaluated_at,
|
||||
review.updated_at
|
||||
FROM assessment_attempt_reviews AS review
|
||||
JOIN policy_versions AS policy ON policy.id = review.policy_version_id
|
||||
WHERE review.attempt_id = sqlc.arg(attempt_id);
|
||||
|
||||
-- name: InsertAssessmentReviewRead :exec
|
||||
INSERT INTO assessment_review_reads (id, attempt_id, actor, found, read_at)
|
||||
VALUES (sqlc.arg(id), sqlc.arg(attempt_id), sqlc.arg(actor), sqlc.arg(found), sqlc.arg(read_at));
|
||||
Reference in new issue
Block a user