Files
archie/internal/infrastructure/postgres/queries/events.sql
T
sam cadf248b1f feat(postgres): port events/identities/status surfaces, fix shipped defects
CHECKPOINT -- the full State Store port is in progress; this lands the
compile-clean core so the review pass has a diff to work against. Remaining:
advisory-lock ownership, event-insert serialization, maintenance, and the
ported test battery.

Two corrections to behaviour that already shipped, not new features:
- UpdateTask wrote park_reason raw where SQLite applies clip(park_reason,
  4000); the PostgreSQL adapter now clips before binding (clip.go).
- tasks.review_cursor existed on the protobuf wire and in workflow.Task but
  not in the PostgreSQL schema; 0004 adds it as a NEW migration rather than
  amending 0001 because a dev database may already have applied 0001 and
  goose tracks version numbers, not checksums.

Also moves the control-plane Resource/ResourceWrite types and their sentinels
into storecontract so the postgres package can stop importing internal/store
(they stay aliased there for daemon callers).
2026-09-24 02:17:44 +10:00

74 lines
2.7 KiB
SQL

-- Event-log queries. The (at, id) cursor is a wire contract: at is the
-- fixed-width sort key and id only breaks ties, so a reader pages by
-- "after (at, id)" and never skips or repeats a row. See
-- internal/domain/storecontract/cursor.go.
-- name: InsertEvent :one
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
RETURNING id;
-- name: ListEventsFromBeginning :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
FROM events
ORDER BY at, id
LIMIT $1;
-- name: ListEventsAfter :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
FROM events
WHERE at > $1 OR (at = $1 AND id > $2)
ORDER BY at, id
LIMIT $3;
-- name: TaskEventsByID :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
FROM events
WHERE task_id = $1
ORDER BY id;
-- name: WorkflowStats :many
SELECT workflow,
COUNT(*)::int AS runs,
COUNT(*) FILTER (WHERE status = 'merged')::int AS merged,
COUNT(*) FILTER (WHERE status = 'pr_open')::int AS pr_open,
COUNT(*) FILTER (WHERE status = 'parked')::int AS parked,
CAST(AVG(tokens_used) AS bigint) AS avg_tokens,
AVG(iterations)::float8 AS avg_steps,
SUM(tokens_used)::bigint AS total_tokens
FROM tasks
WHERE workflow <> ''
GROUP BY workflow
ORDER BY COUNT(*) DESC;
-- name: StageStats :many
SELECT workflow, stage,
COUNT(*)::int AS runs,
CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint) AS avg_ms,
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
FROM events
WHERE kind = 'stage_finish'
GROUP BY workflow, stage
ORDER BY workflow, stage;
-- name: TokensByDay :many
WITH event_totals AS (
SELECT task_id, to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day,
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number'
THEN (data::jsonb ->> 'tokens')::bigint ELSE 0 END) AS tokens,
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number' THEN 1 ELSE 0 END) AS valid_tokens
FROM events WHERE kind = 'agent_finish'
GROUP BY task_id, day
), usage AS (
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
UNION ALL
SELECT to_char(t.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'), t.tokens_used
FROM tasks t
WHERE t.tokens_used != 0 AND NOT EXISTS (
SELECT 1 FROM event_totals e
WHERE e.task_id = t.id AND e.valid_tokens > 0
)
)
SELECT day, SUM(tokens)::bigint AS tokens
FROM usage GROUP BY day ORDER BY day DESC LIMIT $1;