Files
archie/internal/infrastructure/postgres/postgresdb/events.sql.go
T
sam 36b25f78cf feat(taskstate): count completed runs in the per-workflow stats
The transition table moved the no-change outcomes to StatusCompleted, so a
run that opens no pull request now ends in a status the per-workflow stats
did not count at all: it was not merged, not in review, not parked, and the
workflows page would have shown it as a run that went nowhere. The count
travels the whole State Store path, since every dashboard consumer reads it
through the gRPC client rather than the local store.

- WorkflowStats filters `completed` beside merged and pr_open; the SQL is
  the count, so a wrong filter reads as zero rather than failing, and
  TestWorkflowStatsCountsCompletedRuns holds it against a real store.
- storecontract.WorkflowStat gains Completed (json "completed"), the proto
  message gains field 9, and workflowStatProto/workflowStatValue carry it;
  TestWorkflowStatProtoCarriesEveryField round-trips every field by name.
- The gateway's chat task report names the status.
- The dashboard counts a completed run as delivered in GatePulseCard's
  totals and badge numerator, and the WorkflowStat/WorkflowStats/WorkflowRow
  interfaces carry the field.
- TestOutcomeSitesAreTransitionsFromRunning is the guard that keeps the
  producer and the table agreeing: it re-reads this package's tc.Outcome
  assignment sites from source and requires each status to be one
  internal/taskstate allows from running, so a new or moved site has to be
  adjudicated against the table instead of discovered on a live run.
2026-09-26 04:01:08 +10:00

351 lines
8.0 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: events.sql
package postgresdb
import (
"context"
"time"
)
const insertEvent = `-- name: InsertEvent :one
WITH append_lock AS (
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0))
)
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
FROM append_lock
RETURNING id
`
type InsertEventParams struct {
At time.Time
Kind string
TaskID int64
Repo string
Issue int64
Workflow string
Stage string
Attempt int64
ActorID string
ActorKind string
PrincipalID string
Detail string
Data string
}
// 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.
// InsertEvent takes the event-append lock before the row draws its id, and
// holds it until the enclosing transaction commits, so ids become visible in
// the order they were drawn. The lock is inside the statement so a standalone
// insert and one inside a longer transaction both take it.
func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64, error) {
row := q.db.QueryRow(ctx, insertEvent,
arg.At,
arg.Kind,
arg.TaskID,
arg.Repo,
arg.Issue,
arg.Workflow,
arg.Stage,
arg.Attempt,
arg.ActorID,
arg.ActorKind,
arg.PrincipalID,
arg.Detail,
arg.Data,
)
var id int64
err := row.Scan(&id)
return id, err
}
const listEventsAfter = `-- 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
`
type ListEventsAfterParams struct {
At time.Time
ID int64
Limit int32
}
func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams) ([]Event, error) {
rows, err := q.db.Query(ctx, listEventsAfter, arg.At, arg.ID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Event
for rows.Next() {
var i Event
if err := rows.Scan(
&i.ID,
&i.At,
&i.Kind,
&i.TaskID,
&i.Repo,
&i.Issue,
&i.Workflow,
&i.Stage,
&i.Attempt,
&i.ActorID,
&i.ActorKind,
&i.PrincipalID,
&i.Detail,
&i.Data,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listEventsFromBeginning = `-- 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
`
func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]Event, error) {
rows, err := q.db.Query(ctx, listEventsFromBeginning, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Event
for rows.Next() {
var i Event
if err := rows.Scan(
&i.ID,
&i.At,
&i.Kind,
&i.TaskID,
&i.Repo,
&i.Issue,
&i.Workflow,
&i.Stage,
&i.Attempt,
&i.ActorID,
&i.ActorKind,
&i.PrincipalID,
&i.Detail,
&i.Data,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const stageStats = `-- 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
`
type StageStatsRow struct {
Workflow string
Stage string
Runs int32
AvgMs int64
Errors int32
}
func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
rows, err := q.db.Query(ctx, stageStats)
if err != nil {
return nil, err
}
defer rows.Close()
var items []StageStatsRow
for rows.Next() {
var i StageStatsRow
if err := rows.Scan(
&i.Workflow,
&i.Stage,
&i.Runs,
&i.AvgMs,
&i.Errors,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const taskEventsByID = `-- 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
`
func (q *Queries) TaskEventsByID(ctx context.Context, taskID int64) ([]Event, error) {
rows, err := q.db.Query(ctx, taskEventsByID, taskID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Event
for rows.Next() {
var i Event
if err := rows.Scan(
&i.ID,
&i.At,
&i.Kind,
&i.TaskID,
&i.Repo,
&i.Issue,
&i.Workflow,
&i.Stage,
&i.Attempt,
&i.ActorID,
&i.ActorKind,
&i.PrincipalID,
&i.Detail,
&i.Data,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const tokensByDay = `-- 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
`
type TokensByDayRow struct {
Day string
Tokens int64
}
func (q *Queries) TokensByDay(ctx context.Context, limit int32) ([]TokensByDayRow, error) {
rows, err := q.db.Query(ctx, tokensByDay, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []TokensByDayRow
for rows.Next() {
var i TokensByDayRow
if err := rows.Scan(&i.Day, &i.Tokens); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const workflowStats = `-- name: WorkflowStats :many
SELECT workflow,
COUNT(*)::int AS runs,
COUNT(*) FILTER (WHERE status = 'merged')::int AS merged,
COUNT(*) FILTER (WHERE status = 'completed')::int AS completed,
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
`
type WorkflowStatsRow struct {
Workflow string
Runs int32
Merged int32
Completed int32
PrOpen int32
Parked int32
AvgTokens int64
AvgSteps float64
TotalTokens int64
}
func (q *Queries) WorkflowStats(ctx context.Context) ([]WorkflowStatsRow, error) {
rows, err := q.db.Query(ctx, workflowStats)
if err != nil {
return nil, err
}
defer rows.Close()
var items []WorkflowStatsRow
for rows.Next() {
var i WorkflowStatsRow
if err := rows.Scan(
&i.Workflow,
&i.Runs,
&i.Merged,
&i.Completed,
&i.PrOpen,
&i.Parked,
&i.AvgTokens,
&i.AvgSteps,
&i.TotalTokens,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}